Class thrust::zip_function

zip_function is a function object that allows the easy use of N-ary function objects with zip_iterators without redefining them to take a tuple instead of N arguments.

This means that if a functor that takes 2 arguments which could be used with the transform function and device_iterators can be extended to take 3 arguments and zip_iterators without rewriting the functor in terms of tuple.

The make_zip_function convenience function is provided to avoid having to explicitely define the type of the functor when creating a zip_function, whic is especially helpful when using lambdas as the functor.

#include <thrust/iterator/zip_iterator.h>
#include <thrust/device_vector.h>
#include <thrust/transform.h>
#include <thrust/zip_function.h>

struct SumTuple {
  float operator()(auto tup) const {
    return thrust::get<0>(tup) + thrust::get<1>(tup) + thrust::get<2>(tup);
  }
};
struct SumArgs {
  float operator()(float a, float b, float c) const {
    return a + b + c;
  }
};

int main() {
  thrust::device_vector<float> A{0.f, 1.f, 2.f};
  thrust::device_vector<float> B{1.f, 2.f, 3.f};
  thrust::device_vector<float> C{2.f, 3.f, 4.f};
  thrust::device_vector<float> D(3);

  auto begin = thrust::make_zip_iterator(thrust::make_tuple(A.begin(), B.begin(), C.begin()));
  auto end = thrust::make_zip_iterator(thrust::make_tuple(A.end(), B.end(), C.end()));

  // The following four invocations of transform are equivalent:
  // Transform with 3-tuple
  thrust::transform(begin, end, D.begin(), SumTuple{});

  // Transform with 3 parameters
  thrust::zip_function<SumArgs> adapted{};
  thrust::transform(begin, end, D.begin(), adapted);

  // Transform with 3 parameters with convenience function
  thrust::transform(begin, end, D.begin(), thrust::make_zip_function(SumArgs{}));

  // Transform with 3 parameters with convenience function and lambda
  thrust::transform(begin, end, D.begin(), thrust::make_zip_function([] (float a, float b, float c) {
                                                                       return a + b + c;
                                                                     }));
  return 0;
}

See:

#include <thrust/zip_function.h>
template <typename Function> class thrust::zip_function { public:   /* Default constructs the contained function object. */  zip_function() = default;
  _CCCL_HOST_DEVICE   zip_function(Function func);
  template <typename Tuple>   _CCCL_HOST_DEVICE auto   operator()(Tuple && args) const;
   /* Returns a reference to the underlying function. */  _CCCL_HOST_DEVICE Function &   underlying_function() const; };

Member Functions

Function thrust::zip_function::zip_function

zip_function() = default; Default constructs the contained function object.

Function thrust::zip_function::zip_function

_CCCL_HOST_DEVICE zip_function(Function func);

Function thrust::zip_function::operator()

template <typename Tuple> _CCCL_HOST_DEVICE auto operator()(Tuple && args) const;

Function thrust::zip_function::underlying_function

_CCCL_HOST_DEVICE Function & underlying_function() const; Returns a reference to the underlying function.