I have a templated code, in which I want to choose between cuda or omp execution policies while calling thrust's algorithms. It seems that the compiler doesn't automatically detect the appropriate policy based on the container. I think a proper way to do select the policy (without using if else everywhere), would be to write a wrapper around thrust's algorithms with an integer template parameter. Here is an example of a minimal code for thrust::transform and it doesn't compile. However a similar wrapper for thrust::for_each compiles without any problem. What am I missing here?
#include <thrust/iterator/counting_iterator.h>
#include<thrust/transform.h>
#include<thrust/execution_policy.h>
#include<thrust/host_vector.h>
#include<thrust/device_vector.h>
#include <thrust/system/omp/execution_policy.h>
#include<iostream>
typedef thrust::device_vector<int> d_vec;
typedef thrust::host_vector<int> h_vec;
template<typename T>
struct myfunc{
__host__ __device__
T operator()(const T& i) const {
return T(2)*i;
}
};
template<int N, typename InputIter, typename OutputIter, typename Functor>
void transform(InputIter& begin1, InputIter& end1, OutputIter& begin2, Functor& func){
switch (N){
case 0:
thrust::transform(thrust::device, begin1, end1, begin2, func);
break;
case 1:
thrust::transform(thrust::omp::par, begin1, end1, begin2, func);
break;
}
}
int main(){
int n = 4;
thrust::counting_iterator<int> begin(0);
thrust::counting_iterator<int> end = begin+n;
d_vec device_vector(n);
h_vec host_vector(n);
transform<0>(begin, end, device_vector.begin(), myfunc<int>());
transform<1>(begin, end, host_vector.begin(), myfunc<int>());
}