[ndarray] inplace operations

Hi all,

I am still newish to Rust and currently trying to do some (hopefully) fast operations with the ndarray crate. Specifically I want to do an inplace addition using the output of a matrix-multiplication (element-wise) of the general form a += b * c.

I know that the AddAssign of the form a += &b should work. But will a += &(&b * &c) generate a new temporary array before addition? Do I have to resort to

Zip::from(&mut a).and(&b).and(&c).for_each(|a, &b, &c| { *a += b * c; });

or what would be the fastest way to achieve this?

Technically, b and c are broadcasted ArrayViews in my case, not sure how relevant this is.

Either way, the multiplication b * c (or &b * &c) will create a temporary array as its output.

If you find that the allocation/deallocation overhead of this is expensive, you can try something like this to allocate just a single temporary array outside of the loop:

let mut product = Array::default(shape);
Zip::from(&mut a).and(&b).and(&c).for_each(|a, b, c| {
    product.clone_from(b);
    product *= c;
    *a += &product;
});

I assumed that when using Zip and for_each the values will be scalars already and therfore the additional product variable to avoid allocations is not necessary anymore. Or am I misunderstanding something here?

As they said...

Their alternative reuses the same variable to hold the product for each multiplication, instead of allocating and dropping a new variable each loop. It may have to reallocate in order to hold a larger value some times, but then that larger reallocation is also reused subsequent times through the loop.

Oh, sorry, I wasn’t familiar with ndarray::Zip and was thinking it was iterating over a collection of arrays.

&b * &c will definitely allocate a new array.

b * &c might not allocate, but it consumes b, so it might not be an option.

If you care about avoiding temporary allocations, your Zip version is good. Or if you are doing this calculation repeatedly, maybe you can do some version where you re-use the same temporary array.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.