CUDA called from HOST C++

Started by
2 comments, last by Misery 12 years, 7 months ago
Hello,

I am trying to write some libs using CUDA architecture. However program will use GP-GPU only if it will be found on computer.
My question is how to write an application that is a normal C++ program, but uses CUDA if it is possible.
All tutorials and info are using .cu files. But I am just using lets say msvcc and I place my code in *.h and *.cpp files.

Lets say i have a such a code:

int CountSomething(*arg1,*arg2,*result)
{
if (HasCUDA()) return CudaFunction(*arg1,*arg2,*result);
else return CPUFunction(*arg1,*arg2,*result);
}



How sould the project look like? Should I make header and .cu file for CUDA functions, and include the header in my host code?
Can I call CUDA function from a normal *.h or *.cpp file?
Should I make *.dll's with CUDA code, and then call it from the host code?
What is the proper/best way of solving this?

Thanks for any help,
Best Regards
Advertisement
It's been a while since I've used CUDA, but you need to put all your GPU code and any host-side code that calls the GPU kernel(s) in a *.cu file. You may be able to put pure C++ code that strictly calls host functions that run on the CPU into *.cpp files, but I don't know for sure. The main important point though is that your CPU and GPU code need to be passed to NVIDIA's compiler driver (nvcc), which comes with the CUDA SDK. The compiler driver knows how to take *.cu and compile them into GPU code and the necessary bindings used to call GPU kernels from the host using NVIDIA's tool chain, and all other C++ code ought to be passed on to your system compiler to build the pure CPU code executed by the host. The SDK docs ought to go more into detail on how to use nvcc from your environment.
I've never written any CUDA code, but I have written OpenCL code which is quite similar in principle.

The way I'd tackle this problem depends on how large the project is. If you have a smallish project (especially if the CUDA part is small), then I'd probably just keep it all in a single project instead of resorting to DLLs that are linked in later. In OpenCL at least, I'd probably just define a CPU implementation and OpenCL implementation in their own source files, include headers from both and choose which functions to invoke at run time based on whether or not it detected an OpenCL platform. If the code chunks were more than a single file each, I'd probably move them into their own directories.

If on the other hand your CUDA code is quite extensive, it might be worth moving the kernels into separate DLLs that are linked in later. For most projects this is probably going to be overkill though.

Now, if you were providing a host CPU implementation, CUDA implementation and OpenCL (CPU and GPU possibly, as the platforms from ATi and nVidia work slightly differently) implementation (and maybe more), then that's another story. On Windows providing DLLs that are updated independently as you improve particular implementations might be a good way to approach the problem. On Linux you could always take the lazy route and just force people to recompile with some conditional defines from the makefile.
Success requires no explanation. Failure allows none.
Yep,

The solution with using cu files just like normal cpp files works very well. There just have to be made a wrapper function around pure CUDA C kernels.

Thanks guys
:D

This topic is closed to new replies.

Advertisement