Skip to content

Commit

Permalink
[SYCL] Implement accessor iterator (#6815)
Browse files Browse the repository at this point in the history
Added base type for implementing accessor iterators.
Added `accessor::begin()`, `accessor::end()`, `accessor::cbegin()` and
`accessor::cend()` methods.
  • Loading branch information
AlexeySachkov authored Oct 7, 2022
1 parent 0e455c9 commit c7b1a00
Show file tree
Hide file tree
Showing 5 changed files with 905 additions and 2 deletions.
39 changes: 37 additions & 2 deletions sycl/include/sycl/accessor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <CL/__spirv/spirv_types.hpp>
#include <sycl/atomic.hpp>
#include <sycl/buffer.hpp>
#include <sycl/detail/accessor_iterator.hpp>
#include <sycl/detail/cl.h>
#include <sycl/detail/common.hpp>
#include <sycl/detail/export.hpp>
Expand All @@ -30,6 +31,7 @@
#include <sycl/property_list_conversion.hpp>
#include <sycl/sampler.hpp>

#include <iterator>
#include <type_traits>

#include <utility>
Expand Down Expand Up @@ -334,7 +336,7 @@ class accessor_common {

public:
AccessorSubscript(AccType Accessor, id<Dims> IDs)
: MAccessor(Accessor), MIDs(IDs) {}
: MIDs(IDs), MAccessor(Accessor) {}

// Only accessor class is supposed to use this c'tor for the first
// operator[].
Expand Down Expand Up @@ -1201,7 +1203,12 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
using value_type = DataT;
using reference = DataT &;
using const_reference = const DataT &;
using difference_type = size_t;

using iterator = typename detail::accessor_iterator<DataT, Dimensions>;
using const_iterator =
typename detail::accessor_iterator<const DataT, Dimensions>;
using difference_type =
typename std::iterator_traits<iterator>::difference_type;

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

iterator begin() const noexcept {
return iterator::getBegin(
get_pointer(),
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
get_offset());
}

iterator end() const noexcept {
return iterator::getEnd(
get_pointer(),
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
get_offset());
}

const_iterator cbegin() const noexcept {
return const_iterator::getBegin(
get_pointer(),
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
get_offset());
}

const_iterator cend() const noexcept {
return const_iterator::getEnd(
get_pointer(),
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
get_offset());
}

private:
#ifdef __SYCL_DEVICE_ONLY__
size_t getTotalOffset() const {
Expand Down
Loading

0 comments on commit c7b1a00

Please sign in to comment.