Use of cudaMallocHost Segmentation fault

I’m trying to use cudaMallocHost but it doesn’t work so far. Maybe I’m doing something wrong and you may be able to help me.

I declare one pointer as a member of a C++ class.

float * array;

Then from my class, I call one function to initiate all arrays on the GPU, so I give this pointer.

InitGPU(..., array);

In InitGPU (in a .cu file so compiled with nvcc) I allocate the memory with cudaMallocHost.

cudaMallocHost((void**)&array, sizeArray*sizeof(float));

Then if I try to access this array from one function on my C++ class, I got a segmentation fault (like array[i] = something). Same thing if I try a memset. And I’m not making any mistake with the size of the array. Did I do something wrong? I looked at the bandwidth test example from the SDK. But everything is done from the GPU (no interaction with a C++ class), and uses only memcpy. Or we have to use memcpy only with this kind of memory?

You are calling cudaMallocHost and giving it the array that is local to the InitGPU() function. So, of course, you are running into problems with the array that is the class member, it is never being set!

Why is it local? I’m giving float* array in argument of InitGPU. So array contains the address of the first element declared from the host. I’m passing like this the arrays I use for initializing the GPU arrays. Why is it different with this one?

Your call to cudaMallocHost is making a copy of the float * array pointer as a local parameter, which you are then changing only locally within InitGPU(). This doesn’t change the value of the original array variable. What you want to do is:

float * array;


InitGPU(..., &array); 


InitGPU(..., float ** arrayPtr) {


cudaMallocHost((void**)arrayPtr, sizeArray*sizeof(float));


Thank you! I hadn’t realized I was doing a copy with cudaMallocHost. But indeed the & takes the address of the local array. Thank you!