Ginkgo  Generated from pipelines/1478841010 branch based on develop. Ginkgo version 1.9.0
A numerical linear algebra library targeting many-core architectures
dim.hpp
1 // SPDX-FileCopyrightText: 2017 - 2024 The Ginkgo authors
2 //
3 // SPDX-License-Identifier: BSD-3-Clause
4 
5 #ifndef GKO_PUBLIC_CORE_BASE_DIM_HPP_
6 #define GKO_PUBLIC_CORE_BASE_DIM_HPP_
7 
8 
9 #include <iostream>
10 
11 #include <ginkgo/core/base/types.hpp>
12 
13 
14 namespace gko {
15 
16 
25 template <size_type Dimensionality, typename DimensionType = size_type>
26 struct dim {
27  static constexpr size_type dimensionality = Dimensionality;
28  friend struct dim<dimensionality + 1>;
29 
30  using dimension_type = DimensionType;
31 
35  constexpr GKO_ATTRIBUTES dim() : dim{dimension_type{}} {}
36 
42  explicit constexpr GKO_ATTRIBUTES dim(const dimension_type& size)
43  : first_{size}, rest_{size}
44  {}
45 
59  template <typename... Rest, std::enable_if_t<sizeof...(Rest) ==
60  Dimensionality - 1>* = nullptr>
61  constexpr GKO_ATTRIBUTES dim(const dimension_type& first,
62  const Rest&... rest)
63  : first_{first}, rest_{static_cast<dimension_type>(rest)...}
64  {}
65 
77  constexpr GKO_ATTRIBUTES const dimension_type& operator[](
78  const size_type& dimension) const noexcept
79  {
80  return GKO_ASSERT(dimension < dimensionality),
81  dimension == 0 ? first_ : rest_[dimension - 1];
82  }
83 
87  GKO_ATTRIBUTES dimension_type& operator[](
88  const size_type& dimension) noexcept
89  {
90  return GKO_ASSERT(dimension < dimensionality),
91  dimension == 0 ? first_ : rest_[dimension - 1];
92  }
93 
105  explicit constexpr GKO_ATTRIBUTES operator bool() const
106  {
107  return static_cast<bool>(first_) && static_cast<bool>(rest_);
108  }
109 
118  friend constexpr GKO_ATTRIBUTES bool operator==(const dim& x, const dim& y)
119  {
120  return x.first_ == y.first_ && x.rest_ == y.rest_;
121  }
122 
131  friend constexpr GKO_ATTRIBUTES bool operator!=(const dim& x, const dim& y)
132  {
133  return !(x == y);
134  }
135 
144  friend constexpr GKO_ATTRIBUTES dim operator*(const dim& x, const dim& y)
145  {
146  return dim(x.first_ * y.first_, x.rest_ * y.rest_);
147  }
148 
157  friend std::ostream& operator<<(std::ostream& os, const dim& x)
158  {
159  os << "(";
160  x.print_to(os);
161  os << ")";
162  return os;
163  }
164 
165 private:
166  void inline print_to(std::ostream& os) const
167  {
168  os << first_ << ", ";
169  rest_.print_to(os);
170  }
171 
172 
173  constexpr GKO_ATTRIBUTES dim(const dimension_type first,
174  dim<dimensionality - 1> rest)
175  : first_{first}, rest_{rest}
176  {}
177 
178  dimension_type first_;
179  dim<dimensionality - 1, dimension_type> rest_;
180 };
181 
182 
183 // base case for dim recursive template
184 template <typename DimensionType>
186  static constexpr size_type dimensionality = 1u;
187  friend struct dim<2>;
188 
189  using dimension_type = DimensionType;
190 
191  constexpr GKO_ATTRIBUTES dim(const dimension_type& size = dimension_type{})
192  : first_{size}
193  {}
194 
195  constexpr GKO_ATTRIBUTES const dimension_type& operator[](
196  const size_type& dimension) const noexcept
197  {
198  return GKO_ASSERT(dimension == 0), first_;
199  }
200 
201  GKO_ATTRIBUTES dimension_type& operator[](const size_type& dimension)
202  {
203  return GKO_ASSERT(dimension == 0), first_;
204  }
205 
206  explicit constexpr GKO_ATTRIBUTES operator bool() const
207  {
208  return static_cast<bool>(first_);
209  }
210 
211  friend constexpr GKO_ATTRIBUTES bool operator==(const dim& x, const dim& y)
212  {
213  return x.first_ == y.first_;
214  }
215 
216  friend constexpr GKO_ATTRIBUTES bool operator!=(const dim& x, const dim& y)
217  {
218  return !(x == y);
219  }
220 
221  friend constexpr GKO_ATTRIBUTES dim operator*(const dim& x, const dim& y)
222  {
223  return dim(x.first_ * y.first_);
224  }
225 
226  friend std::ostream& operator<<(std::ostream& os, const dim& x)
227  {
228  os << "(";
229  x.print_to(os);
230  os << ")";
231  return os;
232  }
233 
234 private:
235  void inline print_to(std::ostream& os) const { os << first_; }
236 
237  dimension_type first_;
238 };
239 
240 
250 template <typename DimensionType>
251 constexpr GKO_ATTRIBUTES GKO_INLINE dim<2, DimensionType> transpose(
252  const dim<2, DimensionType>& dimensions) noexcept
253 {
254  return {dimensions[1], dimensions[0]};
255 }
256 
257 
258 } // namespace gko
259 
260 
261 #endif // GKO_PUBLIC_CORE_BASE_DIM_HPP_
gko::dim::operator==
constexpr friend bool operator==(const dim &x, const dim &y)
Checks if two dim objects are equal.
Definition: dim.hpp:118
gko::dim::operator*
constexpr friend dim operator*(const dim &x, const dim &y)
Multiplies two dim objects.
Definition: dim.hpp:144
gko::dim< 1u, DimensionType >
Definition: dim.hpp:185
gko::size_type
std::size_t size_type
Integral type used for allocation quantities.
Definition: types.hpp:86
gko::dim::operator<<
friend std::ostream & operator<<(std::ostream &os, const dim &x)
A stream operator overload for dim.
Definition: dim.hpp:157
gko::dim::operator[]
constexpr const dimension_type & operator[](const size_type &dimension) const noexcept
Returns the requested dimension.
Definition: dim.hpp:77
gko
The Ginkgo namespace.
Definition: abstract_factory.hpp:20
gko::dim::operator[]
dimension_type & operator[](const size_type &dimension) noexcept
Definition: dim.hpp:87
gko::dim
A type representing the dimensions of a multidimensional object.
Definition: dim.hpp:26
gko::dim::operator!=
constexpr friend bool operator!=(const dim &x, const dim &y)
Checks if two dim objects are not equal.
Definition: dim.hpp:131
gko::dim::dim
constexpr dim(const dimension_type &first, const Rest &... rest)
Creates a dimension object with the specified dimensions.
Definition: dim.hpp:61
gko::transpose
batch_dim< 2, DimensionType > transpose(const batch_dim< 2, DimensionType > &input)
Returns a batch_dim object with its dimensions swapped for batched operators.
Definition: batch_dim.hpp:119
gko::dim::dim
constexpr dim()
Creates a dimension object with all dimensions set to zero.
Definition: dim.hpp:35
gko::dim::dim
constexpr dim(const dimension_type &size)
Creates a dimension object with all dimensions set to the same value.
Definition: dim.hpp:42