DPC++ Runtime
Runtime libraries for oneAPI DPC++
array.hpp
Go to the documentation of this file.
1 //==-------- array.hpp --- SYCL common iteration object --------------------==//
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 #include <functional>
11 #include <stdexcept>
13 #include <sycl/exception.hpp>
14 
15 namespace sycl {
17 template <int dimensions> class id;
18 template <int dimensions> class range;
19 namespace detail {
20 
21 template <int dimensions = 1> class array {
22  static_assert(dimensions >= 1, "Array cannot be 0-dimensional.");
23 
24 public:
25  /* The following constructor is only available in the array struct
26  * specialization where: dimensions==1 */
27  template <int N = dimensions>
28  array(typename detail::enable_if_t<(N == 1), size_t> dim0 = 0)
29  : common_array{dim0} {}
30 
31  /* The following constructors are only available in the array struct
32  * specialization where: dimensions==2 */
33  template <int N = dimensions>
34  array(typename detail::enable_if_t<(N == 2), size_t> dim0, size_t dim1)
35  : common_array{dim0, dim1} {}
36 
37  template <int N = dimensions, detail::enable_if_t<(N == 2), size_t> = 0>
38  array() : array(0, 0) {}
39 
40  /* The following constructors are only available in the array struct
41  * specialization where: dimensions==3 */
42  template <int N = dimensions>
43  array(typename detail::enable_if_t<(N == 3), size_t> dim0, size_t dim1,
44  size_t dim2)
45  : common_array{dim0, dim1, dim2} {}
46 
47  template <int N = dimensions, detail::enable_if_t<(N == 3), size_t> = 0>
48  array() : array(0, 0, 0) {}
49 
50  // Conversion operators to derived classes
51  operator sycl::id<dimensions>() const {
52  sycl::id<dimensions> result;
53  for (int i = 0; i < dimensions; ++i) {
54  result[i] = common_array[i];
55  }
56  return result;
57  }
58 
59  operator sycl::range<dimensions>() const {
60  sycl::range<dimensions> result;
61  for (int i = 0; i < dimensions; ++i) {
62  result[i] = common_array[i];
63  }
64  return result;
65  }
66 
67  size_t get(int dimension) const {
68  check_dimension(dimension);
69  return common_array[dimension];
70  }
71 
72  size_t &operator[](int dimension) {
73  check_dimension(dimension);
74  return common_array[dimension];
75  }
76 
77  size_t operator[](int dimension) const {
78  check_dimension(dimension);
79  return common_array[dimension];
80  }
81 
82  array(const array<dimensions> &rhs) = default;
83  array(array<dimensions> &&rhs) = default;
84  array<dimensions> &operator=(const array<dimensions> &rhs) = default;
86 
87  // Returns true iff all elements in 'this' are equal to
88  // the corresponding elements in 'rhs'.
89  bool operator==(const array<dimensions> &rhs) const {
90  for (int i = 0; i < dimensions; ++i) {
91  if (this->common_array[i] != rhs.common_array[i]) {
92  return false;
93  }
94  }
95  return true;
96  }
97 
98  // Returns true iff there is at least one element in 'this'
99  // which is not equal to the corresponding element in 'rhs'.
100  bool operator!=(const array<dimensions> &rhs) const {
101  for (int i = 0; i < dimensions; ++i) {
102  if (this->common_array[i] != rhs.common_array[i]) {
103  return true;
104  }
105  }
106  return false;
107  }
108 
109 protected:
110  size_t common_array[dimensions];
111  __SYCL_ALWAYS_INLINE void check_dimension(int dimension) const {
112 #ifndef __SYCL_DEVICE_ONLY__
113  if (dimension >= dimensions || dimension < 0) {
114  throw sycl::invalid_parameter_error("Index out of range",
115  PI_ERROR_INVALID_VALUE);
116  }
117 #endif
118  (void)dimension;
119  }
120 };
121 
122 } // namespace detail
123 } // __SYCL_INLINE_VER_NAMESPACE(_V1)
124 } // namespace sycl
sycl::_V1::detail::array::operator[]
size_t operator[](int dimension) const
Definition: array.hpp:77
type_traits.hpp
__SYCL_INLINE_VER_NAMESPACE
#define __SYCL_INLINE_VER_NAMESPACE(X)
Definition: defines_elementary.hpp:11
sycl::_V1::detail::array::check_dimension
__SYCL_ALWAYS_INLINE void check_dimension(int dimension) const
Definition: array.hpp:111
__SYCL_ALWAYS_INLINE
#define __SYCL_ALWAYS_INLINE
Definition: defines_elementary.hpp:25
sycl
---— Error handling, matching OpenCL plugin semantics.
Definition: access.hpp:14
sycl::_V1::detail::array::operator==
bool operator==(const array< dimensions > &rhs) const
Definition: array.hpp:89
sycl::_V1::id
A unique identifier of an item in an index space.
Definition: array.hpp:17
sycl::_V1::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
sycl::_V1::detail::array::common_array
size_t common_array[dimensions]
Definition: array.hpp:110
sycl::_V1::detail::array::get
size_t get(int dimension) const
Definition: array.hpp:67
sycl::_V1::detail::array::array
array(typename detail::enable_if_t<(N==2), size_t > dim0, size_t dim1)
Definition: array.hpp:34
sycl::_V1::range
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: buffer.hpp:28
sycl::_V1::ext::oneapi::experimental::operator=
annotated_arg & operator=(annotated_arg &)=default
sycl::_V1::detail::array::array
array()
Definition: array.hpp:38
sycl::_V1::detail::array::array
array(typename detail::enable_if_t<(N==3), size_t > dim0, size_t dim1, size_t dim2)
Definition: array.hpp:43
exception.hpp
sycl::_V1::detail::array::operator!=
bool operator!=(const array< dimensions > &rhs) const
Definition: array.hpp:100
sycl::_V1::detail::array::operator[]
size_t & operator[](int dimension)
Definition: array.hpp:72
sycl::_V1::detail::array::array
array(typename detail::enable_if_t<(N==1), size_t > dim0=0)
Definition: array.hpp:28
sycl::_V1::detail::array
Definition: array.hpp:21