大数组的前缀扫描

时间:2015-07-02 21:47:13

标签: cuda gpgpu

我想使用GPUgem中的指令为大型数组编写前缀扫描,这是我的并行类的作业。我确实按照书中的所有步骤操作,但我的代码仍无效。我让它适用于阵列大小4096,但它不适用于更大的阵列。这是我的代码:

#include <stdio.h>
#include <sys/time.h>
#define THREADS 1024
typedef int mytype;

__global__ void phaseI(mytype *g_odata, mytype *g_idata, int n, mytype *aux)
{
  __shared__ mytype temp[THREADS];
  const int tid1 = threadIdx.x;
  int offset = 1;
  temp[2*tid1] = g_idata[2*tid1]; // load input into shared memory
  temp[2*tid1+1] = g_idata[2*tid1+1];
  for (int d = THREADS>>1; d > 0; d >>= 1) // build sum in place up the tree
  {
    __syncthreads();
    if (tid1 < d)
    {
      int ai = offset*(2*tid1+1)-1;
      int bi = offset*(2*tid1+2)-1;
      temp[bi] += temp[ai];
    }
    offset *= 2;
  }
  __syncthreads();
  if (tid1 == 0) {
    aux[blockIdx.x] = temp[THREADS - 1]; 
    temp[THREADS - 1] = 0;
  }
 for (int d = 1; d < THREADS; d *= 2) // traverse down tree & build scan
    {
      offset >>= 1;
      __syncthreads();
      if (tid1 < d)
      {
         int ai = offset*(2*tid1+1)-1;
         int bi = offset*(2*tid1+2)-1;
         mytype t = temp[ai];
         temp[ai] = temp[bi];
         temp[bi] += t;
      }
    }
  __syncthreads();
  g_odata[2*thid] = temp[2*thid]; // write results to device memory
  g_odata[2*thid+1] = temp[2*thid+1];
  }

__global__ void phaseII(mytype *g_odata, mytype *aux, int n)
{
  const int tid1 = threadIdx.x;
  const int B = (n / THREADS);
  int offset = 1;
 for (int d = B>>1; d > 0; d >>= 1) // build sum in place up the tree
  {
    __syncthreads();
    if (tid1 < d)
    {
      int ai = offset*(2*tid1+1)-1;
      int bi = offset*(2*tid1+2)-1;
      temp[bi] += temp[ai];
    }
    offset *= 2;
  }
  __syncthreads();
  if (tid1 == 0 && blockIdx.x == 0) {
    aux[B - 1] = 0;
  }
for (int d = 1; d < B; d *= 2) // traverse down tree & build scan
    {
      offset >>= 1;
      __syncthreads();
      if (tid1 < d)
      {
         int ai = offset*(2*tid1+1)-1;
         int bi = offset*(2*tid1+2)-1;
         mytype t = temp[ai];
         temp[ai] = temp[bi];
         temp[bi] += t;
      }
    }
  __syncthreads();  
  g_odata[2*thid] += aux[blockIdx.x];
  g_odata[2*thid+1] += aux[blockIdx.x];
}

int main(int argc, char *argv[])
{
  if (argc != 2) {
    printf("usage: %s n\n", argv[0]);
    return -1;
  }
  const int n = atoi(argv[1]);
  mytype *h_i, *d_i, *h_o, *d_o, *d_temp;
  const int size = n * sizeof(mytype);
  h_i = (mytype *)malloc(size);
  h_o = (mytype *)malloc(size);
  if ((h_i == NULL) || (h_o == NULL)) {
    printf("malloc failed\n");
    return -1;
  }
  for (int i = 0; i < n; i++) {
    h_i[i] = i;
    h_o[i] = 0;
  }
  cudaMalloc(&d_i, size);
  cudaMalloc(&d_temp, (n / THREADS) );
  cudaMalloc(&d_o, size);
  cudaMemset(d_o, 0, size);
  cudaMemset(d_temp, 0, (n / THREADS));
  cudaMemcpy(d_i, h_i, size, cudaMemcpyHostToDevice);
  int blocks = n / THREADS;
  phaseI<<<blocks, THREADS / 2 >>>(d_o, d_i, n, d_temp);
  phaseII<<<blocks, THREADS / 2>>>(d_o, d_temp, n);
  cudaThreadSynchronize();
  cudaMemcpy(h_o, d_o, size, cudaMemcpyDeviceToHost);
  printf("\n");
  for (int i = 0; i < n ; i++) {
    printf(" %d", h_o[i]); 
  }
  printf("\n\n");

  return 0;
}

有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

我在代码中看到的一个可能的错误是:

>>> message=[[0," "],[1,"yes","no","maybe"]]
>>> message[1][2]
'no'
>>> 

如果你的 aux[thid] = temp[THREADS]; 数组是temp,正如你所说,每个块有1024个线程,那么如果THREADS是1024,temp [THREADS]将访问你的共享内存阵列-of-bounds(结束一个。)1024个元素的数组只有0到1023之间的有效索引。

除此之外,您似乎在询问如何从共享内存数组(temp[1024])中取出最后一个元素并将其置于(可能是全局的)temp数组中的某个位置,每个块都有一个元素。

这是一个完全有效的例子:

aux