Note that there are some explanatory texts on larger screens.

plurals
  1. POcopy data which is allocated in device from device to host
    primarykey
    data
    text
    <p>I have a pointer which is dynamically allocated in device,then how can I copy it from device to host. </p> <pre><code>#include &lt;stdio.h&gt; #define cudaSafeCall(call){ \ cudaError err = call; \ if(cudaSuccess != err){ \ fprintf(stderr, "%s(%i) : %s.\n", __FILE__, __LINE__, cudaGetErrorString(err)); \ exit(EXIT_FAILURE); \ }} #define cudaCheckErr(errorMessage) { \ cudaError_t err = cudaGetLastError(); \ if(cudaSuccess != err){ \ fprintf(stderr, "%s(%i) : %s : (code %d) %s.\n", __FILE__, __LINE__, errorMessage, err, cudaGetErrorString(err)); \ exit(EXIT_FAILURE); \`` }} struct num{ int *a; int b; }; __device__ struct num *gun; int main() { int i; char c[100]; struct num *dun,*cun; cudaSafeCall(cudaSetDevice(1)); cun=(struct num*)malloc(10*sizeof(struct num)); cudaSafeCall(cudaMalloc(&amp;dun,10*sizeof(struct num))); cudaSafeCall(cudaMemcpyToSymbol(gun,&amp;dun,sizeof(struct num*))); __global__ void kernel(); kernel&lt;&lt;&lt;1,10&gt;&gt;&gt;(); cudaSafeCall(cudaDeviceSynchronize()); cudaCheckErr(c); cudaSafeCall(cudaMemcpyFromSymbol(&amp;dun,gun,sizeof(struct num*))); cudaSafeCall(cudaMemcpy(cun,dun,10*sizeof(struct num),cudaMemcpyDeviceToHost)); for(i=0;i&lt;10;i++) cudaSafeCall(cudaMalloc(&amp;csu[i].a,10*sizeof(int))); cudaSafeCall(cudaGetSymbolAddress((void**)csu[0].a,(void**)gun[0].a)); for(i=0;i&lt;10;i++) cun[i].a=(int*)malloc(10*sizeof(int)); for(i=0;i&lt;10;i++) cudaSafeCall(cudaMemcpy(cun[i].a,dun[i].a,10*sizeof(int),cudaMemcpyDeviceToHost)); printf("%d ",cun[8].b); printf("%d ",cun[8].a[8]); cudaSafeCall(cudaFree(dun)); free(cun); } __global__ void kernel() { int i; int tid=threadIdx.x; gun[tid].b=tid; gun[tid].a=(int*)malloc(10*sizeof(int));/*this is dynamically allocated in device.*/ for(i=0;i&lt;10;i++) gun[tid].a[i]=tid+i; } </code></pre> <p>In this program, it always comes to a "segmentation fault" in </p> <pre><code>cudaSafeCall(cudaMemcpy(cun[i].a,dun[i].a,10*sizeof(int),cudaMemcpyDeviceToHost)) </code></pre> <p>Why? And what can I do to copy this data from device to host? </p>
    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.
 

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