//! Compact BLAS — batched dense kernels packed for SIMD.
//!
//! When you need to apply the same small dense kernel (a 4×4 GEMM, a
//! 8×8 LU, etc.) to many independent matrices simultaneously, the
//! Compact BLAS API achieves better throughput than calling the
//! standard BLAS in a loop or even using the pointer-array batched
//! API. It packs `nm` matrices into a single buffer with SIMD-width
//! interleaving so each underlying instruction processes a
//! `pack-width`-sized batch in lockstep.
//!
//! Three pack widths are supported:
//!
//! | [`CompactFormat`] | SIMD width | Best on |
//! | --- | --- | --- |
//! | `Sse` | 4 (f32) / 2 (f64) | older Intel cores |
//! | `Avx` | 8 / 4 | most CPUs since Haswell |
//! | `Avx512` | 16 / 8 | Skylake-X, Sapphire Rapids, … |
//!
//! Typical flow:
//!
//! 1. Compute the packed-buffer size with [`get_size`].
//! 2. Pack a slice of `*const T` pointers into the buffer with
//! [`gepack`].
//! 3. Run the desired compact kernel ([`gemm`], …) on the packed
//! buffers.
//! 4. Unpack with [`geunpack`] when results are needed back as
//! regular matrices.
//!
//! Compact BLAS is real-only for the full lifecycle (`f32` / `f64`).
//! Complex GEMM exists but lacks a matching pack / unpack pair, so
//! the Rust wrapper restricts this module to real types.
use core::ffi::c_int;
use onemkl_sys as sys;
use crate::enums::{Diag, Layout, Side, Transpose, UpLo};
use crate::error::{Error, Result};
/// SIMD pack width to use when laying out compact matrices.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CompactFormat {
/// 128-bit SSE pack (4 f32 / 2 f64 lanes).
Sse,
/// 256-bit AVX pack (8 f32 / 4 f64 lanes).
Avx,
/// 512-bit AVX-512 pack (16 f32 / 8 f64 lanes).
Avx512,
}
impl CompactFormat {
#[inline]
fn as_mkl(self) -> sys::MKL_COMPACT_PACK::Type {
match self {
Self::Sse => sys::MKL_COMPACT_PACK::MKL_COMPACT_SSE,
Self::Avx => sys::MKL_COMPACT_PACK::MKL_COMPACT_AVX,
Self::Avx512 => sys::MKL_COMPACT_PACK::MKL_COMPACT_AVX512,
}
}
}
#[inline]
fn map_layout(layout: Layout) -> sys::MKL_LAYOUT::Type {
match layout {
Layout::RowMajor => sys::MKL_LAYOUT::MKL_ROW_MAJOR,
Layout::ColMajor => sys::MKL_LAYOUT::MKL_COL_MAJOR,
}
}
#[inline]
fn map_transpose(t: Transpose) -> sys::MKL_TRANSPOSE::Type {
match t {
Transpose::NoTrans => sys::MKL_TRANSPOSE::MKL_NOTRANS,
Transpose::Trans => sys::MKL_TRANSPOSE::MKL_TRANS,
Transpose::ConjTrans => sys::MKL_TRANSPOSE::MKL_CONJTRANS,
}
}
/// Real scalar types supported by the Compact BLAS lifecycle.
#[allow(missing_docs)]
pub trait CompactScalar: Copy + 'static {
/// Raw `get_size_compact` call.
///
/// # Safety
///
/// `ld` is the leading dimension, `sd` the second dimension of one matrix, `format` a `MKL_COMPACT_*` constant, `nm >= 0` the number of matrices; there are no pointers. Returns the buffer size in **bytes** MKL needs for the matching compact buffer (**verified**: `mkl_dget_size_compact(100, 100, MKL_COMPACT_AVX, 10)` returned 960000 = 3 groups x 4 lanes x 8 bytes x 100 x 100, i.e. `nm` rounded up to whole SIMD groups; and 1600000 for `nm = 20`; oneMKL 2026.2, Linux x86_64). Callers must check it is non-negative and fits `usize`.
/// The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
unsafe fn get_size_compact(
ld: c_int,
sd: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) -> c_int;
/// Raw `gepack_compact` call.
///
/// # Safety
///
/// `a` must be valid for reads of `nm` matrix pointers; each pointer must be non-null and address a `rows x cols` matrix with leading dimension `lda` per `layout` (`lda >= rows` column-major, `>= cols` row-major), readable for `lda * cols` (column-major) or `lda * rows` (row-major) elements.
/// `ap` must be valid for writes of a compact buffer for those `nm` matrices: A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed.
/// `ap` must not overlap any matrix addressed through `a`. The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn gepack_compact(
layout: sys::MKL_LAYOUT::Type,
rows: c_int,
cols: c_int,
a: *const *const Self,
lda: c_int,
ap: *mut Self,
ldap: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
/// Raw `geunpack_compact` call.
///
/// # Safety
///
/// `ap` must be valid for reads of a compact buffer that was packed with the same `rows`, `cols`, `format` and `nm`: A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed.
/// `a` must be valid for reads of `nm` matrix pointers; each pointer must be non-null and address distinct writable storage for a `rows x cols` matrix with leading dimension `lda` per `layout`. No Rust reference to that storage may be live, and none may overlap `ap`. The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn geunpack_compact(
layout: sys::MKL_LAYOUT::Type,
rows: c_int,
cols: c_int,
a: *const *mut Self,
lda: c_int,
ap: *const Self,
ldap: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
/// Raw `gemm_compact` call.
///
/// # Safety
///
/// `a`, `b` and `c` are compact buffers (`ap`-style) for `nm` matrices in the same `format`; each must be valid per: A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed. with the per-matrix shapes `op(A): m x k`, `op(B): k x n`, `C: m x n` and leading dimensions `ldap`, `ldbp`, `ldcp`.
/// `c` is read (when `beta != 0`) and written, and must not overlap `a` or `b`. `m`, `n`, `k` must be non-negative. The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn gemm_compact(
layout: sys::MKL_LAYOUT::Type,
transa: sys::MKL_TRANSPOSE::Type,
transb: sys::MKL_TRANSPOSE::Type,
m: c_int,
n: c_int,
k: c_int,
alpha: Self,
a: *const Self,
ldap: c_int,
b: *const Self,
ldbp: c_int,
beta: Self,
c: *mut Self,
ldcp: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
}
impl CompactScalar for f32 {
unsafe fn get_size_compact(
ld: c_int,
sd: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) -> c_int {
unsafe { sys::mkl_sget_size_compact(ld, sd, format, nm) }
}
unsafe fn gepack_compact(
layout: sys::MKL_LAYOUT::Type,
rows: c_int,
cols: c_int,
a: *const *const Self,
lda: c_int,
ap: *mut Self,
ldap: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::mkl_sgepack_compact(layout, rows, cols, a, lda, ap, ldap, format, nm) }
}
unsafe fn geunpack_compact(
layout: sys::MKL_LAYOUT::Type,
rows: c_int,
cols: c_int,
a: *const *mut Self,
lda: c_int,
ap: *const Self,
ldap: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::mkl_sgeunpack_compact(layout, rows, cols, a, lda, ap, ldap, format, nm) }
}
unsafe fn gemm_compact(
layout: sys::MKL_LAYOUT::Type,
transa: sys::MKL_TRANSPOSE::Type,
transb: sys::MKL_TRANSPOSE::Type,
m: c_int,
n: c_int,
k: c_int,
alpha: Self,
a: *const Self,
ldap: c_int,
b: *const Self,
ldbp: c_int,
beta: Self,
c: *mut Self,
ldcp: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe {
sys::mkl_sgemm_compact(
layout, transa, transb, m, n, k, alpha, a, ldap, b, ldbp, beta, c, ldcp, format, nm,
)
}
}
}
impl CompactScalar for f64 {
unsafe fn get_size_compact(
ld: c_int,
sd: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) -> c_int {
unsafe { sys::mkl_dget_size_compact(ld, sd, format, nm) }
}
unsafe fn gepack_compact(
layout: sys::MKL_LAYOUT::Type,
rows: c_int,
cols: c_int,
a: *const *const Self,
lda: c_int,
ap: *mut Self,
ldap: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::mkl_dgepack_compact(layout, rows, cols, a, lda, ap, ldap, format, nm) }
}
unsafe fn geunpack_compact(
layout: sys::MKL_LAYOUT::Type,
rows: c_int,
cols: c_int,
a: *const *mut Self,
lda: c_int,
ap: *const Self,
ldap: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::mkl_dgeunpack_compact(layout, rows, cols, a, lda, ap, ldap, format, nm) }
}
unsafe fn gemm_compact(
layout: sys::MKL_LAYOUT::Type,
transa: sys::MKL_TRANSPOSE::Type,
transb: sys::MKL_TRANSPOSE::Type,
m: c_int,
n: c_int,
k: c_int,
alpha: Self,
a: *const Self,
ldap: c_int,
b: *const Self,
ldbp: c_int,
beta: Self,
c: *mut Self,
ldcp: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe {
sys::mkl_dgemm_compact(
layout, transa, transb, m, n, k, alpha, a, ldap, b, ldbp, beta, c, ldcp, format, nm,
)
}
}
}
/// Number of scalars needed to hold `num_matrices` compact-packed
/// matrices of shape `ld × sd` (leading dimension × short dimension)
/// in the given format. Wraps `mkl_*get_size_compact`.
pub fn get_size<T: CompactScalar>(
ld: usize,
sd: usize,
format: CompactFormat,
num_matrices: usize,
) -> Result<usize> {
let ld_i: c_int = ld.try_into().map_err(|_| Error::DimensionOverflow)?;
let sd_i: c_int = sd.try_into().map_err(|_| Error::DimensionOverflow)?;
let nm_i: c_int = num_matrices
.try_into()
.map_err(|_| Error::DimensionOverflow)?;
let bytes = unsafe { T::get_size_compact(ld_i, sd_i, format.as_mkl(), nm_i) };
if bytes < 0 {
return Err(Error::LapackComputationFailure { info: bytes });
}
let elem_size = core::mem::size_of::<T>();
if elem_size == 0 {
return Err(Error::InvalidArgument("zero-sized scalar"));
}
Ok((bytes as usize).div_ceil(elem_size))
}
/// Pack `num_matrices` regular matrices (one per pointer in `a`)
/// into the compact buffer `ap`. Wraps `mkl_*gepack_compact`.
///
/// `ldap` is the leading dimension of the compact buffer (use the
/// value returned by [`get_size`] / `num_matrices` arithmetic, or
/// the value MKL's documentation specifies for the chosen format).
///
/// # Safety
///
/// Each pointer in `a` must point to a buffer of at least the
/// matrix's storage size and remain valid for the duration of this
/// call.
#[allow(clippy::too_many_arguments)]
pub unsafe fn gepack<T: CompactScalar>(
layout: Layout,
rows: usize,
cols: usize,
a: &[*const T],
lda: usize,
ap: &mut [T],
ldap: usize,
format: CompactFormat,
) -> Result<()> {
let rows_i: c_int = rows.try_into().map_err(|_| Error::DimensionOverflow)?;
let cols_i: c_int = cols.try_into().map_err(|_| Error::DimensionOverflow)?;
let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
let ldap_i: c_int = ldap.try_into().map_err(|_| Error::DimensionOverflow)?;
let nm_i: c_int = a.len().try_into().map_err(|_| Error::DimensionOverflow)?;
unsafe {
T::gepack_compact(
map_layout(layout),
rows_i,
cols_i,
a.as_ptr(),
lda_i,
ap.as_mut_ptr(),
ldap_i,
format.as_mkl(),
nm_i,
);
}
Ok(())
}
/// Unpack a compact buffer into individual matrices addressed by
/// `a` (one `*mut T` per matrix). Wraps `mkl_*geunpack_compact`.
///
/// # Safety
///
/// Each pointer in `a` must point to a buffer of at least the
/// matrix's storage size and remain valid for the duration of this
/// call.
#[allow(clippy::too_many_arguments)]
pub unsafe fn geunpack<T: CompactScalar>(
layout: Layout,
rows: usize,
cols: usize,
a: &[*mut T],
lda: usize,
ap: &[T],
ldap: usize,
format: CompactFormat,
) -> Result<()> {
let rows_i: c_int = rows.try_into().map_err(|_| Error::DimensionOverflow)?;
let cols_i: c_int = cols.try_into().map_err(|_| Error::DimensionOverflow)?;
let lda_i: c_int = lda.try_into().map_err(|_| Error::DimensionOverflow)?;
let ldap_i: c_int = ldap.try_into().map_err(|_| Error::DimensionOverflow)?;
let nm_i: c_int = a.len().try_into().map_err(|_| Error::DimensionOverflow)?;
unsafe {
T::geunpack_compact(
map_layout(layout),
rows_i,
cols_i,
a.as_ptr(),
lda_i,
ap.as_ptr(),
ldap_i,
format.as_mkl(),
nm_i,
);
}
Ok(())
}
/// Compact GEMM: perform `num_matrices` GEMMs `C ← α · op(A) · op(B)
/// + β · C` in lockstep on packed buffers. Wraps `mkl_*gemm_compact`.
#[allow(clippy::too_many_arguments)]
pub fn gemm<T: CompactScalar>(
layout: Layout,
transa: Transpose,
transb: Transpose,
m: usize,
n: usize,
k: usize,
alpha: T,
ap: &[T],
ldap: usize,
bp: &[T],
ldbp: usize,
beta: T,
cp: &mut [T],
ldcp: usize,
format: CompactFormat,
num_matrices: usize,
) -> Result<()> {
let m_i: c_int = m.try_into().map_err(|_| Error::DimensionOverflow)?;
let n_i: c_int = n.try_into().map_err(|_| Error::DimensionOverflow)?;
let k_i: c_int = k.try_into().map_err(|_| Error::DimensionOverflow)?;
let ldap_i: c_int = ldap.try_into().map_err(|_| Error::DimensionOverflow)?;
let ldbp_i: c_int = ldbp.try_into().map_err(|_| Error::DimensionOverflow)?;
let ldcp_i: c_int = ldcp.try_into().map_err(|_| Error::DimensionOverflow)?;
let nm_i: c_int = num_matrices
.try_into()
.map_err(|_| Error::DimensionOverflow)?;
unsafe {
T::gemm_compact(
map_layout(layout),
map_transpose(transa),
map_transpose(transb),
m_i,
n_i,
k_i,
alpha,
ap.as_ptr(),
ldap_i,
bp.as_ptr(),
ldbp_i,
beta,
cp.as_mut_ptr(),
ldcp_i,
format.as_mkl(),
nm_i,
);
}
Ok(())
}
#[inline]
fn map_side(s: Side) -> sys::MKL_SIDE::Type {
match s {
Side::Left => sys::MKL_SIDE::MKL_LEFT,
Side::Right => sys::MKL_SIDE::MKL_RIGHT,
}
}
#[inline]
fn map_uplo(u: UpLo) -> sys::MKL_UPLO::Type {
match u {
UpLo::Upper => sys::MKL_UPLO::MKL_UPPER,
UpLo::Lower => sys::MKL_UPLO::MKL_LOWER,
}
}
#[inline]
fn map_diag(d: Diag) -> sys::MKL_DIAG::Type {
match d {
Diag::NonUnit => sys::MKL_DIAG::MKL_NONUNIT,
Diag::Unit => sys::MKL_DIAG::MKL_UNIT,
}
}
/// Factorisation / solve kernels beyond GEMM (`f32` / `f64`).
#[allow(missing_docs)]
pub trait CompactFactorScalar: CompactScalar {
fn zero() -> Self;
fn to_workspace_len(self) -> usize;
/// Raw `trsm_compact` call.
///
/// # Safety
///
/// `a` and `b` are compact buffers for `nm` matrices in the same `format` (see below); `a` is the triangular `m x m` (side left) or `n x n` (side right) factor with leading dimension `ldap`, `b` the `m x n` right-hand side overwritten with the solution, leading dimension `ldbp`. Each must satisfy: A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed.
/// `b` must not overlap `a`; the diagonal of `a` must be non-singular unless `diag` is unit. The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn trsm_compact(
layout: sys::MKL_LAYOUT::Type,
side: sys::MKL_SIDE::Type,
uplo: sys::MKL_UPLO::Type,
transa: sys::MKL_TRANSPOSE::Type,
diag: sys::MKL_DIAG::Type,
m: c_int,
n: c_int,
alpha: Self,
a: *const Self,
ldap: c_int,
b: *mut Self,
ldbp: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
/// Raw `potrf_compact` call.
///
/// # Safety
///
/// `ap` must be a compact buffer for `nm` `n x n` matrices (updated in place with the Cholesky factor of the triangle given by `uplo`): A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed.
/// `info` must be valid for writes of the per-matrix status array MKL defines for compact routines (one `c_int` per matrix in the compact group set, `nm` in total); entries are written even on partial failure.
/// The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn potrf_compact(
layout: sys::MKL_LAYOUT::Type,
uplo: sys::MKL_UPLO::Type,
n: c_int,
ap: *mut Self,
ldap: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
/// Raw `getrfnp_compact` call.
///
/// # Safety
///
/// `ap` must be a compact buffer for `nm` `m x n` matrices (updated in place with the LU factors; no pivoting is performed, so a zero pivot yields a non-zero `info` entry): A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed.
/// `info` must be valid for writes of the per-matrix status array MKL defines for compact routines (one `c_int` per matrix in the compact group set, `nm` in total); entries are written even on partial failure.
/// The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn getrfnp_compact(
layout: sys::MKL_LAYOUT::Type,
m: c_int,
n: c_int,
ap: *mut Self,
ldap: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
/// Raw `geqrf_compact` call.
///
/// # Safety
///
/// `ap` must be a compact buffer for `nm` `m x n` matrices (updated in place with the QR factors): A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed.
/// `taup` must be a compact buffer holding `min(m, n)` scalars per matrix, in the same `format` and `nm`, writable for its full compact size.
/// `work` must be valid for writes of `lwork` elements, and `lwork` must be at least the workspace MKL requires (`lwork = -1` is a size query writing only `work[0]`).
/// `info` must be valid for writes of the per-matrix status array MKL defines for compact routines (one `c_int` per matrix in the compact group set, `nm` in total); entries are written even on partial failure.
/// The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn geqrf_compact(
layout: sys::MKL_LAYOUT::Type,
m: c_int,
n: c_int,
ap: *mut Self,
ldap: c_int,
taup: *mut Self,
work: *mut Self,
lwork: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
/// Raw `getrinp_compact` call.
///
/// # Safety
///
/// `ap` must be a compact buffer for `nm` `n x n` matrices previously LU-factorised without pivoting by `getrfnp_compact`; it is overwritten with the inverses: A compact buffer `ap` holds `nm` matrices interleaved in SIMD-width groups per `format`. It must be valid for the byte size returned by `get_size_compact(ldap, stored_cols, format, nm)` (column-major; `stored_rows` for row-major), where `ldap` is at least the stored row (column-major) or column (row-major) count times the group width and `stored_rows x stored_cols` is the per-matrix shape the routine defines for it, and `format` and `nm` must be the same values used when the buffer was packed.
/// `work` must be valid for writes of `lwork` elements, and `lwork` at least the workspace MKL requires.
/// `info` must be valid for writes of the per-matrix status array MKL defines for compact routines (one `c_int` per matrix in the compact group set, `nm` in total); entries are written even on partial failure.
/// The MKL library must be linked and usable; invalid arguments are reported through MKL's error handler, not by this wrapper.
#[allow(clippy::too_many_arguments)]
unsafe fn getrinp_compact(
layout: sys::MKL_LAYOUT::Type,
n: c_int,
ap: *mut Self,
ldap: c_int,
work: *mut Self,
lwork: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
);
}
macro_rules! impl_compact_factor {
($t:ty, $trsm:ident, $potrf:ident, $getrfnp:ident, $geqrf:ident, $getrinp:ident) => {
impl CompactFactorScalar for $t {
fn zero() -> Self {
0.0
}
fn to_workspace_len(self) -> usize {
self.ceil() as usize
}
unsafe fn trsm_compact(
layout: sys::MKL_LAYOUT::Type,
side: sys::MKL_SIDE::Type,
uplo: sys::MKL_UPLO::Type,
transa: sys::MKL_TRANSPOSE::Type,
diag: sys::MKL_DIAG::Type,
m: c_int,
n: c_int,
alpha: Self,
a: *const Self,
ldap: c_int,
b: *mut Self,
ldbp: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe {
sys::$trsm(
layout, side, uplo, transa, diag, m, n, alpha, a, ldap, b, ldbp, format, nm,
)
}
}
unsafe fn potrf_compact(
layout: sys::MKL_LAYOUT::Type,
uplo: sys::MKL_UPLO::Type,
n: c_int,
ap: *mut Self,
ldap: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::$potrf(layout, uplo, n, ap, ldap, info, format, nm) }
}
unsafe fn getrfnp_compact(
layout: sys::MKL_LAYOUT::Type,
m: c_int,
n: c_int,
ap: *mut Self,
ldap: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::$getrfnp(layout, m, n, ap, ldap, info, format, nm) }
}
unsafe fn geqrf_compact(
layout: sys::MKL_LAYOUT::Type,
m: c_int,
n: c_int,
ap: *mut Self,
ldap: c_int,
taup: *mut Self,
work: *mut Self,
lwork: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::$geqrf(layout, m, n, ap, ldap, taup, work, lwork, info, format, nm) }
}
unsafe fn getrinp_compact(
layout: sys::MKL_LAYOUT::Type,
n: c_int,
ap: *mut Self,
ldap: c_int,
work: *mut Self,
lwork: c_int,
info: *mut c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) {
unsafe { sys::$getrinp(layout, n, ap, ldap, work, lwork, info, format, nm) }
}
}
};
}
impl_compact_factor!(
f32,
mkl_strsm_compact,
mkl_spotrf_compact,
mkl_sgetrfnp_compact,
mkl_sgeqrf_compact,
mkl_sgetrinp_compact
);
impl_compact_factor!(
f64,
mkl_dtrsm_compact,
mkl_dpotrf_compact,
mkl_dgetrfnp_compact,
mkl_dgeqrf_compact,
mkl_dgetrinp_compact
);
/// Packed buffer length needed for a `rows x cols` matrix in `layout`
/// with packed leading dimension `ldap`, or an error if `ldap` is smaller
/// than the matrix's leading dimension.
fn packed_len<T: CompactScalar>(
layout: Layout,
rows: usize,
cols: usize,
ldap: usize,
format: CompactFormat,
nm: usize,
) -> Result<usize> {
let (ld, sd) = match layout {
Layout::ColMajor => (rows, cols),
Layout::RowMajor => (cols, rows),
};
if ldap < ld {
return Err(Error::InvalidArgument(
"ldap is smaller than the matrix's leading dimension",
));
}
get_size::<T>(ldap, sd, format, nm)
}
fn to_c(v: usize) -> Result<c_int> {
v.try_into().map_err(|_| Error::DimensionOverflow)
}
/// Compact TRSM: solve `op(A) X = alpha B` (or `X op(A) = alpha B`) for
/// `num_matrices` triangular systems in lockstep; `B` is overwritten by
/// `X`. `A` is `m x m` for `Side::Left`, `n x n` for `Side::Right`; `B` is
/// `m x n`. Both are in the compact layout of `format`. Buffer lengths are
/// checked against [`get_size`]. Wraps `mkl_*trsm_compact`.
#[allow(clippy::too_many_arguments)]
pub fn trsm<T: CompactFactorScalar>(
layout: Layout,
side: Side,
uplo: UpLo,
transa: Transpose,
diag: Diag,
m: usize,
n: usize,
alpha: T,
ap: &[T],
ldap: usize,
bp: &mut [T],
ldbp: usize,
format: CompactFormat,
num_matrices: usize,
) -> Result<()> {
let k = match side {
Side::Left => m,
Side::Right => n,
};
if ap.len() < packed_len::<T>(layout, k, k, ldap, format, num_matrices)? {
return Err(Error::InvalidArgument(
"ap is smaller than the compact size of A",
));
}
if bp.len() < packed_len::<T>(layout, m, n, ldbp, format, num_matrices)? {
return Err(Error::InvalidArgument(
"bp is smaller than the compact size of B",
));
}
let (m_i, n_i, ldap_i, ldbp_i, nm_i) = (
to_c(m)?,
to_c(n)?,
to_c(ldap)?,
to_c(ldbp)?,
to_c(num_matrices)?,
);
unsafe {
T::trsm_compact(
map_layout(layout),
map_side(side),
map_uplo(uplo),
map_transpose(transa),
map_diag(diag),
m_i,
n_i,
alpha,
ap.as_ptr(),
ldap_i,
bp.as_mut_ptr(),
ldbp_i,
format.as_mkl(),
nm_i,
);
}
Ok(())
}
/// Compact Cholesky: factor `num_matrices` symmetric positive-definite
/// `n x n` matrices in place (`uplo` selects the stored triangle). Returns
/// MKL's `info` (0 on success; MKL's compact interface reports one value
/// for the whole batch). Wraps `mkl_*potrf_compact`.
pub fn potrf<T: CompactFactorScalar>(
layout: Layout,
uplo: UpLo,
n: usize,
ap: &mut [T],
ldap: usize,
format: CompactFormat,
num_matrices: usize,
) -> Result<i32> {
if ap.len() < packed_len::<T>(layout, n, n, ldap, format, num_matrices)? {
return Err(Error::InvalidArgument(
"ap is smaller than the compact size of A",
));
}
let (n_i, ldap_i, nm_i) = (to_c(n)?, to_c(ldap)?, to_c(num_matrices)?);
let mut info: c_int = 0;
unsafe {
T::potrf_compact(
map_layout(layout),
map_uplo(uplo),
n_i,
ap.as_mut_ptr(),
ldap_i,
&mut info,
format.as_mkl(),
nm_i,
);
}
Ok(info)
}
/// Compact LU without pivoting: factor `num_matrices` `m x n` matrices in
/// place (`L` unit-lower and `U` share the buffer). Returns MKL's `info`.
/// No pivoting means the result is only meaningful for matrices whose
/// leading minors are non-singular. Wraps `mkl_*getrfnp_compact`.
pub fn getrfnp<T: CompactFactorScalar>(
layout: Layout,
m: usize,
n: usize,
ap: &mut [T],
ldap: usize,
format: CompactFormat,
num_matrices: usize,
) -> Result<i32> {
if ap.len() < packed_len::<T>(layout, m, n, ldap, format, num_matrices)? {
return Err(Error::InvalidArgument(
"ap is smaller than the compact size of A",
));
}
let (m_i, n_i, ldap_i, nm_i) = (to_c(m)?, to_c(n)?, to_c(ldap)?, to_c(num_matrices)?);
let mut info: c_int = 0;
unsafe {
T::getrfnp_compact(
map_layout(layout),
m_i,
n_i,
ap.as_mut_ptr(),
ldap_i,
&mut info,
format.as_mkl(),
nm_i,
);
}
Ok(info)
}
/// Compact QR: factor `num_matrices` `m x n` matrices in place (`R` in the
/// upper triangle, Householder vectors below). `taup` receives the
/// Householder scalars as a compact buffer of `min(m, n) x 1` vectors with
/// leading dimension `min(m, n)`; its length is checked against
/// [`get_size`]. The workspace is allocated internally from MKL's
/// workspace query. Returns MKL's `info`. Wraps `mkl_*geqrf_compact`, whose
/// arguments it takes one for one (layout, m, n, ap, ldap, taup, format,
/// num_matrices).
#[allow(
clippy::too_many_arguments,
reason = "mirrors the oneMKL C signature mkl_?geqrf_compact argument for argument"
)]
pub fn geqrf<T: CompactFactorScalar>(
layout: Layout,
m: usize,
n: usize,
ap: &mut [T],
ldap: usize,
taup: &mut [T],
format: CompactFormat,
num_matrices: usize,
) -> Result<i32> {
if ap.len() < packed_len::<T>(layout, m, n, ldap, format, num_matrices)? {
return Err(Error::InvalidArgument(
"ap is smaller than the compact size of A",
));
}
let k = m.min(n);
if k > 0 && taup.len() < get_size::<T>(k, 1, format, num_matrices)? {
return Err(Error::InvalidArgument(
"taup is smaller than the compact size of tau",
));
}
let (m_i, n_i, ldap_i, nm_i) = (to_c(m)?, to_c(n)?, to_c(ldap)?, to_c(num_matrices)?);
let mut info: c_int = 0;
// Workspace query (lwork = -1): MKL writes the required size to work[0].
let mut query = [T::zero()];
unsafe {
T::geqrf_compact(
map_layout(layout),
m_i,
n_i,
ap.as_mut_ptr(),
ldap_i,
taup.as_mut_ptr(),
query.as_mut_ptr(),
-1,
&mut info,
format.as_mkl(),
nm_i,
);
}
let lwork = query[0].to_workspace_len().max(1);
let mut work = vec![T::zero(); lwork];
unsafe {
T::geqrf_compact(
map_layout(layout),
m_i,
n_i,
ap.as_mut_ptr(),
ldap_i,
taup.as_mut_ptr(),
work.as_mut_ptr(),
to_c(lwork)?,
&mut info,
format.as_mkl(),
nm_i,
);
}
Ok(info)
}
/// Compact in-place inverse of `num_matrices` `n x n` matrices that were
/// factored by [`getrfnp`] (no pivoting): on exit `ap` holds `A⁻¹`. The
/// workspace is allocated internally from MKL's workspace query. Returns
/// MKL's `info`. Wraps `mkl_*getrinp_compact`.
pub fn getrinp<T: CompactFactorScalar>(
layout: Layout,
n: usize,
ap: &mut [T],
ldap: usize,
format: CompactFormat,
num_matrices: usize,
) -> Result<i32> {
if ap.len() < packed_len::<T>(layout, n, n, ldap, format, num_matrices)? {
return Err(Error::InvalidArgument(
"ap is smaller than the compact size of A",
));
}
let (n_i, ldap_i, nm_i) = (to_c(n)?, to_c(ldap)?, to_c(num_matrices)?);
let mut info: c_int = 0;
// Workspace query (lwork = -1): MKL writes the required size to work[0].
let mut query = [T::zero()];
unsafe {
T::getrinp_compact(
map_layout(layout),
n_i,
ap.as_mut_ptr(),
ldap_i,
query.as_mut_ptr(),
-1,
&mut info,
format.as_mkl(),
nm_i,
);
}
let lwork = query[0].to_workspace_len().max(1);
let mut work = vec![T::zero(); lwork];
unsafe {
T::getrinp_compact(
map_layout(layout),
n_i,
ap.as_mut_ptr(),
ldap_i,
work.as_mut_ptr(),
to_c(lwork)?,
&mut info,
format.as_mkl(),
nm_i,
);
}
Ok(info)
}