What is the best way to copy 2d array in host to 1d array in device in this situation for CUDA?
<p>Alright, so in the CUDA image processing program I'm working on, I'm having trouble copying an array allocated as a 2D array on the host to a 1D array in my device. My host array is used like this:</p>

<pre><code>png_bytep *row_pointers;

(intermediate code)

row_pointers = (png_bytep*) malloc(sizeof(png_bytep) * height);
for (y=0; y&lt;height; y++)
    row_pointers[y] = (png_byte*) malloc(png_get_rowbytes(png_ptr,info_ptr));
</code></pre>

<p>So it becomes an array of pointers. My device array is used like this:</p>

<pre><code>png_bytep *dev_row_pointers;
cudaMalloc((void**)&amp;dev_row_pointers, size_png);
cudaMemcpy(row_pointers, &amp;dev_row_pointers, ImageSize, cudaMemcpyHostToDevice);
</code></pre>

<p>Inside my kernel, my device array is used this once:</p>

<pre><code>for (int y=0; y&lt;dev_height; y++) 
{
    png_byte* row = dev_row_pointers[y];
    (other code)
</code></pre>

<p>I was able to determine that the copy functions weren't working properly because my output image was identical to my input image, which meant that my array didn't get adjusted in the kernel and copied back like it was supposed to. My professor told me that it was because I was trying to copy an array of pointers to a 1D array. He said that I could try copying the 2D array to a 1D array in the host and then copying that to the device, but I need to maintain the array of pointers so that my device function will work properly.</p>

<p>I've tried to allocate my device array as two dimensional with</p>

<pre><code>png_bytep **dev_row_pointers;
</code></pre>

<p>but then my compiler throws an error at this part</p>

<pre><code>png_byte* row = dev_row_pointers[y];
</code></pre>

<p>since the array itself is not the correct dimensions. Ideally, I'd like to copy the entries of the host array to the device array, so that they would both become arrays of pointers. Would something like this work?</p>

<pre><code>for (i=0; i&lt;height; i++)
    cudaMemcpy(row_pointers[i], &amp;dev_row_pointers[i], sizeof(int), cudaMemcpyHostToDevice);
</code></pre>

<p>If so, would I need to allocate memory individually for each entry? If not, what would be the best way to copy this array over while maintaining the "array of pointers" property?</p>