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).
Sarnath
January 28, 2008, 11:38am
3
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));
[snapback]315921[/snapback]
“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.
AndreiB
January 28, 2008, 12:47pm
4
Umm, he’s not writing or reading to it. Or I’m definetely missing something.
Umm, he’s not writing or reading to it. Or I’m definetely missing something.
[snapback]316124[/snapback]
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.