Skip to content

Commit 71ad0ba

Browse files
authored
Merge pull request #64 from IvanUkhov/unsafe
Update blas and lapack
2 parents 318a194 + 00b7e81 commit 71ad0ba

File tree

16 files changed

+62
-59
lines changed

16 files changed

+62
-59
lines changed

Cargo.toml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ license = "MIT"
1111

1212
[features]
1313
default = ["openblas"]
14-
openblas = ["blas/openblas", "lapack/openblas"]
15-
netlib = ["blas/netlib", "lapack/netlib"]
14+
openblas = ["lapack/openblas"]
15+
netlib = ["lapack/netlib"]
1616

1717
[dependencies]
1818
rand = "0.3"
@@ -21,5 +21,4 @@ enum-error-derive = "0.1"
2121
num-traits = "0.1"
2222
num-complex = "0.1"
2323
ndarray = { version = "0.9", default-features = false, features = ["blas"] }
24-
lapack = { version = "0.11", default-features = false }
25-
blas = { version = "0.15", default-features = false }
24+
lapack = { version = "0.13", default-features = false }

src/cholesky.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ where
3434
S: DataMut<Elem = A>,
3535
{
3636
fn cholesky_into(mut self, uplo: UPLO) -> Result<Self> {
37-
A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
37+
unsafe { A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)? };
3838
Ok(self.into_triangular(uplo))
3939
}
4040
}
@@ -45,7 +45,7 @@ where
4545
S: DataMut<Elem = A>,
4646
{
4747
fn cholesky_mut(&mut self, uplo: UPLO) -> Result<&mut Self> {
48-
A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)?;
48+
unsafe { A::cholesky(self.square_layout()?, uplo, self.as_allocated_mut()?)? };
4949
Ok(self.into_triangular(uplo))
5050
}
5151
}
@@ -59,7 +59,7 @@ where
5959

6060
fn cholesky(&self, uplo: UPLO) -> Result<Self::Output> {
6161
let mut a = replicate(self);
62-
A::cholesky(a.square_layout()?, uplo, a.as_allocated_mut()?)?;
62+
unsafe { A::cholesky(a.square_layout()?, uplo, a.as_allocated_mut()?)? };
6363
Ok(a.into_triangular(uplo))
6464
}
6565
}

src/eigh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ where
6363
type EigVal = Array1<A::Real>;
6464

6565
fn eigh_mut(&mut self, uplo: UPLO) -> Result<(Self::EigVal, &mut Self)> {
66-
let s = A::eigh(true, self.square_layout()?, uplo, self.as_allocated_mut()?)?;
66+
let s = unsafe { A::eigh(true, self.square_layout()?, uplo, self.as_allocated_mut()?)? };
6767
Ok((ArrayBase::from_vec(s), self))
6868
}
6969
}
@@ -119,7 +119,7 @@ where
119119
type EigVal = Array1<A::Real>;
120120

121121
fn eigvalsh_mut(&mut self, uplo: UPLO) -> Result<Self::EigVal> {
122-
let s = A::eigh(true, self.square_layout()?, uplo, self.as_allocated_mut()?)?;
122+
let s = unsafe { A::eigh(true, self.square_layout()?, uplo, self.as_allocated_mut()?)? };
123123
Ok(ArrayBase::from_vec(s))
124124
}
125125
}

src/lapack_traits/cholesky.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use types::*;
99
use super::{UPLO, into_result};
1010

1111
pub trait Cholesky_: Sized {
12-
fn cholesky(MatrixLayout, UPLO, a: &mut [Self]) -> Result<()>;
12+
unsafe fn cholesky(MatrixLayout, UPLO, a: &mut [Self]) -> Result<()>;
1313
}
1414

