batmat 0.0.15
Batched linear algebra routines
Loading...
Searching...
No Matches
matrix.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file
4/// Class for a batch of matrices that owns its storage.
5/// @ingroup topic-matrix
6
9
10#include <type_traits>
11#include <utility>
12
13namespace batmat::matrix {
14
15namespace detail {
16
17template <class, class I, class Stride>
19 using type = std::integral_constant<I, 0>;
20};
21template <class T, class I, class Stride>
22 requires requires {
23 { Stride::value } -> std::convertible_to<I>;
24 }
25struct default_alignment<T, I, Stride> {
26 using type = std::integral_constant<I, alignof(T) * Stride::value>;
27};
28template <class T, class I, class Stride>
30
33
34} // namespace detail
35
36/// Class for a batch of matrices that owns its storage.
37/// @tparam T
38/// Element value type.
39/// @tparam I
40/// Index type.
41/// @tparam S
42/// Inner stride (batch size).
43/// @tparam D
44/// Depth type.
45/// @tparam O
46/// Storage order (column or row major).
47/// @tparam A
48/// Batch alignment type.
49/// @ingroup topic-matrix
50template <class T, class I = index_t, class S = std::integral_constant<I, 1>, class D = I,
51 StorageOrder O = StorageOrder::ColMajor, class A = detail::default_alignment_t<T, I, S>>
52struct Matrix {
53 static_assert(!std::is_const_v<T>);
57 using plain_layout_type = typename layout_type::PlainLayout;
58 using value_type = T;
59 using index_type = typename layout_type::index_type;
60 using batch_size_type = typename layout_type::batch_size_type;
61 using depth_type = typename layout_type::depth_type;
62 using standard_stride_type = typename layout_type::standard_stride_type;
63 using alignment_type = A;
66 static constexpr bool is_row_major = view_type::is_row_major;
67 static constexpr bool has_single_batch_at_compile_time =
69 static constexpr bool has_single_layer_at_compile_time =
71
72 private:
74
75 static constexpr auto default_alignment(layout_type layout) {
76 if constexpr (std::is_integral_v<alignment_type>)
77 return alignof(T) * static_cast<size_t>(layout.batch_size); // TODO
78 if constexpr (alignment_type::value == 0)
79 return alignof(T) * static_cast<size_t>(layout.batch_size);
80 else
81 return alignment_type{};
82 }
83 [[nodiscard]] static auto allocate(layout_type layout) {
84 const auto alignment = default_alignment(layout);
85 return make_aligned_unique_ptr<T>(layout.padded_size(), alignment);
86 }
87 [[nodiscard]] static auto allocate(layout_type layout, uninitialized_t init) {
88 const auto alignment = default_alignment(layout);
89 return make_aligned_unique_ptr<T>(layout.padded_size(), alignment, init);
90 }
91 void clear() {
92 const auto alignment = default_alignment(view_.layout);
93 if (auto d = std::exchange(view_.data_ptr, nullptr))
94 aligned_deleter<T, decltype(alignment)>(view_.layout.padded_size(), alignment)(d);
95 view_.layout.rows = 0;
96 }
97
98 template <class U, class J, class R, class E, class M>
99 requires(std::convertible_to<U, T> && std::equality_comparable_with<index_type, J>)
101 layout_type new_layout{{.depth = static_cast<depth_type>(other.depth()),
102 .rows = other.rows(),
103 .cols = other.cols(),
104 .batch_size = static_cast<batch_size_type>(other.batch_size())}};
105 resize(new_layout);
106 view().copy_values(other); // TODO: exception safety
107 }
108
109 public:
110 /// @name Constructors, assignment and resizing
111 /// @{
112
113 // TODO: allowing the user to specify strides during construction is kind of pointless,
114 // because it introduces many padding elements, which can never be accessed.
115
116 Matrix() = default;
122 /// Copy the values from another matrix.
123 Matrix(const Matrix &o) : Matrix{o.layout()} {
124 this->view().copy_values(o.view()); // TODO: exception safety
125 }
126 Matrix(Matrix &&o) noexcept : view_{o.view()} { o.view_.reassign({}); }
127 /// Cheap move assignment. No data is copied.
128 Matrix &operator=(Matrix &&o) noexcept {
129 using std::swap;
130 if (&o != this) {
131 swap(o.view_.data_ptr, this->view_.data_ptr);
132 swap(o.view_.layout, this->view_.layout);
133 }
134 return *this;
135 }
136 ~Matrix() { clear(); }
137
138 /// Resize the matrix to a new layout, reallocating if the padded size changes.
139 void resize(layout_type new_layout) {
140 if (new_layout.padded_size() != layout().padded_size()) {
141 clear();
142 view_.data_ptr = allocate(new_layout).release();
143 }
144 view_.layout = new_layout;
145 }
146
147 /// @}
148
149 /// @name Element access
150 /// @{
151
152 /// Access a single element at layer @p l, row @p r and column @p c.
154 return view()(l, r, c);
155 }
156 /// Access a single element at layer @p l, row @p r and column @p c.
157 [[nodiscard]] const value_type &operator()(index_type l, index_type r, index_type c) const {
158 return view()(l, r, c);
159 }
160
161 /// @}
162
163 /// @name Batch-wise slicing
164 /// @{
165
166 /// @copydoc View::batch()
167 [[nodiscard]] auto batch(index_type b) { return view().batch(b); }
168 /// @copydoc View::batch()
169 [[nodiscard]] auto batch(index_type b) const { return view().batch(b); }
170 /// @copydoc View::batch_dyn()
171 [[nodiscard]] auto batch_dyn(index_type b) { return view().batch_dyn(b); }
172 /// @copydoc View::batch_dyn()
173 [[nodiscard]] auto batch_dyn(index_type b) const { return view().batch_dyn(b); }
174 /// @copydoc View::middle_batches()
175 [[nodiscard]] auto middle_batches(index_type b, index_type n, index_type stride = 1) {
176 return view().middle_batches(b, n, stride);
177 }
178 /// @copydoc View::middle_batches()
179 [[nodiscard]] auto middle_batches(index_type b, index_type n, index_type stride = 1) const {
180 return view().middle_batches(b, n, stride);
181 }
182
183 /// @}
184
185 /// @name Layer-wise slicing
186 /// @{
187
188 /// Access a single layer @p l as a non-batched view.
192 /// Access a single layer @p l as a non-batched view.
195 return view()(l);
196 }
197 /// @copydoc View::first_layers()
198 template <class N>
199 [[nodiscard]] auto first_layers(N n) {
200 return view().first_layers(n);
201 }
202 /// @copydoc View::first_layers()
203 template <class N>
204 [[nodiscard]] auto first_layers(N n) const {
205 return view().first_layers(n);
206 }
207 /// @copydoc View::middle_layers()
208 template <class N>
209 [[nodiscard]] auto middle_layers(index_type l, N n) {
210 return view().middle_layers(l, n);
211 }
212 /// @copydoc View::middle_layers()
213 template <class N>
214 [[nodiscard]] auto middle_layers(index_type l, N n) const {
215 return view().middle_layers(l, n);
216 }
217
218 /// @}
219
220 /// @name Iterators and buffer access
221 /// @{
222
223 /// @copydoc View::data()
224 [[nodiscard]] value_type *data() { return view().data(); }
225 /// @copydoc View::data()
226 [[nodiscard]] const value_type *data() const { return view().data(); }
227 /// @copydoc View::begin()
228 [[nodiscard]] auto begin() { return view().begin(); }
229 /// @copydoc View::begin()
230 [[nodiscard]] auto begin() const { return view().begin(); }
231 /// @copydoc View::end()
232 [[nodiscard]] auto end() { return view().end(); }
233 /// @copydoc View::end()
234 [[nodiscard]] auto end() const { return view().end(); }
235
236 /// @}
237
238 /// @name Dimensions
239 /// @{
240
241 [[nodiscard]] layout_type layout() const { return view_.layout; }
242 /// @copydoc View::size()
243 [[nodiscard]] index_type size() const { return view().size(); }
244 /// @copydoc View::padded_size()
245 [[nodiscard]] index_type padded_size() const { return view().padded_size(); }
246 /// @copydoc View::depth()
247 [[nodiscard]] depth_type depth() const { return view().depth(); }
248 /// @copydoc View::ceil_depth()
249 [[nodiscard]] index_type ceil_depth() const { return view().ceil_depth(); }
250 /// @copydoc View::num_batches()
251 [[nodiscard]] index_type num_batches() const { return view().num_batches(); }
252 /// @copydoc View::rows()
253 [[nodiscard]] index_type rows() const { return view().rows(); }
254 /// @copydoc View::cols()
255 [[nodiscard]] index_type cols() const { return view().cols(); }
256 /// @copydoc View::outer_size()
257 [[nodiscard]] index_type outer_size() const { return view().outer_size(); }
258 /// @copydoc View::inner_size()
259 [[nodiscard]] index_type inner_size() const { return view().inner_size(); }
260
261 /// @}
262
263 /// @name Strides
264 /// @{
265
266 /// @copydoc View::outer_stride()
267 [[nodiscard]] index_type outer_stride() const { return view().outer_stride(); }
268 /// @copydoc View::inner_stride()
269 [[nodiscard]] constexpr auto inner_stride() const { return view().inner_stride(); }
270 /// @copydoc View::row_stride()
271 [[nodiscard]] constexpr auto row_stride() const { return view().row_stride(); }
272 /// @copydoc View::col_stride()
273 [[nodiscard]] constexpr auto col_stride() const { return view().col_stride(); }
274 /// @copydoc View::layer_stride()
275 [[nodiscard]] index_type layer_stride() const { return view().layer_stride(); }
276 /// @copydoc View::has_full_layer_stride()
277 [[nodiscard]] bool has_full_layer_stride() const { return view().has_full_layer_stride(); }
278 /// @copydoc View::has_full_outer_stride()
279 [[nodiscard]] bool has_full_outer_stride() const { return view().has_full_outer_stride(); }
280 /// @copydoc View::has_full_inner_stride()
281 [[nodiscard]] bool has_full_inner_stride() const { return view().has_full_inner_stride(); }
282 /// @copydoc View::batch_size()
283 [[nodiscard]] batch_size_type batch_size() const { return view().batch_size(); }
284
285 /// @}
286
287 /// @name Reshaping and slicing
288 /// @{
289
290 /// @copydoc View::reshaped()
291 [[nodiscard]] auto reshaped(index_type rows, index_type cols) {
292 return view().reshaped(rows, cols);
293 }
294 /// @copydoc View::reshaped()
295 [[nodiscard]] auto reshaped(index_type rows, index_type cols) const {
296 return view().reshaped(rows, cols);
297 }
298 /// @copydoc View::top_rows()
299 [[nodiscard]] auto top_rows(index_type n) { return view().top_rows(n); }
300 /// @copydoc View::top_rows()
301 [[nodiscard]] auto top_rows(index_type n) const { return view().top_rows(n); }
302 /// @copydoc View::left_cols()
303 [[nodiscard]] auto left_cols(index_type n) { return view().left_cols(n); }
304 /// @copydoc View::left_cols()
305 [[nodiscard]] auto left_cols(index_type n) const { return view().left_cols(n); }
306 /// @copydoc View::bottom_rows()
307 [[nodiscard]] auto bottom_rows(index_type n) { return view().bottom_rows(n); }
308 /// @copydoc View::bottom_rows()
309 [[nodiscard]] auto bottom_rows(index_type n) const { return view().bottom_rows(n); }
310 /// @copydoc View::right_cols()
311 [[nodiscard]] auto right_cols(index_type n) { return view().right_cols(n); }
312 /// @copydoc View::right_cols()
313 [[nodiscard]] auto right_cols(index_type n) const { return view().right_cols(n); }
314 /// @copydoc View::middle_rows
315 [[nodiscard]] auto middle_rows(index_type r, index_type n) { return view().middle_rows(r, n); }
316 /// @copydoc View::middle_rows
317 [[nodiscard]] auto middle_rows(index_type r, index_type n) const {
318 return view().middle_rows(r, n);
319 }
320 /// @copydoc View::middle_rows
321 [[nodiscard]] auto middle_rows(index_type r, index_type n, index_type stride)
323 {
324 return view().middle_rows(r, n, stride);
325 }
326 /// @copydoc View::middle_rows
327 [[nodiscard]] auto middle_rows(index_type r, index_type n, index_type stride) const
329 {
330 return view().middle_rows(r, n, stride);
331 }
332 /// @copydoc View::middle_cols
333 [[nodiscard]] auto middle_cols(index_type c, index_type n) { return view().middle_cols(c, n); }
334 /// @copydoc View::middle_cols
335 [[nodiscard]] auto middle_cols(index_type c, index_type n) const {
336 return view().middle_cols(c, n);
337 }
338 /// @copydoc View::middle_cols
339 [[nodiscard]] auto middle_cols(index_type c, index_type n, index_type stride)
341 {
342 return view().middle_cols(c, n, stride);
343 }
344 /// @copydoc View::middle_cols
345 [[nodiscard]] auto middle_cols(index_type c, index_type n, index_type stride) const
347 {
348 return view().middle_cols(c, n, stride);
349 }
350 /// @copydoc View::top_left()
351 [[nodiscard]] auto top_left(index_type nr, index_type nc) { return view().top_left(nr, nc); }
352 /// @copydoc View::top_left()
353 [[nodiscard]] auto top_left(index_type nr, index_type nc) const {
354 return view().top_left(nr, nc);
355 }
356 /// @copydoc View::top_right()
357 [[nodiscard]] auto top_right(index_type nr, index_type nc) { return view().top_right(nr, nc); }
358 /// @copydoc View::top_right()
359 [[nodiscard]] auto top_right(index_type nr, index_type nc) const {
360 return view().top_right(nr, nc);
361 }
362 /// @copydoc View::bottom_left()
363 [[nodiscard]] auto bottom_left(index_type nr, index_type nc) {
364 return view().bottom_left(nr, nc);
365 }
366 /// @copydoc View::bottom_left()
367 [[nodiscard]] auto bottom_left(index_type nr, index_type nc) const {
368 return view().bottom_left(nr, nc);
369 }
370 /// @copydoc View::bottom_right()
371 [[nodiscard]] auto bottom_right(index_type nr, index_type nc) {
372 return view().bottom_right(nr, nc);
373 }
374 /// @copydoc View::bottom_right()
375 [[nodiscard]] auto bottom_right(index_type nr, index_type nc) const {
376 return view().bottom_right(nr, nc);
377 }
378 /// @copydoc View::block()
379 [[nodiscard]] auto block(index_type r, index_type c, index_type nr, index_type nc) {
380 return view().block(r, c, nr, nc);
381 }
382 /// @copydoc View::block()
383 [[nodiscard]] auto block(index_type r, index_type c, index_type nr, index_type nc) const {
384 return view().block(r, c, nr, nc);
385 }
386 /// @copydoc View::transposed()
387 [[nodiscard]] auto transposed() { return view().transposed(); }
388 /// @copydoc View::transposed()
389 [[nodiscard]] auto transposed() const { return view().transposed(); }
390
391 /// @}
392
393 /// @name Value manipulation
394 /// @{
395
396 /// Copy the values of another matrix, resizing if necessary.
398 if (&o != this) {
399 clear();
400 view_.reassign({allocate(o.layout()).release(), o.layout()});
401 // TODO: use allocate_for_overwrite or similar to avoid copy
402 // assignment
403 this->view_.copy_values(o.view_); // TODO: exception safety
404 }
405 return *this;
406 }
407
408 /// Copy the values from a compatible view, resizing if necessary.
409 template <class U, class J, class R, class E, class M>
410 requires(std::convertible_to<U, T> && std::equality_comparable_with<index_type, J>)
411 Matrix &operator=(View<U, J, R, E, M, O> other) {
412 assign_from_view(other);
413 return *this;
414 }
415
416 /// @copydoc View::set_constant()
418 /// @copydoc View::add_to_diagonal()
420 /// @copydoc View::negate()
421 void negate() { view().negate(); }
422 /// @copydoc View::copy_values()
423 template <class Other>
424 void copy_values(const Other &other) {
425 view().copy_values(other);
426 }
427 /// @copydoc View::operator+=()
428 template <class U, class J, class R, class E, class M>
429 requires(std::convertible_to<U, T> && std::equality_comparable_with<index_type, J>)
430 Matrix &operator+=(View<U, J, R, E, M, O> other) {
431 view() += other;
432 return *this;
433 }
434
435 /// @}
436
437 /// @name View conversions
438 /// @{
439
440 /// @copydoc View::view()
441 [[nodiscard, gnu::always_inline]] view_type view() { return view_; }
442 /// @copydoc View::view()
443 [[nodiscard, gnu::always_inline]] const_view_type view() const { return view_.as_const(); }
444 /// @copydoc View::as_const()
445 [[nodiscard]] auto as_const() const { return view(); }
446
447 operator view_type() { return view(); }
448 operator const_view_type() const { return view(); }
449 operator View<T, I, S, D, I, O>() { return view(); }
450 operator View<const T, I, S, D, I, O>() const { return view(); }
452 requires(!std::same_as<integral_value_type_t<D>, D>)
453 {
454 return view();
455 }
457 requires(!std::same_as<integral_value_type_t<D>, D>)
458 {
459 return view();
460 }
471
472 /// @}
473};
474
475template <class T, class I, class S, class D, class A, StorageOrder O>
476constexpr auto data(Matrix<T, I, S, D, O, A> &v) {
477 return v.data();
478}
479template <class T, class I, class S, class D, class A, StorageOrder O>
480constexpr auto data(Matrix<T, I, S, D, O, A> &&v) = delete;
481template <class T, class I, class S, class D, class A, StorageOrder O>
482constexpr auto data(const Matrix<T, I, S, D, O, A> &v) {
483 return v.data();
484}
485template <class T, class I, class S, class D, class A, StorageOrder O>
486constexpr auto rows(const Matrix<T, I, S, D, O, A> &v) {
487 return v.rows();
488}
489template <class T, class I, class S, class D, class A, StorageOrder O>
490constexpr auto cols(const Matrix<T, I, S, D, O, A> &v) {
491 return v.cols();
492}
493template <class T, class I, class S, class D, class A, StorageOrder O>
494constexpr auto outer_stride(const Matrix<T, I, S, D, O, A> &v) {
495 return v.outer_stride();
496}
497template <class T, class I, class S, class D, class A, StorageOrder O>
498constexpr auto depth(const Matrix<T, I, S, D, O, A> &v) {
499 return v.depth();
500}
501
502} // namespace batmat::matrix
auto make_aligned_unique_ptr(size_t size, A align)
Returns a smart pointer to an array of T that satisfies the given alignment requirements.
Definition storage.hpp:77
std::integral_constant< I, 0 > type
Definition matrix.hpp:19
typename default_alignment< T, I, Stride >::type default_alignment_t
Definition matrix.hpp:29
std::integral_constant< I, alignof(T) *Stride::value > type
Definition matrix.hpp:26
constexpr auto cols(const Matrix< T, I, S, D, O, A > &v)
Definition matrix.hpp:490
typename integral_value_type< T >::type integral_value_type_t
Definition layout.hpp:25
constexpr auto data(Matrix< T, I, S, D, O, A > &v)
Definition matrix.hpp:476
constexpr auto outer_stride(const Matrix< T, I, S, D, O, A > &v)
Definition matrix.hpp:494
constexpr auto rows(const Matrix< T, I, S, D, O, A > &v)
Definition matrix.hpp:486
constexpr auto depth(const Matrix< T, I, S, D, O, A > &v)
Definition matrix.hpp:498
Aligned allocation for matrix storage.
Class for a batch of matrices that owns its storage.
Definition matrix.hpp:52
auto top_right(index_type nr, index_type nc) const
Get a view of the top-right nr by nc block of the matrices.
Definition matrix.hpp:359
bool has_full_inner_stride() const
Whether the inner_stride() == 1. Always true.
Definition matrix.hpp:281
void add_to_diagonal(const value_type &t)
Definition matrix.hpp:419
Matrix(const Matrix &o)
Copy the values from another matrix.
Definition matrix.hpp:123
auto bottom_rows(index_type n) const
Get a view of the last n rows.
Definition matrix.hpp:309
auto bottom_left(index_type nr, index_type nc) const
Get a view of the bottom-left nr by nc block of the matrices.
Definition matrix.hpp:367
auto middle_layers(index_type l, N n)
Get a view of n layers starting at layer l.
Definition matrix.hpp:209
auto batch_dyn(index_type b)
Same as batch(), but returns a view with a dynamic batch size.
Definition matrix.hpp:171
void assign_from_view(View< U, J, R, E, M, O > other)
Definition matrix.hpp:100
View< S, index_t, simd_stride_t, index_t, DefaultStride, O > view_type
Definition matrix.hpp:54
auto first_layers(N n) const
Get a view of the first n layers. Note that n can be a compile-time constant.
Definition matrix.hpp:204
auto middle_batches(index_type b, index_type n, index_type stride=1)
Get a view of n batches starting at batch b, with a stride of stride layers.
Definition matrix.hpp:175
Matrix(plain_layout_type p)
Definition matrix.hpp:120
Matrix(plain_layout_type p, uninitialized_t init)
Definition matrix.hpp:121
auto middle_batches(index_type b, index_type n, index_type stride=1) const
Get a view of n batches starting at batch b, with a stride of stride layers.
Definition matrix.hpp:179
Matrix & operator=(Matrix &&o) noexcept
Cheap move assignment. No data is copied.
Definition matrix.hpp:128
auto reshaped(index_type rows, index_type cols) const
Reshape the view to the given dimensions. The total size should not change.
Definition matrix.hpp:295
auto middle_layers(index_type l, N n) const
Get a view of n layers starting at layer l.
Definition matrix.hpp:214
static auto allocate(layout_type layout)
Definition matrix.hpp:83
index_type padded_size() const
Total number of elements in the view (including all padding).
Definition matrix.hpp:245
void resize(layout_type new_layout)
Resize the matrix to a new layout, reallocating if the padded size changes.
Definition matrix.hpp:139
batch_size_type batch_size() const
The batch size, i.e. the number of layers in each batch. Equals the inner stride.
Definition matrix.hpp:283
value_type * data()
Get a pointer to the first element of the first layer.
Definition matrix.hpp:224
index_type size() const
Total number of elements in the view (excluding padding).
Definition matrix.hpp:243
Matrix & operator=(const Matrix &o)
Copy the values of another matrix, resizing if necessary.
Definition matrix.hpp:397
auto block(index_type r, index_type c, index_type nr, index_type nc)
Get a view of the nr by nc block of the matrices starting at row r and column c.
Definition matrix.hpp:379
auto top_rows(index_type n) const
Get a view of the first n rows.
Definition matrix.hpp:301
auto transposed() const
Get a transposed view of the matrices.
Definition matrix.hpp:389
index_type rows() const
Number of rows of the matrices.
Definition matrix.hpp:253
Matrix(layout_type layout)
Definition matrix.hpp:117
auto reshaped(index_type rows, index_type cols)
Reshape the view to the given dimensions. The total size should not change.
Definition matrix.hpp:291
void set_constant(value_type t)
Definition matrix.hpp:417
auto top_left(index_type nr, index_type nc) const
Get a view of the top-left nr by nc block of the matrices.
Definition matrix.hpp:353
auto bottom_rows(index_type n)
Get a view of the last n rows.
Definition matrix.hpp:307
auto left_cols(index_type n)
Get a view of the first n columns.
Definition matrix.hpp:303
constexpr auto inner_stride() const
The inner stride of the matrices.
Definition matrix.hpp:269
auto left_cols(index_type n) const
Get a view of the first n columns.
Definition matrix.hpp:305
auto middle_cols(index_type c, index_type n)
Get a view of n columns starting at column c.
Definition matrix.hpp:333
auto top_rows(index_type n)
Get a view of the first n rows.
Definition matrix.hpp:299
auto batch(index_type b)
Access a batch of batch_size() layers, starting at batch index b (i.e.
Definition matrix.hpp:167
auto middle_cols(index_type c, index_type n) const
Get a view of n columns starting at column c.
Definition matrix.hpp:335
auto right_cols(index_type n)
Get a view of the last n columns.
Definition matrix.hpp:311
auto bottom_right(index_type nr, index_type nc) const
Get a view of the bottom-right nr by nc block of the matrices.
Definition matrix.hpp:375
void copy_values(const Other &other)
Definition matrix.hpp:424
view_type view()
Returns the same view. For consistency with Matrix.
Definition matrix.hpp:441
auto top_right(index_type nr, index_type nc)
Get a view of the top-right nr by nc block of the matrices.
Definition matrix.hpp:357
auto end()
Sentinel for begin().
Definition matrix.hpp:232
auto first_layers(N n)
Get a view of the first n layers. Note that n can be a compile-time constant.
Definition matrix.hpp:199
auto bottom_left(index_type nr, index_type nc)
Get a view of the bottom-left nr by nc block of the matrices.
Definition matrix.hpp:363
bool has_full_outer_stride() const
Whether the outer_stride() == inner_stride() * inner_size().
Definition matrix.hpp:279
index_type layer_stride() const
The layer stride, i.e.
Definition matrix.hpp:275
bool has_full_layer_stride() const
Whether the layer_stride() == outer_stride() * outer_size().
Definition matrix.hpp:277
auto right_cols(index_type n) const
Get a view of the last n columns.
Definition matrix.hpp:313
Matrix(layout_type layout, uninitialized_t init)
Definition matrix.hpp:118
index_type inner_size() const
The size of the inner dimension, i.e.
Definition matrix.hpp:259
guanaqo::MatrixView< const T, I, standard_stride_type, O > operator()(index_type l) const
Access a single layer l as a non-batched view.
Definition matrix.hpp:194
static constexpr auto default_alignment(layout_type layout)
Definition matrix.hpp:75
auto top_left(index_type nr, index_type nc)
Get a view of the top-left nr by nc block of the matrices.
Definition matrix.hpp:351
auto batch_dyn(index_type b) const
Same as batch(), but returns a view with a dynamic batch size.
Definition matrix.hpp:173
auto middle_rows(index_type r, index_type n, index_type stride)
Get a view of n rows starting at row r.
Definition matrix.hpp:321
value_type & operator()(index_type l, index_type r, index_type c)
Access a single element at layer l, row r and column c.
Definition matrix.hpp:153
index_type outer_stride() const
Outer stride of the matrices (leading dimension in BLAS parlance).
Definition matrix.hpp:267
Matrix(Matrix &&o) noexcept
Definition matrix.hpp:126
const_view_type view() const
Returns the same view. For consistency with Matrix.
Definition matrix.hpp:443
index_type outer_size() const
The size of the outer dimension, i.e.
Definition matrix.hpp:257
auto batch(index_type b) const
Access a batch of batch_size() layers, starting at batch index b (i.e.
Definition matrix.hpp:169
constexpr auto row_stride() const
The row stride of the matrices, i.e.
Definition matrix.hpp:271
auto begin() const
Iterate linearly (in storage order) over all elements of the view.
Definition matrix.hpp:230
constexpr auto col_stride() const
The column stride of the matrices, i.e.
Definition matrix.hpp:273
auto end() const
Sentinel for begin().
Definition matrix.hpp:234
auto block(index_type r, index_type c, index_type nr, index_type nc) const
Get a view of the nr by nc block of the matrices starting at row r and column c.
Definition matrix.hpp:383
static auto allocate(layout_type layout, uninitialized_t init)
Definition matrix.hpp:87
const value_type & operator()(index_type l, index_type r, index_type c) const
Access a single element at layer l, row r and column c.
Definition matrix.hpp:157
index_type ceil_depth() const
The depth rounded up to a multiple of the batch size.
Definition matrix.hpp:249
auto middle_rows(index_type r, index_type n) const
Get a view of n rows starting at row r.
Definition matrix.hpp:317
auto begin()
Iterate linearly (in storage order) over all elements of the view.
Definition matrix.hpp:228
auto bottom_right(index_type nr, index_type nc)
Get a view of the bottom-right nr by nc block of the matrices.
Definition matrix.hpp:371
auto transposed()
Get a transposed view of the matrices.
Definition matrix.hpp:387
auto as_const() const
Explicit conversion to a const view.
Definition matrix.hpp:445
auto middle_cols(index_type c, index_type n, index_type stride) const
Get a view of n columns starting at column c.
Definition matrix.hpp:345
guanaqo::MatrixView< T, I, standard_stride_type, O > operator()(index_type l)
Access a single layer l as a non-batched view.
Definition matrix.hpp:189
auto middle_cols(index_type c, index_type n, index_type stride)
Get a view of n columns starting at column c.
Definition matrix.hpp:339
auto middle_rows(index_type r, index_type n, index_type stride) const
Get a view of n rows starting at row r.
Definition matrix.hpp:327
auto middle_rows(index_type r, index_type n)
Get a view of n rows starting at row r.
Definition matrix.hpp:315
index_type cols() const
Number of columns of the matrices.
Definition matrix.hpp:255
const value_type * data() const
Get a pointer to the first element of the first layer.
Definition matrix.hpp:226
depth_type depth() const
Number of layers in the view (i.e. depth).
Definition matrix.hpp:247
index_type num_batches() const
Number of batches in the view, i.e. ceil_depth() / batch_size().
Definition matrix.hpp:251
Non-owning view of a batch of matrices.
Definition view.hpp:33
Layout< I, S, D, L, O > layout_type
Definition view.hpp:34
static constexpr bool is_column_major
Definition view.hpp:43
View< T, I, S, N, L, O > first_layers(N n) const
Get a view of the first n layers. Note that n can be a compile-time constant.
Definition view.hpp:192
general_slice_view_type bottom_right(index_type nr, index_type nc) const
Get a view of the bottom-right nr by nc block of the matrices.
Definition view.hpp:519
constexpr bool has_full_outer_stride() const
Whether the outer_stride() == inner_stride() * inner_size().
Definition view.hpp:362
general_slice_view_type block(index_type r, index_type c, index_type nr, index_type nc) const
Get a view of the nr by nc block of the matrices starting at row r and column c.
Definition view.hpp:524
void set_constant(value_type t)
Definition view.hpp:565
auto transposed() const
Get a transposed view of the matrices.
Definition view.hpp:536
constexpr auto inner_stride() const
The inner stride of the matrices.
Definition view.hpp:338
constexpr index_type num_batches() const
Number of batches in the view, i.e. ceil_depth() / batch_size().
Definition view.hpp:308
col_slice_view_type middle_cols(index_type c, index_type n) const
Get a view of n columns starting at column c.
Definition view.hpp:480
constexpr index_type padded_size() const
Total number of elements in the view (including all padding).
Definition view.hpp:295
void add_to_diagonal(const value_type &t)
Definition view.hpp:552
linear_iterator begin() const
Iterate linearly (in storage order) over all elements of the view.
Definition view.hpp:258
row_slice_view_type bottom_rows(index_type n) const
Get a view of the last n rows.
Definition view.hpp:426
general_slice_view_type reshaped(index_type rows, index_type cols) const
Reshape the view to the given dimensions. The total size should not change.
Definition view.hpp:386
View< T, I, S, N, L, O > middle_layers(index_type l, N n) const
Get a view of n layers starting at layer l.
Definition view.hpp:207
static constexpr bool has_single_batch_at_compile_time
True if batch_size() and depth() are compile-time constants and are equal.
Definition view.hpp:49
constexpr auto col_stride() const
The column stride of the matrices, i.e.
Definition view.hpp:348
general_slice_view_type bottom_left(index_type nr, index_type nc) const
Get a view of the bottom-left nr by nc block of the matrices.
Definition view.hpp:514
View< const T, I, S, D, L, O > const_view_type
Definition view.hpp:41
row_slice_view_type middle_rows(index_type r, index_type n) const
Get a view of n rows starting at row r.
Definition view.hpp:456
constexpr batch_size_type batch_size() const
The batch size, i.e. the number of layers in each batch. Equals the inner stride.
Definition view.hpp:304
constexpr index_type cols() const
Number of columns of the matrices.
Definition view.hpp:314
std::default_sentinel_t end() const
Sentinel for begin().
Definition view.hpp:285
col_slice_view_type right_cols(index_type n) const
Get a view of the last n columns.
Definition view.hpp:441
View< T, I, S, I, L, O > batch_dyn(index_type b) const
Same as batch(), but returns a view with a dynamic batch size.
Definition view.hpp:149
constexpr bool has_full_inner_stride() const
Whether the inner_stride() == 1. Always true.
Definition view.hpp:366
col_slice_view_type left_cols(index_type n) const
Get a view of the first n columns.
Definition view.hpp:413
constexpr bool has_full_layer_stride() const
Whether the layer_stride() == outer_stride() * outer_size().
Definition view.hpp:358
static constexpr bool has_single_layer_at_compile_time
True if depth() is a compile-time constant and is equal to one.
Definition view.hpp:54
row_slice_view_type top_rows(index_type n) const
Get a view of the first n rows.
Definition view.hpp:400
constexpr index_type rows() const
Number of rows of the matrices.
Definition view.hpp:312
constexpr index_type inner_size() const
The size of the inner dimension, i.e.
Definition view.hpp:322
constexpr index_type ceil_depth() const
The depth rounded up to a multiple of the batch size.
Definition view.hpp:300
constexpr index_type size() const
Total number of elements in the view (excluding padding).
Definition view.hpp:293
constexpr auto row_stride() const
The row stride of the matrices, i.e.
Definition view.hpp:343
T * data() const
Get a pointer to the first element of the first layer.
Definition view.hpp:225
general_slice_view_type top_left(index_type nr, index_type nc) const
Get a view of the top-left nr by nc block of the matrices.
Definition view.hpp:504
constexpr index_type outer_size() const
The size of the outer dimension, i.e.
Definition view.hpp:317
constexpr index_type layer_stride() const
The layer stride, i.e.
Definition view.hpp:354
static constexpr bool is_row_major
Definition view.hpp:44
View< T, I, S, I, I, O > middle_batches(index_type b, index_type n, index_type stride=1) const
Get a view of n batches starting at batch b, with a stride of stride layers.
Definition view.hpp:163
general_slice_view_type top_right(index_type nr, index_type nc) const
Get a view of the top-right nr by nc block of the matrices.
Definition view.hpp:509
constexpr depth_type depth() const
Number of layers in the view (i.e. depth).
Definition view.hpp:298
void copy_values(const Other &other) const
Definition view.hpp:594
static constexpr StorageOrder storage_order
Definition view.hpp:42
constexpr index_type outer_stride() const
Outer stride of the matrices (leading dimension in BLAS parlance).
Definition view.hpp:333
batch_view_type batch(index_type b) const
Access a batch of batch_size() layers, starting at batch index b (i.e.
Definition view.hpp:137
Deleter for aligned memory allocated with operator new(size, align_val).
Definition storage.hpp:31
Non-owning view of a batch of matrices.