Anoracx
Anoracx

Reputation: 438

2D array with CUDA and cudaMallocPitch

I have been reading a few threads on stackoverflow about 2D arrays and cudaMallocPitch and I have tried to use cudaMallocPitch with the small documentation I have found. However I'm now facing a problem.

I need to go through an array and do something similar :

 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;
     }
  }

I was thus wondering, how should I adapt this code to make it work with the pitch, because I have read that I had to update the pointer to the beginning of the row. Of course my kernel receives the following :

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

And I'm not really sure how to make things working, I've been reading and trying, but it seems not working at the moment.

Thank you.

Edit 1: I have been reading this thread in particular :How to use 2D Arrays in CUDA? and came across the lines :

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];  
      }  
 }  

Which is updating the pointer of the next row, I'am not sure how to use to make my 2 for loops and while working such as in the previous code.

At the moment I can only access one dimension of my array but not the other one.

it returns the value 2, but when I try my multiple comparisons, it only returns 0, or even comparing two values do not work.

Upvotes: 1

Views: 3329

Answers (1)

Hugo Maxwell
Hugo Maxwell

Reputation: 763

In the CUDA Reference Manual it says:

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

[...]

Given the row and column of an array element of type T, the address is computed as:

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

So you need to cast your pointer first to char*, do the math and then cast it back to your type.

Upvotes: 2

Related Questions