lava
lava

Reputation: 1

Cudamemcpy2d error

cudamemcpy2d is returning error -- following is the code in the code below i have mentioned the line where i am getting an error. please look through and help me.

main(){
    int nrow = 16,ncol = 41; 
// 
    double **x = new double*[nrow];
    double **y = new double*[nrow];

    for(int i=0; i<nrow; i++){
        x[i] = new double[ncol];
        y[i] = new double[ncol];
    }

 // both x and y are filled with some values and function is called;
    function(x,y, nrow, ncol);
}

void function(double **x, double **y, int nrow, ncol){
      double *dev_x, *dev_y;
      size_t pitch_x, pitch_y;

      cudaMallocPitch((void **) &dev_x, &pitch_x, (n_col*sizeof(double)), nrow);
      cudaMallocPitch((void **) &dev_y, &pitch_y, (n_col*sizeof(double)), nrow);

   // this below line is returning error invalid value

      cudaMemcpy2D((void *)dev_x, pitch_x, (void *) *x, sizeof(double)*ncol, sizeof(double)*ncol, nrow, cudaMemcpyDeviceToHost);

  //launch a kernel
  kernel<<< 1, 1>>>(dev_x, dev_y, nrow, ncol);

   //below also gives the same error..

      cudaMemcpy2D((void *) *x, sizeof(double)*ncol, dev_x, pitch_x, sizeof(double)*ncol, nrow, cudaMemcpyHostToDevice);
}

please help thank you

Upvotes: 0

Views: 605

Answers (1)

Robert Crovella
Robert Crovella

Reputation: 151799

You have your directions reversed. The first cudaMemcpy2D operation is copying from the Host to the Device, and you should specify cudaMemcpyHostToDevice.

Likewise the second cudaMemcpy2D is going in the other direction (based on the order of pointers you are passing) and so should specify cudaMemcpyDeviceToHost.

The first pointer you pass to cudaMemcpy2D is the destination pointer.

Your usage of *d_IntPts_X may be a problem as well. Normally you should pass a single pointer (*) flattened 1D array to cudaMemcpy type operations. But since you haven't shown the definition of that variable/pointer, I can't say for sure.

EDIT: Now that you've changed the parameters, you can't use your x array this way in cudaMemcpy2D. Instead, flatten x into a one dimensional array, perhaps something like this:

double *flat_x = new double[nrow*ncol];

and use that instead in your cudaMemcpy2D calls.

Also, there are plenty of questions like this on the CUDA tag. Please search and read some of those before asking "why?" "Doesn't it handle 2D matrices?" "What if I really want to use a 2D matrix?" etc.

Upvotes: 1

Related Questions