thrust::zip_iterator
Defined in thrust/iterator/zip_iterator.h
-
template<typename IteratorTuple>
class zip_iterator : public detail::zip_iterator_base::type<IteratorTuple> zip_iterator
is an iterator which represents a pointer into a range oftuples
whose elements are themselves taken from atuple
of input iterators. This iterator is useful for creating a virtual array of structures while achieving the same performance and bandwidth as the structure of arrays idiom.zip_iterator
also facilitates kernel fusion by providing a convenient means of amortizing the execution of the same operation over multiple ranges.The following code snippet demonstrates how to create a
zip_iterator
which represents the result of “zipping” multiple ranges together.#include <thrust/iterator/zip_iterator.h> #include <thrust/tuple.h> #include <thrust/device_vector.h> ... thrust::device_vector<int> int_v{0, 1, 2}; thrust::device_vector<float> float_v{0.0f, 1.0f, 2.0f}; thrust::device_vector<char> char_v{'a', 'b', 'c'}; // aliases for iterators using IntIterator = thrust::device_vector<int>::iterator; using FloatIterator = thrust::device_vector<float>::iterator; using CharIterator = thrust::device_vector<char>::iterator; // alias for a tuple of these iterators using IteratorTuple = thrust::tuple<IntIterator, FloatIterator, CharIterator>; // alias the zip_iterator of this tuple using ZipIterator = thrust::zip_iterator<IteratorTuple>; // finally, create the zip_iterator ZipIterator iter(thrust::make_tuple(int_v.begin(), float_v.begin(), char_v.begin())); *iter; // returns (0, 0.0f, 'a') iter[0]; // returns (0, 0.0f, 'a') iter[1]; // returns (1, 1.0f, 'b') iter[2]; // returns (2, 2.0f, 'c') thrust::get<0>(iter[2]); // returns 2 thrust::get<1>(iter[0]); // returns 0.0f thrust::get<2>(iter[1]); // returns 'b' // iter[3] is an out-of-bounds error
Defining the type of a
zip_iterator
can be complex. The next code example demonstrates how to use themake_zip_iterator
function with themake_tuple
function to avoid explicitly specifying the type of thezip_iterator
. This example shows how to usezip_iterator
to copy multiple ranges with a single call tothrust::copy
.#include <thrust/zip_iterator.h> #include <thrust/tuple.h> #include <thrust/device_vector.h> int main() { thrust::device_vector<int> int_in{0, 1, 2}, int_out(3); thrust::device_vector<float> float_in{0.0f, 10.0f, 20.0f}, float_out(3); thrust::copy(thrust::make_zip_iterator(thrust::make_tuple(int_in.begin(), float_in.begin())), thrust::make_zip_iterator(thrust::make_tuple(int_in.end(), float_in.end())), thrust::make_zip_iterator(thrust::make_tuple(int_out.begin(),float_out.begin()))); // int_out is now [0, 1, 2] // float_out is now [0.0f, 10.0f, 20.0f] return 0; }
See also
make_zip_iterator
See also
make_tuple
See also
Tuple
See also
get
Public Types
-
using iterator_tuple = IteratorTuple
The underlying iterator tuple type. Alias to zip_iterator’s first template argument.
Public Functions
-
zip_iterator() = default
Default constructor does nothing.
-
inline zip_iterator(IteratorTuple iterator_tuple)
This constructor creates a new
zip_iterator
from atuple
of iterators.- Parameters
iterator_tuple – The
tuple
of iterators to copy from.
-
template<typename OtherIteratorTuple, detail::enable_if_convertible_t<OtherIteratorTuple, IteratorTuple, int> = 0>
inline zip_iterator(const zip_iterator<OtherIteratorTuple> &other) This copy constructor creates a new
zip_iterator
from anotherzip_iterator
.- Parameters
other – The
zip_iterator
to copy.
-
inline const IteratorTuple &get_iterator_tuple() const
This method returns a
const
reference to thiszip_iterator's
tuple
of iterators.- Returns
A
const
reference to thiszip_iterator's
tuple
of iterators.
-
using iterator_tuple = IteratorTuple