8#include <batmat/config.hpp>
10#include <guanaqo/mat-view.hpp>
31template <
class T,
class I = index_t,
class S = std::
integral_constant<I, 1>,
class D = I,
32 class L = DefaultStr
ide, StorageOrder O = StorageOrder::ColMajor>
63 std::conditional_t<has_single_batch_at_compile_time, View, View<T, I, S, D, I, O>>;
96 constexpr View(PlainBatchedMatrixView p = {})
100 .outer_stride = p.outer_stride,
101 .batch_size = p.batch_size,
102 .layer_stride = p.layer_stride}} {}
138 const auto layer = b *
static_cast<index_t
>(
batch_size());
139 return {{.data =
data() +
layout.layer_index(layer),
150 const auto d =
static_cast<I
>(
depth());
151 const auto layer = b *
static_cast<index_t
>(
batch_size());
153 return {{.data =
data() +
layout.layer_index(layer),
159 .layer_stride =
layout.layer_stride}};
166 const auto layer = b * bs;
168 return {{.data =
data() +
layout.layer_index(layer),
174 .layer_stride =
layout.get_layer_stride() * stride}};
194 return {{.data =
data(),
200 .layer_stride =
layout.layer_stride}};
210 return {{.data =
data() +
layout.layer_index(l),
216 .layer_stride =
layout.layer_stride}};
258 [[nodiscard]] linear_iterator
begin()
const {
264 const auto contig_layers =
layout.floor_depth();
268 const auto remaining_layers =
depth - contig_layers;
270 const auto first_jump = contig_layers *
size + remaining_layers;
272 const auto padded_end =
layout.ceil_depth();
273 const auto padding_layers = padded_end -
depth;
274 const auto end = padded_end *
size - padding_layers;
279 .next_jump = remaining_layers ?
data() + first_jump :
nullptr,
280 .padding_size =
batch_size - remaining_layers,
285 [[nodiscard]] std::default_sentinel_t
end()
const {
return {}; }
301 return layout.ceil_depth();
309 return layout.num_batches();
318 return layout.outer_size();
323 return layout.inner_size();
334 return layout.outer_stride;
338 [[nodiscard, gnu::always_inline]]
constexpr auto inner_stride()
const {
339 return layout.inner_stride;
343 [[nodiscard, gnu::always_inline]]
constexpr auto row_stride()
const {
344 return layout.row_stride();
348 [[nodiscard, gnu::always_inline]]
constexpr auto col_stride()
const {
349 return layout.col_stride();
355 return layout.get_layer_stride();
359 return layout.has_full_layer_stride();
363 return layout.has_full_outer_stride();
367 return layout.has_full_inner_stride();
375 if constexpr (std::is_same_v<typename V::layer_stride_type, layer_stride_type>)
376 return layout.layer_stride;
431 .data =
data() + offset,
446 .data =
data() + offset,
470 .data =
data() + offset,
494 .data =
data() + offset,
531 return {{.data = v.data(), .rows =
static_cast<index_type>(v.size()), .cols = 1}};
537 using TpBm =
View<T, I, S, D, L, transpose(O)>;
538 return TpBm{
typename TpBm::PlainBatchedMatrixView{.data =
data(),
544 .layer_stride =
layout.layer_stride}};
554 const auto n = std::min(
rows(),
cols());
556 auto *p =
batch(b).data();
568 auto *dst = this->
batch(b).data();
582 auto *dst = this->
batch(b).data();
593 template <
class Other>
596 assert(other.rows() == this->rows());
597 assert(other.cols() == this->cols());
598 assert(other.batch_size() == this->batch_size());
601 const auto *src = other.batch(b).data();
602 auto *dst = this->
batch(b).data();
604 const auto *src_ = src;
609 src += bs * other.outer_stride();
622 template <
class U,
class J,
class R,
class E,
class M>
623 requires(!std::is_const_v<T> && std::convertible_to<U, std::remove_cv_t<T>> &&
624 std::equality_comparable_with<I, J>)
630 template <
class U,
class J,
class R,
class E,
class M>
631 requires(!std::is_const_v<T> && std::convertible_to<U, std::remove_cv_t<T>> &&
632 std::equality_comparable_with<I, J>)
634 assert(other.
rows() == this->rows());
635 assert(other.
cols() == this->cols());
636 assert(other.
batch_size() == this->batch_size());
640 auto *dst = this->
batch(b).data();
642 const auto *src_ = src;
666 requires(!std::is_const_v<T>)
683 return {{.data =
data(),
689 .layer_stride =
layout.layer_stride}};
696 return {{.data =
data(),
702 .layer_stride =
layout.layer_stride}};
706 requires(!std::same_as<I, L>)
708 return {{.data =
data(),
718 requires(!std::is_const_v<T> && !std::same_as<I, L>)
720 return {{.data =
data(),
732template <
class T,
class I,
class S,
class D,
class L, StorageOrder P>
736template <
class T,
class I,
class S,
class D,
class L, StorageOrder P>
740template <
class T,
class I,
class S,
class D,
class L, StorageOrder P>
747template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
751template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
755template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
759template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
763template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
Layout description for a batch of matrices, independent of any storage.
constexpr auto cols(const Matrix< T, I, S, D, O, A > &v)
typename integral_value_type< T >::type integral_value_type_t
constexpr auto data(Matrix< T, I, S, D, O, A > &v)
constexpr auto outer_stride(const Matrix< T, I, S, D, O, A > &v)
bool operator!=(std::default_sentinel_t s, typename View< T, I, S, D, L, P >::linear_iterator i)
constexpr auto rows(const Matrix< T, I, S, D, O, A > &v)
bool operator==(std::default_sentinel_t s, typename View< T, I, S, D, L, P >::linear_iterator i)
constexpr auto depth(const Matrix< T, I, S, D, O, A > &v)
Shape and strides describing a batch of matrices, independent of any storage.
static constexpr bool is_column_major
static constexpr bool is_row_major
static constexpr StorageOrder storage_order
std::conditional_t< requires { S::value; }, std::integral_constant< index_t, S::value >, index_t > standard_stride_type
Iterator over all elements of a view.
bool operator==(std::default_sentinel_t) const
linear_iterator operator++(int)
reference operator*() const
linear_iterator & operator++()
Non-owning view of a batch of matrices.
constexpr View(PlainBatchedMatrixView p={})
Create a new view.
Layout< index_t, stride, stride, layer_stride, O > layout_type
static constexpr bool is_column_major
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.
typename layout_type::layer_stride_type layer_stride_type
guanaqo::MatrixView< T, I, standard_stride_type, O > operator()(index_type l) const
Access a single layer l as a non-batched view.
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.
typename layout_type::batch_size_type batch_size_type
typename layout_type::standard_stride_type standard_stride_type
View< T, index_t, stride, stride, DefaultStride, O > batch_view_type
constexpr bool has_full_outer_stride() const
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.
void set_constant(value_type t)
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.
auto transposed() const
Get a transposed view of the matrices.
constexpr auto inner_stride() const
The inner stride of the matrices.
constexpr index_type num_batches() const
Number of batches in the view, i.e. ceil_depth() / batch_size().
col_slice_view_type middle_cols(index_type c, index_type n, index_type stride) const
Get a view of n columns starting at column c, with stride stride.
constexpr View(value_type *data, layout_type layout)
Create a new view with the given layout, using the given buffer.
col_slice_view_type middle_cols(index_type c, index_type n) const
Get a view of n columns starting at column c.
constexpr index_type padded_size() const
Total number of elements in the view (including all padding).
void add_to_diagonal(const value_type &t)
linear_iterator begin() const
Iterate linearly (in storage order) over all elements of the view.
constexpr auto get_layer_stride_for() const
row_slice_view_type bottom_rows(index_type n) const
Get a view of the last n rows.
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.
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.
View(const View &)=default
Copy a view. No data is copied.
static constexpr bool has_single_batch_at_compile_time
constexpr auto col_stride() const
The column stride of the matrices, i.e.
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.
View< const T, index_t, stride, stride, layer_stride, O > const_view_type
row_slice_view_type middle_rows(index_type r, index_type n) const
Get a view of n rows starting at row r.
constexpr batch_size_type batch_size() const
constexpr index_type cols() const
Number of columns of the matrices.
std::default_sentinel_t end() const
col_slice_view_type right_cols(index_type n) const
Get a view of the last n columns.
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.
layer_stride_type layer_stride
constexpr bool has_full_inner_stride() const
Whether the inner_stride() == 1. Always true.
col_slice_view_type left_cols(index_type n) const
Get a view of the first n columns.
static View as_column(std::span< T > v)
Get a view of the given span as a column vector.
typename layout_type::index_type index_type
constexpr bool has_full_layer_stride() const
static constexpr bool has_single_layer_at_compile_time
row_slice_view_type top_rows(index_type n) const
Get a view of the first n rows.
constexpr index_type rows() const
Number of rows of the matrices.
constexpr index_type inner_size() const
The size of the inner dimension, i.e.
constexpr index_type ceil_depth() const
The depth rounded up to a multiple of the batch size.
constexpr index_type size() const
View & operator=(const View &other)
Copy assignment copies the values from another view with the same layout to this view.
std::conditional_t< has_single_batch_at_compile_time, View, View< T, index_t, stride, stride, index_t, O > > general_slice_view_type
constexpr auto row_stride() const
The row stride of the matrices, i.e.
std::conditional_t< is_column_major, View, general_slice_view_type > row_slice_view_type
row_slice_view_type middle_rows(index_type r, index_type n, index_type stride) const
Get a view of n rows starting at row r, with stride stride.
constexpr View(std::span< T > data, layout_type layout)
Create a new view with the given layout, using the given buffer.
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.
batch_size_type batch_size
constexpr index_type outer_size() const
The size of the outer dimension, i.e.
View view() const
Returns the same view. For consistency with Matrix.
constexpr index_type layer_stride() const
The layer stride, i.e.
static constexpr bool is_row_major
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.
const_view_type as_const() const
Explicit conversion to a const view.
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.
constexpr depth_type depth() const
std::conditional_t< is_row_major, View, general_slice_view_type > col_slice_view_type
typename layout_type::depth_type depth_type
void copy_values(const Other &other) const
View & reassign(View other)
Reassign the buffer and layout of this view to those of another view. No data is copied.
static constexpr StorageOrder storage_order
constexpr index_type outer_stride() const
Outer stride of the matrices (leading dimension in BLAS parlance).
batch_view_type batch(index_type b) const
Access a batch of batch_size() layers, starting at batch index b (i.e.
POD helper struct to enable designated initializers during construction.