基于共享存储器的1d模板CUDA实现中的负数组索引

use*_*610 6 arrays cuda

我目前正在使用CUDA编程,我正在尝试从我在网上找到的工作室学习幻灯片,可以在这里找到.我遇到的问题是幻灯片48.可以在那里找到以下代码:

__global__ void stencil_1d(int *in, int *out) {

    __shared__ int temp[BLOCK_SIZE + 2 * RADIUS];

    int gindex = threadIdx.x + blockIdx.x * blockDim.x;
    int lindex = threadIdx.x + RADIUS;

    // Read input elements into shared memory
    temp[lindex] = in[gindex];
    if (threadIdx.x < RADIUS) {
        temp[lindex - RADIUS] = in[gindex - RADIUS];
        temp[lindex + BLOCK_SIZE] = in[gindex + BLOCK_SIZE];
    }

....
Run Code Online (Sandbox Code Playgroud)

添加一些上下文.我们有一个叫做in长度的数组N.然后,我们有另一个out具有长度的数组N+(2*RADIUS),其中RADIUS具有3此特定示例的值.我们的想法是复制阵列in,到数组out但到阵列放置in在适当的位置3从阵列的开头outout = [RADIUS][in][RADIUS],参见图形表示幻灯片.

混乱来自以下几行:

 temp[lindex - RADIUS] = in[gindex - RADIUS];
Run Code Online (Sandbox Code Playgroud)

如果gindex是0我们的话in[-3].我们如何读取数组中的负数索引?真的很感激任何帮助.

pQB*_*pQB 6

您假设该in数组指向已为此数组分配的内存的第一个位置.但是,如果您看到幻灯片47,则in数组在数据之前之后具有三个元素的光晕(橙色框)(表示为绿色立方体).

我的假设是(我还没有完成研讨会)输入数组首先用晕圈初始化,然后指针在内核调用中移动.就像是:

stencil_1d<<<dimGrid, dimBlock>>>(in + RADIUS, out);
Run Code Online (Sandbox Code Playgroud)

因此,在内核中,这样做in[-3]是安全的,因为指针不在数组的开头.


Jac*_*ern 5

pQB的答案是正确的.您应该通过偏移输入数组指针RADIUS.

为了表明这一点,我在下面提供了一个完整的例子.希望它对其他用户有益.

(我会说__syncthreads()在共享内存加载后你需要一个.我在下面的例子中添加了它).

#include <thrust/device_vector.h>

#define RADIUS      3
#define BLOCKSIZE   32

/*******************/
/* iDivUp FUNCTION */
/*******************/
int iDivUp(int a, int b){ return ((a % b) != 0) ? (a / b + 1) : (a / b); }

/********************/
/* CUDA ERROR CHECK */
/********************/
#define gpuErrchk(ans) { gpuAssert((ans), __FILE__, __LINE__); }
inline void gpuAssert(cudaError_t code, const char *file, int line, bool abort=true)
{
   if (code != cudaSuccess) 
   {
      fprintf(stderr,"GPUassert: %s %s %d\n", cudaGetErrorString(code), file, line);
      if (abort) exit(code);
   }
}

/**********/
/* KERNEL */
/**********/
__global__ void moving_average(unsigned int *in, unsigned int *out, unsigned int N) {

    __shared__ unsigned int temp[BLOCKSIZE + 2 * RADIUS];

    unsigned int gindexx = threadIdx.x + blockIdx.x * blockDim.x;

    unsigned int lindexx = threadIdx.x + RADIUS;

    // --- Read input elements into shared memory
    temp[lindexx] = (gindexx < N)? in[gindexx] : 0;
    if (threadIdx.x < RADIUS) {
        temp[threadIdx.x] = (((gindexx - RADIUS) >= 0)&&(gindexx <= N)) ? in[gindexx - RADIUS] : 0;
        temp[threadIdx.x + (RADIUS + BLOCKSIZE)] = ((gindexx + BLOCKSIZE) < N)? in[gindexx + BLOCKSIZE] : 0;
    }

    __syncthreads();

    // --- Apply the stencil
    unsigned int result = 0;
    for (int offset = -RADIUS ; offset <= RADIUS ; offset++) {
        result += temp[lindexx + offset];
    }

    // --- Store the result
    out[gindexx] = result;
}

/********/
/* MAIN */
/********/
int main() {

    const unsigned int N        = 55 + 2 * RADIUS;

    const unsigned int constant = 4;

    thrust::device_vector<unsigned int> d_in(N, constant);
    thrust::device_vector<unsigned int> d_out(N);

    moving_average<<<iDivUp(N, BLOCKSIZE), BLOCKSIZE>>>(thrust::raw_pointer_cast(d_in.data()), thrust::raw_pointer_cast(d_out.data()), N);
    gpuErrchk(cudaPeekAtLastError());
    gpuErrchk(cudaDeviceSynchronize());

    thrust::host_vector<unsigned int> h_out = d_out;

    for (int i=0; i<N; i++)
        printf("Element i = %i; h_out = %i\n", i, h_out[i]);

    return 0;

}
Run Code Online (Sandbox Code Playgroud)