在3D中指定CUDA内核中的线程ID

时间:2014-03-10 18:37:02

标签: 3d cuda 2d

我正在努力将2D代码转换为3D实现

在2D中我有以下内容:

int row_number = blockIdx.y * blockDim.y + threadIdx.y
int column_number = blockIdx.x * blockDim.x + threadIdx.x
int threadId = row_number * grid_dimension + column_number

我正试图让这个工作在3D

int row_number = 
int column_number =
int depth_number = 
int threadId = row_number * grid_dimension + column_number + depth_number * grid_dimension * grid_dimension 

我的第一次尝试是:

int row_number = blockIdx.y * blockDim.y + threadIdx.y
int column_number = blockIdx.x * blockDim.x + threadIdx.x
int depth_number = blockIdx.z * blockDim.z + threadIdx.z
int threadId = row_number * grid_dimension + column_number + depth_number * grid_dimension * grid_dimension 

我在3D中的threadId的表达式是否正确,如果不正确,我如何获得3D中的行,列和深度数字?我已经看到了直接找到blockId和threadId的表达式,但这并不是我一直在寻找的。如果这不是问题,我可能还有其他需要调查的问题。

感谢。

1 个答案:

答案 0 :(得分:1)

您尚未定义grid_dimension的含义。

当然,这些配方可以获得行/列/深度指数:

unsigned int row_number = blockIdx.y * blockDim.y + threadIdx.y;
unsigned int column_number = blockIdx.x * blockDim.x + threadIdx.x;
unsigned int depth_number = blockIdx.z * blockDim.z + threadIdx.z;

要从上述变量构建全局唯一的线程ID,我们可以使用:

unsigned long long idx = column_number + (row_number * gridDim.x) + (depth_number * (gridDim.x * gridDim.y);