Skip to content

Commit c7b1a00

Browse files
[SYCL] Implement accessor iterator (#6815)
Added base type for implementing accessor iterators. Added `accessor::begin()`, `accessor::end()`, `accessor::cbegin()` and `accessor::cend()` methods.
1 parent 0e455c9 commit c7b1a00

File tree

5 files changed

+905
-2
lines changed

5 files changed

+905
-2
lines changed

sycl/include/sycl/accessor.hpp

+37-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <CL/__spirv/spirv_types.hpp>
1212
#include <sycl/atomic.hpp>
1313
#include <sycl/buffer.hpp>
14+
#include <sycl/detail/accessor_iterator.hpp>
1415
#include <sycl/detail/cl.h>
1516
#include <sycl/detail/common.hpp>
1617
#include <sycl/detail/export.hpp>
@@ -30,6 +31,7 @@
3031
#include <sycl/property_list_conversion.hpp>
3132
#include <sycl/sampler.hpp>
3233

34+
#include <iterator>
3335
#include <type_traits>
3436

3537
#include <utility>
@@ -334,7 +336,7 @@ class accessor_common {
334336

335337
public:
336338
AccessorSubscript(AccType Accessor, id<Dims> IDs)
337-
: MAccessor(Accessor), MIDs(IDs) {}
339+
: MIDs(IDs), MAccessor(Accessor) {}
338340

339341
// Only accessor class is supposed to use this c'tor for the first
340342
// operator[].
@@ -1201,7 +1203,12 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
12011203
using value_type = DataT;
12021204
using reference = DataT &;
12031205
using const_reference = const DataT &;
1204-
using difference_type = size_t;
1206+
1207+
using iterator = typename detail::accessor_iterator<DataT, Dimensions>;
1208+
using const_iterator =
1209+
typename detail::accessor_iterator<const DataT, Dimensions>;
1210+
using difference_type =
1211+
typename std::iterator_traits<iterator>::difference_type;
12051212

12061213
// The list of accessor constructors with their arguments
12071214
// -------+---------+-------+----+-----+--------------
@@ -2100,6 +2107,34 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
21002107
bool operator==(const accessor &Rhs) const { return impl == Rhs.impl; }
21012108
bool operator!=(const accessor &Rhs) const { return !(*this == Rhs); }
21022109

2110+
iterator begin() const noexcept {
2111+
return iterator::getBegin(
2112+
get_pointer(),
2113+
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
2114+
get_offset());
2115+
}
2116+
2117+
iterator end() const noexcept {
2118+
return iterator::getEnd(
2119+
get_pointer(),
2120+
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
2121+
get_offset());
2122+
}
2123+
2124+
const_iterator cbegin() const noexcept {
2125+
return const_iterator::getBegin(
2126+
get_pointer(),
2127+
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
2128+
get_offset());
2129+
}
2130+
2131+
const_iterator cend() const noexcept {
2132+
return const_iterator::getEnd(
2133+
get_pointer(),
2134+
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
2135+
get_offset());
2136+
}
2137+
21032138
private:
21042139
#ifdef __SYCL_DEVICE_ONLY__
21052140
size_t getTotalOffset() const {

0 commit comments

Comments
 (0)