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

Port usage of uninitialized to build_uninit #287

Merged
merged 1 commit into from
May 27, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion ndarray-linalg/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ rand = "0.8.3"
thiserror = "1.0.24"

[dependencies.ndarray]
version = "0.15.1"
version = "0.15.2"
features = ["blas", "approx", "std"]
default-features = false

Expand Down
37 changes: 16 additions & 21 deletions ndarray-linalg/src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,31 +46,19 @@ where
}
}

fn uninitialized<A, S>(l: MatrixLayout) -> ArrayBase<S, Ix2>
where
A: Copy,
S: DataOwned<Elem = A>,
{
match l {
MatrixLayout::C { row, lda } => unsafe {
ArrayBase::uninitialized((row as usize, lda as usize))
},
MatrixLayout::F { col, lda } => unsafe {
ArrayBase::uninitialized((lda as usize, col as usize).f())
},
}
}

pub fn replicate<A, Sv, So, D>(a: &ArrayBase<Sv, D>) -> ArrayBase<So, D>
where
A: Copy,
Sv: Data<Elem = A>,
So: DataOwned<Elem = A> + DataMut,
D: Dimension,
{
let mut b = unsafe { ArrayBase::uninitialized(a.dim()) };
b.assign(a);
b
unsafe {
let ret = ArrayBase::<So, D>::build_uninit(a.dim(), |view| {
a.assign_to(view);
});
ret.assume_init()
}
}

fn clone_with_layout<A, Si, So>(l: MatrixLayout, a: &ArrayBase<Si, Ix2>) -> ArrayBase<So, Ix2>
Expand All @@ -79,9 +67,16 @@ where
Si: Data<Elem = A>,
So: DataOwned<Elem = A> + DataMut,
{
let mut b = uninitialized(l);
b.assign(a);
b
let shape_builder = match l {
MatrixLayout::C { row, lda } => (row as usize, lda as usize).set_f(false),
MatrixLayout::F { col, lda } => (lda as usize, col as usize).set_f(true),
};
unsafe {
let ret = ArrayBase::<So, _>::build_uninit(shape_builder, |view| {
a.assign_to(view);
});
ret.assume_init()
}
}

pub fn transpose_data<A, S>(a: &mut ArrayBase<S, Ix2>) -> Result<&mut ArrayBase<S, Ix2>>
Expand Down
16 changes: 8 additions & 8 deletions ndarray-linalg/src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,7 @@ where
S2: DataMut<Elem = A> + DataOwned,
{
let av = a.slice(s![..n as isize, ..m as isize]);
let mut a = unsafe { ArrayBase::uninitialized((n, m)) };
a.assign(&av);
a
replicate(&av)
}

fn take_slice_upper<A, S1, S2>(a: &ArrayBase<S1, Ix2>, n: usize, m: usize) -> ArrayBase<S2, Ix2>
Expand All @@ -146,10 +144,12 @@ where
S1: Data<Elem = A>,
S2: DataMut<Elem = A> + DataOwned,
{
let av = a.slice(s![..n as isize, ..m as isize]);
let mut a = unsafe { ArrayBase::uninitialized((n, m)) };
for ((i, j), val) in a.indexed_iter_mut() {
*val = if i <= j { av[(i, j)] } else { A::zero() };
}
let av = a.slice(s![..n, ..m]);
let mut a = replicate(&av);
Zip::indexed(&mut a).for_each(|(i, j), elt| {
if i > j {
*elt = A::zero()
}
});
a
Copy link
Member Author

Choose a reason for hiding this comment

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

This PR has an inefficiency here since zero elements are overwritten after copying (not benchmarked, in some cases probably faster/just as fast because the indexed iterator is slow).

}