Skip to content

Commit 5b73f67

Browse files
meggartAlexander-Barthrafaqz
authored
Update #189 (#196)
* optimization for associative reductions (sum, prod, count, all, any, minimum and maximum) * add test for associative reductions * added functional form of reducerds * add test for early stopping * Update src/mapreduce.jl Co-authored-by: Rafael Schouten <[email protected]> --------- Co-authored-by: Alexander Barth <[email protected]> Co-authored-by: Rafael Schouten <[email protected]>
1 parent 19b7044 commit 5b73f67

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/mapreduce.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,3 +47,20 @@ macro implement_mapreduce(t)
4747
end
4848
end
4949
end
50+
51+
52+
# Implementation for special cases and if fallback breaks in future julia versions
53+
54+
for fname in [:sum,:prod,:all,:any,:minimum,:maximum]
55+
@eval function Base.$fname(f::Function, v::AbstractDiskArray)
56+
$fname(eachchunk(v)) do chunk
57+
$fname(f,v[chunk...])
58+
end
59+
end
60+
end
61+
62+
function Base.count(f,v::AbstractDiskArray)
63+
sum(eachchunk(v)) do chunk
64+
count(f,v[chunk...])
65+
end
66+
end

test/runtests.jl

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,9 +121,11 @@ function test_reductions(af)
121121
for f in (
122122
minimum,
123123
maximum,
124+
prod,
124125
sum,
125126
(i, args...; kwargs...) -> all(j -> j > 0.1, i, args...; kwargs...),
126127
(i, args...; kwargs...) -> any(j -> j < 0.1, i, args...; kwargs...),
128+
(i, args...; kwargs...) -> count(j -> j < 0.1, i, args...; kwargs...),
127129
(i, args...; kwargs...) -> mapreduce(x -> 2 * x, +, i, args...; kwargs...),
128130
)
129131
a = af(data)
@@ -336,6 +338,19 @@ import Statistics: mean
336338
@testset "Reductions" begin
337339
a = data -> AccessCountDiskArray(data; chunksize=(5, 4, 2))
338340
test_reductions(a)
341+
342+
@testset "Early stopping for all and any" begin
343+
a = trues(10)
344+
b = falses(10)
345+
da = AccessCountDiskArray(a, chunksize=(2,))
346+
db = AccessCountDiskArray(b, chunksize=(2,))
347+
@test any(da)
348+
@test any(==(true),da)
349+
@test !all(db)
350+
@test !all(==(true),db)
351+
@test getindex_count(da)==2
352+
@test getindex_count(db)==2
353+
end
339354
end
340355

341356
@testset "Broadcast" begin
@@ -919,10 +934,3 @@ end
919934
@test getindex_count(A) == 0
920935
end
921936

922-
923-
924-
# @test offsets == [[1:1,2:3,4:4],[5:5,6:6,7:7],[8:8,9:9]]
925-
# inds = [1,1,1,3,5,6,6,7,10,13,16,16,19,20]
926-
# readranges, offsets = find_subranges_sorted(inds,false)
927-
# @test readranges == [1:1, 3:3, 5:7, 10:10, 13:13, 16:16, 19:20]
928-
# @test offsets == [[1:3], [4:4], [5:5,6:7,8:8], [9:9], [10:10], [11:12], [13:13,14:14]]

0 commit comments

Comments
 (0)