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