I had a need to call a CUDA kernel from inside a C++ class. When I went looking through the NVIDIA examples and the wealth of information on the internet, I could not find a clear solution to my problem. So, let me state plainly how to accomplish this:
- Create a ".cu" file for your kernels;
- For each kernel you would like to call from C++, create a wrapper that can be called from inside your class;
- Inside this wrapper, call your kernel function;
- Include the prototype for your wrapper in the header file of your class, but not inside the class definition.
An example of the ".cu" file and the class header are shown below. One thing you apparently do not need to do (particularly in Visual Studio 2010) is include the ".cu" file in your class. As long as it is part of your project, everything should work just fine.
#ifndef _PROJECT_KERNELS
#define _PROJECT_KERNELS
__global__ void kernel(double Param1, double Param2){
// Do Kernel Stuff here
}
extern "C" void functionName(double Param1, double Param2){
kernel<<<Nb, Nt>>kernel(Param1, Param2);
}
#endif
#ifndef MYCLASS_H_
#define MYCLASS_H_
#include <cuda.h>
#include "cuda_runtime.h"
extern "C" void functionName(double Param1, double Param2);
class myClass{
myClass();
}
#endif