Skip to content

Commit 8ab29ce

Browse files
Add example loading offset via transform_iterator and extend doc
1 parent e214f13 commit 8ab29ce

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

thrust/testing/cuda/offset_iterator.cu

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include <cub/util_type.cuh>
22

33
#include <thrust/distance.h>
4+
#include <thrust/functional.h>
45
#include <thrust/iterator/offset_iterator.h>
6+
#include <thrust/iterator/transform_iterator.h>
57

68
#include <cuda/std/iterator>
79

@@ -131,3 +133,18 @@ void TestOffsetIteratorIndirectValue()
131133
ASSERT_EQUAL(*iter, 2);
132134
}
133135
DECLARE_VECTOR_UNITTEST(TestOffsetIteratorIndirectValue);
136+
137+
// this is not strictly a CUDA test, but it uses CUB
138+
template <typename Vector>
139+
void TestOffsetIteratorIndirectValueFancyIterator()
140+
{
141+
using thrust::placeholders::_1;
142+
143+
Vector v{0, 1, 2, 3, 4, 5, 6, 7, 8};
144+
thrust::device_vector<typename Vector::difference_type> offsets{2};
145+
auto it = thrust::make_transform_iterator(offsets.begin(), _1 * 3);
146+
thrust::offset_iterator iter(v.begin(), cub::FutureValue{it});
147+
148+
ASSERT_EQUAL(*iter, 6);
149+
}
150+
DECLARE_VECTOR_UNITTEST(TestOffsetIteratorIndirectValueFancyIterator);

thrust/thrust/iterator/offset_iterator.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,33 @@ THRUST_NAMESPACE_BEGIN
5353
//! return 0;
5454
//! }
5555
//! \endcode
56+
//!
57+
//! Combining a \p offset_iterator with \p cub::FutureValue can also be used to retrieve the offset from an iterator.
58+
//! However, such an \p offset_iterator cannot be moved anymore, since changes to the offset can not be written back.
59+
//!
60+
//! \code
61+
//! #include <thrust/iterator/offset_iterator.h>
62+
//! #include <thrust/fill.h>
63+
//! #include <thrust/functional.h>
64+
//! #include <thrust/device_vector.h>
65+
//! #include <cub/util_type.h>
66+
//!
67+
//! int main()
68+
//! {
69+
//! using thrust::placeholders::_1;
70+
//! thrust::device_vector<int> data{1, 2, 3, 4};
71+
//!
72+
//! thrust::device_vector<ptrdiff> offsets{1}; // offset is only available on device
73+
//! auto offset = cub::FutureValue{thrust::make_transform_iterator(offsets.begin(), _1 * 2)};
74+
//! thrust::offset_iterator iter(v.begin(), offset); // load and transform offset upon access
75+
//! // iter is at position 2 (= 1 * 2) in data, and would return 3 in device code
76+
//!
77+
//! return 0;
78+
//! }
79+
//! \endcode
80+
//!
81+
//! In the above example, the offset is loaded from a device vector, transformed by a \p transform_iterator, and then
82+
//! applied to the underlying iterator, when the \p offset_iterator is accessed.
5683
template <typename Iterator, typename Offset = typename ::cuda::std::iterator_traits<Iterator>::difference_type>
5784
class offset_iterator : public iterator_adaptor<offset_iterator<Iterator, Offset>, Iterator>
5885
{

0 commit comments

Comments
 (0)