I have an global array struct which is allocated in host while it’s element pointer is dynamic allocated in device function. I have to copy the result from device to host and use cudaMemcpyFromSymbol, but it comes to a segmentation fault.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define cudaSafeCall(call){
cudaError err = call;
if(cudaSuccess != err){
fprintf(stderr, “%s(%i) : %s.\n”, FILE, LINE, cudaGetErrorString(err));
exit(EXIT_FAILURE);
}}
struct num{
int *a;
int b;
};
device struct num gun;
int main(int argc, char argv[])
{
int result;
struct num cun,dcun;
cudaSafeCall(cudaSetDevice(1));
dcun=(struct num)malloc(10sizeof(struct num));
cudaMalloc(&result,sizeof(int));
cudaMalloc(&cun,10sizeof(struct num));
cudaMemcpyToSymbol(gun,&cun,sizeof(struct num));
timeval tv, tv1;
global void kernel(int);
gettimeofday(&tv, 0);
kernel<<<1,10>>>(result);
cudaDeviceSynchronize();
gettimeofday(&tv1, 0);
cudaMemcpyFromSymbol(cun,&gun,sizeof(struct num*));
cudaMemcpy(dcun,cun,10sizeof(struct num),cudaMemcpyDeviceToHost);
cudaMemcpy(dcun->a,cun->a,10sizeof(int),cudaMemcpyDeviceToHost);//segmentation fault
printf("%d “,dcun[8].b);
printf(”%d “,dcun[9].a[9]);//segmentation fault
cudaFree(cun);
cudaFree(result);
free(dcun);
printf(”%f ",tv1.tv_sec-tv.tv_sec + (double)(tv1.tv_usec-tv.tv_usec)/CLOCKS_PER_SEC);
}
global void kernel(int result)
{
int k=2;
result=k;
int i;
int tid=threadIdx.x;
gun[tid].b=tid;
gun[tid].a=(int)malloc(10sizeof(int));
for(i=0;i<10;i++)
gun[tid].a[i]=tid+i;
}
Why can’t I transfer the struct’s pointer element while the transfer of the struct’s constant is succeed? Is it because that the pointer element is dynamic allocated in device function? What can I do?