1

我一直在阅读有关 2D 数组和 cudaMallocPitch 的 stackoverflow 上的一些线程,并且我尝试将 cudaMallocPitch 与我找到的小文档一起使用。但是我现在面临一个问题。

我需要通过一个数组并做类似的事情:

 for(int k=0; k<100; ++k){
     for(i=SID; i<SID+stride; ++i){
        while(-1 < j && Driver[k][j] != Road[i]){
            j = Pilot[j][k];

        }
        ++j;
     }
  }

因此我想知道,我应该如何调整这段代码以使其适用于音高,因为我已经读过我必须将指针更新到行的开头。当然,我的内核收到以下内容:

__global__ void driving(char *Driver, size_t pitch_driver, 
                        char *Road, int *Pilot, size_t pitch_pilot) 

而且我不确定如何使事情正常进行,我一直在阅读和尝试,但目前似乎不起作用。

谢谢你。

编辑 1:我一直在阅读这个主题:如何在 CUDA 中使用 2D 数组?并遇到了以下问题:

for (int row = 0; row < rowCount; row++)  
 {  
     // update the pointer to point to the beginning of the next row  
    float* rowData = (float*)(((char*)d_array) + (row * pitch));  
    for (int column = 0; column < columnCount; column++)  
     {  
       rowData[column] = 123.0; // make every value in the array 123.0  
       destinationArray[(row*columnCount) + column] = rowData[column];  
      }  
 }  

这是更新下一行的指针,我不确定如何使用我的 2 for 循环以及在前面的代码中工作时。

目前我只能访问数组的一维,而不能访问另一维。

它返回值 2,但是当我尝试多重比较时,它只返回 0,甚至比较两个值都不起作用。

4

1 回答 1

2

在 CUDA 参考手册中它说:

5.8.2.17 cudaError_t cudaMallocPitch (void devPtr, size_t pitch, size_t width, size_t height)

[...]

给定类型 T 的数组元素的行和列,地址计算如下:

T* pElement = (T*)((char*)BaseAddress + Row * pitch) + Column;

因此,您需要先将指针转换为 char*,进行数学运算,然后再将其转换回您的类型。

于 2013-06-04T15:34:20.760 回答