Skip to content

Commit 00b7e81

Browse files
committed
Make lapack_traits functions unsafe
1 parent 22cc405 commit 00b7e81

File tree

14 files changed

+71
-66
lines changed

14 files changed

+71
-66
lines changed

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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ 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();
20-
let info = unsafe { $potrf(l.lapacke_layout(), uplo as u8, n, &mut a, n) };
20+
let info = $potrf(l.lapacke_layout(), uplo as u8, n, &mut a, n);
2121
into_result(info, ())
2222
}
2323
}

src/lapack_traits/eigh.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,17 @@ 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];
24-
let info = unsafe { $ev(l.lapacke_layout(), jobz, uplo as u8, n, &mut a, n, &mut w) };
24+
let info = $ev(l.lapacke_layout(), jobz, uplo as u8, n, &mut a, n, &mut w);
2525
into_result(info, w)
2626
}
2727
}

src/lapack_traits/opnorm.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,16 @@ 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 {
35-
MatrixLayout::F((col, lda)) => unsafe { $lange(cm, t as u8, lda, col, a, lda) },
36-
MatrixLayout::C((row, lda)) => unsafe { $lange(cm, t.transpose() as u8, lda, row, a, lda) },
35+
MatrixLayout::F((col, lda)) => $lange(cm, t as u8, lda, col, a, lda),
36+
MatrixLayout::C((row, lda)) => $lange(cm, t.transpose() as u8, lda, row, a, lda),
3737
}
3838
}
3939
}

src/lapack_traits/qr.rs

Lines changed: 8 additions & 8 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];
27-
let info = unsafe { $qrf(l.lapacke_layout(), row, col, &mut a, l.lda(), &mut tau) };
27+
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);
34-
let info = unsafe { $gqr(l.lapacke_layout(), row, k, k, &mut a, l.lda(), &tau) };
34+
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: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,34 +12,34 @@ 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];
28-
let info = unsafe { $getrf(l.lapacke_layout(), row, col, a, l.lda(), &mut ipiv) };
28+
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();
34-
let info = unsafe { $getri(l.lapacke_layout(), n, a, l.lda(), ipiv) };
34+
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;
42-
let info = unsafe { $getrs(l.lapacke_layout(), t as u8, n, nrhs, a, l.lda(), ipiv, b, ldb) };
42+
let info = $getrs(l.lapacke_layout(), t as u8, n, nrhs, a, l.lda(), ipiv, b, ldb);
4343
into_result(info, ())
4444
}
4545
}

src/lapack_traits/svd.rs

Lines changed: 3 additions & 3 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();
@@ -52,7 +52,7 @@ impl SVD_ for $scalar {
5252
};
5353
let mut s = vec![Self::Real::zero(); k as usize];
5454
let mut superb = vec![Self::Real::zero(); (k-2) as usize];
55-
let info = unsafe { $gesvd(l.lapacke_layout(), ju as u8, jvt as u8, m, n, &mut a, lda, &mut s, &mut u, ldu, &mut vt, ldvt, &mut superb) };
55+
let info = $gesvd(l.lapacke_layout(), ju as u8, jvt as u8, m, n, &mut a, lda, &mut s, &mut u, ldu, &mut vt, ldvt, &mut superb);
5656
into_result(info, SVDOutput {
5757
s: s,
5858
u: if ldu > 0 { Some(u) } else { None },

src/lapack_traits/triangular.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,33 +16,34 @@ 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();
30-
let info = unsafe { $trtri(l.lapacke_layout(), uplo as u8, diag as u8, n, a, lda) };
37+
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);
45-
let info = unsafe { $trtrs(al.lapacke_layout(), uplo as u8, Transpose::No as u8, diag as u8, n, nrhs, a, lda, &mut b, ldb) };
46+
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
}
4849
}

src/opnorm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,6 @@ where
4343
fn opnorm(&self, t: NormType) -> Result<Self::Output> {
4444
let l = self.layout()?;
4545
let a = self.as_allocated()?;
46-
Ok(A::opnorm(t, l, a))
46+
Ok(unsafe { A::opnorm(t, l, a) })
4747
}
4848
}

0 commit comments

Comments
 (0)