I pass a struct pointer to a device function, and this device function edits this pointer.
struct:
struct substruct{
int main;
int sub;
};
struct mainstruct{
int main;
int sub;
substruct* subs;
};
device function:
__global__ void update(mainstruct* target){
printf("in device: target: %d/%d/%dn", target->main, target->sub, target->subs);
//substruct *sub = new substruct();
substruct* sub;
cudaMalloc(&sub, sizeof(substruct));
sub->main = 5;
sub->sub = 5;
target->subs = sub;
printf("in device: sub created/allocated: %d/%dn", sub->main, sub->sub);
}
and this is main:
int main(){
mainstruct *main = new mainstruct();
main->main = 10;
main->sub = 10;
main->subs = nullptr;
mainstruct* main_device;
handleerr(cudaMalloc(&main_device, sizeof(mainstruct)));
handleerr(cudaMemcpy(main_device, main, sizeof(mainstruct), cudaMemcpyHostToDevice));
update << <1, 1 >> > (main_device);
handleerr(cudaDeviceSynchronize());
handleerr(cudaGetLastError());
handleerr(cudaMemcpy(main, main_device, sizeof(mainstruct), cudaMemcpyDeviceToHost));
substruct *subs = new substruct();
handleerr(cudaMallocHost(&subs, sizeof(substruct)));
handleerr(cudaMemcpy(subs, main->subs, sizeof(substruct), cudaMemcpyDeviceToHost));
main->subs = subs;
printf("done");
}
I keep encountering an error at the line :
handleerr(cudaMemcpy(subs, main->subs, sizeof(substruct), cudaMemcpyDeviceToHost));
how can I correctly copy memory that was dynamically allocated??