@@ -245,17 +245,44 @@ dims3 = (2,3,4)
245
245
@benchmark newdims4 ($ as4_2, $ dims3)
246
246
247
247
# ###############
248
- # Tests and example use for varargs mapreduce
249
248
A1 = rand (5 ,5 ,5 ,5 );
250
249
A2 = rand (5 ,5 ,5 ,5 );
251
250
A3 = rand (5 ,5 ,5 ,5 );
252
251
A4 = rand (5 ,5 ,5 ,5 );
253
252
A5 = rand (5 ,5 ,5 ,5 );
254
253
A6 = rand (1 : 10 , 5 ,5 ,5 ,5 );
255
254
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
256
276
@benchmark vvmapreduce (+ , + , zero, as, (1 ,2 ,4 ))
257
277
@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 ))
259
286
vvmapreduce (+ , + , zero, as, (1 ,2 ,4 )) ≈ mapreduce (+ , + , A1, A2, A3, dims= (1 , 2 ,4 ))
260
287
g (x, y, z) = x * y + z
261
288
@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
284
311
@benchmark vvmapreduce (abs2, + , 1 : 10 )
285
312
@benchmark mapreduce (abs2, + , 1 : 10 )
286
313
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
+
287
321
# interface tests
288
322
@benchmark vvmapreduce (* , + , zero, A1, A2, A3)
289
323
@benchmark vvmapreduce (* , + , A1, A2, A3)
@@ -300,11 +334,11 @@ vvmapreduce(+, +, zero, (A1, A4), (2,3,4)) ≈ mapreduce(+, +, A1, A4, dims=(2,3
300
334
@benchmark vvmapreduce (+ , + , A1, A2, A3, A4)
301
335
302
336
# 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)
304
338
@benchmark vvmapreduce ((x,y,z) -> ifelse (x* y+ z ≥ 1 , 1 , 0 ), + , A1, A2, A3, dims= (2 ,3 ,4 ))
305
339
# using ifelse for just a boolean is quite slow, but the above is just for demonstration
306
340
@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)
308
342
@benchmark vvmapreduce ((x,y,z) -> ≥ (x* y+ z, 1 ), + , A1, A2, A3, dims= (2 ,3 ,4 ))
309
343
@benchmark mapreduce ((x,y,z) -> ≥ (x* y+ z, 1 ), + , A1, A2, A3)
310
344
# What I mean by posterior predictive transformation? Well, one might encounter
@@ -317,10 +351,20 @@ vvmapreduce(≥, +, A1, A2) / length(A1)
317
351
# Or, if only the probability is of interest, and we do not wish to use the functionals
318
352
# for any other purpose, we could compute it as:
319
353
vvmapreduce ((x, y) -> ≥ (f (x), f (y)), + , A1, A2)
354
+ mapreduce ((x, y) -> ≥ (f (x), f (y)), + , A1, A2)
320
355
# where `f` is the functional of interest, e.g.
321
356
@benchmark vvmapreduce ((x, y) -> ≥ (abs2 (x), abs2 (y)), + , A1, A2)
322
357
@benchmark vvmapreduce ((x, y) -> ≥ (abs2 (x), abs2 (y)), + , A1, A2, dims= (2 ,3 ,4 ))
323
358
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
+
324
368
# multi-threading examples
325
369
B1 = rand (20 ,20 ,20 ,20 );
326
370
B2 = rand (20 ,20 ,20 ,20 );
@@ -399,8 +443,16 @@ B3 = rand(5,5,5,5);
399
443
bs = (B1, B2, B3);
400
444
@benchmark vfindminmax (+ , > , typemin, bs, :)
401
445
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
+
404
456
@benchmark vfindmax (+ , bs)
405
457
@benchmark vfindmax ((x, y, z) -> x * y + z, B1, B2, B3)
406
458
0 commit comments