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

**URL:** <https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240>\
**Category:** CUDA Programming and Performance\
**Created:** [August 29, 2008, 4:40pm UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240 "2008-08-29T16:40:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![wesarmour](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wesarmour](https://forums.developer.nvidia.com/u/wesarmour)\
**Post date:** [August 29, 2008, 4:40pm UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240/1 "2008-08-29T16:40:36Z")

</div>

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

Thanks,

Wes.

---

<div class="post-metadata">

**Author:** ![XFer](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@XFer](https://forums.developer.nvidia.com/u/XFer)\
**Post date:** [August 29, 2008, 6:23pm UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240/2 "2008-08-29T18:23:55Z")

</div>

Sure!

I use something like this:

```auto
// 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));

```

Fernando

---

<div class="post-metadata">

**Author:** ![tmurray](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@tmurray](https://forums.developer.nvidia.com/u/tmurray)\
**Post date:** [August 29, 2008, 8:00pm UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240/3 "2008-08-29T20:00:55Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![wesarmour](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wesarmour](https://forums.developer.nvidia.com/u/wesarmour)\
**Post date:** [August 30, 2008, 9:38am UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240/4 "2008-08-30T09:38:21Z")

</div>

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,

Wes.

---

<div class="post-metadata">

**Author:** ![XFer](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@XFer](https://forums.developer.nvidia.com/u/XFer)\
**Post date:** [August 30, 2008, 12:37pm UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240/5 "2008-08-30T12:37:36Z")

</div>

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)]

Fernando

---

<div class="post-metadata">

**Author:** ![Ailleur](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@Ailleur](https://forums.developer.nvidia.com/u/Ailleur)\
**Post date:** [August 30, 2008, 1:04pm UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240/6 "2008-08-30T13:04:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![wesarmour](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@wesarmour](https://forums.developer.nvidia.com/u/wesarmour)\
**Post date:** [September 4, 2008, 5:35pm UTC](https://forums.developer.nvidia.com/t/copying-a-2d-array-from-host-to-device-an-example-needed/5240/7 "2008-09-04T17:35:32Z")

</div>

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();

Thanks,

Wes.