1515
macro_rules! impl_cholesky {
1616
($scalar:ty, $potrf:path) => {
1717
impl Cholesky_ for $scalar {
18-
fn cholesky(l: MatrixLayout, uplo: UPLO, mut a: &mut [Self]) -> Result<()> {
18+
unsafe fn cholesky(l: MatrixLayout, uplo: UPLO, mut a: &mut [Self]) -> Result<()> {
1919
let (n, _) = l.size();
2020
let info = $potrf(l.lapacke_layout(), uplo as u8, n, &mut a, n);
2121
into_result(info, ())

src/lapack_traits/eigh.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ use super::{UPLO, into_result};
1111

1212
/// Wraps `*syev` for real and `*heev` for complex
1313
pub trait Eigh_: AssociatedReal {
14-
fn eigh(calc_eigenvec: bool, MatrixLayout, UPLO, a: &mut [Self]) -> Result<Vec<Self::Real>>;
14+
unsafe fn eigh(calc_eigenvec: bool, MatrixLayout, UPLO, a: &mut [Self]) -> Result<Vec<Self::Real>>;
1515
}
1616

1717
macro_rules! impl_eigh {
1818
($scalar:ty, $ev:path) => {
1919
impl Eigh_ for $scalar {
20-
fn eigh(calc_v: bool, l: MatrixLayout, uplo: UPLO, mut a: &mut [Self]) -> Result<Vec<Self::Real>> {
20+
unsafe fn eigh(calc_v: bool, l: MatrixLayout, uplo: UPLO, mut a: &mut [Self]) -> Result<Vec<Self::Real>> {
2121
let (n, _) = l.size();
2222
let jobz = if calc_v { b'V' } else { b'N' };
2323
let mut w = vec![Self::Real::zero(); n as usize];

src/lapack_traits/opnorm.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ impl NormType {
2424
}
2525

2626
pub trait OperatorNorm_: AssociatedReal {
27-
fn opnorm(NormType, MatrixLayout, &[Self]) -> Self::Real;
27+
unsafe fn opnorm(NormType, MatrixLayout, &[Self]) -> Self::Real;
2828
}
2929

3030
macro_rules! impl_opnorm {
3131
($scalar:ty, $lange:path) => {
3232
impl OperatorNorm_ for $scalar {
33-
fn opnorm(t: NormType, l: MatrixLayout, a: &[Self]) -> Self::Real {
33+
unsafe fn opnorm(t: NormType, l: MatrixLayout, a: &[Self]) -> Self::Real {
3434
match l {
3535
MatrixLayout::F((col, lda)) => $lange(cm, t as u8, lda, col, a, lda),
3636
MatrixLayout::C((row, lda)) => $lange(cm, t.transpose() as u8, lda, row, a, lda),

src/lapack_traits/qr.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ use super::into_result;
1212

1313
/// Wraps `*geqrf` and `*orgqr` (`*ungqr` for complex numbers)
1414
pub trait QR_: Sized {
15-
fn householder(MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
16-
fn q(MatrixLayout, a: &mut [Self], tau: &[Self]) -> Result<()>;
17-
fn qr(MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
15+
unsafe fn householder(MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
16+
unsafe fn q(MatrixLayout, a: &mut [Self], tau: &[Self]) -> Result<()>;
17+
unsafe fn qr(MatrixLayout, a: &mut [Self]) -> Result<Vec<Self>>;
1818
}
1919

2020
macro_rules! impl_qr {
2121
($scalar:ty, $qrf:path, $gqr:path) => {
2222
impl QR_ for $scalar {
23-
fn householder(l: MatrixLayout, mut a: &mut [Self]) -> Result<Vec<Self>> {
23+
unsafe fn householder(l: MatrixLayout, mut a: &mut [Self]) -> Result<Vec<Self>> {
2424
let (row, col) = l.size();
2525
let k = min(row, col);
2626
let mut tau = vec![Self::zero(); k as usize];
2727
let info = $qrf(l.lapacke_layout(), row, col, &mut a, l.lda(), &mut tau);
2828
into_result(info, tau)
2929
}
3030

31-
fn q(l: MatrixLayout, mut a: &mut [Self], tau: &[Self]) -> Result<()> {
31+
unsafe fn q(l: MatrixLayout, mut a: &mut [Self], tau: &[Self]) -> Result<()> {
3232
let (row, col) = l.size();
3333
let k = min(row, col);
3434
let info = $gqr(l.lapacke_layout(), row, k, k, &mut a, l.lda(), &tau);
3535
into_result(info, ())
3636
}
3737

38-
fn qr(l: MatrixLayout, mut a: &mut [Self]) -> Result<Vec<Self>> {
38+
unsafe fn qr(l: MatrixLayout, mut a: &mut [Self]) -> Result<Vec<Self>> {
3939
let tau = Self::householder(l, a)?;
4040
let r = Vec::from(&*a);
4141
Self::q(l, a, &tau)?;

src/lapack_traits/solve.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,30 +12,30 @@ pub type Pivot = Vec<i32>;
1212

1313
/// Wraps `*getrf`, `*getri`, and `*getrs`
1414
pub trait Solve_: Sized {
15-
fn lu(MatrixLayout, a: &mut [Self]) -> Result<Pivot>;
16-
fn inv(MatrixLayout, a: &mut [Self], &Pivot) -> Result<()>;
17-
fn solve(MatrixLayout, Transpose, a: &[Self], &Pivot, b: &mut [Self]) -> Result<()>;
15+
unsafe fn lu(MatrixLayout, a: &mut [Self]) -> Result<Pivot>;
16+
unsafe fn inv(MatrixLayout, a: &mut [Self], &Pivot) -> Result<()>;
17+
unsafe fn solve(MatrixLayout, Transpose, a: &[Self], &Pivot, b: &mut [Self]) -> Result<()>;
1818
}
1919

2020
macro_rules! impl_solve {
2121
($scalar:ty, $getrf:path, $getri:path, $getrs:path) => {
2222

2323
impl Solve_ for $scalar {
24-
fn lu(l: MatrixLayout, a: &mut [Self]) -> Result<Pivot> {
24+
unsafe fn lu(l: MatrixLayout, a: &mut [Self]) -> Result<Pivot> {
2525
let (row, col) = l.size();
2626
let k = ::std::cmp::min(row, col);
2727
let mut ipiv = vec![0; k as usize];
2828
let info = $getrf(l.lapacke_layout(), row, col, a, l.lda(), &mut ipiv);
2929
into_result(info, ipiv)
3030
}
3131

32-
fn inv(l: MatrixLayout, a: &mut [Self], ipiv: &Pivot) -> Result<()> {
32+
unsafe fn inv(l: MatrixLayout, a: &mut [Self], ipiv: &Pivot) -> Result<()> {
3333
let (n, _) = l.size();
3434
let info = $getri(l.lapacke_layout(), n, a, l.lda(), ipiv);
3535
into_result(info, ())
3636
}
3737

38-
fn solve(l: MatrixLayout, t: Transpose, a: &[Self], ipiv: &Pivot, b: &mut [Self]) -> Result<()> {
38+
unsafe fn solve(l: MatrixLayout, t: Transpose, a: &[Self], ipiv: &Pivot, b: &mut [Self]) -> Result<()> {
3939
let (n, _) = l.size();
4040
let nrhs = 1;
4141
let ldb = 1;

src/lapack_traits/svd.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ pub struct SVDOutput<A: AssociatedReal> {
2929

3030
/// Wraps `*gesvd`
3131
pub trait SVD_: AssociatedReal {
32-
fn svd(MatrixLayout, calc_u: bool, calc_vt: bool, a: &mut [Self]) -> Result<SVDOutput<Self>>;
32+
unsafe fn svd(MatrixLayout, calc_u: bool, calc_vt: bool, a: &mut [Self]) -> Result<SVDOutput<Self>>;
3333
}
3434

3535
macro_rules! impl_svd {
3636
($scalar:ty, $gesvd:path) => {
3737

3838
impl SVD_ for $scalar {
39-
fn svd(l: MatrixLayout, calc_u: bool, calc_vt: bool, mut a: &mut [Self]) -> Result<SVDOutput<Self>> {
39+
unsafe fn svd(l: MatrixLayout, calc_u: bool, calc_vt: bool, mut a: &mut [Self]) -> Result<SVDOutput<Self>> {
4040
let (m, n) = l.size();
4141
let k = ::std::cmp::min(n, m);
4242
let lda = l.lda();

src/lapack_traits/triangular.rs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,32 +16,33 @@ pub enum Diag {
1616

1717
/// Wraps `*trtri` and `*trtrs`
1818
pub trait Triangular_: Sized {
19-
fn inv_triangular(l: MatrixLayout, UPLO, Diag, a: &mut [Self]) -> Result<()>;
20-
fn solve_triangular(al: MatrixLayout, bl: MatrixLayout, UPLO, Diag, a: &[Self], b: &mut [Self]) -> Result<()>;
19+
unsafe fn inv_triangular(l: MatrixLayout, UPLO, Diag, a: &mut [Self]) -> Result<()>;
20+
unsafe fn solve_triangular(
21+
al: MatrixLayout,
22+
bl: MatrixLayout,
23+
UPLO,
24+
Diag,
25+
a: &[Self],
26+
b: &mut [Self],
27+
) -> Result<()>;
2128
}
2229

2330
macro_rules! impl_triangular {
2431
($scalar:ty, $trtri:path, $trtrs:path) => {
2532

2633
impl Triangular_ for $scalar {
27-
fn inv_triangular(l: MatrixLayout, uplo: UPLO, diag: Diag, a: &mut [Self]) -> Result<()> {
34+
unsafe fn inv_triangular(l: MatrixLayout, uplo: UPLO, diag: Diag, a: &mut [Self]) -> Result<()> {
2835
let (n, _) = l.size();
2936
let lda = l.lda();
3037
let info = $trtri(l.lapacke_layout(), uplo as u8, diag as u8, n, a, lda);
3138
into_result(info, ())
3239
}
3340

34-
fn solve_triangular(al: MatrixLayout, bl: MatrixLayout, uplo: UPLO, diag: Diag, a: &[Self], mut b: &mut [Self]) -> Result<()> {
41+
unsafe fn solve_triangular(al: MatrixLayout, bl: MatrixLayout, uplo: UPLO, diag: Diag, a: &[Self], mut b: &mut [Self]) -> Result<()> {
3542
let (n, _) = al.size();
3643
let lda = al.lda();
3744
let (_, nrhs) = bl.size();
3845
let ldb = bl.lda();
39-
println!("al = {:?}", al);
40-
println!("bl = {:?}", bl);
41-
println!("n = {}", n);
42-
println!("lda = {}", lda);
43-
println!("nrhs = {}", nrhs);
44-
println!("ldb = {}", ldb);
4546
let info = $trtrs(al.lapacke_layout(), uplo as u8, Transpose::No as u8, diag as u8, n, nrhs, a, lda, &mut b, ldb);
4647
into_result(info, ())
4748
}

0 commit comments

Comments
 (0)