Hi !
I’m having some trouble with my code. I have a structure which should stay on the device, and I want to pass this structure to kernels. I’m wondering why the following example does not work.
struct data {
float * dev_PRE ;
data(int size){
cudaMalloc((void**)&dev_PRE, size*sizeof(float) );
cudaMemset(dev_PRE,0.0f,size*sizeof(float));
}
~data(){
cudaFree(dev_PRE);
}
};
__global__ void kernel(float * dev_pre){
dev_pre[0] = 10 ;
return;
}
int main(int argc, char * argv[]){
int N = 5 ;
int threads = 10 ;
int blocks = 10 ;
data A(N);
struct data * dev_A ;
cudaMalloc((void**)&dev_A,sizeof(A));
cudaMemcpy(dev_A,&A,sizeof(A),cudaMemcpyHostToDevice);
kernel<<<blocks,threads>>>(dev_A->dev_PRE); //Segmentation fault
cudaMemcpy(&A,dev_A,sizeof(A),cudaMemcpyDeviceToHost);
cudaFree(dev_A);
}
I know that I can avoid the problem by giving a pointer to the entire structure dev_A to the kernel instead of the member dev_PRE, but I would like to find a way to do that.
Thanks :)