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  const size_t 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(size_t x, size_t y = 1, size_t z = 1) : x(x), y(y), z(z) {}
42 
43  constexpr size_t size() const { return x * y * z; }
44 
45  operator sycl::range<3>() const { return sycl::range<3>(z, y, x); }
46  operator sycl::range<2>() const {
47  if (z != 1)
48  throw std::invalid_argument(
49  "Attempting to convert a 3D dim3 into sycl::range<2>");
50  return sycl::range<2>(y, x);
51  }
52  operator sycl::range<1>() const {
53  if (z != 1 || y != 1)
54  throw std::invalid_argument(
55  "Attempting to convert a 2D or 3D dim3 into sycl::range<1>");
56  return sycl::range<1>(x);
57  }
58 }; // namespace dim3
59 
60 inline dim3 operator*(const dim3 &a, const dim3 &b) {
61  return dim3{a.x * b.x, a.y * b.y, a.z * b.z};
62 }
63 
64 inline dim3 operator+(const dim3 &a, const dim3 &b) {
65  return dim3{a.x + b.x, a.y + b.y, a.z + b.z};
66 }
67 
68 inline dim3 operator-(const dim3 &a, const dim3 &b) {
69  return dim3{a.x - b.x, a.y - b.y, a.z - b.z};
70 }
71 
72 } // namespace syclcompat
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: range.hpp:26
constexpr dim3(size_t x, size_t y=1, size_t z=1)
Definition: dims.hpp:41
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:43
dim3(const sycl::range< 3 > &r)
Definition: dims.hpp:35
const size_t y
Definition: dims.hpp:33
const size_t z
Definition: dims.hpp:33
const size_t x
Definition: dims.hpp:33
dim3 operator-(const dim3 &a, const dim3 &b)
Definition: dims.hpp:68
dim3 operator+(const dim3 &a, const dim3 &b)
Definition: dims.hpp:64
dim3 operator*(const dim3 &a, const dim3 &b)
Definition: dims.hpp:60