Note that there are some explanatory texts on larger screens.

plurals
  1. POCopying a dynamically allocated 2D array from host to device in CUDA
    primarykey
    data
    text
    <p>I want to copy a dynamically allocated 2D array from host to device to get its Discrete Fourier Transform. </p> <p>I'm using below code to copy the array to the device </p> <pre><code>cudaMalloc((void**)&amp;array_d, sizeof(cufftComplex)*NX*(NY/2+1)); cudaMemcpy(array_d, array_h, sizeof(float)*NX*NY, cudaMemcpyHostToDevice); </code></pre> <p>This works fine with static arrays, i get the intended output from my fft. But it doesn't work with dynamic arrays. After little bit searching I learnt I can not copy dynamic arrays like this from host to device. So I found this solution.</p> <pre><code>cudaMalloc((void**)&amp;array_d, sizeof(cufftComplex)*NX*(NY/2+1)); for(int i=0; i&lt;NX; ++i){ cudaMemcpy(array_d+ i*NY, array_h[i], sizeof(float)*NY, cudaMemcpyHostToDevice); } </code></pre> <p>But it's also not doing the task properly since I get wrong values from my fft.</p> <p>Given below is my fft code. </p> <pre><code>cufftPlanMany(&amp;plan, NRANK, n,NULL, 1, 0,NULL, 1, 0,CUFFT_R2C,BATCH); cufftSetCompatibilityMode(plan, CUFFT_COMPATIBILITY_NATIVE); cufftExecR2C(plan, (cufftReal*)data, data); cudaThreadSynchronize(); cudaMemcpy(c, data, sizeof(float)*NX*NY, cudaMemcpyDeviceToHost); </code></pre> <p>How can I overcome this problem ?</p> <p>EDIT</p> <p>given below is the code</p> <pre><code>#define NX 4 #define NY 5 #define NRANK 2 #define BATCH 10 #include "cuda_runtime.h" #include "device_launch_parameters.h" #include &lt;cufft.h&gt; #include &lt;stdio.h&gt; #include &lt;iostream&gt; int check(); int main() { // static array float b[NX][NY] ={ {0.7943 , 0.6020 , 0.7482 , 0.9133 , 0.9961}, {0.3112 , 0.2630 , 0.4505 , 0.1524 , 0.0782}, {0.5285 , 0.6541 , 0.0838 , 0.8258 , 0.4427}, {0.1656 , 0.6892 , 0.2290 , 0.5383 , 0.1067} }; // dynamic array float **a = new float*[NX]; for (int r = 0; r &lt; NX; ++r) { a[r] = new float[NY]; for (int c = 0; c &lt; NY; ++c) { a[r][c] = b[r][c]; } } // arrray to store the results - host side float c[NX][NY] = { 0 }; cufftHandle plan; cufftComplex *data; int n[NRANK] = {NX, NY}; cudaMalloc((void**)&amp;data, sizeof(cufftComplex)*NX*(NY/2+1)); cudaMemcpy(data, b, sizeof(float)*NX*NY, cudaMemcpyHostToDevice); /* Create a 2D FFT plan. */ cufftPlanMany(&amp;plan, NRANK, n,NULL, 1, 0,NULL, 1, 0,CUFFT_R2C,BATCH); cufftSetCompatibilityMode(plan, CUFFT_COMPATIBILITY_NATIVE); cufftExecR2C(plan, (cufftReal*)data, data); cudaThreadSynchronize(); cudaMemcpy(c, data, sizeof(float)*NX*NY, cudaMemcpyDeviceToHost); cufftDestroy(plan); cudaFree(data); return 0; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload