Copying a 2d array from host to device. An example needed.

Hi all,

I’m having real problems copying a 2d array of ints or float complex from host to device.
I’m allocating the host memory using malloc, could this cause a problem when copying?

Has anybody got an example they could post???




I use something like this:

// Device buffers allocation

CUDA_SAFE_CALL ( cudaMalloc ((void **) &dfpInRe, (samples * sizeof(float))) );

if (dfpInRe == NULL)

  return 2;

CUDA_SAFE_CALL ( cudaMalloc ((void **) &dfpInIm, (samples * sizeof(float))) );

if (dfpInIm == NULL)

  return 2;

// Data transfers

CUDA_SAFE_CALL (cudaMemcpy (dfpInRe, in_re, samples * sizeof(float), cudaMemcpyHostToDevice));

CUDA_SAFE_CALL (cudaMemcpy (dfpInIm, in_im, samples * sizeof(float), cudaMemcpyHostToDevice));


By 2D array, you need to have an int with length x*y and index into that based on offsets/length. An int is actually an array of pointers to arrays of ints, so you won’t be able to copy that directly to the device.

Thanks for both replies. I did try to convert the int[i][j] into a int[k] and index it using somthings along the lines of k= (max_i)*i + j but when I tried to use it on the device I got a error telling me I couldn’t use (max_i)*i + j as an argument to my array.
I have a feeling that this was because the array was an argument to another array…

expv[ishf[(max_i)*i + j]]

I there a way around this???

Thansk again for the help,


Hi, I’m not sure I’m following you…

Anyway, the code I posted works perfectly, and the arrays you see (in_re, in_im, dfpInRe and dfpInIm) are the Real and Imaginary parts of 2D complex arrays (of floats, but works just as fine with ints).

dfpInRe and dfpInIm are device arrays, in_re and in_im are host arrays.

I index the arrays with:

#define IDX(row, col, width) ((col) + ((row)*(width)))

so from C that I can easily access any (Col,Row) member with say

in_re[IDX(Row, Col, NCols)]


That would be max_i*j + i
As to “now being able to use it as an argument”, post more code.

Thanks for the help, For future readers I indexed the 2D array as per this thread then allocated memory statically using device outside of main(); and copied my data to the devices global memory using copyToSymbol();