Skip to content

Commit 45dbbd1

Browse files
alexeyvoronov-intelbader
authored andcommitted
[SYCL] Remove the offset field from item<dimensions, false> class (#452)
Moved part of logic from item class to new class item_base to hide implementation details. Signed-off-by: Alexey Voronov <alexey.voronov@intel.com>
1 parent 1f0d4c5 commit 45dbbd1

File tree

4 files changed

+129
-71
lines changed

4 files changed

+129
-71
lines changed

sycl/include/CL/sycl/detail/helpers.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,12 @@ class Builder {
7575
return cl::sycl::item<dimensions, with_offset>(R, I);
7676
}
7777

78+
template <int dimensions, bool with_offset>
79+
static void updateItemIndex(cl::sycl::item<dimensions, with_offset> &Item,
80+
const id<dimensions> &NextIndex) {
81+
Item.MImpl.MIndex = NextIndex;
82+
}
83+
7884
template <int dimensions>
7985
static nd_item<dimensions>
8086
createNDItem(const cl::sycl::item<dimensions, true> &GL,
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//==---------- item_base.hpp --- SYCL iteration ItemBase ------------------==//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#pragma once
10+
11+
#include <CL/sycl/id.hpp>
12+
#include <CL/sycl/range.hpp>
13+
14+
namespace cl {
15+
namespace sycl {
16+
template <int dimensions> class id;
17+
template <int dimensions> class range;
18+
19+
namespace detail {
20+
template <int Dims, bool WithOffset> struct ItemBase;
21+
22+
template <int Dims> struct ItemBase<Dims, true> {
23+
24+
bool operator==(const ItemBase &Rhs) const {
25+
return (Rhs.MIndex == MIndex) && (Rhs.MExtent == MExtent) &&
26+
(Rhs.MOffset == MOffset);
27+
}
28+
29+
bool operator!=(const ItemBase &Rhs) const { return !((*this) == Rhs); }
30+
31+
size_t get_linear_id() const {
32+
if (1 == Dims) {
33+
return MIndex[0] - MOffset[0];
34+
}
35+
if (2 == Dims) {
36+
return (MIndex[0] - MOffset[0]) * MExtent[1] + (MIndex[1] - MOffset[1]);
37+
}
38+
return ((MIndex[0] - MOffset[0]) * MExtent[1] * MExtent[2]) +
39+
((MIndex[1] - MOffset[1]) * MExtent[2]) + (MIndex[2] - MOffset[2]);
40+
}
41+
42+
range<Dims> MExtent;
43+
id<Dims> MIndex;
44+
id<Dims> MOffset;
45+
};
46+
47+
template <int Dims> struct ItemBase<Dims, false> {
48+
49+
bool operator==(const ItemBase &Rhs) const {
50+
return (Rhs.MIndex == MIndex) && (Rhs.MExtent == MExtent);
51+
}
52+
53+
bool operator!=(const ItemBase &Rhs) const { return !((*this) == Rhs); }
54+
55+
operator ItemBase<Dims, true>() const {
56+
return ItemBase<Dims, true>(MExtent, MIndex, id<Dims>{});
57+
}
58+
59+
size_t get_linear_id() const {
60+
if (1 == Dims) {
61+
return MIndex[0];
62+
}
63+
if (2 == Dims) {
64+
return MIndex[0] * MExtent[1] + MIndex[1];
65+
}
66+
return (MIndex[0] * MExtent[1] * MExtent[2]) + (MIndex[1] * MExtent[2]) +
67+
MIndex[2];
68+
}
69+
70+
range<Dims> MExtent;
71+
id<Dims> MIndex;
72+
};
73+
74+
} // namespace detail
75+
} // namespace sycl
76+
} // namespace cl

sycl/include/CL/sycl/h_item.hpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88

99
#pragma once
1010

11+
#include <CL/sycl/detail/helpers.hpp>
1112
#include <CL/sycl/id.hpp>
1213
#include <CL/sycl/item.hpp>
14+
#include <CL/sycl/range.hpp>
1315

1416
namespace cl {
1517
namespace sycl {
@@ -22,9 +24,9 @@ template <int dimensions> class h_item {
2224
public:
2325
h_item() = delete;
2426

25-
h_item(const h_item<dimensions> &hi) = default;
27+
h_item(const h_item &hi) = default;
2628

27-
h_item<dimensions> &operator=(const h_item<dimensions> &hi) = default;
29+
h_item &operator=(const h_item &hi) = default;
2830

2931
/* -- public interface members -- */
3032
item<dimensions, false> get_global() const { return globalItem; }
@@ -93,29 +95,29 @@ template <int dimensions> class h_item {
9395
return get_physical_local().get_id(dimension);
9496
}
9597

96-
bool operator==(const h_item<dimensions> &rhs) const {
98+
bool operator==(const h_item &rhs) const {
9799
return (rhs.localItem == localItem) && (rhs.globalItem == globalItem) &&
98100
(rhs.logicalLocalItem == logicalLocalItem);
99101
}
100102

101-
bool operator!=(const h_item<dimensions> &rhs) const {
102-
return !((*this) == rhs);
103-
}
103+
bool operator!=(const h_item &rhs) const { return !((*this) == rhs); }
104104

105105
protected:
106106
friend class detail::Builder;
107107
friend class group<dimensions>;
108108
h_item(const item<dimensions, false> &GL, const item<dimensions, false> &L,
109109
const range<dimensions> &flexLocalRange)
110110
: globalItem(GL), localItem(L),
111-
logicalLocalItem(flexLocalRange, L.get_id()) {}
111+
logicalLocalItem(detail::Builder::createItem<dimensions, false>(
112+
flexLocalRange, L.get_id())) {}
112113

113114
h_item(const item<dimensions, false> &GL, const item<dimensions, false> &L)
114115
: globalItem(GL), localItem(L),
115-
logicalLocalItem(localItem.get_range(), localItem.get_id()) {}
116+
logicalLocalItem(detail::Builder::createItem<dimensions, false>(
117+
localItem.get_range(), localItem.get_id())) {}
116118

117119
void setLogicalLocalID(const id<dimensions> &ID) {
118-
logicalLocalItem.setID(ID);
120+
detail::Builder::updateItemIndex(logicalLocalItem, ID);
119121
}
120122

121123
private:

sycl/include/CL/sycl/item.hpp

Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,11 @@
88

99
#pragma once
1010

11+
#include <CL/sycl/detail/helpers.hpp>
12+
#include <CL/sycl/detail/item_base.hpp>
13+
#include <CL/sycl/detail/type_traits.hpp>
1114
#include <CL/sycl/id.hpp>
1215
#include <CL/sycl/range.hpp>
13-
#include <stdexcept>
14-
#include <type_traits>
1516

1617
namespace cl {
1718
namespace sycl {
@@ -20,93 +21,66 @@ class Builder;
2021
}
2122
template <int dimensions> class id;
2223
template <int dimensions> class range;
23-
template <int dimensions> class h_item;
2424

2525
template <int dimensions = 1, bool with_offset = true> class item {
2626
public:
2727
item() = delete;
2828

29-
id<dimensions> get_id() const { return index; }
29+
id<dimensions> get_id() const { return MImpl.MIndex; }
3030

31-
size_t get_id(int dimension) const { return index[dimension]; }
31+
size_t get_id(int dimension) const { return MImpl.MIndex[dimension]; }
3232

33-
size_t operator[](int dimension) const { return index[dimension]; }
33+
size_t operator[](int dimension) const { return MImpl.MIndex[dimension]; }
3434

35-
range<dimensions> get_range() const { return extent; }
35+
range<dimensions> get_range() const { return MImpl.MExtent; }
3636

37-
size_t get_range(int dimension) const { return extent.get(dimension); }
37+
size_t get_range(int dimension) const { return MImpl.MExtent[dimension]; }
3838

39-
// only available if with_offset is true;
40-
template <bool W = with_offset,
41-
typename = typename std::enable_if<(W == true)>::type>
42-
id<dimensions> get_offset() const {
43-
return offset;
39+
template <bool has_offset = with_offset>
40+
detail::enable_if_t<has_offset, id<dimensions>> get_offset() const {
41+
return MImpl.MOffset;
4442
}
4543

46-
template <bool W = with_offset>
47-
operator typename std::enable_if<W == false, item<dimensions, true>>::type()
48-
const {
49-
return item<dimensions, true>(extent, index, offset);
44+
template <bool has_offset = with_offset>
45+
detail::enable_if_t<has_offset, size_t> get_offset(int dimension) const {
46+
return MImpl.MOffset[dimension];
5047
}
5148

52-
/* The following member function is only available in the id class
53-
* specialization where: dimensions>0 and dimensions<4 */
54-
template <int N = dimensions,
55-
typename = typename std::enable_if<((N > 0) && (N < 4))>::type>
56-
size_t get_linear_id() const {
57-
if (1 == dimensions) {
58-
return index[0] - offset[0];
59-
}
60-
if (2 == dimensions) {
61-
return (index[0] - offset[0]) * extent[1] + (index[1] - offset[1]);
62-
}
63-
return ((index[0] - offset[0]) * extent[1] * extent[2]) +
64-
((index[1] - offset[1]) * extent[2]) + (index[2] - offset[2]);
49+
template <bool has_offset = with_offset>
50+
operator detail::enable_if_t<!has_offset, item<dimensions, true>>() const {
51+
return detail::Builder::createItem<dimensions, true>(
52+
MImpl.MExtent, MImpl.MIndex, /*Offset*/ {});
6553
}
6654

67-
item<dimensions, with_offset>(const item<dimensions, with_offset> &rhs) =
68-
default;
55+
size_t get_linear_id() const { return MImpl.get_linear_id(); }
6956

70-
item<dimensions, with_offset>(item<dimensions, with_offset> &&rhs) = default;
57+
item(const item &rhs) = default;
7158

72-
item<dimensions, with_offset> &
73-
operator=(const item<dimensions, with_offset> &rhs) = default;
59+
item(item<dimensions, with_offset> &&rhs) = default;
7460

75-
item<dimensions, with_offset> &
76-
operator=(item<dimensions, with_offset> &&rhs) = default;
61+
item &operator=(const item &rhs) = default;
7762

78-
bool operator==(const item<dimensions, with_offset> &rhs) const {
79-
return (rhs.index == this->index) && (rhs.extent == this->extent) &&
80-
(rhs.offset == this->offset);
81-
}
63+
item &operator=(item &&rhs) = default;
8264

83-
bool operator!=(const item<dimensions, with_offset> &rhs) const {
84-
return !((*this) == rhs);
85-
}
65+
bool operator==(const item &rhs) const { return rhs.MImpl == MImpl; }
8666

87-
protected:
88-
// For call constructor inside conversion operator
89-
friend class item<dimensions, false>;
90-
friend class item<dimensions, true>;
91-
friend class h_item<dimensions>;
92-
friend class detail::Builder;
67+
bool operator!=(const item &rhs) const { return rhs.MImpl != MImpl; }
9368

94-
template <size_t W = with_offset>
95-
item(typename std::enable_if<(W == true), const range<dimensions>>::type &R,
96-
const id<dimensions> &I, const id<dimensions> &O)
97-
: extent(R), index(I), offset(O) {}
69+
protected:
70+
template <bool has_offset = with_offset>
71+
item(detail::enable_if_t<has_offset, const range<dimensions>> &extent,
72+
const id<dimensions> &index, const id<dimensions> &offset)
73+
: MImpl{extent, index, offset} {}
9874

99-
template <size_t W = with_offset>
100-
item(typename std::enable_if<(W == false), const range<dimensions>>::type &R,
101-
const id<dimensions> &I)
102-
: extent(R), index(I), offset() {}
75+
template <bool has_offset = with_offset>
76+
item(detail::enable_if_t<!has_offset, const range<dimensions>> &extent,
77+
const id<dimensions> &index)
78+
: MImpl{extent, index} {}
10379

104-
void setID(const id<dimensions> &ID) { index = ID; }
80+
friend struct detail::Builder;
10581

10682
private:
107-
range<dimensions> extent;
108-
id<dimensions> index;
109-
id<dimensions> offset;
83+
detail::ItemBase<dimensions, with_offset> MImpl;
11084
};
11185

11286
} // namespace sycl

0 commit comments

Comments
 (0)