#[cfg(feature = "cuda")]
use crate::gpu_cuda::nvg_dev as cu;
use crate::gpu_wgpu::nvg_dev as w;
pub use w::{ABS, ACC, COL, INPLACE, LOWER, ROW, SIGN};
pub const GV_MAX_RANK: usize = w::GV_MAX_RANK;
pub fn on_cuda() -> bool {
#[cfg(feature = "cuda")]
{
static ON: std::sync::OnceLock<bool> = std::sync::OnceLock::new();
*ON.get_or_init(|| match std::env::var("CMF_NVG_ENC").as_deref() {
Ok("cuda") => cu::available(),
Ok("wgpu") => false,
_ => crate::gpu_cuda::selected() && cu::available(),
})
}
#[cfg(not(feature = "cuda"))]
{
false
}
}
pub fn backend_name() -> &'static str {
if on_cuda() { "cuda" } else { "wgpu" }
}
macro_rules! be {
($w:expr, $c:expr) => {{
#[cfg(feature = "cuda")]
{
if on_cuda() { $c } else { $w }
}
#[cfg(not(feature = "cuda"))]
{
$w
}
}};
}
enum Inner {
W(w::DMat),
#[cfg(feature = "cuda")]
C(cu::DMat),
}
pub struct DMat {
pub r: usize,
pub c: usize,
i: Inner,
}
impl DMat {
fn of_w(m: w::DMat) -> DMat {
DMat { r: m.r, c: m.c, i: Inner::W(m) }
}
#[cfg(feature = "cuda")]
fn of_c(m: cu::DMat) -> DMat {
DMat { r: m.r, c: m.c, i: Inner::C(m) }
}
fn w(&self) -> &w::DMat {
match &self.i {
Inner::W(m) => m,
#[cfg(feature = "cuda")]
Inner::C(_) => panic!("a CUDA matrix in a wgpu NVG call"),
}
}
#[cfg(feature = "cuda")]
fn cu(&self) -> &cu::DMat {
match &self.i {
Inner::C(m) => m,
Inner::W(_) => panic!("a wgpu matrix in a CUDA NVG call"),
}
}
pub fn zeros(r: usize, c: usize) -> DMat {
be!(DMat::of_w(w::DMat::zeros(r, c)), DMat::of_c(cu::DMat::zeros(r, c)))
}
pub fn from_f32(r: usize, c: usize, v: &[f32]) -> DMat {
be!(DMat::of_w(w::DMat::from_f32(r, c, v)), DMat::of_c(cu::DMat::from_f32(r, c, v)))
}
pub fn from_f64(r: usize, c: usize, v: &[f64]) -> DMat {
be!(DMat::of_w(w::DMat::from_f64(r, c, v)), DMat::of_c(cu::DMat::from_f64(r, c, v)))
}
pub fn write_f64(&self, v: &[f64]) {
be!(self.w().write_f64(v), self.cu().write_f64(v))
}
pub fn len(&self) -> usize {
self.r * self.c
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn to_f32(&self) -> Vec<f32> {
be!(self.w().to_f32(), self.cu().to_f32())
}
pub fn to_f64(&self) -> Vec<f64> {
be!(self.w().to_f64(), self.cu().to_f64())
}
pub fn dup(&self) -> DMat {
be!(DMat::of_w(self.w().dup()), DMat::of_c(self.cu().dup()))
}
}
pub enum U32Buf {
W(wgpu::Buffer),
#[cfg(feature = "cuda")]
C(cu::U32Buf),
}
impl U32Buf {
fn w(&self) -> &wgpu::Buffer {
match self {
U32Buf::W(b) => b,
#[cfg(feature = "cuda")]
U32Buf::C(_) => panic!("a CUDA index vector in a wgpu NVG call"),
}
}
#[cfg(feature = "cuda")]
fn cu(&self) -> &cu::U32Buf {
match self {
U32Buf::C(b) => b,
U32Buf::W(_) => panic!("a wgpu index vector in a CUDA NVG call"),
}
}
}
pub fn available() -> bool {
on_cuda() || w::available()
}
pub fn sync() {
be!(w::sync(), cu::sync())
}
pub fn private_lane() -> LaneGuard {
LaneGuard {
#[cfg(feature = "cuda")]
_c: if on_cuda() { cu::private_lane() } else { None },
_thread: std::marker::PhantomData,
}
}
pub struct LaneGuard {
#[cfg(feature = "cuda")]
_c: Option<cu::LaneGuard>,
_thread: std::marker::PhantomData<*const ()>,
}
pub fn copy(src: &DMat, dst: &DMat) {
be!(w::copy(src.w(), dst.w()), cu::copy(src.cu(), dst.cu()))
}
pub fn copy_range(src: &DMat, so: usize, dst: &DMat, do_: usize, len: usize) {
be!(w::copy_range(src.w(), so, dst.w(), do_, len), cu::copy_range(src.cu(), so, dst.cu(), do_, len))
}
pub fn upload_u32(v: &[u32]) -> U32Buf {
be!(U32Buf::W(w::upload_u32(v)), U32Buf::C(cu::upload_u32(v)))
}
#[derive(Clone, Copy)]
pub struct V<'a> {
pub m: &'a DMat,
pub off: usize,
pub ld: usize,
pub t: bool,
}
impl<'a> V<'a> {
pub fn n(m: &'a DMat) -> V<'a> {
V { m, off: 0, ld: m.c, t: false }
}
pub fn t(m: &'a DMat) -> V<'a> {
V { m, off: 0, ld: m.c, t: true }
}
fn w(&self) -> w::V<'a> {
w::V { m: self.m.w(), off: self.off, ld: self.ld, t: self.t }
}
#[cfg(feature = "cuda")]
fn cu(&self) -> cu::V<'a> {
cu::V { m: self.m.cu(), off: self.off, ld: self.ld, t: self.t }
}
}
#[allow(clippy::too_many_arguments)]
pub fn gemm_v(m: usize, n: usize, k: usize, alpha: f32, a: V, b: V, beta: f32, c: &DMat, oc: usize, ldc: usize, e: Option<&DMat>) {
be!(
w::gemm_v(m, n, k, alpha, a.w(), b.w(), beta, c.w(), oc, ldc, e.map(|x| x.w())),
cu::gemm_v(m, n, k, alpha, a.cu(), b.cu(), beta, c.cu(), oc, ldc, e.map(|x| x.cu()))
)
}
pub fn gemm(alpha: f32, a: &DMat, ta: bool, b: &DMat, tb: bool, beta: f32, c: &DMat) {
be!(w::gemm(alpha, a.w(), ta, b.w(), tb, beta, c.w()), cu::gemm(alpha, a.cu(), ta, b.cu(), tb, beta, c.cu()))
}
pub fn mm(a: &DMat, ta: bool, b: &DMat, tb: bool) -> DMat {
be!(DMat::of_w(w::mm(a.w(), ta, b.w(), tb)), DMat::of_c(cu::mm(a.cu(), ta, b.cu(), tb)))
}
#[allow(clippy::too_many_arguments)]
pub fn scale_rc(rows: usize, cols: usize, a: Option<(&DMat, usize, usize)>, c: (&DMat, usize, usize), r: Option<&DMat>, s: Option<&DMat>, flags: u32, alpha: f32, beta: f32) {
be!(
w::scale_rc(rows, cols, a.map(|(m, o, l)| (m.w(), o, l)), (c.0.w(), c.1, c.2), r.map(|x| x.w()), s.map(|x| x.w()), flags, alpha, beta),
cu::scale_rc(rows, cols, a.map(|(m, o, l)| (m.cu(), o, l)), (c.0.cu(), c.1, c.2), r.map(|x| x.cu()), s.map(|x| x.cu()), flags, alpha, beta)
)
}
pub fn scale_into(a: &DMat, c: &DMat, r: Option<&DMat>, s: Option<&DMat>, flags: u32, alpha: f32) {
be!(
w::scale_into(a.w(), c.w(), r.map(|x| x.w()), s.map(|x| x.w()), flags, alpha),
cu::scale_into(a.cu(), c.cu(), r.map(|x| x.cu()), s.map(|x| x.cu()), flags, alpha)
)
}
pub fn axpby(alpha: f32, a: &DMat, beta: f32, c: &DMat) {
be!(w::axpby(alpha, a.w(), beta, c.w()), cu::axpby(alpha, a.cu(), beta, c.cu()))
}
#[allow(clippy::too_many_arguments)]
pub fn rowdot(rows: usize, cols: usize, a: &DMat, oa: usize, lda: usize, b: &DMat, ob: usize, ldb: usize, out: &DMat) {
be!(w::rowdot(rows, cols, a.w(), oa, lda, b.w(), ob, ldb, out.w()), cu::rowdot(rows, cols, a.cu(), oa, lda, b.cu(), ob, ldb, out.cu()))
}
pub fn rownorm(rows: usize, cols: usize, c: &DMat, oc: usize, ldc: usize) {
be!(w::rownorm(rows, cols, c.w(), oc, ldc), cu::rownorm(rows, cols, c.cu(), oc, ldc))
}
#[allow(clippy::too_many_arguments)]
pub fn colsum_prod(rows: usize, cols: usize, a: &DMat, oa: usize, lda: usize, b: &DMat, ob: usize, ldb: usize) -> Vec<f64> {
be!(w::colsum_prod(rows, cols, a.w(), oa, lda, b.w(), ob, ldb), cu::colsum_prod(rows, cols, a.cu(), oa, lda, b.cu(), ob, ldb))
}
#[allow(clippy::too_many_arguments)]
pub fn colsum_dev(rows: usize, cols: usize, a: &DMat, oa: usize, lda: usize, b: &DMat, ob: usize, ldb: usize, out: &DMat, oo: usize) {
be!(
w::colsum_dev(rows, cols, a.w(), oa, lda, b.w(), ob, ldb, out.w(), oo),
cu::colsum_dev(rows, cols, a.cu(), oa, lda, b.cu(), ob, ldb, out.cu(), oo)
)
}
pub fn frob(a: &DMat, b: &DMat, len: usize) -> f64 {
be!(w::frob(a.w(), b.w(), len), cu::frob(a.cu(), b.cu(), len))
}
pub enum FrobBatch {
W(w::FrobBatch),
#[cfg(feature = "cuda")]
C(cu::FrobBatch),
}
impl FrobBatch {
pub fn new() -> Self {
be!(FrobBatch::W(w::FrobBatch::new()), FrobBatch::C(cu::FrobBatch::new()))
}
pub fn add(&mut self, a: &DMat, b: &DMat, len: usize) -> usize {
match self {
FrobBatch::W(f) => f.add(a.w(), b.w(), len),
#[cfg(feature = "cuda")]
FrobBatch::C(f) => f.add(a.cu(), b.cu(), len),
}
}
pub fn read(self) -> Vec<f64> {
match self {
FrobBatch::W(f) => f.read(),
#[cfg(feature = "cuda")]
FrobBatch::C(f) => f.read(),
}
}
}
impl Default for FrobBatch {
fn default() -> Self {
Self::new()
}
}
pub fn diag(a: &DMat, n: usize, oa: usize, lda: usize) -> Vec<f64> {
be!(w::diag(a.w(), n, oa, lda), cu::diag(a.cu(), n, oa, lda))
}
pub fn equil(s: &DMat, f: &DMat, dv: &DMat, ridge: f32, perm: Option<&U32Buf>) {
be!(w::equil(s.w(), f.w(), dv.w(), ridge, perm.map(|p| p.w())), cu::equil(s.cu(), f.cu(), dv.cu(), ridge, perm.map(|p| p.cu())))
}
pub fn make_uc(x: &DMat, dp: &DMat, uc: &DMat) {
be!(w::make_uc(x.w(), dp.w(), uc.w()), cu::make_uc(x.cu(), dp.cu(), uc.cu()))
}
pub fn permute_rows(a: &DMat, c: &DMat, perm: &U32Buf, scatter: bool) {
be!(w::permute_rows(a.w(), c.w(), perm.w(), scatter), cu::permute_rows(a.cu(), c.cu(), perm.cu(), scatter))
}
pub fn transpose(a: &DMat) -> DMat {
be!(DMat::of_w(w::transpose(a.w())), DMat::of_c(cu::transpose(a.cu())))
}
pub fn spd_solve_d(a: &DMat, b: &DMat, rel: f64) -> DMat {
be!(DMat::of_w(w::spd_solve_d(a.w(), b.w(), rel)), DMat::of_c(cu::spd_solve_d(a.cu(), b.cu(), rel)))
}
#[allow(clippy::too_many_arguments)]
pub fn hsolve(m: usize, np: usize, rh0: &DMat, rh1: &DMat, g00: &DMat, g01: &DMat, g11: &DMat, h0: &DMat, h1: &DMat) {
be!(
w::hsolve(m, np, rh0.w(), rh1.w(), g00.w(), g01.w(), g11.w(), h0.w(), h1.w()),
cu::hsolve(m, np, rh0.cu(), rh1.cu(), g00.cu(), g01.cu(), g11.cu(), h0.cu(), h1.cu())
)
}
pub fn scatter_vec(src: &DMat, so: usize, dst: &DMat, perm: &U32Buf, n: usize) {
be!(w::scatter_vec(src.w(), so, dst.w(), perm.w(), n), cu::scatter_vec(src.cu(), so, dst.cu(), perm.cu(), n))
}
pub fn normalize_frob(a: &DMat, out: &DMat) {
be!(w::normalize_frob(a.w(), out.w()), cu::normalize_frob(a.cu(), out.cu()))
}
pub fn chol_solve_dev(l: &DMat, y: &DMat) {
be!(w::chol_solve_dev(l.w(), y.w()), cu::chol_solve_dev(l.cu(), y.cu()))
}
pub fn chol_in_place(a: &DMat) -> Option<bool> {
be!(w::chol_in_place(a.w()), cu::chol_in_place(a.cu()))
}
pub fn tri_inv(l: &DMat, x: &DMat) -> Option<()> {
be!(w::tri_inv(l.w(), x.w()), cu::tri_inv(l.cu(), x.cu()))
}
#[allow(clippy::too_many_arguments)]
pub fn flipu_blocked(su: &DMat, qs: &DMat, whb: &DMat, q: &DMat, h: &DMat, l: &DMat, m: usize, r: usize, tol: f32) -> Option<u64> {
be!(
w::flipu_blocked(su.w(), qs.w(), whb.w(), q.w(), h.w(), l.w(), m, r, tol),
cu::flipu_blocked(su.cu(), qs.cu(), whb.cu(), q.cu(), h.cu(), l.cu(), m, r, tol)
)
}
#[allow(clippy::too_many_arguments)]
pub fn flipu(sut: &DMat, qst: &DMat, whbt: &DMat, q: &DMat, h: &DMat, l: &DMat, m: usize, r: usize, tol: f32) -> u64 {
be!(
w::flipu(sut.w(), qst.w(), whbt.w(), q.w(), h.w(), l.w(), m, r, tol),
cu::flipu(sut.cu(), qst.cu(), whbt.cu(), q.cu(), h.cu(), l.cu(), m, r, tol)
)
}
#[allow(clippy::too_many_arguments)]
pub fn gv_block(bv: &DMat, s: &DMat, ks0: &DMat, k: &DMat, uc: &DMat, eg: &DMat, cnt: &DMat, b0: usize, b1: usize, max_flips: usize, tol: f32) {
be!(
w::gv_block(bv.w(), s.w(), ks0.w(), k.w(), uc.w(), eg.w(), cnt.w(), b0, b1, max_flips, tol),
cu::gv_block(bv.cu(), s.cu(), ks0.cu(), k.cu(), uc.cu(), eg.cu(), cnt.cu(), b0, b1, max_flips, tol)
)
}
pub fn sym_eig_top(a: &DMat, r: usize, pool: Option<&crate::pool::Pool>) -> (Vec<f64>, DMat) {
be!(
{
let (lam, z) = w::sym_eig_top(a.w(), r, pool);
(lam, DMat::of_w(z))
},
{
let (lam, z) = cu::sym_eig_top(a.cu(), r, pool);
(lam, DMat::of_c(z))
}
)
}