Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

pass a 2D array from a C++ class to a CUDA function

Tags:

c++

cuda

I am a Java guy jumping into CUDA and the syntax is tripping me. I'm trying to create a matrix in the .cpp file then pass that off to the .cu file to be processed. I see examples where the CUDA function expects the 2D array to come in looking like

void handleMatrix(float* A){
    // do stuff
}

when I create the matrix I'm used to doing it in code that looks like this:

    int main()
{

   const int row=8;
   const int column=8;
   int rnum;
   srand(time(0));
   rnum = (rand() % 100) + 1;  

  float table[row][column];  
    for(int r=0; r<row; r++){ 
      for(int c=0; c<column;c++){       
        table[row][column] = (rand()%100) + 1.f;    
      }
      cout << "\n";
    }
   
   handleMatrix(table);
   return 0;
}

When I compile the code I'm getting the error cannot convert ‘float ()[8]’ to ‘float*’ for argument ‘1’ to ‘void handleMatrix(float*)’*

Is there a different way I should be declaring the matrix or creating it?

Thanks in advance for the help.

like image 675
Winter Avatar asked Nov 20 '25 06:11

Winter


1 Answers

You can do

handleMatrix(table[0]);

or, equivalently,

handleMatrix(&table[0][0]);

That's if 'handleMatrix' is host code. If it's device code, you can't allocate the buffer like that. You'll want to assemble an array in local memory, fill the entries, allocate another array in the device memory using cudaMalloc() or cudaMallocPitch(), and then copy from local to device using cudaMemcpy() or cudaMemcpy2D().

like image 133
Eugene Smith Avatar answered Nov 21 '25 18:11

Eugene Smith



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!