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