I need a function that computes some "math" function of several variables on GPU. I decided to use Thrust and its zip_iterator
to pack variables in a tuple, and implement my math function as a functor foк for_each
. But I'd like to have a universal function that can compute different "math" functions. So, I need to pass this functor in the function.
As I think, to do this task, I should implement some simple hierarchy (with the only base class) of functors with different versions of operator()(Tuple t)
. For example, functors can be like these:
struct Func {
template <typename Tuple>
__host__ __device__
void operator()(Tuple arg) {
thrust::get<1>(arg) = 0;
}
};
strust f1 : public Func {
...
};
strust f2 : public Func {
...
};
The question is how can I properly pass required functor into the function, and how to define such a function? This function can be like:
thrust::host_vector evaluateFunction(Func func, thrust::host_vector point) {
thrust::host_vector result(point.size());
thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(point.begin(), result.begin())),
thrust::make_zip_iterator(thrust::make_tuple(point.end(), result.end())),
func);
return result;
}
but with such a definition I cannot pass f1
or f2
into it. How can I define it properly?