Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor div-conq SVD, impl svd_rand #165

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ default-features = false
version = "0.3"
default-features = false

[dependencies.sprs]
git = "https://github.com/pmarks/sprs.git"
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required for the Dot impl in @pmarks branch, which itself is dependent on rust-lang/rust#55437

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to create nightly CI

branch = "pmarks/scan-rs"
optional = true

[dependencies.openblas-src]
version = "0.6"
default-features = false
Expand Down
4 changes: 1 addition & 3 deletions src/lapack/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ pub mod qr;
pub mod solve;
pub mod solveh;
pub mod svd;
pub mod svddc;
pub mod triangular;

pub use self::cholesky::*;
Expand All @@ -17,7 +16,6 @@ pub use self::qr::*;
pub use self::solve::*;
pub use self::solveh::*;
pub use self::svd::*;
pub use self::svddc::*;
pub use self::triangular::*;

use super::error::*;
Expand All @@ -26,7 +24,7 @@ use super::types::*;
pub type Pivot = Vec<i32>;

/// Trait for primitive types which implements LAPACK subroutines
pub trait Lapack: OperatorNorm_ + QR_ + SVD_ + SVDDC_ + Solve_ + Solveh_ + Cholesky_ + Eigh_ + Triangular_ {}
pub trait Lapack: OperatorNorm_ + QR_ + SVD_ + Solve_ + Solveh_ + Cholesky_ + Eigh_ + Triangular_ {}

impl Lapack for f32 {}
impl Lapack for f64 {}
Expand Down
95 changes: 68 additions & 27 deletions src/lapack/svd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,7 @@ use crate::types::*;

use super::into_result;

#[repr(u8)]
enum FlagSVD {
All = b'A',
// OverWrite = b'O',
// Separately = b'S',
No = b'N',
}
use crate::svd::FlagSVD;

/// Result of SVD
pub struct SVDOutput<A: Scalar> {
Expand All @@ -27,35 +21,45 @@ pub struct SVDOutput<A: Scalar> {
pub vt: Option<Vec<A>>,
}

/// Wraps `*gesvd`
/// Wraps `*gesvd` and `*gesdd`
pub trait SVD_: Scalar {
unsafe fn svd(l: MatrixLayout, calc_u: bool, calc_vt: bool, a: &mut [Self]) -> Result<SVDOutput<Self>>;
unsafe fn svd(l: MatrixLayout, jobu: FlagSVD, jobvt: FlagSVD, a: &mut [Self]) -> Result<SVDOutput<Self>>;
unsafe fn svd_dc(l: MatrixLayout, jobz: FlagSVD, a: &mut [Self]) -> Result<SVDOutput<Self>>;
}

macro_rules! impl_svd {
($scalar:ty, $gesvd:path) => {
($scalar:ty, $gesvd:path, $gesdd:path) => {
impl SVD_ for $scalar {
unsafe fn svd(l: MatrixLayout, calc_u: bool, calc_vt: bool, mut a: &mut [Self]) -> Result<SVDOutput<Self>> {
unsafe fn svd(
l: MatrixLayout,
jobu: FlagSVD,
jobvt: FlagSVD,
mut a: &mut [Self],
) -> Result<SVDOutput<Self>> {
let (m, n) = l.size();
let k = ::std::cmp::min(n, m);
let lda = l.lda();
let (ju, ldu, mut u) = if calc_u {
(FlagSVD::All, m, vec![Self::zero(); (m * m) as usize])
} else {
(FlagSVD::No, 1, Vec::new())
let ucol = match jobu {
FlagSVD::All => m,
FlagSVD::Some => k,
FlagSVD::None => 0,
};
let (jvt, ldvt, mut vt) = if calc_vt {
(FlagSVD::All, n, vec![Self::zero(); (n * n) as usize])
} else {
(FlagSVD::No, n, Vec::new())
let vtrow = match jobvt {
FlagSVD::All => n,
FlagSVD::Some => k,
FlagSVD::None => 0,
};
let mut u = vec![Self::zero(); (m * ucol).max(1) as usize];
let ldu = l.resized(m, ucol).lda();
let mut vt = vec![Self::zero(); (vtrow * n).max(1) as usize];
let ldvt = l.resized(vtrow, n).lda();
let mut s = vec![Self::Real::zero(); k as usize];
let mut superb = vec![Self::Real::zero(); (k - 1) as usize];
dbg!(ldvt);
let info = $gesvd(
l.lapacke_layout(),
ju as u8,
jvt as u8,
jobu as u8,
jobvt as u8,
m,
n,
&mut a,
Expand All @@ -71,16 +75,53 @@ macro_rules! impl_svd {
info,
SVDOutput {
s: s,
u: if calc_u { Some(u) } else { None },
vt: if calc_vt { Some(vt) } else { None },
u: if jobu == FlagSVD::None { None } else { Some(u) },
vt: if jobvt == FlagSVD::None { None } else { Some(vt) },
},
)
}

unsafe fn svd_dc(l: MatrixLayout, jobz: FlagSVD, mut a: &mut [Self]) -> Result<SVDOutput<Self>> {
let (m, n) = l.size();
let k = m.min(n);
let lda = l.lda();
let (ucol, vtrow) = match jobz {
FlagSVD::All => (m, n),
FlagSVD::Some => (k, k),
FlagSVD::None => (0, 0),
};
let mut s = vec![Self::Real::zero(); k.max(1) as usize];
let mut u = vec![Self::zero(); (m * ucol).max(1) as usize];
let ldu = l.resized(m, ucol).lda();
let mut vt = vec![Self::zero(); (vtrow * n).max(1) as usize];
let ldvt = l.resized(vtrow, n).lda();
let info = $gesdd(
l.lapacke_layout(),
jobz as u8,
m,
n,
&mut a,
lda,
&mut s,
&mut u,
ldu,
&mut vt,
ldvt,
);
into_result(
info,
SVDOutput {
s: s,
u: if jobz == FlagSVD::None { None } else { Some(u) },
vt: if jobz == FlagSVD::None { None } else { Some(vt) },
},
)
}
}
};
} // impl_svd!

impl_svd!(f64, lapacke::dgesvd);
impl_svd!(f32, lapacke::sgesvd);
impl_svd!(c64, lapacke::zgesvd);
impl_svd!(c32, lapacke::cgesvd);
impl_svd!(f64, lapacke::dgesvd, lapacke::dgesdd);
impl_svd!(f32, lapacke::sgesvd, lapacke::sgesdd);
impl_svd!(c64, lapacke::zgesvd, lapacke::zgesdd);
impl_svd!(c32, lapacke::cgesvd, lapacke::cgesdd);
69 changes: 0 additions & 69 deletions src/lapack/svddc.rs

This file was deleted.

4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub mod qr;
pub mod solve;
pub mod solveh;
pub mod svd;
pub mod svddc;
pub mod svd_rand;
pub mod trace;
pub mod triangular;
pub mod types;
Expand All @@ -77,7 +77,7 @@ pub use qr::*;
pub use solve::*;
pub use solveh::*;
pub use svd::*;
pub use svddc::*;
pub use svd_rand::*;
pub use trace::*;
pub use triangular::*;
pub use types::*;
Loading