I’m unfamiliar with C++ and CUDA but trying to learn them both. I tried to use cudaMalloc
and cudaMemcpy
but failed.
Header file:
... // Normal include
namespace calcu{
namespace cpu{
int var;
}
namespace gpu{
__device__ int* var;
}
void cuda_test_print(int useless);
}
Source file:
...
namespace calcu{
void cuda_test_print(int useless){
cuda_test_print_kernel<<<1,1>>>(useless);
}
__global__ void cuda_test_print_kernel(int useless)
{
if(gpu::var == nullptr){
printf("wtf... n");
}else{
printf("ftw... n");
}
}
}
Main source file:
int main(){
...
using namespace calcu;
cpu::var = 1;
cugo(cudaMalloc((void**)&gpu::var, sizeof(int))); // cugo is a function that checks whether cuda API works.
cugo(cudaMemcpy(gpu::var, &cpu::var, sizeof(int),
cudaMemcpyHostToDevice));
cuda_test_print(0);
...
}
When I ran these codes, wtf
is printed which means that gpu::var
is a nullptr, and cudaMemcpy seems not to work. Could anyone help me out?