I found that cuda support use "template" keyword for the code, now I would like to link the usage of "template" between nvcc and g++. But it seems that I cannot find a proper way to implement it, so I use the string of datatype to deliever the datatype declaration. Could I find a better method to do it?
//in .cpp
extern "C" void function(string T);
int main(){
function("float");
}
//in .cu
extern "C" void function(string T){
if(T == "short")
func<short>(...);
if(T == "int")
func<int>(...);
.......
}
You could use C-style function overloading.
This is significantly faster than comparing strings every time you call the function. If you wanted to, you could create a wrapper template function on the C++ side to make the usage a bit cleaner.
To make maintenance a little easier, you could define some macros.
You could put those
DECLARE_FUNClines in a common header so that you only have to update the list in one place. If you wanted to add adoublefunction, you could just addDECLARE_FUNC(double)to the header.I've gone from easy-to-setup to easy-to-maintain. You'll have to decide what is appropriate for your situation.