#include #include #include #include #include struct Functor { template __host__ __device__ float operator()(const Tuple& tuple) const { const float x = thrust::get<0>(tuple); const float y = thrust::get<1>(tuple); return x*y*2.0f / 3.0f; } }; int main(void) { float u[4] = { 4 , 3, 2, 1}; float v[4] = {-1, 1, 1, -1}; int idx[3] = {3, 0, 1}; float w[3] = {0, 0, 0}; thrust::device_vector U(u, u + 4); thrust::device_vector V(v, v + 4); thrust::device_vector IDX(idx, idx + 3); thrust::device_vector W(w, w + 3); // gather multiple elements and apply a function before writing result in memory thrust::gather( IDX.begin(), IDX.end(), thrust::make_zip_iterator(thrust::make_tuple(U.begin(), V.begin())), thrust::make_transform_output_iterator(W.begin(), Functor())); std::cout << "result= [ "; for (size_t i = 0; i < 3; i++) std::cout << W[i] << " "; std::cout << "] \n"; return 0; }