8#include <batmat/config.hpp>
10#include <guanaqo/mat-view.hpp>
30template <
class T,
class I = index_t,
class S = std::
integral_constant<I, 1>,
class D = I,
31 class L = DefaultStr
ide, StorageOrder O = StorageOrder::ColMajor>
62 std::conditional_t<has_single_batch_at_compile_time, View, View<T, I, S, D, I, O>>;
92 constexpr View(PlainBatchedMatrixView p = {})
96 .outer_stride = p.outer_stride,
97 .batch_size = p.batch_size,
98 .layer_stride = p.layer_stride}} {}
111 requires(!std::is_const_v<T>)
144 const auto layer = b *
static_cast<index_t
>(
batch_size());
145 return {{.data =
data() +
layout.layer_index(layer),
156 const auto d =
static_cast<I
>(
depth());
157 const auto layer = b *
static_cast<index_t
>(
batch_size());
159 return {{.data =
data() +
layout.layer_index(layer),
165 .layer_stride =
layout.layer_stride}};
172 return {{.data =
data(),
178 .layer_stride =
layout.layer_stride}};
188 return {{.data =
data() +
layout.layer_index(l),
194 .layer_stride =
layout.layer_stride}};
236 [[nodiscard]] linear_iterator
begin()
const {
242 const auto contig_layers =
layout.floor_depth();
246 const auto remaining_layers =
depth - contig_layers;
248 const auto first_jump = contig_layers *
size + remaining_layers;
250 const auto padded_end =
layout.ceil_depth();
251 const auto padding_layers = padded_end -
depth;
252 const auto end = padded_end *
size - padding_layers;
257 .next_jump = remaining_layers ?
data() + first_jump :
nullptr,
258 .padding_size =
batch_size - remaining_layers,
263 [[nodiscard]] std::default_sentinel_t
end()
const {
return {}; }
277 return layout.ceil_depth();
281 return layout.num_batches();
290 return layout.outer_stride;
295 return layout.outer_size();
300 return layout.inner_size();
313 return layout.get_layer_stride();
317 return layout.has_full_layer_stride();
321 return layout.has_full_outer_stride();
325 return layout.has_full_inner_stride();
333 if constexpr (std::is_same_v<typename V::layer_stride_type, layer_stride_type>)
334 return layout.layer_stride;
389 .data =
data() + offset,
404 .data =
data() + offset,
451 return {{.data = v.data(), .rows =
static_cast<index_type>(v.size()), .cols = 1}};
457 using TpBm =
View<T, I, S, D, L, transpose(O)>;
458 return TpBm{
typename TpBm::PlainBatchedMatrixView{.data =
data(),
464 .layer_stride =
layout.layer_stride}};
474 const auto n = std::min(
rows(),
cols());
476 auto *p =
batch(b).data();
488 auto *dst = this->
batch(b).data();
502 auto *dst = this->
batch(b).data();
513 template <
class Other>
516 assert(other.rows() == this->rows());
517 assert(other.cols() == this->cols());
518 assert(other.batch_size() == this->batch_size());
521 const auto *src = other.batch(b).data();
522 auto *dst = this->
batch(b).data();
524 const auto *src_ = src;
529 src += bs * other.outer_stride();
542 template <
class U,
class J,
class R,
class E,
class M>
543 requires(!std::is_const_v<T> && std::convertible_to<U, std::remove_cv_t<T>> &&
544 std::equality_comparable_with<I, J>)
550 template <
class U,
class J,
class R,
class E,
class M>
551 requires(!std::is_const_v<T> && std::convertible_to<U, std::remove_cv_t<T>> &&
552 std::equality_comparable_with<I, J>)
554 assert(other.
rows() == this->rows());
555 assert(other.
cols() == this->cols());
556 assert(other.
batch_size() == this->batch_size());
560 auto *dst = this->
batch(b).data();
562 const auto *src_ = src;
588 return {{.data =
data(),
594 .layer_stride =
layout.layer_stride}};
601 return {{.data =
data(),
607 .layer_stride =
layout.layer_stride}};
611 requires(!std::same_as<I, L>)
613 return {{.data =
data(),
623 requires(!std::is_const_v<T> && !std::same_as<I, L>)
625 return {{.data =
data(),
635template <
class T,
class I,
class S,
class D,
class L, StorageOrder P>
639template <
class T,
class I,
class S,
class D,
class L, StorageOrder P>
643template <
class T,
class I,
class S,
class D,
class L, StorageOrder P>
650template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
654template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
658template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
662template <
class T,
class I,
class S,
class D,
class L, StorageOrder O>
666template <
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< S, index_t, standard_stride_type, O > operator()(index_type l) const
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 index_type num_batches() const
Number of batches in the view, i.e. ceil_depth() / batch_size().
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
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 inner_stride() const
The inner stride of the matrices.
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
std::conditional_t< is_column_major, View, general_slice_view_type > row_slice_view_type
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.
constexpr index_type layer_stride() const
The layer stride, i.e.
static constexpr bool is_row_major
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.