-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
implement count
using mapreduce
#34048
Conversation
|
43b93ac
to
1786182
Compare
1786182
to
90f7561
Compare
added compat annotations and tests. |
90f7561
to
304116d
Compare
buildbot failures are unrelated |
88cb5df
to
f976b9a
Compare
Would it make sense for @tkf to review since he overhauled the mapreduce machinery recently? |
Actually, I tweaked only |
f976b9a
to
658f631
Compare
658f631
to
d3dd296
Compare
d3dd296
to
f0156c5
Compare
this seems ready, freebsd test failure looks unrelated |
f0156c5
to
3a3b4f4
Compare
3a3b4f4
to
5fcc4cd
Compare
base/reducedim.jl
Outdated
count(A::AbstractArray; dims=:) = count(identity, A, dims=dims) | ||
count(f, A::AbstractArray; dims=:) = mapreduce(_bool(f), add_sum, A, dims=dims, init=0) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'd be nice to use A::AbstractArrayOrBroadcasted
instead (with some tests).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, I changed it. You already added a test for Broadcasted
and count()
in 2f90dde.
Do you still feel we need more?
This creates the same calling interface for `count` as for other mapreduce-type functions like e.g. `sum`, namely allowing the `dims` keyword. The implementation itself is shorter than before without sacrificing performance. More detailed documentation for `count` was added too.
d453af0
to
9439134
Compare
test fail on |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM. Re-running failed CI: https://build.julialang.org/#/builders/28/builds/10356
I am not quite sure how this is possible, given the minimal code change of this PR, but using LinearAlgebra, Test
X = [randn(n,n) for n = 1:5];
f(iter, init) = mapreduce(sum, +, iter; init = init);
g(iter, init) = mapreduce(norm, +, iter; init = init);
@inferred f(X, 0.)
@inferred g(X, 0.)
using LinearAlgebra, Test
X = [randn(n,n) for n = 1:5]
f1(iter, init) = reduce(+, sum(b) for b in iter; init = init);
f2(iter, init) = mapreduce(sum, +, iter; init = init);
g1(iter, init) = reduce(+, norm(b) for b in iter; init = init);
g2(iter, init) = mapreduce(norm, +, iter; init = init);
h1(iter, p, init) = reduce(+, norm(b,p) for b in iter; init = init);
h2(iter, p, init) = mapreduce(b->norm(b,p), +, iter; init = init);
@inferred f1(X, 0.)
@inferred f2(X, 0.)
@inferred g1(X, 0.)
@inferred g2(X, 0.)
@inferred h1(X, 2, 0.)
@inferred h2(X, 2, 0.)
So I guess the |
I expect the lines
in norm in |
It seems to work if you add type assertions:
Also for me only |
Regarding failing and working, it is the one which is inferred firstly that fails. So if you change the order of the |
Maybe we should use
Yeah, I agree. Maybe some kind of caching bug? I think it is worth opening a dedicated issue. |
This creates the same calling interface for
count
as for e.g.sum
,namely allowing the
dims
keyword.The implementation is also shorter than before without sacrificing
performance.
mapreduce
withadd_sum
may even yield performance benefits throughchunking, though this was not observed in simple tests.