一维 grid 唯一索引
__global__ void unique_gid_calculation(int * input)
{
int tid = threadIdx.x;
int offset = blockIdx.x * blockDim.x;
int gid = tid + offset;
printf("blockIdx.x : %d, threadIdx.x : %d, gid : %d, value : %d \n",
blockIdx.x, tid, gid, input[gid]);
}
二维 grid 唯一索引
__global__ void unique_gid_calculation_2d(int * data)
{
int tid = threadIdx.x;
int block_offset = blockIdx.x * blockDim.x;
int row_offset = blockDim.x * gridDim.x * blockIdx.y;
int gid = row_offset + block_offset + tid;
printf("blockIdx.x : %d, blockIdx.y : %d, threadIdx.x : %d, gid : %d - data : %d \n",
blockIdx.x, blockIdx.y, tid, gid, data[gid]);
}
二维 grid 二维 block
__global__ void unique_gid_calculation_2d_2d(int * data)
{
int tid = blockDim.x * threadIdx.y + threadIdx.x;
int num_threads_in_a_block = blockDim.x * blockDim.y;
int block_offset = blockIdx.x * num_threads_in_a_block;
int num_threads_in_a_row = num_threads_in_a_block * gridDim.x;
int row_offset = num_threads_in_a_row * blockIdx.y;
int gid = tid + block_offset + row_offset;
printf("blockIdx.x : %d, blockIdx.y : %d, threadIdx.x : %d, gid : %d - data : %d \n",
blockIdx.x, blockIdx.y, tid, gid, data[gid]);
}
三维 grid 三维 block
__global__ void mem_trans(int* input)
{
int threadId_3D = threadIdx.x + threadIdx.y * blockDim.x +
threadIdx.z * blockDim.x * blockDim.y;
int blockId_3D = blockIdx.x + blockIdx.y * gridDim.x +
blockIdx.z * gridDim.x * gridDim.y;
int gid = threadId_3D + (blockDim.x * blockDim.y * blockDim.z) * blockId_3D;
printf("tid : %d, gid : %d, value : %d\n",
threadIdx.x, gid, input[gid]);
}