579
社区成员
发帖
与我相关
我的任务
分享
__global__ void vecAdd(float* d_C,int* d_flag)
{
int index=threadIdx.x;
int row = index / 10;
int col = index % 10;
if(row == 0)
{
d_C[index] = 1;
d_flag[index] = 1;
}
else
{
int ref = (row - 1) * 10 + col + 1;//依赖的数据块
//if(ref >= 0)
//{
while(d_flag[ref] != 1); //等待依赖块执行完毕
//{
d_C[index] = d_C[ref] + index;
d_flag[index] = 1;
//}
//}
}
}
运行后,黑屏,nvidia提示显示器驱动停止响应。输出的数据结果表明任何块都没执行运算。[/quote]
没有进行线程同步,有的线程死循环了,导致显卡崩溃。
第一行的线程在执行 if(row == 0)这一步的时候,后面的线程已经进入while循环的判断了。
可以这么做...__global__ void vecAdd(float* d_C)
{
int index=threadIdx.x;
int row = index / 10;
int col = index % 10;
int ref = (row - 1) * 10 + col + 1;//依赖的数据块
if(row == 0 || col == 9)
{
d_C[index] = 1;
}
__syncthreads();
else if(row == 1 || col == 8)
{
d_C[index] = d_C[ref] + index;
}
__syncthreads();
else if(row == 2 || col == 7)
{
d_C[index] = d_C[ref] + index;
}
__syncthreads();
//-----TODO---
//......
//------------
else if(row == 9 || col == 0)
{
d_C[index] = d_C[ref] + index;
}
__syncthreads();
}
__global__ void vecAdd(float* d_C,int* d_flag)
{
int index=threadIdx.x;
int row = index / 10;
int col = index % 10;
if(row == 0)
{
d_C[index] = 1;
d_flag[index] = 1;
}
else
{
int ref = (row - 1) * 10 + col + 1;//依赖的数据块
//if(ref >= 0)
//{
while(d_flag[ref] != 1); //等待依赖块执行完毕
//{
d_C[index] = d_C[ref] + index;
d_flag[index] = 1;
//}
//}
}
}
运行后,黑屏,nvidia提示显示器驱动停止响应。输出的数据结果表明任何块都没执行运算。offset = THREAD_NUM / 2;
while(offset > 0) {
if(tid < offset) {
shared[tid] += shared[tid + offset];
}
offset >>= 1;
__syncthreads();
}
当THREAD_NUM = 256 时循环展开就是这样
if(tid < 128) { shared[tid] += shared[tid + 128]; }
__syncthreads();
if(tid < 64) { shared[tid] += shared[tid + 64]; }
__syncthreads();
if(tid < 32) { shared[tid] += shared[tid + 32]; }
__syncthreads();
if(tid < 16) { shared[tid] += shared[tid + 16]; }
__syncthreads();
if(tid < 8) { shared[tid] += shared[tid + 8]; }
__syncthreads();
if(tid < 4) { shared[tid] += shared[tid + 4]; }
__syncthreads();
if(tid < 2) { shared[tid] += shared[tid + 2]; }
__syncthreads();
if(tid < 1) { shared[tid] += shared[tid + 1]; }
__syncthreads();
通过对线程tid的条件判断实现了其执行顺序的控制