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

Reduction functions with pre-allocated output storage. #6197

Merged
merged 4 commits into from
Mar 18, 2014
Merged

Reduction functions with pre-allocated output storage. #6197

merged 4 commits into from
Mar 18, 2014

Conversation

lindahua
Copy link
Contributor

Inplace reduction functions (including sum!, prod!, maximum!, minimum!, all!, and any!) have been in the Base for a while (without being exported). This PR exports them with a modified interface that is safe & flexible.

The interface is defined as:

sum!(r, A[; init=true])

The function reduces A to r along dimensions determined by the size of r and A. This behavior has been in the Base before this PR. What I modify is to add an init keyword argument, such that when init is true (default), it initializes r properly before doing the reduction, and when init is false, it simply accumulates A to r.

A few downstream packages rely on these functions, so that they can do reduction along certain dimensions without allocating new arrays.

Inplace reduction functions (including sum!, prod!, maximum!, minimum!, all!, and any!) have been within the Base for a while. This PR exports them with a modified interface that is safe & flexible.
@StefanKarpinski
Copy link
Sponsor Member

I have to confess I'm a bit confused about what these functions do. Can you add some documentation and/or just post it here? In-place cumsum and cumprod make sense to me, but I'm not sure this does.

@mbauman
Copy link
Sponsor Member

mbauman commented Mar 18, 2014

It's not really an in-place reduction - I had that thought, too. They just allow you to provide a pre-allocated output buffer, r, to store the output of the reduction of A.

@timholy
Copy link
Sponsor Member

timholy commented Mar 18, 2014

Thanks for doing this. Your implementation looks fine to me.

We'll need documentation too. Right now I'm too busy with other things to contribute towards that, but if this isn't done in a week or two I can help out.

@lindahua
Copy link
Contributor Author

Sorry, this is not in-place. They just allow you to write the results to a pre-allocated storage.

For example,

x = rand(4, 3);
r = zeros(1, 3);
sum!(r, x);    # write sum(x, 1) to a pre-allocated r

@lindahua lindahua changed the title Inplace reduction functions Reduction functions with pre-allocated output storage. Mar 18, 2014
@lindahua
Copy link
Contributor Author

I have changed to title to express the meaning more accurately.

@StefanKarpinski
Copy link
Sponsor Member

Oh, right – yes, that makes perfect sense. Thanks for doing this.

@lindahua
Copy link
Contributor Author

I have added the documentation & news entry. Should be ready to merge when it passes travis.

lindahua added a commit that referenced this pull request Mar 18, 2014
Reduction functions with pre-allocated output storage.
@lindahua lindahua merged commit 14641d3 into JuliaLang:master Mar 18, 2014
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants