I'm trying to passing atomicAdd function into another function as template parameter.
Here is my Kernel1:
template<typename T, typename TAtomic>
__global__ void myfunc1(T *address, TAtomic atomicFunc) {
atomicFunc(address, 1);
}
Try 1:
myfunc1<<<1,1>>>(val.dev_ptr, atomicAdd);
It does not work due to the compiler cannot match the expected function signature.
Try 2: Firstly, I wrap the atomicAdd into a custom function called MyAtomicAdd.
template<typename T>
__device__ void MyAtomicAdd(T *address, T val) {
atomicAdd(address, val);
}
Then, I defined a function pointer called "TAtomic" and declare the TAtomic as template parameter.
typedef void (*TAtomic)(float *,float);
template<typename T, TAtomic atomicFunc>
__global__ void myfunc2(T *address) {
atomicFunc(address, 1);
}
myfunc2<float, MyAtomicAdd><<<1,1>>>(dev_ptr);
CUDA_CHECK(cudaDeviceSynchronize());
Actually, Try 2 works. But, I don't want to use typedef. I need something more generic.
Try 3: Just passing MyAtomicAdd to myfunc1.
myfunc1<<<1,1>>>(dev_ptr, MyAtomicAdd<float>);
CUDA_CHECK(cudaDeviceSynchronize());
The compiler can compile the code. But when I run the program, a error reported:
"ERROR in /home/liang/groute-dev/samples/framework/pagerank.cu:70: invalid program counter (76)"
I just wondering, why try 3 doesn't work? And any simple or gentle way exists to implement this requirement? Thank you.