0

I have a hard time getting my struct malloc to work for cuda.

my struct looks like this:

typedef struct Map{
    int length;
    double *A;
    int *x;
    int *dx;
    int *y;
    int *dy;
    int *delta;
    int *phi;
}Map;

The malloc function I try to call is as follows:

void cudaMallocMap(Map **m, int p){
    cudaMalloc((void**)m, sizeof(Map));
    cudaMemset(&((**m).length), p, 1);
    if(p>0){
        cudaMalloc((void**)&((**m).A), p*sizeof(double));
        cudaMalloc((void**)&((**m).x), p*sizeof(int));
        cudaMalloc((void**)&((**m).dx), p*sizeof(int));
        cudaMalloc((void**)&((**m).y), p*sizeof(int));
        cudaMalloc((void**)&((**m).dy), p*sizeof(int));
        cudaMalloc((void**)&((**m).delta), p*sizeof(int));
        cudaMalloc((void**)&((**m).phi), p*sizeof(int));
    }
}

It is called in the following way:

Map *dev_x;
int xSize = 31;
...
cudaMallocMap(&dev_x, xSize);

It gives an "Access violation writing location 0x________" at or after the first cudaMalloc in the if-statement.

What is wrong with this code and how can I fix it?

Rik Schaaf
  • 1,101
  • 2
  • 11
  • 30
  • 2
    Your interpretation of `cudaMemset` could be possibly wrong. Take a look at [cudaMemset() usage](http://stackoverflow.com/questions/13387101/cudamemset-usage). – Vitality Jun 23 '14 at 14:14
  • @JackOLantern thanks for your correction. That was indeed wrong, but it is not the cause of the "Access violation writing location ..." – Rik Schaaf Jun 23 '14 at 14:26
  • In [`cudaMalloc`](http://www.cs.cmu.edu/afs/cs/academic/class/15668-s11/www/cuda-doc/html/group__CUDART__MEMORY_gc63ffd93e344b939d6399199d8b12fef.html), the `devPtr` pointer points to a device memory location but physically resides on the host. In your case, `((**m).A)` is a pointer residing on the device. – Vitality Jun 23 '14 at 14:52
  • so you're saying that I should either make cudaMallocMap into a CUDA kernel OR place the map itself on the host with the arrays on the GPU? – Rik Schaaf Jun 23 '14 at 15:12
  • I would say the second option you mentioned, namely, allocating `m` on the host and then proceed with the `cudaMalloc`s of arrays pointed to by the struct fields. Lately, you could move `m` to the device, if needed. – Vitality Jun 23 '14 at 21:50
  • Thank you, it was indeed what caused the error, but now I got rid of it (in 2 different versions) I still don't get the right values as output (further explanation in [this question](http://stackoverflow.com/questions/24376386/unable-to-get-right-value-assignment-in-struct-cuda-c). Could you please take a look at that question? – Rik Schaaf Jun 24 '14 at 00:03

0 Answers0