cuda

Using cudaMalloc to allocate a matrix


I am using cudaMalloc and cudaMemcpy to allocate a matrix and copy into it arrays of vectors , like this:

float **pa;    
cudaMalloc((void***)&pa,  N*sizeof(float*)); //this seems to be ok
for(i=0; i<N; i++) {
    cudaMalloc((void**) &(pa[i]), N*sizeof(float)); //this gives seg fault
    cudaMemcpy (pa[i], A[i], N*sizeof(float), cudaMemcpyHostToDevice); // also i am not sure about this
}

What is wrong with my instructions? Thanks in advance

P.S. A[i] is a vector


Now i'm trying to copy a matrix from the Device to a matrix from the host:

Supposing I have **pc in the device, and **pgpu is in the host:

cudaMemcpy (pgpu, pc, N*sizeof(float*), cudaMemcpyDeviceToHost);
for (i=0; i<N; i++)
    cudaMemcpy(pgpu[i], pc[i], N*sizeof(float), cudaMemcpyDeviceToHost);

= is wrong....


Solution

  • pa is in device memory, so &(pa[i]) does not do what you are expecting it will. This will work

    float **pa;
    float **pah = (float **)malloc(pah, N * sizeof(float *));    
    cudaMalloc((void***)&pa,  N*sizeof(float*));
    for(i=0; i<N; i++) {
        cudaMalloc((void**) &(pah[i]), N*sizeof(float));
        cudaMemcpy (pah[i], A[i], N*sizeof(float), cudaMemcpyHostToDevice);
    }
    cudaMemcpy (pa, pah, N*sizeof(float *), cudaMemcpyHostToDevice);
    

    ie. build the array of pointers in host memory and then copy it to the device. I am not sure what you are hoping to read from A, but I suspect that the inner cudaMemcpy probably isn't doing what you want as written.

    Be forewarned that from a performance point of view, arrays of pointers are not a good idea on the GPU.