Reputation: 1
all programmers. I'm converting the existing C++ project to CUDA enhanced program. I'm new to CUDA. So I'm learning during work. I have to allocate memory to a dynamic struct member variable and the struct variable is allocated as device variable.
like this:
_cuda_params* dcuda_params;
cudaMalloc(&dcuda_params, sizeof(_cuda_params));
cudaMemcpy((void *)dcuda_params, (void*)cuda_params, sizeof(_cuda_params), cudaMemcpyHostToDevice);
dcuda_params->DPht = (hashtb_entry *)malloc(c); // c is size to allocate.
But during run time I get Exception 0xC0000022. I also tried this:
cudaMalloc(&dcuda_params->DPht, c);
but the result is the same. How can I handle this?
.h file
typedef struct {
int blocksPerGrid;
int threadsPerBlock;
uint64_t HASH_SIZE;
hashtb_entry* DPht;
} _cuda_params;
.cu file
void _GpuSearch(_cuda_params* cuda_params){
...
_cuda_params* dcuda_params;
cudaMalloc(&dcuda_params, sizeof(_cuda_params));
cudaMemcpy((void *)dcuda_params, (void*)cuda_params, sizeof(_cuda_params),
cudaMemcpyHostToDevice);
dcuda_params->DPht = (hashtb_entry *)malloc(c); //c: size to allocate.
...
}
Upvotes: 0
Views: 417
Reputation: 7374
You are dereferencing a device pointer, dcuda_params->DPht = (hashtb_entry *)malloc(c)
; it is not allowed as the host doesn't have access to device memory.
The easy solution for your problem would be not using a pointer to an instance of your struct. You are not using an array of it anyway. So the function call changes to:
void _GpuSearch(_cuda_params cuda_params)
let's say cuda_params
is not a pointer anymore you can simply do:
cudaMalloc(&cuda_params.DPht , sizeof(hashtb_entry));
from now on you are fine to pass cuda_params
by value to the kernel. And if needed you copy from host to cuda_params.DPht
.
Upvotes: 1