CUDA:在大型二维阵列上共享内存

标签 cuda

我在类作业中遇到了一个简单的 CUDA 问题,但教授添加了一个可选任务来使用共享内存来实现相同的算法。我无法在截止日期之前完成它(因为上交日期是一周前),但我仍然很好奇,所以现在我要上网 ;)。

基本任务是在顺序和 CUDA 中实现红黑连续过度松弛的 SCSS 版本,确保在两者中获得相同的结果,然后比较加速。就像我说的,使用共享内存来做是一个可选的 +10% 附加组件。

我将发布我的工作版本和我尝试做的伪代码,因为我目前没有代码,但如果有人需要,我可以稍后用实际代码更新它。

在任何人说出来之前:是的,我知道使用 CUtil 是蹩脚的,但它使比较和计时器更容易。

工作全局内存版本:

#include <stdlib.h>
#include <stdio.h>
#include <cutil_inline.h>

#define N 1024

__global__ void kernel(int *d_A, int *d_B) {
    unsigned int index_x = blockIdx.x * blockDim.x + threadIdx.x;
    unsigned int index_y = blockIdx.y * blockDim.y + threadIdx.y;

    // map the two 2D indices to a single linear, 1D index
    unsigned int grid_width = gridDim.x * blockDim.x;
    unsigned int index = index_y * grid_width + index_x;

    // check for boundaries and write out the result
    if((index_x > 0) && (index_y > 0) && (index_x < N-1) && (index_y < N-1))
        d_B[index] = (d_A[index-1]+d_A[index+1]+d_A[index+N]+d_A[index-N])/4;

}

main (int argc, char **argv) {

    int A[N][N], B[N][N];
    int *d_A, *d_B; // These are the copies of A and B on the GPU
    int *h_B; // This is a host copy of the output of B from the GPU
    int i, j;
    int num_bytes = N * N * sizeof(int);

    // Input is randomly generated
    for(i=0;i<N;i++) {
        for(j=0;j<N;j++) {
            A[i][j] = rand()/1795831;
            //printf("%d\n",A[i][j]);
        }
    }

    cudaEvent_t start_event0, stop_event0;
    float elapsed_time0;
    CUDA_SAFE_CALL( cudaEventCreate(&start_event0) );
    CUDA_SAFE_CALL( cudaEventCreate(&stop_event0) );
    cudaEventRecord(start_event0, 0);
    // sequential implementation of main computation
    for(i=1;i<N-1;i++) {
        for(j=1;j<N-1;j++) {
            B[i][j] = (A[i-1][j]+A[i+1][j]+A[i][j-1]+A[i][j+1])/4;
        }
    }
    cudaEventRecord(stop_event0, 0);
    cudaEventSynchronize(stop_event0);
    CUDA_SAFE_CALL( cudaEventElapsedTime(&elapsed_time0,start_event0, stop_event0) );



    h_B = (int *)malloc(num_bytes);
    memset(h_B, 0, num_bytes);
    //ALLOCATE MEMORY FOR GPU COPIES OF A AND B
    cudaMalloc((void**)&d_A, num_bytes);
    cudaMalloc((void**)&d_B, num_bytes);
    cudaMemset(d_A, 0, num_bytes);
    cudaMemset(d_B, 0, num_bytes);

    //COPY A TO GPU
    cudaMemcpy(d_A, A, num_bytes, cudaMemcpyHostToDevice);

    // create CUDA event handles for timing purposes
    cudaEvent_t start_event, stop_event;
    float elapsed_time;
    CUDA_SAFE_CALL( cudaEventCreate(&start_event) );
    CUDA_SAFE_CALL( cudaEventCreate(&stop_event) );
    cudaEventRecord(start_event, 0);

// TODO: CREATE BLOCKS AND THREADS AND INVOKE GPU KERNEL
    dim3 block_size(256,1,1); //values experimentally determined to be fastest

    dim3 grid_size;
    grid_size.x = N / block_size.x;
    grid_size.y = N / block_size.y;

    kernel<<<grid_size,block_size>>>(d_A,d_B);

    cudaEventRecord(stop_event, 0);
    cudaEventSynchronize(stop_event);
    CUDA_SAFE_CALL( cudaEventElapsedTime(&elapsed_time,start_event, stop_event) );

    //COPY B BACK FROM GPU
    cudaMemcpy(h_B, d_B, num_bytes, cudaMemcpyDeviceToHost);

    // Verify result is correct
    CUTBoolean res = cutComparei( (int *)B, (int *)h_B, N*N);
    printf("Test %s\n",(1 == res)?"Passed":"Failed");
    printf("Elapsed Time for Sequential: \t%.2f ms\n", elapsed_time0);
    printf("Elapsed Time for CUDA:\t%.2f ms\n", elapsed_time);
    printf("CUDA Speedup:\t%.2fx\n",(elapsed_time0/elapsed_time));

    cudaFree(d_A);
    cudaFree(d_B);
    free(h_B);

    cutilDeviceReset();
}

对于共享内存版本,这是我迄今为止尝试过的:
#define N 1024

__global__ void kernel(int *d_A, int *d_B, int width) {
    //assuming width is 64 because that's the biggest number I can make it
    //each MP has 48KB of shared mem, which is 12K ints, 32 threads/warp, so max 375 ints/thread?
    __shared__ int A_sh[3][66];

    //get x and y index and turn it into linear index

    for(i=0; i < width+2; i++)  //have to load 2 extra values due to the -1 and +1 in algo
          A_sh[index_y%3][i] = d_A[index+i-1]; //so A_sh[index_y%3][0] is actually d_A[index-1]

    __syncthreads(); //and hope that previous and next row have been loaded by other threads in the block?

    //ignore boundary conditions because it's pseudocode
    for(i=0; i < width; i++)
        d_B[index+i] = A_sh[index_y%3][i] + A_sh[index_y%3][i+2] + A_sh[index_y%3-1][i+1] + A_sh[index_y%3+1][i+1];

}

