0

我有这个内核

__global__ void kernel1(int keep, int include, int width, int* d_Xco, 
              int* d_Xnum, bool* d_Xvalid, float* d_Xblas)
{

  int i, k;  
  i = threadIdx.x + blockIdx.x * blockDim.x;

  if(i < keep){

    for(k = 0; k < include ; k++){

      int val = (d_Xblas[i*include + k] >= 1e5);
      int aux = d_Xnum[i];

      d_Xblas[i*include + k] *= (!val);
      d_Xco[i*width + aux] = k;
      d_Xnum[i] +=val;
      d_Xvalid[i*include + k] = (!val);
    }
  }
}

推出与

int keep = 9000;
int include = 23000;
int width = 0.2*include;

int threads = 192;
int blocks = keep+threads-1/threads;
kernel1 <<< blocks,threads  >>>( keep, include, width,
                                 d_Xco, d_Xnum, d_Xvalid, d_Xblas );

kernel1工作正常,但显然没有完全优化。我认为消除内部循环会很简单,k但由于某种原因它不能正常工作。我的第一个想法是:

__global__ void kernel2(int keep, int include, int width, 
               int* d_Xco, int* d_Xnum, bool* d_Xvalid, 
               float* d_Xblas)
{

  int i, k;  
  i = threadIdx.x + blockIdx.x * blockDim.x;
  k = threadIdx.y + blockIdx.y * blockDim.y;

  if((i < keep)  && (k < include) ) {

      int val = (d_Xblas[i*include + k] >= 1e5);
      int aux = d_Xnum[i];
      d_Xblas[i*include + k] *= (float)(!val);
      d_Xco[i*width + aux] = k;
      atomicAdd(&d_Xnum[i], val);
      d_Xvalid[i*include + k] = (!val);
  }
}

使用 2D 网格启动:

int keep = 9000;
int include = 23000;
int width = 0.2*include;

int th = 32;
dim3 threads(th,th);
dim3 blocks ((keep+threads.x-1)/threads.x, (include+threads.y-1)/threads.y);
kernel2 <<< blocks,threads >>>( keep, include, width, d_Xco, d_Xnum, 
                               d_Xvalid, d_Xblas );

虽然我相信这个想法很好,但它不起作用,我的想法已经用完了。你能帮帮我吗?我还认为问题可能在于d_Xco将位置存储k在较小的数组中并将它们推到数组的开头,因此顺序很重要。

d_Xco
-------------------------------
| 2|3 |15 |4 |5 |5 | | | | | | .......
-------------------------------
4

1 回答 1

1

在原始代码中,您有

for(k = 0; k < include ; k++){
  ...
  int aux = d_Xnum[i];
  ...
  d_Xco[i*width + aux] = k;
  ...
}

数组的索引d_Xco不依赖于k,因此每次迭代都写入它是多余的。最终值将始终为include-1。因此,将循环内的这两行替换为循环k一行:k

 d_Xco[i*width + d_Xnum[i]] = include - 1;

一旦你这样做了,当你并行化循环时,当许多线程同时将不同的值分配给同一位置时,k你将不再有当前的竞争条件(不保证排序)。kd_Xco

于 2012-10-10T11:53:01.293 回答