Why is this error occurring?
Since you included your header in FooDevice.cu and Main.cu, where it is defined, so now you have two copies of the same function, and the linker detects this.
How to fix it?
If in foo.h
the following is indicated:
template<typename T> __device__ T foo(T x) { return x; }
And two .cu files that include foo.h and also contain a call, for example.
int x = foo<int>(1);
Then you can force foo () inline:
template<typename T> inline __device__ T foo(T x) { return x; }
and call:
int x = foo<int>(1);
This will not be announced several times.
Function templates are exempted from one definition rule and there may be more than one definition in different translation units. A full specialized function and not a template, rather a normal function, so you need to use the built-in keyword not to violate ODR if you want to put them in the header file in several translation units.
Taken from http://www.velocityreviews.com/forums/t447911-why-does-explicit-specialization-of-function-templates-cause-generation-of-code.html
See also: http://en.wikipedia.org/wiki/One_Definition_Rule
I changed your code as follows:
inline __device__ int getCurThreadIdx() { return ( ( blockIdx.x * blockDim.x ) + threadIdx.x ); } template< typename T > __global__ void fooKernel( const T* inArr, int num, T* outArr ) { const int threadNum = ( gridDim.x * blockDim.x ); for ( int idx = getCurThreadIdx(); idx < num; idx += threadNum ) outArr[ idx ] = inArr[ idx ]; return; }
And now it compiles. Your ad without the built-in getCurThreadIdx () violated one definition rule.
Ade miller
source share