novice problem...please help

Hi

How can I do the following

Matrix * dMatrix1 = NULL;

CUDA_SAFE_CALL(cudaMalloc((void**)&dMatrix1, sizeof(Matrix)));

CUDA_SAFE_CALL(cudaMalloc((void**)dMatrix1->elements, sizeof(float) * NUM_THREADS));

I am getting an error here

CUDA_SAFE_CALL(cudaMalloc((void**)dMatrix1->elements, sizeof(float) * NUM_THREADS));

What kind of error?

There’s something wrong with pointers. You cast (float*) to (void**) which differs in level of indirection. Try (void**)&(dMatrix1->elements).

“dMatrix1” is a pointer to DEVICE MEMORY. You CANNOT INDIRECT IT from HOST CODE.

To READ or WRITE to it, you can only use “cudaMemcpy” functions. No wonder, you are getting a segmentation fault.

Umm, he’s not writing or reading to it. Or I’m definetely missing something.

But he is.

cudaMalloc((void**)&dMatrix1)

allocates memory on the device.

dMatrix1->elements dereferences that memory on the host: seg fault.

To allocate structures like these, you must allocate dMatrix1 on the device and hMatrix1 on the host. Then cudaMalloc(hMatrix1->elements …) and cudaMemcpy hMatrix1 to dMatrix1. However, I might suggest skipping the step of allocating dMatrix1 and just pass it as an argument to the kernel.

i see now i get it…

thanks !