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

Improve performance of "no-broadcasting-needed" scenario in &array +&array operation #965

Merged
merged 3 commits into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 26 additions & 2 deletions src/impl_methods.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1804,14 +1804,38 @@ where
E: Dimension,
{
let shape = co_broadcast::<D, E, <D as DimMax<E>>::Output>(&self.dim, &other.dim)?;
if let Some(view1) = self.broadcast(shape.clone()) {
if let Some(view2) = other.broadcast(shape) {
let view1 = if shape.slice() == self.dim.slice() {
self.to_dimensionality::<<D as DimMax<E>>::Output>()
} else {
self.broadcast(shape.clone())
};
let view2 = if shape.slice() == other.dim.slice() {
other.to_dimensionality::<<D as DimMax<E>>::Output>()
} else {
other.broadcast(shape)
};
if let Some(view1) = view1 {
if let Some(view2) = view2 {
return Ok((view1, view2));
}
}
Err(from_kind(ErrorKind::IncompatibleShape))
}

/// Creat an array view from an array with the same shape, but different dimensionality
/// type. Return None if the numbers of axes mismatch.
#[inline]
pub(crate) fn to_dimensionality<D2>(&self) -> Option<ArrayView<'_, A, D2>>
Copy link
Member

Choose a reason for hiding this comment

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

If this method is added, it should be just next to (after) into_dimensionality in this file.

where
D2: Dimension,
S: Data,
{
let dim = <D2>::from_dimension(&self.dim)?;
let strides = <D2>::from_dimension(&self.strides)?;

unsafe { Some(ArrayView::new(self.ptr, dim, strides)) }
}

/// Swap axes `ax` and `bx`.
///
/// This does not move any data, it just adjusts the array’s dimensions
Expand Down
8 changes: 7 additions & 1 deletion src/impl_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ where
{
type Output = Array<A, <D as DimMax<E>>::Output>;
fn $mth(self, rhs: &'a ArrayBase<S2, E>) -> Self::Output {
let (lhs, rhs) = self.broadcast_with(rhs).unwrap();
let (lhs, rhs) = if self.ndim() == rhs.ndim() && self.shape() == rhs.shape() {
let lhs = self.to_dimensionality::<<D as DimMax<E>>::Output>().unwrap();
Copy link
Member

Choose a reason for hiding this comment

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

These to_dimensionality calls seem redundant with the ones already in broadcast_with. Is there a reason this PR shouldn't just do one or the other, not both?

If it was just the calls in this method, here it looks like self.view().into_dimensionality::<...>() is enough, i.e. using the existing method.

Copy link
Contributor Author

@SparrowLii SparrowLii Apr 2, 2021

Choose a reason for hiding this comment

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

That's right. We should use view().into_dimensionality() here. It is faster in the benchmark test (though I don’t know why)

Copy link
Contributor Author

@SparrowLii SparrowLii Apr 2, 2021

Choose a reason for hiding this comment

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

Adding the same shape detection in impl_ops can avoid executing co_broadcast in broadcast_with. I think it is worth to reserve.

let rhs = rhs.to_dimensionality::<<D as DimMax<E>>::Output>().unwrap();
(lhs, rhs)
} else {
self.broadcast_with(rhs).unwrap()
};
Zip::from(&lhs).and(&rhs).map_collect(clone_opf(A::$mth))
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
Zip::from(&lhs).and(&rhs).map_collect(clone_opf(A::$mth))
Zip::from(lhs).and(rhs).map_collect(clone_opf(A::$mth))

I think here we should just consume the views, saves more redundant view creations in Zip (won't be a very noticeable change, maybe the compiler can remove the difference).

Copy link
Contributor Author

@SparrowLii SparrowLii Apr 2, 2021

Choose a reason for hiding this comment

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

OK. It has been correct.

Copy link
Member

Choose a reason for hiding this comment

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

*improved. Cool.

}
}
Expand Down
6 changes: 6 additions & 0 deletions tests/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,12 @@ fn arithmetic_broadcast() {
sa2 + &sb2 + sc2.into_owned(),
arr3(&[[[15, 19], [32, 36]], [[14, 18], [31, 35]]])
);

// Same shape
let a = s.slice(s![..;-1, ..;2, ..]);
let b = s.slice(s![.., ..;2, ..]);
assert_eq!(a.shape(), b.shape());
assert_eq!(&a + &b, arr3(&[[[3, 7], [19, 23]], [[3, 7], [19, 23]]]));
}

#[test]
Expand Down