batchlas::Matrix< T, MType > Class Template Reference

Overview

template<typename T, MatrixFormat MType>
class batchlas::Matrix< T, MType >

Owning batch of batch_size() matrices of equal (capacity) shape.

Dense (the default): column-major, element (i, j) of item b at data()[b * stride() + j * ld() + i], with ld() >= rows() and stride() >= ld() * cols(). CSR: per item, rows() + 1 row offsets (items offset_stride() apart) and nnz_capacity() values and column indices (items matrix_stride() apart), zero-based.

Storage is USM shared memory (UnifiedVector) on Device::default_device(), so it is host-accessible and device-accessible. A Matrix is a MatrixView of its own storage, so it is accepted wherever a MatrixView is and has every view member. Copy construction and copy assignment deep-copy the data; moves transfer it. The shape members are public but are not to be written.

A dense batch may be heterogeneous: set_active_dims() attaches per-item active extents no larger than the allocated rows() x cols().

Template Parameters
Tscalar type (float, double, std::complex<float>, std::complex<double>)
MTypeMatrixFormat::Dense (default) or MatrixFormat::CSR
See also
Matrix model, and the user guide's data-layout section in docs/cpp-api.md

Definition at line 1399 of file matrix.hh.

#include <batchlas/blas/matrix.hh>

Inheritance diagram for batchlas::Matrix< T, MType >:

Constructors

