在kernel函数中,同样是for循环为什么一个并行,一个是串行?(附代码)

dfingeng 2016-09-22 12:09:19
以下代码来自 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在编译的时候,遇到循环就自动分配到不同的线程中并行进行吗?
刚入门,还请多多指教
...全文
941 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
dfingeng 2016-10-31
  • 打赏
  • 举报
回复
引用 1 楼 andyise 的回复:
const int tid = threadIdx.x; const int bid = blockIdx.x; 编译器根据这两个量来确定是否并行化
那我是不是可以理解成,如果后面的for循环里如有使用 tid或者bid这两个变量,则编译器编译的时候就会是多线程并行计算的,如果我自定义一个int型变量,而且其值比如是i++,则编译器不会将其列为并行计算
andyise 2016-09-26
  • 打赏
  • 举报
回复
const int tid = threadIdx.x; const int bid = blockIdx.x; 编译器根据这两个量来确定是否并行化

579

社区成员

发帖
与我相关
我的任务
社区描述
CUDA™是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。 它包含了CUDA指令集架构(ISA)以及GPU内部的并行计算引擎。
社区管理员
  • CUDA编程社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