Skip to content

Commit 47e6c46

Browse files
committed
Add cbegin/cend and some tests for those methods
1 parent a4a1817 commit 47e6c46

File tree

2 files changed

+70
-31
lines changed

2 files changed

+70
-31
lines changed

sycl/include/sycl/accessor.hpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,8 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
11521152
using const_reference = const DataT &;
11531153

11541154
using iterator = typename detail::__accessor_iterator<DataT, Dimensions>;
1155+
using const_iterator =
1156+
typename detail::__accessor_iterator<const DataT, Dimensions>;
11551157
using difference_type =
11561158
typename std::iterator_traits<iterator>::difference_type;
11571159

@@ -2040,6 +2042,20 @@ class __SYCL_SPECIAL_CLASS __SYCL_TYPE(accessor) accessor :
20402042
get_offset());
20412043
}
20422044

2045+
const_iterator cbegin() const noexcept {
2046+
return const_iterator::__get_begin(
2047+
get_pointer(),
2048+
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
2049+
get_offset());
2050+
}
2051+
2052+
const_iterator cend() const noexcept {
2053+
return const_iterator::__get_end(
2054+
get_pointer(),
2055+
detail::convertToArrayOfN<Dimensions, 1>(getMemoryRange()), get_range(),
2056+
get_offset());
2057+
}
2058+
20432059
private:
20442060
#ifdef __SYCL_DEVICE_ONLY__
20452061
size_t getTotalOffset() const {

sycl/unittests/accessor/AccessorIterator.cpp

Lines changed: 54 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ class AccessorIteratorTest : public ::testing::Test {
1515
std::iota(reference.begin(), reference.end(), 0);
1616
sycl::buffer<T, Dimensions> buffer(reference.data(), shape);
1717
auto accessor = buffer.template get_access<sycl::access_mode::read_write>();
18-
std::vector<T> copied =
19-
copyThroughIterators<T>(accessor.begin(), accessor.end());
2018

21-
ASSERT_EQ(copied.size(), reference.size());
22-
for (size_t i = 0, e = reference.size(); i < e; ++i) {
23-
ASSERT_EQ(copied[i], reference[i]);
24-
}
19+
ASSERT_NO_FATAL_FAILURE(checkFullCopyThroughIteratorImpl(
20+
reference, accessor.begin(), accessor.end()));
21+
ASSERT_NO_FATAL_FAILURE(checkFullCopyThroughIteratorImpl(
22+
reference, accessor.cbegin(), accessor.cend()));
2523
}
2624

2725
template <int Dimensions, typename T = int>
@@ -33,49 +31,74 @@ class AccessorIteratorTest : public ::testing::Test {
3331
std::iota(reference.begin(), reference.end(), 0);
3432
sycl::buffer<T, Dimensions> buffer(reference.data(), fullShape);
3533
std::vector<T> copied;
34+
3635
{
3736
auto accessor = buffer.template get_access<sycl::access_mode::read_write>(
3837
copyShape, offset);
3938
copied = copyThroughIterators<T>(accessor.begin(), accessor.end());
4039
}
41-
42-
ASSERT_EQ(copied.size(), copyShape.size());
40+
ASSERT_NO_FATAL_FAILURE(
41+
validatePartialCopyThroughIterator(copied, buffer, copyShape, offset));
4342

4443
{
45-
auto fullAccessor = buffer.template get_access<sycl::access_mode::read>();
46-
size_t linearId = 0;
47-
48-
sycl::id<3> offsetToUse(Dimensions > 2 ? offset[Dimensions - 3] : 1,
49-
Dimensions > 1 ? offset[Dimensions - 2] : 1,
50-
offset[Dimensions - 1]);
51-
52-
sycl::id<3> shapeToCheck(
53-
(Dimensions > 2 ? copyShape[Dimensions - 3] : 1) + offsetToUse[0],
54-
(Dimensions > 1 ? copyShape[Dimensions - 2] : 1) + offsetToUse[1],
55-
copyShape[Dimensions - 1] + offsetToUse[2]);
56-
57-
for (size_t z = offsetToUse[0]; z < shapeToCheck[0]; ++z) {
58-
for (size_t y = offsetToUse[1]; y < shapeToCheck[1]; ++y) {
59-
for (size_t x = offsetToUse[2]; x < shapeToCheck[2]; ++x) {
60-
auto value = accessHelper<Dimensions>(fullAccessor, z, y, x);
61-
ASSERT_EQ(copied[linearId], value);
62-
++linearId;
63-
}
64-
}
65-
}
44+
auto accessor = buffer.template get_access<sycl::access_mode::read_write>(
45+
copyShape, offset);
46+
copied = copyThroughIterators<T>(accessor.cbegin(), accessor.cend());
6647
}
48+
ASSERT_NO_FATAL_FAILURE(
49+
validatePartialCopyThroughIterator(copied, buffer, copyShape, offset));
6750
}
6851

6952
private:
53+
template <typename IteratorT, typename T = int>
54+
void checkFullCopyThroughIteratorImpl(const std::vector<T> &reference,
55+
IteratorT begin, IteratorT end) {
56+
std::vector<T> copied = copyThroughIterators<T>(begin, end);
57+
58+
ASSERT_EQ(copied.size(), reference.size());
59+
for (size_t i = 0, e = reference.size(); i < e; ++i) {
60+
ASSERT_EQ(copied[i], reference[i]);
61+
}
62+
}
63+
7064
template <typename T, typename IteratorT>
7165
std::vector<T> copyThroughIterators(IteratorT begin, IteratorT end) {
7266
std::vector<T> copied;
73-
for (auto it = begin; it != end; ++it) {
67+
for (auto it = begin; it != end; ++it)
7468
copied.push_back(*it);
75-
}
69+
7670
return copied;
7771
}
7872

73+
template <int Dimensions, typename T = int>
74+
void
75+
validatePartialCopyThroughIterator(const std::vector<T> &copied,
76+
sycl::buffer<T, Dimensions> &buffer,
77+
const sycl::range<Dimensions> &copyShape,
78+
const sycl::id<Dimensions> &offset = {}) {
79+
auto fullAccessor = buffer.template get_access<sycl::access_mode::read>();
80+
size_t linearId = 0;
81+
82+
sycl::id<3> offsetToUse(Dimensions > 2 ? offset[Dimensions - 3] : 1,
83+
Dimensions > 1 ? offset[Dimensions - 2] : 1,
84+
offset[Dimensions - 1]);
85+
86+
sycl::id<3> shapeToCheck(
87+
(Dimensions > 2 ? copyShape[Dimensions - 3] : 1) + offsetToUse[0],
88+
(Dimensions > 1 ? copyShape[Dimensions - 2] : 1) + offsetToUse[1],
89+
copyShape[Dimensions - 1] + offsetToUse[2]);
90+
91+
for (size_t z = offsetToUse[0]; z < shapeToCheck[0]; ++z) {
92+
for (size_t y = offsetToUse[1]; y < shapeToCheck[1]; ++y) {
93+
for (size_t x = offsetToUse[2]; x < shapeToCheck[2]; ++x) {
94+
auto value = accessHelper<Dimensions>(fullAccessor, z, y, x);
95+
ASSERT_EQ(copied[linearId], value);
96+
++linearId;
97+
}
98+
}
99+
}
100+
}
101+
79102
template <int TotalDimensions, int CurrentDimension = 3, typename Container,
80103
typename... Indices>
81104
auto &&accessHelper(Container &&C, int Idx, Indices... Ids) {

0 commit comments

Comments
 (0)