DPC++ Runtime
Runtime libraries for oneAPI Data Parallel C++
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
11 #include <CL/sycl/exception.hpp>
12 #include <functional>
13 #include <stdexcept>
14 
16 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 cl::sycl::id<dimensions>() const {
53  for (int i = 0; i < dimensions; ++i) {
54  result[i] = common_array[i];
55  }
56  return result;
57  }
58 
59  operator cl::sycl::range<dimensions>() const {
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;
85  array<dimensions> &operator=(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 cl::sycl::invalid_parameter_error("Index out of range",
116  }
117 #endif
118  (void)dimension;
119  }
120 };
121 
122 } // namespace detail
123 } // namespace sycl
124 } // __SYCL_INLINE_NAMESPACE(cl)
cl::sycl::detail::array::array
array(typename detail::enable_if_t<(N==3), size_t > dim0, size_t dim1, size_t dim2)
Definition: array.hpp:43
type_traits.hpp
cl::sycl::detail::array::array
array(typename detail::enable_if_t<(N==1), size_t > dim0=0)
Definition: array.hpp:28
cl::sycl::id
A unique identifier of an item in an index space.
Definition: array.hpp:17
cl::sycl::detail::array::common_array
size_t common_array[dimensions]
Definition: array.hpp:110
cl::sycl::detail::array::array
array()
Definition: array.hpp:38
cl::sycl::range
Defines the iteration domain of either a single work-group in a parallel dispatch,...
Definition: buffer.hpp:24
cl::sycl::detail::array::operator==
bool operator==(const array< dimensions > &rhs) const
Definition: array.hpp:89
__SYCL_ALWAYS_INLINE
#define __SYCL_ALWAYS_INLINE
Definition: defines_elementary.hpp:29
cl::sycl::detail::array::get
size_t get(int dimension) const
Definition: array.hpp:67
cl
We provide new interfaces for matrix muliply in this patch:
Definition: access.hpp:13
cl::sycl::detail::array::check_dimension
__SYCL_ALWAYS_INLINE void check_dimension(int dimension) const
Definition: array.hpp:111
cl::sycl::detail::array
Definition: array.hpp:21
PI_INVALID_VALUE
@ PI_INVALID_VALUE
Definition: pi.h:87
exception.hpp
cl::sycl::detail::array::array
array(typename detail::enable_if_t<(N==2), size_t > dim0, size_t dim1)
Definition: array.hpp:34
cl::sycl::detail::array::operator[]
size_t operator[](int dimension) const
Definition: array.hpp:77
cl::sycl::detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: stl_type_traits.hpp:24
cl::sycl::detail::array::operator!=
bool operator!=(const array< dimensions > &rhs) const
Definition: array.hpp:100
cl::sycl::detail::array::operator[]
size_t & operator[](int dimension)
Definition: array.hpp:72
__SYCL_INLINE_NAMESPACE
#define __SYCL_INLINE_NAMESPACE(X)
Definition: defines_elementary.hpp:12