【发布时间】:2011-06-17 22:16:09
【问题描述】:
我正在编写一个 CUDA 内核来为 rows*cols 主矩阵中的每个位置创建一个 3x3 协方差矩阵。所以这个 3D 矩阵的大小是 rows*cols*9,我相应地分配在一个 malloc 中。我需要在单个索引值中访问它
3x3 协方差矩阵的 9 个值根据其他一些 2D 数组中的相应行 r 和列 c 获取它们的值。
换句话说 - 我需要计算适当的索引来访问 3x3 协方差矩阵的 9 个元素,以及作为值输入的 2D 矩阵的行和列偏移量,以及适当的索引用于存储阵列。
我已尝试将其简化为以下内容:
//I am calling this kernel with 1D blocks who are 512 cols x 1row. TILE_WIDTH=512
int bx = blockIdx.x;
int by = blockIdx.y;
int tx = threadIdx.x;
int ty = threadIdx.y;
int r = by + ty;
int c = bx*TILE_WIDTH + tx;
int offset = r*cols+c;
int ndx = r*cols*rows + c*cols;
if((r < rows) && (c < cols)){ //this IF statement is trying to avoid the case where a threadblock went bigger than my original array..not sure if correct
d_cov[ndx + 0] = otherArray[offset];//otherArray just contains a value that I might do some operations on to set each of the ndx0-ndx9 values in d_cov
d_cov[ndx + 1] = otherArray[offset];
d_cov[ndx + 2] = otherArray[offset];
d_cov[ndx + 3] = otherArray[offset];
d_cov[ndx + 4] = otherArray[offset];
d_cov[ndx + 5] = otherArray[offset];
d_cov[ndx + 6] = otherArray[offset];
d_cov[ndx + 7] = otherArray[offset];
d_cov[ndx + 8] = otherArray[offset];
}
当我用 CPU 上计算的值检查这个数组时,它会循环 i=rows, j=cols, k = 1..9
结果不匹配。
换句话说 d_cov[i*rows*cols + j*cols + k] != correctAnswer[i][j][k]
谁能给我一些关于如何解决这个问题的提示?是索引问题,还是其他逻辑错误?
【问题讨论】:
标签: c++ arrays indexing multidimensional-array cuda