繁体   English   中英

无法将偏移量索引计算为3D数组

[英]trouble calculating offset index into 3D array

我正在编写CUDA内核,以为row * cols主矩阵中的每个位置创建3x3协方差矩阵。 这样3D矩阵的大小为row * 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 =行,j = cols,k = 1..9

结果不匹配。

换句话说d_cov [i * rowscols + j * cols + k]!= correctAnswer [i] [j] [k]

谁能给我有关解决这个问题的任何技巧? 是索引问题还是其他逻辑错误?

而不是答案(我还没有足够努力地寻找答案),这是我通常用于调试此类问题的技术。 首先,将目标数组中的所有值设置为NaN。 (您可以通过cudaMemset做到这一点-将每个字节设置为0xFF。)然后尝试将每个位置统一设置为该行的值,然后检查结果。 从理论上讲,它应该类似于:

0 0 0 ... 0
1 1 1 ... 1
. . . .   .
. . .  .  .
. . .   . .
n n n ... n

如果看到NaN,则表示您无法写入元素。 如果您看到行元素错位,则说明出现了问题,并且通常会以提示性模式错位。 对列值和平面执行类似的操作。 通常,此技巧可帮助我发现索引计算的一部分出错了,这是大部分工作。 希望能有所帮助。

我可能只是愚蠢,但这行的逻辑是什么?

int ndx = r*cols*rows + c*cols;

你不应该

int ndx = offset*9;

如果您说协方差数组的大小是rows * cols * 9,那么不会偏移* 9将您带到3D协方差数组中与输入数组相同的位置。 因此,offset * 9 + 0将是元素在偏移处的3x3协方差矩阵的位置(0,0),offset * 9 + 1将是(0,1),offset * 9 + 2将是(0, 2),offset * 9 + 3将为(1,0),依此类推,直到offset * 9 + 8。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM