Note that there are some explanatory texts on larger screens.

plurals
  1. POCopy array of structure from host to device using constant memory
    text
    copied!<p>I want to copy the int m_CellParticleNumber and m_aCellParticleID[CELLMAXPARTICLENUM] value from host to device.For this i use constant memory. But i am not being able to copy the value to the constant memory. can you please help me in this code for copying the value.</p> <pre><code>#include "cuda_runtime.h" #include "device_launch_parameters.h" #include &lt;stdio.h&gt; #include &lt;memory&gt; #include&lt;iostream&gt; #define m_CellNum 100 #define CELLMAXPARTICLENUM 100 int numPartilces = 10; extern void Initialize(); struct CCell { int m_CellParticleNumber ; int m_aCellParticleID[CELLMAXPARTICLENUM]; } ; CCell* hvalue; CCell* dvalue; int* dCellParticleID; __constant__ CCell* c_value; __global__ void Sum_constant(CCell* value, int N) { int index; index = blockIdx.x * blockDim.x + threadIdx.x; if (index&lt;N) for(int idx=0;idx&lt;N ;++idx) value[index].m_aCellParticleID[idx]= value[index].m_aCellParticleID[idx]+ c_value[index].m_aCellParticleID[idx] ; //return; } int main() { hvalue = new CCell[m_CellNum]; cudaMalloc((void**)&amp;dvalue,m_CellNum * sizeof(CCell)); //calling function to initialize the value Initialize(); //initializing the device momory cudaMemcpy(dvalue, hvalue, sizeof(CCell)*m_CellNum,cudaMemcpyHostToDevice); //copying value to constant memory cudaMemcpyToSymbol(c_value-&gt;m_aCellParticleID, &amp;dvalue-&gt;m_aCellParticleID, sizeof(int)*m_CellNum); //dividing bolcks and grid int block_size = 4; int n_blocks = numPartilces/block_size + (numPartilces%block_size == 0 ? 0:1); //invocking kernel function Sum_constant &lt;&lt;&lt; n_blocks, block_size &gt;&gt;&gt; (c_value,numPartilces); //copying value from host to device cudaMemcpy(hvalue, dvalue,numPartilces * sizeof(int),cudaMemcpyDeviceToHost); //showing result for(int i = 0; i &lt; 2; ++i) { for(int j = 0; j &lt; numPartilces; ++j) { std::cout&lt;&lt;hvalue[i].m_aCellParticleID[j]&lt;&lt;"\n"; } } free(hvalue); cudaFree(dvalue); return 0; } void Initialize() { cudaMalloc((void**)&amp;dCellParticleID,m_CellNum * sizeof(int)); for(int i = 0; i &lt; numPartilces; ++i) { hvalue[i].m_CellParticleNumber = 0; for(int j = 0; j &lt; numPartilces; ++j) { hvalue[i].m_aCellParticleID[j] = j+2; } hvalue[i].m_CellParticleNumber++; } } </code></pre> <hr> <p>This is what i tried according to the suggestion given but still it dont works. can you please help me. #include "cuda_runtime.h" #include "device_launch_parameters.h"</p> <pre><code> #include &lt;stdio.h&gt; #include &lt;memory&gt; #include&lt;iostream&gt; #define m_CellNum 100 #define CELLMAXPARTICLENUM 100 int numPartilces = 10; extern void Initialize(); struct CCell { int m_CellParticleNumber ; int m_aCellParticleID[CELLMAXPARTICLENUM]; } ; CCell* hvalue; CCell* dvalue; int* dCellParticleID; __constant__ CCell c_value[m_CellNum]; __global__ void Sum_constant(CCell* value, int N) { int index; index = blockIdx.x * blockDim.x + threadIdx.x; if (index&lt;N) for(int idx=0;idx&lt;N ;++idx) value[index].m_aCellParticleID[idx]= value[index].m_aCellParticleID[idx]+ c_value[index].m_aCellParticleID[idx] ; //return; } int main() { int numPartilces = 10; hvalue = new CCell[m_CellNum]; cudaMalloc((void**)&amp;dvalue,m_CellNum * sizeof(CCell)); //calling function to initialize the value Initialize(); //initializing the device momory cudaMemcpy(dvalue, hvalue, sizeof(CCell)*m_CellNum,cudaMemcpyHostToDevice); //copying value to constant memory cudaMemcpyToSymbol(c_value, &amp;dvalue, sizeof(dvalue)); //dividing bolcks and grid int block_size = 4; int n_blocks = numPartilces/block_size + (numPartilces%block_size == 0 ? 0:1); //invocking kernel function Sum_constant &lt;&lt;&lt; n_blocks, block_size &gt;&gt;&gt; (dvalue,numPartilces); //copying value from host to device cudaMemcpy(hvalue, dvalue,2*m_CellNum * sizeof(int),cudaMemcpyDeviceToHost); //showing result for(int i = 0; i &lt; numPartilces; ++i) { for(int j = 0; j &lt; numPartilces; ++j) { std::cout&lt;&lt;hvalue[i].m_aCellParticleID[j]&lt;&lt;"\n"; } } free(hvalue); cudaFree(dvalue); return 0; } void Initialize() { cudaMalloc((void**)&amp;dCellParticleID,m_CellNum * sizeof(int)); for(int i = 0; i &lt; numPartilces; ++i) { hvalue[i].m_CellParticleNumber = 0; for(int j = 0; j &lt; numPartilces; ++j) { hvalue[i].m_aCellParticleID[j] = j+2; } hvalue[i].m_CellParticleNumber++; } } </code></pre>
 

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