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

zip_mut_with 'f' order #754

Merged
merged 3 commits into from
Jan 21, 2020
Merged
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
20 changes: 20 additions & 0 deletions benches/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,23 @@ fn iter_axis_chunks_5_iter_sum(bench: &mut Bencher) {
.sum::<f32>()
});
}

pub fn zip_mut_with(data: &Array3<f32>, out: &mut Array3<f32>) {
out.zip_mut_with(&data, |o, &i| {
*o = i;
});
}

#[bench]
fn zip_mut_with_cc(b: &mut Bencher) {
let data: Array3<f32> = Array3::zeros((ISZ, ISZ, ISZ));
let mut out = Array3::zeros(data.dim());
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
}

#[bench]
fn zip_mut_with_ff(b: &mut Bencher) {
let data: Array3<f32> = Array3::zeros((ISZ, ISZ, ISZ).f());
let mut out = Array3::zeros(data.dim().f());
b.iter(|| black_box(zip_mut_with(&data, &mut out)));
}
21 changes: 11 additions & 10 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::cmp;
use std::ptr as std_ptr;
use std::slice;

Expand Down Expand Up @@ -1917,18 +1916,20 @@ where
F: FnMut(&mut A, &B),
{
debug_assert_eq!(self.shape(), rhs.shape());
if let Some(self_s) = self.as_slice_mut() {
if let Some(rhs_s) = rhs.as_slice() {
let len = cmp::min(self_s.len(), rhs_s.len());
let s = &mut self_s[..len];
let r = &rhs_s[..len];
for i in 0..len {
f(&mut s[i], &r[i]);

// Same shape and order should have same strides
if self.strides() == rhs.strides() {
jturner314 marked this conversation as resolved.
Show resolved Hide resolved
if let Some(self_s) = self.as_slice_memory_order_mut() {
if let Some(rhs_s) = rhs.as_slice_memory_order() {
for (s, r) in self_s.iter_mut().zip(rhs_s) {
f(s, &r);
}
return;
}
return;
}
}
// otherwise, fall back to the outer iter

// Otherwise, fall back to the outer iter
self.zip_mut_with_by_rows(rhs, f);
}

Expand Down