Madrugada
Madrugada

Reputation: 1289

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....

Upvotes: 3

Views: 10398

Answers (2)

Adam27X
Adam27X

Reputation: 883

What is your eventual goal of this code? As hinted at above, it would probably be in your best interest to flatten pa into a 1-dimensional array for usage on the GPU. Something like:

float *pa;
cudaMalloc((void**)&pa, N*N*sizeof(float));

Unfortunately you'd have to adjust A[i] to do your memory copy this way though.

Upvotes: 3

talonmies
talonmies

Reputation: 72349

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.

Upvotes: 5

Related Questions