use core::ffi::c_int;
use onemkl_sys as sys;
use crate::enums::{Diag, Layout, Side, Transpose, UpLo};
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CompactFormat {
Sse,
Avx,
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,
}
}
#[allow(missing_docs)]
pub trait CompactScalar: Copy + 'static {
unsafe fn get_size_compact(
ld: c_int,
sd: c_int,
format: sys::MKL_COMPACT_PACK::Type,
nm: c_int,
) -> c_int;
#[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,
);
#[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,
);
#[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,
)
}
}
}
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))
}
#[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(())
}
#[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(())
}
#[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,
}
}
#[allow(missing_docs)]
pub trait CompactFactorScalar: CompactScalar {
fn zero() -> Self;
fn to_workspace_len(self) -> usize;
#[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,
);
#[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,
);
#[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,
);
#[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,
);
#[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);
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)
}
#[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(())
}
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)
}
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)
}
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;
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)
}
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;
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)
}