我正试图使用CUDA将数组缩减为其元素的总和。我在将设备中计算出的总和传回主机以便打印出来时遇到问题。
这是我得到的输出:
阵列内容:33 36 27 15 43 35 36 42 49 21
阵列元素的减少总和=4204303
减少总和显然是错误的。
这是我的密码。
#include <stdio.h>
#include <cuda.h>
#define N 10
__global__ void reduce(int *g_idata, int *g_odata);
void random_ints (int *a, int n);
int main( void ) {
int a[N], b[N]; // copies of a, b, c
int *dev_a, *dev_b; // device copies of a, b, c
int size = N * sizeof( int ); // we need space for 512 integers
// allocate device copies of a, b, c
cudaMalloc( (void**)&dev_a, size );
cudaMalloc( (void**)&dev_b, size );
//a = (int *)malloc( size );
//b = (int *)malloc( size );
random_ints( a, N );
printf("contents of Array: ");
for(int i =0; i<N; i++)
{
printf(" %d ", a[i]);
}
printf("n");
// copy inputs to device
cudaMemcpy( dev_a, a, size, cudaMemcpyHostToDevice );
cudaMemcpy( dev_b, b, size, cudaMemcpyHostToDevice );
// launch dot() kernel with 1 block and N threads
reduce<<< 1, N >>>( dev_a, dev_b);
// copy device result back to host copy of c
cudaMemcpy( b, dev_b, sizeof( int ) , cudaMemcpyDeviceToHost );
printf("Reduced sum of Array elements = %d ", b[0]);
//free( a );
// free( b );
cudaFree( dev_a );
cudaFree( dev_b );
return 0;
}
__global__ void reduce(int *g_idata, int *g_odata) {
extern __shared__ int sdata[];
// each thread loads one element from global to shared mem
int i = blockIdx.x*blockDim.x + threadIdx.x;
sdata[threadIdx.x] = g_idata[i];
__syncthreads();
// do reduction in shared mem
for (int s=1; s < blockDim.x; s *=2)
{
int index = 2 * s * threadIdx.x;;
if (index < blockDim.x)
{
sdata[index] += sdata[index + s];
}
__syncthreads();
}
// write result for this block to global mem
if (threadIdx.x == 0) g_odata[blockIdx.x] = sdata[0];
}
// CPU function to generate a vector of random integers
void random_ints (int *a, int n) {
for (int i = 0; i < n; i++)
a[i] = rand() % 50; // random number between 0 and 49
}
如果使用extern
共享内存,则需要指定共享内存的数量
您有两种解决方案:
带有
extern __shared__ int sdata[];
reduce<<< 1, N, N*sizeof(int) >>>( dev_a, dev_b);
使用此参数,设置要在此内核中使用的共享内存的大小
带有
__shared__ int sdata[N];
其中N是恒定数量的元素。