CUDA 数组缩减为元素总和。如何从设备向主机传达答案并打印?

CUDA array reduction to sum of elements. How to communicate answer from device to host and print?

我正在尝试使用 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 个元素的数量不变。