I was wondering if there was a more efficient way to write a = a + b + c?
thrust::transform(b.begin(), b.end(), c.begin(), b.begin(), thrust::plus<int>()); thrust::transform(a.begin(), a.end(), b.begin(), a.begin(), thrust::plus<int>());
This works, but is there a way to get the same effect using only one line of code? I looked at the saxpy implementation in the examples, however this uses 2 vectors and a constant value;
Is it more efficient?
struct arbitrary_functor { template <typename Tuple> __host__ __device__ void operator()(Tuple t) { // D[i] = A[i] + B[i] + C[i]; thrust::get<3>(t) = thrust::get<0>(t) + thrust::get<1>(t) + thrust::get<2>(t); } }; int main(){ // allocate storage thrust::host_vector<int> A; thrust::host_vector<int> B; thrust::host_vector<int> C; // initialize input vectors A.push_back(10); B.push_back(10); C.push_back(10); // apply the transformation thrust::for_each(thrust::make_zip_iterator(thrust::make_tuple(A.begin(), B.begin(), C.begin(), A.begin())), thrust::make_zip_iterator(thrust::make_tuple(A.end(), B.end(), C.end(), A.end())), arbitrary_functor()); // print the output std::cout << A[0] << std::endl; return 0; }
source share