device function pointer as template parameter

147 views Asked by At

I have a template struct for some reasons (beyond the scope of this question) :

template<typename T, __device__ retV (*funcptr)(T)>
struct func 
{
    __device__ inline retV invoke(T i) { funcptr(i); }
};

which can be used this way:

__device__ double increment(double x) {
    return x + 1.0;
}

__constant__ func<double, double, &increment> myfunc;

__device__ double apply(double x) 
{
    return myfunc.invoke(x);
}

This works well with nvcc (cuda 10.0), but fails with nvrtc (JIT compilation) with the following error:

error: attributes may not appear here

How should I modify this code to make it work with nvrtc? Or should I add flags in my command-line?

1

There are 1 answers

0
Regis Portalez On

Well, answer is quite easy :

__device__ attribute is misplaced (as indicated by the compiler). The funcstruct should look like:

template<typename T, retV (* __device__  funcptr)(T)>
struct func 
{
    __device__ inline retV invoke(T i) { funcptr(i); }
};

But I don't know why nvcc and nvrtc have different expectations on this.

EDIT : Nvrtc 11 and above does not support this syntax anymore. Probably because nvrtc is clang based