◆ Matrix() [1/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( int rows,
int cols,
int batch_size = 1,
int ld = 0,
int stride = 0 )

Allocates an uninitialised dense batch.

Parameters
rowsrows per item
colscolumns per item
batch_sizenumber of items
ldleading dimension; 0 means rows
stridebatch stride in elements; 0 means (resolved ld) * cols
Exceptions
batchlas::invalid_argumenton a negative argument, a resolved ld < rows, or a resolved stride < ld * cols
Note
Exactly stride * batch_size elements are allocated; nothing pads.

◆ Matrix() [2/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires CsrMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( int rows,
int cols,
NonZeros nnz,
int batch_size = 1 )

Allocates an uninitialised CSR batch with room for nnz entries per item.

Parameters
rowsrows per item
colscolumns per item
nnzper-item capacity; matrix_stride() == nnz, offset_stride() == rows + 1
batch_sizenumber of items

◆ Matrix() [3/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires CsrMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( int rows,
int cols,
int nnz,
int batch_size = 1 )
delete

Deleted: spell the count NonZeros{nnz}; for a dense Matrix the third int is the batch size.

◆ Matrix() [4/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( const T * data,
int rows,
int cols,
int ld,
int stride = 0,
int batch_size = 1 )

Dense batch copied from existing host-readable memory.

(ld, stride) describe the source: element (i, j, b) is read from data[b * stride + j * ld + i]. The copy keeps the source's leading dimension but packs the items back to back (stride() == ld() * cols()); padding rows in the copy are zeroed and padding in the source is never read.

Parameters
datasource; must hold (batch_size-1)*stride + (cols-1)*ld + rows elements
rowsrows per item
colscolumns per item
ldsource leading dimension; 0 means rows
stridesource batch stride; 0 means (resolved ld) * cols
batch_sizenumber of items
Exceptions
batchlas::invalid_argumenton null data, rows or cols <= 0, batch_size <= 0, a resolved ld < rows, or (batch_size > 1) a stride < ld * cols

◆ Matrix() [5/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( Span< const T > data,
int rows,
int cols,
int ld,
int stride = 0,
int batch_size = 1 )

Dense batch copied from a Span; as the pointer overload, plus a length check.

Exceptions
batchlas::invalid_argumentalso when data is shorter than the layout needs

◆ Matrix() [6/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( Span< T > data,
int rows,
int cols,
int ld,
int stride = 0,
int batch_size = 1 )
inline

Mutable-Span (and UnifiedVector) spelling of the Span<const T> overload.

Definition at line 1461 of file matrix.hh.

◆ Matrix() [7/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires CsrMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( const T * values,
const int * row_offsets,
const int * col_indices,
int rows,
int cols,
NonZeros nnz,
int matrix_stride = 0,
int offset_stride = 0,
int batch_size = 1 )

CSR batch copied from existing host-readable arrays.

Parameters
valuesmatrix_stride * batch_size values
row_offsetsoffset_stride * batch_size zero-based offsets
col_indicesmatrix_stride * batch_size column indices
rowsrows per item
colscolumns per item
nnzdeclared per-item non-zero count
matrix_stridedistance between items' value arrays; 0 means nnz. May exceed it.
offset_stridedistance between items' offset arrays; 0 means rows + 1
batch_sizenumber of items

◆ Matrix() [8/10]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires CsrMatrixFormat<M>
batchlas::Matrix< T, MType >::Matrix ( const T * values,
const int * row_offsets,
const int * col_indices,
int nnz,
int rows,
int cols,
int matrix_stride = 0,
int offset_stride = 0,
int batch_size = 1 )
delete

Deleted so the pre-tag order (nnz before rows, cols) is a compile error.

◆ Matrix() [9/10]

template<typename T, MatrixFormat MType>
batchlas::Matrix< T, MType >::Matrix ( const Matrix< T, MType > & other)
inline

Definition at line 1606 of file matrix.hh.

◆ Matrix() [10/10]

template<typename T, MatrixFormat MType>
batchlas::Matrix< T, MType >::Matrix ( Matrix< T, MType > && other)
inlinenoexcept

Definition at line 1614 of file matrix.hh.

Functions

◆ Identity()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::Identity ( int n,
int batch_size = 1 )
static

A batch of n x n identity matrices.

◆ Random()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::Random ( int rows,
int cols,
bool hermitian = false,
int batch_size = 1,
unsigned int seed = 42 )
static

Entries uniform in [-1, 1] (real and imaginary parts independently).

Deterministic: the same seed and shape give the same matrix. With hermitian, the lower triangle is overwritten with the conjugate of the upper and the diagonal made real.

Exceptions
batchlas::invalid_argumentif hermitian and rows != cols

◆ RandomSparseHermitian()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires CsrMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::RandomSparseHermitian ( int n,
float density,
int batch_size = 1,
unsigned int seed = 42,
typename base_type< T >::type diagonal_boost = typename base_type< T >::type(1),
bool shared_pattern = true )
static

Random diagonally dominant sparse Hermitian batch; see csr_generators::random_sparse_hermitian_csr().

◆ RandomTriangular()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::RandomTriangular ( int n,
Uplo uplo,
Diag diag = Diag::NonUnit,
int batch_size = 1,
unsigned int seed = 42 )
inlinestatic

Random n x n triangular batch; see MatrixView::fill_triangular_random().

Definition at line 1521 of file matrix.hh.

◆ Zeros()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::Zeros ( int rows,
int cols,
int batch_size = 1 )
static

A batch of zero matrices.

◆ Ones()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::Ones ( int rows,
int cols,
int batch_size = 1 )
static

A batch of all-ones matrices.

◆ Diagonal()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::Diagonal ( const Span< T > & diag_values,
int batch_size = 1 )
static

A batch of n x n diagonal matrices, n = diag_values.size(), each with diag_values on the diagonal.

◆ Triangular()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::Triangular ( int n,
Uplo uplo,
T diagonal_value = T(1),
T non_diagonal_value = T(0.5),
int batch_size = 1 )
static

n x n matrices with diagonal_value on the diagonal, non_diagonal_value in the uplo strict triangle and zero elsewhere.

◆ TriDiagToeplitz()

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::TriDiagToeplitz ( int n,
T diag = T(1),
T sub_diag = T(-0.5),
T super_diag = T(0.5),
int batch_size = 1 )
static

n x n tridiagonal Toeplitz matrices (constant diagonal, sub- and super-diagonal).

◆ to_column_major() [1/2]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::to_column_major ( const Queue & ctx,
int row_pitch = 0 ) const

Reinterprets this matrix's storage as row-major data and returns it column-major.

The row pitch is a parameter, never inferred. The result is packed and owns the memory the kernel writes, so the call waits on ctx before returning.

Parameters
ctxqueue for the conversion kernel
row_pitchsource row pitch in elements; 0 means packed (cols()), accepted only when this matrix is itself packed
Returns
a packed column-major copy
Exceptions
std::invalid_argumentwhen the pitch is below cols(), runs past the allocation, straddles the next item, or is 0 on a non-packed matrix
See also
docs/cpp-api.md, section "Row-major source data"

◆ to_column_major() [2/2]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::to_column_major ( int row_pitch = 0) const

to_column_major() on an internal queue.

◆ convert_to()

template<typename T, MatrixFormat MType>
template<MatrixFormat NewMType>
Matrix< T, NewMType > batchlas::Matrix< T, MType >::convert_to ( const float_t< T > & zero_threshold = 1e-7) const

Copy in another storage format, on the default queue.

Dense to CSR drops entries at or below zero_threshold in magnitude (for complex, both parts) and sizes every item by the batch's largest non-zero count (see nnz()). CSR to Dense is supported; converting to the same format returns a copy.

Template Parameters
NewMTypetarget format
Parameters
zero_thresholdmagnitude at or below which a dense entry is not stored
Exceptions
batchlas::unsupportedfor any other pair of formats

◆ to_row_major() [1/2]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::to_row_major ( const Queue & ctx) const

Packed row-major copy (row pitch cols(), batch stride rows() * cols()).

Exactly what to_column_major() reads with its default pitch, so the two round-trip. Waits on ctx before returning.

◆ to_row_major() [2/2]

template<typename T, MatrixFormat MType>
template<typename U = T, MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > batchlas::Matrix< T, MType >::to_row_major ( ) const

to_row_major() on an internal queue.

◆ operator=() [1/2]

template<typename T, MatrixFormat MType>
Matrix & batchlas::Matrix< T, MType >::operator= ( const Matrix< T, MType > & other)
inline

Definition at line 1615 of file matrix.hh.

◆ operator=() [2/2]

template<typename T, MatrixFormat MType>
Matrix & batchlas::Matrix< T, MType >::operator= ( Matrix< T, MType > && other)
inlinenoexcept

Definition at line 1616 of file matrix.hh.

◆ clone()

template<typename T, MatrixFormat MType>
Matrix< T, MType > batchlas::Matrix< T, MType >::clone ( ) const
inline

Deep copy in this matrix's own layout (ld, stride, CSR strides, active extents).

Definition at line 1631 of file matrix.hh.

◆ view() [1/2]

template<typename T, MatrixFormat MType>
MatrixView< T, MType > batchlas::Matrix< T, MType >::view ( ) const
inline

This matrix as a plain MatrixView (a sliced copy); shares the backend handle.

Definition at line 1659 of file matrix.hh.

◆ view() [2/2]

template<typename T, MatrixFormat MType>
MatrixView< T, MType > batchlas::Matrix< T, MType >::view ( int rows,
int cols,
int ld = -1,
int stride = -1 ) const

A dense view of the leading rows x cols block of every item.

Parameters
ldleading dimension of the view; <= 0 keeps ld()
stridebatch stride of the view; <= 0 keeps stride()
Exceptions
batchlas::unsupportedon a CSR matrix

◆ astype() [1/2]

template<typename T, MatrixFormat MType>
template<typename U>
Matrix< U, MType > batchlas::Matrix< T, MType >::astype ( ) const

Dense copy with every element converted by static_cast<U>, in this layout.

Definition at line 2070 of file matrix.hh.

◆ fill() [1/3]

template<typename T, MatrixFormat MType>
void batchlas::Matrix< T, MType >::fill ( T value)
inline

Sets every element of every item to value; waits.

Definition at line 1672 of file matrix.hh.

◆ operator()() [1/6]

template<typename T, MatrixFormat MType>
template<MatrixFormat M = MType>
requires DenseMatrixFormat<M>
T & batchlas::Matrix< T, MType >::operator() ( int row,
int col,
int batch )
inline

Host reference to element (row, col) of item batch anywhere in the allocated rows() x cols(), active extents ignored (MatrixView's checks them).

No bounds check beyond UnifiedVector's debug assert. The data is USM shared, so wait for any kernel writing it first.

Definition at line 1682 of file matrix.hh.

◆ operator()() [2/6]

template<typename T, MatrixFormat MType>
template<MatrixFormat M = MType>
requires DenseMatrixFormat<M>
const T & batchlas::Matrix< T, MType >::operator() ( int row,
int col,
int batch ) const
inline

Host const reference to element (row, col) of item batch.

Definition at line 1690 of file matrix.hh.

◆ copy_from()

template<typename T, MatrixFormat MType>
void batchlas::Matrix< T, MType >::copy_from ( const MatrixView< T, MType > & src)

Host copy of src into this matrix's own layout.

Precondition
For CSR, src has exactly this matrix's strides and capacity.
Exceptions
batchlas::invalid_argumentif rows, cols or batch size differ

◆ set_active_dims()

template<typename T, MatrixFormat MType>
template<MatrixFormat M = MType>
requires DenseMatrixFormat<M>
Matrix< T, MType > & batchlas::Matrix< T, MType >::set_active_dims ( Span< int > active_rows,
Span< int > active_cols )
inline

Attaches per-item active extents (copied), or clears them when both spans are empty.

Item b then occupies the leading active_rows[b] x active_cols[b] block of its allocated rows() x cols(); layout (ld, stride) is unchanged.

Returns
*this
Exceptions
batchlas::invalid_argumentunless both spans hold batch_size() entries, each in [0, rows()] and [0, cols()] respectively

Definition at line 1709 of file matrix.hh.

◆ astype() [2/2]

template<typename T, MatrixFormat MType>
template<typename U>
Matrix< U, MType > batchlas::MatrixView< T, MType >::astype ( std::span< const int > items) const

astype() of items only, packed in that order (item g at g * rows() * cols()).

Exceptions
batchlas::out_of_rangefor an item outside [0, batch_size())
batchlas::invalid_argumentfor a heterogeneous view

Definition at line 698 of file matrix.hh.

◆ fill() [2/3]

template<typename T, MatrixFormat MType>
Event batchlas::MatrixView< T, MType >::fill ( const Queue & ctx,
T value ) const

Sets every element of every item (dense: within rows() x cols(); CSR: every stored value).

◆ fill() [3/3]

template<typename T, MatrixFormat MType>
Event batchlas::MatrixView< T, MType >::fill ( T value) const
inline

fill() on a new default queue.

Definition at line 1059 of file matrix.hh.

◆ operator()() [3/6]

template<typename T, MatrixFormat MType>
template<MatrixFormat M = MType>
requires DenseMatrixFormat<M>
MatrixView< T, MType > batchlas::MatrixView< T, MType >::operator() ( Slice rows,
Slice cols ) const
inline

View of the sub-block rows x cols of every item; keeps ld and stride, never copies.

Exceptions
batchlas::invalid_argumentif either slice is empty; batchlas::out_of_range past a dimension

Definition at line 911 of file matrix.hh.

◆ operator()() [4/6]

template<typename T, MatrixFormat MType>
template<MatrixFormat M = MType>
requires DenseMatrixFormat<M>
MatrixView< T, MType > batchlas::MatrixView< T, MType >::operator() ( Slice rows) const
inline

View of rows rows over all columns of every item.

Definition at line 927 of file matrix.hh.

◆ operator()() [5/6]

template<typename T, MatrixFormat MType>
template<MatrixFormat M = MType>
requires DenseMatrixFormat<M>
VectorView< T > batchlas::MatrixView< T, MType >::operator() ( int32_t row,
Slice cols ) const
inline

Part of row row of every item as a batched VectorView (inc = ld(), stride = stride()).

Exceptions
batchlas::invalid_argumentif the slice is empty; batchlas::out_of_range past a dimension

Definition at line 935 of file matrix.hh.

◆ operator()() [6/6]

template<typename T, MatrixFormat MType>
template<MatrixFormat M = MType>
requires DenseMatrixFormat<M>
VectorView< T > batchlas::MatrixView< T, MType >::operator() ( Slice rows,
int32_t col ) const
inline

Part of column col of every item as a batched VectorView (inc = 1, stride = stride()).

Exceptions
batchlas::invalid_argumentif the slice is empty; batchlas::out_of_range past a dimension

Definition at line 948 of file matrix.hh.

Variables

◆ rows_

template<typename T, MatrixFormat MType>
int batchlas::MatrixView< T, MType >::rows_

Definition at line 573 of file matrix.hh.

◆ cols_

template<typename T, MatrixFormat MType>
int batchlas::MatrixView< T, MType >::cols_

Definition at line 573 of file matrix.hh.

◆ batch_size_

template<typename T, MatrixFormat MType>
int batchlas::MatrixView< T, MType >::batch_size_

Definition at line 573 of file matrix.hh.