在kernel函数中,同样是for循环为什么一个并行,一个是串行?(附代码)
以下代码来自 http://blog.csdn.net/yanghangjun/article/details/6067534/ 这片帖子的《Thread的同步》这一小节
__global__ static void sumOfSquares(int *num, int* result,
clock_t* time)
{
extern __shared__ int shared[];
const int tid = threadIdx.x;
const int bid = blockIdx.x;
int i;
if(tid == 0) time[bid] = clock();
shared[tid] = 0;
for(i = bid * THREAD_NUM + tid; i < DATA_SIZE;
i += BLOCK_NUM * THREAD_NUM) {
shared[tid] += num[i] * num[i];
}
__syncthreads();
if(tid == 0) {
for(i = 1; i < THREAD_NUM; i++) {
shared[0] += shared[i];
}
result[bid] = shared[0];
}
if(tid == 0) time[bid + BLOCK_NUM] = clock();
}
根据帖子的说明,第一个for循环 ( __syncthreads()之前的for)是并行计算,而 if(tid == 0)语句里面的for是指在Thread0串行执行,这是为什么?
难道不是NVCC在编译的时候,遇到循环就自动分配到不同的线程中并行进行吗?
刚入门,还请多多指教