Skip to content

Commit a6e89bd

Browse files
Examples used in readme
1 parent dc2ad21 commit a6e89bd

File tree

1 file changed

+58
-6
lines changed

1 file changed

+58
-6
lines changed

src/performance.jl

Lines changed: 58 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -245,17 +245,44 @@ dims3 = (2,3,4)
245245
@benchmark newdims4($as4_2, $dims3)
246246

247247
################
248-
# Tests and example use for varargs mapreduce
249248
A1 = rand(5,5,5,5);
250249
A2 = rand(5,5,5,5);
251250
A3 = rand(5,5,5,5);
252251
A4 = rand(5,5,5,5);
253252
A5 = rand(5,5,5,5);
254253
A6 = rand(1:10, 5,5,5,5);
255254
as = (A1, A2, A3);
255+
################
256+
# Simple tests
257+
@benchmark mapreduce(abs2, +, A1, dims=(1,2,4))
258+
@benchmark vvmapreduce(abs2, +, A1, dims=(1,2,4))
259+
@benchmark prod(A1, dims=1)
260+
@benchmark vvprod(A1, dims=1)
261+
@benchmark extrema(log, A1, dims=(1,2))
262+
@benchmark vvextrema(log, A1, dims=(1,2))
263+
@benchmark extrema(A1, dims=(1,2))
264+
@benchmark vvextrema(A1, dims=(1,2))
265+
266+
# In README
267+
@benchmark mapreduce($abs2, $+, $A1, dims=$(1,2,4))
268+
@benchmark vvmapreduce($abs2, $+, $A1, dims=$(1,2,4))
269+
@benchmark extrema($A1, dims=$(1,2))
270+
@benchmark vvextrema($A1, dims=$(1,2))
271+
@benchmark extrema($A1, dims=$(3,4))
272+
@benchmark vvextrema($A1, dims=$(3,4))
273+
274+
################
275+
# Tests and example use for varargs mapreduce
256276
@benchmark vvmapreduce(+, +, zero, as, (1,2,4))
257277
@benchmark vvmapreduce(+, +, as, dims=(1,2,4), init=zero)
258-
@benchmark mapreduce(+, +, A1, A2, A3, dims=(1, 2,4))
278+
@benchmark mapreduce(+, +, A1, A2, A3, dims=(1,2,4))
279+
@benchmark vvmapreduce(+, +, A1, A2, A3, dims=(1,2,4))
280+
@tullio out[1, 1, i_3, 1] := A1[i_1, i_2, i_3, i_4] + A2[i_1, i_2, i_3, i_4] + A3[i_1, i_2, i_3, i_4]
281+
@benchmark mapreduce($+, $+, $A1, $A2, $A3, $A4, dims=$(1,2,4))
282+
@benchmark vvmapreduce($+, $+, $A1, $A2, $A3, $A4, dims=$(1,2,4))
283+
h(w, x, y, z) = w * x + y * z
284+
@benchmark mapreduce($h, $+, $A1, $A2, $A3, $A4, dims=$(1,2,4))
285+
@benchmark vvmapreduce($h, $+, $A1, $A2, $A3, $A4, dims=$(1,2,4))
259286
vvmapreduce(+, +, zero, as, (1,2,4)) mapreduce(+, +, A1, A2, A3, dims=(1, 2,4))
260287
g(x, y, z) = x * y + z
261288
@benchmark vvmapreduce(g, +, zero, as, (1,2,4))
@@ -284,6 +311,13 @@ vvmapreduce(+, +, zero, (A1, A4), (2,3,4)) ≈ mapreduce(+, +, A1, A4, dims=(2,3
284311
@benchmark vvmapreduce(abs2, +, 1:10)
285312
@benchmark mapreduce(abs2, +, 1:10)
286313

314+
@benchmark mapreduce($h, $+, $1:10, $11:20, $21:30, $31:40)
315+
@benchmark vvmapreduce($h, $+, $1:10, $11:20, $21:30, $31:40)
316+
@benchmark mapreduce($h, $+, $1:100, $101:200, $201:300, $301:400)
317+
@benchmark vvmapreduce($h, $+, $1:100, $101:200, $201:300, $301:400)
318+
@benchmark mapreduce($h, $+, $1:1000, $1001:2000, $2001:3000, $3001:4000)
319+
@benchmark vvmapreduce($h, $+, $1:1000, $1001:2000, $2001:3000, $3001:4000)
320+
287321
# interface tests
288322
@benchmark vvmapreduce(*, +, zero, A1, A2, A3)
289323
@benchmark vvmapreduce(*, +, A1, A2, A3)
@@ -300,11 +334,11 @@ vvmapreduce(+, +, zero, (A1, A4), (2,3,4)) ≈ mapreduce(+, +, A1, A4, dims=(2,3
300334
@benchmark vvmapreduce(+, +, A1, A2, A3, A4)
301335

302336
# And for really strange stuff (e.g. posterior predictive transformations)
303-
@benchmark vvmapreduce((x,y,z) -> ifelse(x*y+z 1, 1, 0), +, A1, A2, A3)
337+
@benchmark vvmapreduce((x,y,z) -> ifelse(x*y+z 1, 1, 0), +, $A1, $A2, $A3)
304338
@benchmark vvmapreduce((x,y,z) -> ifelse(x*y+z 1, 1, 0), +, A1, A2, A3, dims=(2,3,4))
305339
# using ifelse for just a boolean is quite slow, but the above is just for demonstration
306340
@benchmark vvmapreduce(, +, A1, A2)
307-
@benchmark vvmapreduce((x,y,z) -> (x*y+z, 1), +, A1, A2, A3)
341+
@benchmark vvmapreduce((x,y,z) -> (x*y+z, 1), +, $A1, $A2, $A3)
308342
@benchmark vvmapreduce((x,y,z) -> (x*y+z, 1), +, A1, A2, A3, dims=(2,3,4))
309343
@benchmark mapreduce((x,y,z) -> (x*y+z, 1), +, A1, A2, A3)
310344
# What I mean by posterior predictive transformation? Well, one might encounter
@@ -317,10 +351,20 @@ vvmapreduce(≥, +, A1, A2) / length(A1)
317351
# Or, if only the probability is of interest, and we do not wish to use the functionals
318352
# for any other purpose, we could compute it as:
319353
vvmapreduce((x, y) -> (f(x), f(y)), +, A1, A2)
354+
mapreduce((x, y) -> (f(x), f(y)), +, A1, A2)
320355
# where `f` is the functional of interest, e.g.
321356
@benchmark vvmapreduce((x, y) -> (abs2(x), abs2(y)), +, A1, A2)
322357
@benchmark vvmapreduce((x, y) -> (abs2(x), abs2(y)), +, A1, A2, dims=(2,3,4))
323358

359+
# One can also express commonly encountered reductions with ease;
360+
# these will be fused once a post-reduction operator can be specified
361+
# MSE
362+
@benchmark vvmapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)) ./ (size(A1, 2) * size(A1, 4) )
363+
@benchmark mapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)) ./ (size(A1, 2) * size(A1, 4) )
364+
# Euclidean distance
365+
B = ().(vvmapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)))
366+
@test B ().(mapreduce((x, y) -> abs2(x - y), +, A1, A2, dims=(2,4)))
367+
324368
# multi-threading examples
325369
B1 = rand(20,20,20,20);
326370
B2 = rand(20,20,20,20);
@@ -399,8 +443,16 @@ B3 = rand(5,5,5,5);
399443
bs = (B1, B2, B3);
400444
@benchmark vfindminmax(+, >, typemin, bs, :)
401445
B′ = @. B1 + B2 + B3;
402-
findmax(B′)
403-
@benchmark vfindmax(+, B1, B2, B3)
446+
findmin(B′) == vfindmin(+, B1, B2, B3)
447+
@benchmark findmin(@. $B1 + $B2 + $B3)
448+
@benchmark vfindmin(+, $B1, $B2, $B3)
449+
450+
@benchmark findmin((@. $B1 + $B2 + $B3), dims=(2,4))
451+
@benchmark vfindmin(+, $B1, $B2, $B3, dims=(2,4))
452+
453+
@benchmark findmin((@. abs2($B1) * $B2 + $B3), dims=$(3,4))
454+
@benchmark vfindmin((x, y, z) -> abs2(x) * y + z, $B1, $B2, $B3, dims=$(3,4))
455+
404456
@benchmark vfindmax(+, bs)
405457
@benchmark vfindmax((x, y, z) -> x * y + z, B1, B2, B3)
406458

0 commit comments

Comments
 (0)