0

I have a class

class Graph{
...
int* encoding;
...
}

And I created an instance.

Graph* q_gpu;
cudaMalloc((void**) &q_gpu, sizeof(Graph));

Now I want to allocate memory for q_gpu->encoding, and I tried

// main.cu
int* q_encoding_device;
cudaMalloc((void**) q_encoding_device, _someValue_);
q_gpu->encoding = q_encoding_device;

While I was debugging, "segmentation fault" appears when reaches the last statement.

someValue is really small, there shouldn't be memory limit exceeded error.

I don't understand why it fails and hope to know the correct way to solve it. Thank you!

Z2001
  • 1
  • 1
  • 1
    The issue is that `q_gpu` resides in GPU memory but you try to write to it from the CPU. There are plenty of ways to fix this like using managed memory or initializing it on the CPU, then copying to the GPU. But first you should ask yourself whether the `Graph` object itself really needs to be in GPU memory. Usually it is better to have complex objects on the CPU and have these objects contain arrays that are on the GPU – Homer512 Aug 27 '23 at 09:19

0 Answers0