我正在尝试将展平的 2D 矩阵加载到共享内存中,沿 x 移动数据,也沿 y 移动写回全局内存。因此,输入数据沿 x 和 y 移动。我有的:
__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);
以及grid和blocks(blockDim.x等于矩阵宽度,即64):
dim3 block(NUM_THREADS, 1);
dim3 grid(1, ny);
我无法实现它。有人可以指出这有什么问题吗?我应该使用跨步索引还是偏移量?