Skip to content

Commit 78ee29d

Browse files
authored
[SYCL][COMPAT] Add default ctor to dim3 and update tests/docs (#14347)
This PR adds a default constructor for `syclcompat::dim3`, and changes the type of members `x`,`y`,`z` from `const size_t` to `unsigned int`. The default constructor sets all 3 dimensions to `1`. This means patterns like this are now possible: ```cpp syclcompat::dim3 myDim3; myDim3.x = 32; ``` --------- Signed-off-by: Joe Todd <[email protected]>
1 parent 3dc75a7 commit 78ee29d

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

sycl/doc/syclcompat/README.md

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,24 @@ those to learn to use the library.
7878

7979
SYCLcompat provides a `dim3` class akin to that of CUDA or HIP programming
8080
models. `dim3` encapsulates other languages iteration spaces that are
81-
represented with coordinate letters (x, y, z).
81+
represented with coordinate letters (x, y, z). In SYCL, the fastest-moving
82+
dimension is the one with the highest index, e.g. in a SYCL 2D range iteration
83+
space, there are two dimensions, 0 and 1, and 1 will be the one that "moves
84+
faster". For CUDA/HIP, the convention is reversed: `x` is the dimension which
85+
moves fastest. `syclcompat::dim3` follows this convention, so that
86+
`syclcompat::dim3(32, 4)` is equivalent to `sycl::range<2>(4, 32)`, and
87+
`syclcompat::dim3(32, 4, 2)` is equivalent to `sycl::range<3>(2, 4, 32)`.
8288

8389
```cpp
8490
namespace syclcompat {
8591

8692
class dim3 {
8793
public:
88-
const size_t x, y, z;
94+
unsigned int x, y, z;
8995
dim3(const sycl::range<3> &r);
9096
dim3(const sycl::range<2> &r);
9197
dim3(const sycl::range<1> &r);
92-
constexpr dim3(size_t x, size_t y = 1, size_t z = 1);
98+
constexpr dim3(unsigned int x = 1, unsigned int y = 1, unsigned int z = 1);
9399

94100
constexpr size_t size();
95101

@@ -106,12 +112,10 @@ inline dim3 operator-(const dim3 &a, const dim3 &b);
106112
} // syclcompat
107113
```
108114
109-
In SYCL, the fastest-moving dimension is the one with the highest index, e.g. in
110-
a SYCL 2D range iteration space, there are two dimensions, 0 and 1, and 1 will
111-
be the one that "moves faster". The compatibility headers for SYCL offer a
112-
number of convenience functions that help the mapping between xyz-based
113-
coordinates to SYCL iteration spaces in the different scopes available. In
114-
addition to the global range, the following helper functions are also provided:
115+
The compatibility headers for SYCL offer a number of convenience functions that
116+
help the mapping between xyz-based coordinates to SYCL iteration spaces in the
117+
different scopes available. In addition to the global range, the following
118+
helper functions are also provided:
115119
116120
``` c++
117121
namespace syclcompat {

sycl/include/syclcompat/dims.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ namespace syclcompat {
3030

3131
class dim3 {
3232
public:
33-
const size_t x, y, z;
33+
unsigned int x, y, z;
3434

3535
dim3(const sycl::range<3> &r) : x(r[2]), y(r[1]), z(r[0]) {}
3636

3737
dim3(const sycl::range<2> &r) : x(r[1]), y(r[0]), z(1) {}
3838

3939
dim3(const sycl::range<1> &r) : x(r[0]), y(1), z(1) {}
4040

41-
constexpr dim3(size_t x, size_t y = 1, size_t z = 1) : x(x), y(y), z(z) {}
41+
constexpr dim3(unsigned int x = 1, unsigned int y = 1, unsigned int z = 1)
42+
: x(x), y(y), z(z) {}
4243

4344
constexpr size_t size() const { return x * y * z; }
4445

sycl/test-e2e/syclcompat/dim.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,33 @@ int main() {
3535
assert(d3.y == 1);
3636
assert(d3.z == 1);
3737
}
38+
std::cout << "Testing Empty Construct" << std::endl;
39+
{
40+
syclcompat::dim3 d3;
41+
assert(d3.x == 1);
42+
assert(d3.y == 1);
43+
assert(d3.z == 1);
44+
}
45+
std::cout << "Testing Empty Construct & Update" << std::endl;
46+
{
47+
syclcompat::dim3 d3;
48+
d3.x = 1;
49+
d3.y = 2;
50+
d3.z = 3;
51+
52+
assert(d3.x == 1);
53+
assert(d3.y == 2);
54+
assert(d3.z == 3);
55+
}
56+
std::cout << "Testing Empty Construct & Update 2" << std::endl;
57+
{
58+
syclcompat::dim3 d3;
59+
d3.x = 32;
60+
61+
assert(d3.x == 32);
62+
assert(d3.y == 1);
63+
assert(d3.z == 1);
64+
}
3865
std::cout << "Testing Convert" << std::endl;
3966
{
4067
syclcompat::dim3 d3(512);

0 commit comments

Comments
 (0)