Hello,
The idea is to have a library of functors with different implementations. In the main program, I would have a string that is selecting the functor of interest, and the whole program should work with the chosen functor. Below, you will find an example of what I’m looking for. It is obviously not working (out of scope; same instance name). One could include the transform algorithm into the switch. It is not an option since the main application has more code. It will be duplicated in each switch case and at several places. I would be thankful for any hints on how I could solve the problem. Thank you.
struct functor_minus : public thrust::binary_function<float,float,float>
{
const float a;
functor_minus(float _a) : a(_a) {}
__host__ __device__
float operator()(const float& x, const float& y) const {
return x - a * y;
}
};
struct functor_plus : public thrust::binary_function<float,float,float>
{
const float a;
functor_plus(float _a) : a(_a) {}
__host__ __device__
float operator()(const float& x, const float& y) const {
return x + a * y;
}
};
int main(int argc, char const *argv[])
{
int N = 10000;
thrust::device_vector<float> d_X(N);
thrust::fill(d_X.begin(), d_X.end(), 0.5);
thrust::device_vector<float> d_Y(N);
thrust::fill(d_Y.begin(), d_Y.end(), 1.5);
thrust::device_vector<float> d_R(N);
int select_functor = 1;
switch(select_functor) {
case 1 :{
functor_minus FUNCTOR(0.3);
break;
}
case 2 :{
functor_plus FUNCTOR(0.5);
break;
}
}
thrust::transform( d_X.begin(), d_X.end(), d_Y.begin(), d_R.begin(), FUNCTOR );
return 0;
}