main(){
   //same init as above until threads/grid init

   dim3 threadsperblk(32,16);
   dim3 numblks(32,64);

   kernel<<<numblks,threadsperblk>>>(d_A,d_B,64);

   //rest is the same
}

这个共享的内存代码崩溃(“由于未指定的错误启动失败”),因为我还没有捕获所有的边界条件,但我并不担心找到正确的方法来让事情顺利进行。我觉得我的代码太复杂了,不能成为正确的路径(尤其是与 SDK 示例相比),但我也看不到另一种方法来做到这一点,因为我的数组不像我的所有示例那样适合共享内存可以找到。

坦率地说,我不确定它在我的硬件上会快得多(GTX 560 Ti - 在 0.121 毫秒内运行全局内存版本),但我需要先向自己证明:P

编辑 2:对于将来遇到此问题的任何人,如果您想做一些共享内存,答案中的代码是一个很好的起点。

最佳答案

在 CUDA 中充分利用这些模板运算符的关键是数据重用。我发现最好的方法通常是让每个块“走过”网格的一个维度。在块将初始数据块加载到共享内存后,只需要从全局内存中读取一个维度(因此行主序二维问题中的行),以便在共享内存中为第二个和后续提供必要的数据行计算。其余的数据可以重复使用。为了可视化共享内存缓冲区如何查看此类算法的前四个步骤:

  • 输入网格的三个“行”(a,b,c)被加载到共享内存,并且为行(b)计算的模板并写入全局内存

    啊啊啊啊啊啊啊啊
    bbbbbbbbbbbbbbbb
    cccccccccccccccc
  • 另一行 (d) 加载到共享内存缓冲区,替换行 (a),并使用不同的模板对行 (c) 进行计算,反射(reflect)行数据在共享内存中的位置

    dddddddddddddddd
    bbbbbbbbbbbbbbbb
    cccccccccccccccc
  • 另一行 (e) 被加载到共享内存缓冲区中,替换行 (b) 和对行 (d) 进行的计算,使用与步骤 1 或 2 不同的模板。

    dddddddddddddddd
    eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
    cccccccccccccccc
  • 另一行 (f) 加载到共享内存缓冲区中,替换行 (c) 以及对行 (e) 进行的计算。现在数据恢复到与步骤 1 中使用的相同布局,并且可以使用步骤 1 中使用的相同模板。

    dddddddddddddddd
    eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
    ffffffffffffffff

  • 整个循环重复进行,直到块遍历了输入网格的全列长度。使用不同的模板而不是移动共享内存缓冲区中的数据的原因归结于性能——共享内存在 Fermi 上只有大约 1000 Gb/s 的带宽,数据的移动将成为完全优化代码的瓶颈。您应该尝试不同的缓冲区大小,因为您可能会发现较小的缓冲区允许更高的占用率和改进的内核吞吐量。

    编辑:给出一个具体的例子,说明如何实现:
    template<int width>
    __device__ void rowfetch(int *in, int *out, int col)
    {
        *out = *in;
        if (col == 1) *(out-1) = *(in-1);   
        if (col == width) *(out+1) = *(in+1);   
    }
    
    template<int width>
    __global__ operator(int *in, int *out, int nrows, unsigned int lda)
    {
        // shared buffer holds three rows x (width+2) cols(threads)
        __shared__ volatile int buffer [3][2+width]; 
    
        int colid = threadIdx.x + blockIdx.x * blockDim.x;
        int tid = threadIdx.x + 1;
    
        int * rowpos = &in[colid], * outpos = &out[colid];
    
        // load the first three rows (compiler will unroll loop)
        for(int i=0; i<3; i++, rowpos+=lda) {
            rowfetch<width>(rowpos, &buffer[i][tid], tid);
        }
    
        __syncthreads(); // shared memory loaded and all threads ready
    
        int brow = 0; // brow is the next buffer row to load data onto
        for(int i=0; i<nrows; i++, rowpos+=lda, outpos+=lda) {
    
            // Do stencil calculations - use the value of brow to determine which
            // stencil to use
            result = ();
            // write result to outpos
            *outpos = result;
    
            // Fetch another row
            __syncthreads(); // Wait until all threads are done calculating
            rowfetch<width>(rowpos, &buffer[brow][tid], tid);
            brow = (brow < 2) ? (brow+1) : 0; // Increment or roll brow over
            __syncthreads(); // Wait until all threads have updated the buffer
        }
    }
    

    关于CUDA:在大型二维阵列上共享内存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5794617/

    相关文章:

    c++ - 即使在初始化结果参数后,CUDA atomicAdd 也会产生错误的结果

    cuda - __global__ 函数如何像 C/C++ 那样返回值或中断

    cudaMemset() - 它设置字节还是整数?

    开源 CUDA IDE

    c++ - OpenCV CUDA 运行速度比 OpenCV CPU 慢

    c++ - CUDA 内核似乎忽略了 "if"语句

    python - 如何将二维数组传递到 pycuda 中的内核?

    linux - Ubuntu 14.04 CUDA 8.0 未满足的依赖关系

    c++ - 二维数组问题-我收到错误 : expression must have pointer-to-object type (opencv, CUDA)

    c++ - cuModuleGetFunction 返回未找到