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