DPC++ Runtime
Runtime libraries for oneAPI DPC++
dims.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  *
3  * Copyright (C) Codeplay Software Ltd.
4  *
5  * Part of the LLVM Project, under the Apache License v2.0 with LLVM
6  * Exceptions. See https://llvm.org/LICENSE.txt for license information.
7  * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  *
15  * SYCLcompat
16  *
17  * dims.hpp
18  *
19  * Description:
20  * dim3 functionality for SYCLcompat
21  **************************************************************************/
22 
23 #pragma once
24 
25 #include <tuple>
26 
27 #include <sycl/range.hpp>
28 
29 namespace syclcompat {
30 
31 class dim3 {
32 public:
33  unsigned int x, y, z;
34 
35  dim3(const sycl::range<3> &r) : x(r[2]), y(r[1]), z(r[0]) {}
36 
37  dim3(const sycl::range<2> &r) : x(r[1]), y(r[0]), z(1) {}
38 
39  dim3(const sycl::range<1> &r) : x(r[0]), y(1), z(1) {}
40 
41  constexpr dim3(unsigned int x = 1, unsigned int y = 1, unsigned int z = 1)
42  : x(x), y(y), z(z) {}
43 
44  constexpr size_t size() const { return x * y * z; }
45 
46  operator sycl::range<3>() const { return sycl::range<3>(z, y, x); }
47  operator sycl::range<2>() const {
48  if (z != 1)
49  throw std::invalid_argument(
50  "Attempting to convert a 3D dim3 into sycl::range<2>");
51  return sycl::range<2>(y, x);
52  }
53  operator sycl::range<1>() const {
54  if (z != 1 || y != 1)
55  throw std::invalid_argument(
56  "Attempting to convert a 2D or 3D dim3 into sycl::range<1>");
57  return sycl::range<1>(x);
58  }
59 }; // namespace dim3
60 
61 inline dim3 operator*(const dim3 &a, const dim3 &b) {
62  return dim3{a.x * b.x, a.y * b.y, a.z * b.z};
63 }
64 
65 inline dim3 operator+(const dim3 &a, const dim3 &b) {
66  return dim3{a.x + b.x, a.y + b.y, a.z + b.z};
67 }
68 
69 inline dim3 operator-(const dim3 &a, const dim3 &b) {
70  return dim3{a.x - b.x, a.y - b.y, a.z - b.z};
71 }
72 
73 } // namespace syclcompat
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: range.hpp:26
unsigned int z
Definition: dims.hpp:33
unsigned int x
Definition: dims.hpp:33
dim3(const sycl::range< 2 > &r)
Definition: dims.hpp:37
dim3(const sycl::range< 1 > &r)
Definition: dims.hpp:39
constexpr size_t size() const
Definition: dims.hpp:44
dim3(const sycl::range< 3 > &r)
Definition: dims.hpp:35
constexpr dim3(unsigned int x=1, unsigned int y=1, unsigned int z=1)
Definition: dims.hpp:41
unsigned int y
Definition: dims.hpp:33
dim3 operator-(const dim3 &a, const dim3 &b)
Definition: dims.hpp:69
dim3 operator+(const dim3 &a, const dim3 &b)
Definition: dims.hpp:65
dim3 operator*(const dim3 &a, const dim3 &b)
Definition: dims.hpp:61