I'm rather new to CUDA/Thrust and have a problem with a code snippet. To make it easier I have trimmed it down to the bare minimum. The code is the following:
struct functor{
functor(float (*g)(const float&)) : _g{g} {}
__host__ __device__ float operator()(const float& x) const {
return _g(x);
}
private:
float (*_g)(const float&);
};
__host__ __device__ float g(const float& x){return 3*x;}
int main(void){
thrust::device_vector<float> X(4,1);
thrust::transform(X.begin(), X.end(), X.begin(), functor(&g));
}
The idea is that I can pass any function to the functor, so I can apply that function to every element in a Vector.
Unfortunately I'm uncertain to why I get the described error.
I Compile with -w -O3 -shared -arch=sm_20 -std=c++11 -DTHRUST_DEBUG
I'm thankful for any help you all can give me :)
The address of a
__device__
function, (or__host__ __device__
) cannot be taken in host code, for use on the device:There are many questions on stackoverflow which discuss usage of CUDA device function addresses passed via kernel calls. This answer links to several which may be of interest.
One possible approach to fix this would be to acquire the device function address in device code, and pass it to the host, for usage like you are describing:
Another possible approach, leveraging the always-clever work by @m.s. here uses templating: