私は、配列を32 * 32の要素を持ち、値が0〜1023の縮小を使って配列の和を求める関数を実装しています。 私の予想される合計値は523776ですが、私の再考は15872です、それは間違っています。ここ は私のコードです:還元によってCUDAの配列の総和を見つける方法
#include <stdio.h>
#include <cuda.h>
#define w 32
#define h 32
#define N w*h
__global__ void reduce(int *g_idata, int *g_odata);
void fill_array (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);
fill_array(a, N);
// copy inputs to device
cudaMemcpy(dev_a, a, size, cudaMemcpyHostToDevice);
cudaMemcpy(dev_b, b, size, cudaMemcpyHostToDevice);
dim3 blocksize(16,16);
dim3 gridsize;
gridsize.x=(w+blocksize.x-1)/blocksize.x;
gridsize.y=(h+blocksize.y-1)/blocksize.y;
reduce<<<gridsize, blocksize>>>(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 \n", b[0]);
cudaFree(dev_a);
cudaFree(dev_b);
return 0;
}
__global__ void reduce(int *g_idata, int *g_odata) {
__shared__ int sdata[256];
// 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)
atomicAdd(g_odata,sdata[0]);
}
// CPU function to generate a vector of random integers
void fill_array (int *a, int n)
{
for (int i = 0; i < n; i++)
a[i] = i;
}
あなたは[SO] – talonmies
@talonmies申し訳ありませんが、私は私のコードを更新上の援助をデバッグしたい場合は、完全な例を提供する必要があります。 –
カーネルを呼び出す前にどこでもbまたはdev_bを初期化することはありません。 – talonmies