我正在嘗試將平鋪的2D矩陣加載到共享內存中,將數據沿x移動,寫回到全局內存也隨y移動。輸入數據因此沿着x和y移動。我有:CUDA:在共享內存上移位數組
__global__ void test_shift(float *data_old, float *data_new)
{
uint glob_index = threadIdx.x + blockIdx.y*blockDim.x;
__shared__ float VAR;
__shared__ float VAR2[NUM_THREADS];
// load from global to shared
VAR = data_old[glob_index];
// do some stuff on VAR
if (threadIdx.x < NUM_THREADS - 1)
{
VAR2[threadIdx.x + 1] = VAR; // shift (+1) along x
}
__syncthreads();
// write to global memory
if (threadIdx.y < ny - 1)
{
glob_index = threadIdx.x + (blockIdx.y + 1)*blockDim.x; // redefine glob_index to shift along y (+1)
data_new[glob_index] = VAR2[threadIdx.x];
}
到內核的呼叫:
test_shift <<< grid, block >>> (data_old, data_new);
和網格和塊(blockDim.x等於矩陣寬度,即64):
dim3 block(NUM_THREADS, 1);
dim3 grid(1, ny);
我無法實現它。有人能指出這有什麼問題嗎?我應該使用一個跨度索引還是偏移量?