device function pointer as template parameter
up vote
0
down vote
favorite
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?
c++ templates cuda nvrtc
add a comment |
up vote
0
down vote
favorite
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?
c++ templates cuda nvrtc
why the downvote please? Questions seems legit
– Regis Portalez
Nov 9 at 15:58
Which CUDA toolkit and host compiler are you using?
– talonmies
Nov 10 at 8:05
Cuda 10.0 on windows. Visual 2017 latest. Same on Ubuntu 18.04. Likely gcc 5.4
– Regis Portalez
Nov 10 at 9:08
I'm really surprised it compiles. Memory space attributes should never be applied to class data. The only time it is valid is for member functions. nvrtc is clang based, as far as I can tell, which might explain the difference, but I'll wager nvrtc is correct in this case
– talonmies
Nov 10 at 9:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
c++ templates cuda nvrtc
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?
c++ templates cuda nvrtc
c++ templates cuda nvrtc
edited Nov 9 at 13:03
asked Nov 9 at 12:33
Regis Portalez
2,80811732
2,80811732
why the downvote please? Questions seems legit
– Regis Portalez
Nov 9 at 15:58
Which CUDA toolkit and host compiler are you using?
– talonmies
Nov 10 at 8:05
Cuda 10.0 on windows. Visual 2017 latest. Same on Ubuntu 18.04. Likely gcc 5.4
– Regis Portalez
Nov 10 at 9:08
I'm really surprised it compiles. Memory space attributes should never be applied to class data. The only time it is valid is for member functions. nvrtc is clang based, as far as I can tell, which might explain the difference, but I'll wager nvrtc is correct in this case
– talonmies
Nov 10 at 9:25
add a comment |
why the downvote please? Questions seems legit
– Regis Portalez
Nov 9 at 15:58
Which CUDA toolkit and host compiler are you using?
– talonmies
Nov 10 at 8:05
Cuda 10.0 on windows. Visual 2017 latest. Same on Ubuntu 18.04. Likely gcc 5.4
– Regis Portalez
Nov 10 at 9:08
I'm really surprised it compiles. Memory space attributes should never be applied to class data. The only time it is valid is for member functions. nvrtc is clang based, as far as I can tell, which might explain the difference, but I'll wager nvrtc is correct in this case
– talonmies
Nov 10 at 9:25
why the downvote please? Questions seems legit
– Regis Portalez
Nov 9 at 15:58
why the downvote please? Questions seems legit
– Regis Portalez
Nov 9 at 15:58
Which CUDA toolkit and host compiler are you using?
– talonmies
Nov 10 at 8:05
Which CUDA toolkit and host compiler are you using?
– talonmies
Nov 10 at 8:05
Cuda 10.0 on windows. Visual 2017 latest. Same on Ubuntu 18.04. Likely gcc 5.4
– Regis Portalez
Nov 10 at 9:08
Cuda 10.0 on windows. Visual 2017 latest. Same on Ubuntu 18.04. Likely gcc 5.4
– Regis Portalez
Nov 10 at 9:08
I'm really surprised it compiles. Memory space attributes should never be applied to class data. The only time it is valid is for member functions. nvrtc is clang based, as far as I can tell, which might explain the difference, but I'll wager nvrtc is correct in this case
– talonmies
Nov 10 at 9:25
I'm really surprised it compiles. Memory space attributes should never be applied to class data. The only time it is valid is for member functions. nvrtc is clang based, as far as I can tell, which might explain the difference, but I'll wager nvrtc is correct in this case
– talonmies
Nov 10 at 9:25
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
Well, answer is quite easy :
__device__
attribute is misplaced (as indicated by the compiler). The func
struct 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.
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Well, answer is quite easy :
__device__
attribute is misplaced (as indicated by the compiler). The func
struct 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.
add a comment |
up vote
1
down vote
Well, answer is quite easy :
__device__
attribute is misplaced (as indicated by the compiler). The func
struct 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.
add a comment |
up vote
1
down vote
up vote
1
down vote
Well, answer is quite easy :
__device__
attribute is misplaced (as indicated by the compiler). The func
struct 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.
Well, answer is quite easy :
__device__
attribute is misplaced (as indicated by the compiler). The func
struct 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.
answered Nov 9 at 13:32
Regis Portalez
2,80811732
2,80811732
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53225799%2fdevice-function-pointer-as-template-parameter%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
why the downvote please? Questions seems legit
– Regis Portalez
Nov 9 at 15:58
Which CUDA toolkit and host compiler are you using?
– talonmies
Nov 10 at 8:05
Cuda 10.0 on windows. Visual 2017 latest. Same on Ubuntu 18.04. Likely gcc 5.4
– Regis Portalez
Nov 10 at 9:08
I'm really surprised it compiles. Memory space attributes should never be applied to class data. The only time it is valid is for member functions. nvrtc is clang based, as far as I can tell, which might explain the difference, but I'll wager nvrtc is correct in this case
– talonmies
Nov 10 at 9:25