gpt4 book ai didi

cuda线程索引

转载 作者:行者123 更新时间:2023-12-01 14:38:33 24 4
gpt4 key购买 nike

这是矩阵索引的正确表达式吗(dim3 threadBlock = (A, B, 1), dim3 blockGrid = (C, D, 1),其中 A、B、C、D 是一些数字)?

int i = (blockIdx.y * gridDim.x + blockIdx.x) * blockDim.x + threadIdx.x;
int j = (blockIdx.x * gridDim.y + blockIdx.y) * blockDim.y + threadIdx.y;

最佳答案

这在我看来不正确。二维 CUDA 网格中任何线程的 (i,j) 索引是

int idx_i = blockIdx.x * blockDim.x + threadIdx.x;
int idx_j = blockIdx.y * blockDim.y + threadIdx.y;

如果您正在访问存储在线性内存中的数组,则等效的 (i,j) 索引是

int mindex_colmajor = idx_i + idx_j * LDA;

int mindex_rowmajor = idx_j + idy_i * LDA;

取决于数组是否存储在row major or column major order中内存中的第一维(或等价的间距)等于 LDA。然后您可以访问内存

value = array[mindex]

其中 mindex 是上面计算的列主索引或行主索引。

关于cuda线程索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9859456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com