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  operator sycl::range<dimensions>() const {
64  for (int i = 0; i < dimensions; ++i) {
65  result[i] = common_array[i];
66  }
67  return result;
68  }
69 
70  size_t get(int dimension) const {
71  check_dimension(dimension);
72  return common_array[dimension];
73  }
74 
75  size_t &operator[](int dimension) {
76  check_dimension(dimension);
77  return common_array[dimension];
78  }
79 
80  size_t operator[](int dimension) const {
81  check_dimension(dimension);
82  return common_array[dimension];
83  }
84 
85  array(const array<dimensions> &rhs) = default;
86  array(array<dimensions> &&rhs) = default;
89 
90  // Returns true iff all elements in 'this' are equal to
91  // the corresponding elements in 'rhs'.
92  bool operator==(const array<dimensions> &rhs) const {
93  for (int i = 0; i < dimensions; ++i) {
94  if (this->common_array[i] != rhs.common_array[i]) {
95  return false;
96  }
97  }
98  return true;
99  }
100 
101  // Returns true iff there is at least one element in 'this'
102  // which is not equal to the corresponding element in 'rhs'.
103  bool operator!=(const array<dimensions> &rhs) const {
104  for (int i = 0; i < dimensions; ++i) {
105  if (this->common_array[i] != rhs.common_array[i]) {
106  return true;
107  }
108  }
109  return false;
110  }
111 
112 protected:
113  size_t common_array[dimensions];
114  __SYCL_ALWAYS_INLINE void check_dimension(int dimension) const {
115 #ifndef __SYCL_DEVICE_ONLY__
116  if (dimension >= dimensions || dimension < 0) {
117  throw sycl::invalid_parameter_error("Index out of range",
118  PI_ERROR_INVALID_VALUE);
119  }
120 #endif
121  (void)dimension;
122  }
123 };
124 
125 } // namespace detail
126 } // namespace _V1
127 } // 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:92
size_t operator[](int dimension) const
Definition: array.hpp:80
size_t common_array[dimensions]
Definition: array.hpp:113
array(array< dimensions > &&rhs)=default
bool operator!=(const array< dimensions > &rhs) const
Definition: array.hpp:103
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:75
size_t get(int dimension) const
Definition: array.hpp:70
array< dimensions > & operator=(array< dimensions > &&rhs)=default
__SYCL_ALWAYS_INLINE void check_dimension(int dimension) const
Definition: array.hpp:114
A unique identifier of an item in an index space.
Definition: id.hpp:36
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: range.hpp:26
#define __SYCL_ALWAYS_INLINE
Definition: access.hpp:18