25
25
put! (pipe. selfchannels. out,valT)
26
26
end
27
27
28
- function indicatemapprogress! (:: Nothing ) end
29
- function indicatemapprogress! (progress:: RemoteChannel )
30
- put! (progress,(true ,false ))
28
+ function indicatemapprogress! (:: Nothing ,rank) end
29
+ function indicatemapprogress! (progress:: RemoteChannel ,rank)
30
+ put! (progress,(true ,false ,rank))
31
+ end
32
+
33
+ function indicatefailure! (:: Nothing ,rank) end
34
+ function indicatefailure! (progress:: RemoteChannel ,rank)
35
+ put! (progress,(false ,false ,rank))
31
36
end
32
37
33
38
function mapTreeNode (fmap:: Function ,iterator,rank,pipe:: BranchChannel ,
@@ -40,11 +45,11 @@ function mapTreeNode(fmap::Function,iterator,rank,pipe::BranchChannel,
40
45
res = fmap (iterator,args... ;kwargs... )
41
46
maybepvalput! (pipe,rank,res)
42
47
put! (pipe. selfchannels. err,false )
48
+ indicatemapprogress! (progress,rank)
43
49
catch
44
50
put! (pipe. selfchannels. err,true )
51
+ indicatefailure! (progress,rank)
45
52
rethrow ()
46
- finally
47
- indicatemapprogress! (progress)
48
53
end
49
54
end
50
55
@@ -59,15 +64,25 @@ struct Unsorted <: Ordering end
59
64
function reducedvalue (freduce:: Function ,rank,
60
65
pipe:: BranchChannel{Tmap,Tred} ,:: Unsorted ) where {Tmap,Tred}
61
66
62
- self = take! (pipe. selfchannels. out) :: Tmap
63
-
64
67
N = nchildren (pipe)
65
- res = if N > 0
68
+ if rank > 0
69
+ self = take! (pipe. selfchannels. out) :: Tmap
70
+ if N > 0
66
71
reducechildren = freduce (take! (pipe. childrenchannels. out):: Tred for i= 1 : N):: Tred
67
- freduce ((reducechildren,self)) :: Tred
68
- else
69
- freduce ((self,)) :: Tred
72
+ res = freduce ((reducechildren, self)) :: Tred
73
+ elseif N == 0
74
+ res = freduce ((self,)) :: Tred
75
+ end
76
+ else
77
+ if N > 0
78
+ res = freduce (take! (pipe. childrenchannels. out):: Tred for i= 1 : N):: Tred
79
+ elseif N == 0
80
+ # N == 0 && rank <= 0
81
+ # shouldn't reach this
82
+ error (" nodes with rank <=0 must have children" )
70
83
end
84
+ end
85
+ return res
71
86
end
72
87
73
88
function reducedvalue (freduce:: Function ,rank,
@@ -95,9 +110,9 @@ function reducedvalue(freduce::Function,rank,
95
110
Tred (rank,freduce (value (v) for v in vals))
96
111
end
97
112
98
- function indicatereduceprogress! (:: Nothing ) end
99
- function indicatereduceprogress! (progress:: RemoteChannel )
100
- put! (progress,(false ,true ))
113
+ function indicatereduceprogress! (:: Nothing ,rank ) end
114
+ function indicatereduceprogress! (progress:: RemoteChannel ,rank )
115
+ put! (progress,(false ,true ,rank ))
101
116
end
102
117
103
118
function reduceTreeNode (freduce:: Function ,rank,pipe:: BranchChannel{Tmap,Tred} ,
@@ -106,8 +121,13 @@ function reduceTreeNode(freduce::Function,rank,pipe::BranchChannel{Tmap,Tred},
106
121
107
122
# Start by checking if there is any error locally in the map,
108
123
# and if there's none then check if there are any errors on the children
109
- anyerr = take! (pipe. selfchannels. err) ||
110
- any (take! (pipe. childrenchannels. err) for i= 1 : nchildren (pipe))
124
+ if rank > 0
125
+ anyerr = take! (pipe. selfchannels. err)
126
+ else
127
+ anyerr = false
128
+ end
129
+ anyerr = anyerr ||
130
+ any (take! (pipe. childrenchannels. err) for i= 1 : nchildren (pipe))
111
131
112
132
# Evaluate the reduction only if there's no error
113
133
# In either case push the error flag to the parent
@@ -116,15 +136,15 @@ function reduceTreeNode(freduce::Function,rank,pipe::BranchChannel{Tmap,Tred},
116
136
res = reducedvalue (freduce,rank,pipe,ifsort) :: Tred
117
137
put! (pipe. parentchannels. out,res)
118
138
put! (pipe. parentchannels. err,false )
139
+ indicatereduceprogress! (progress,rank)
119
140
catch e
120
141
put! (pipe. parentchannels. err,true )
142
+ indicatefailure! (progress,rank)
121
143
rethrow ()
122
- finally
123
- indicatereduceprogress! (progress)
124
144
end
125
145
else
126
146
put! (pipe. parentchannels. err,true )
127
- indicatereduceprogress ! (progress)
147
+ indicatefailure ! (progress,rank )
128
148
end
129
149
130
150
finalize (pipe)
@@ -147,40 +167,67 @@ function pmapreduceworkers(fmap::Function,freduce::Function,iterators::Tuple,
147
167
kwargs... )
148
168
149
169
num_workers_active = nworkersactive (iterators)
170
+ Nmaptotal = num_workers_active
171
+ Nreducetotal = length (branches)
172
+ extrareducenodes = Nreducetotal - Nmaptotal
173
+
174
+ Nprogress = Nmaptotal+ Nreducetotal
175
+ progresschannel = RemoteChannel (()-> Channel {Tuple{Bool,Bool,Int}} (
176
+ ifelse (showprogress,Nprogress,0 )))
177
+ progressbar = Progress (Nprogress,1 ,progressdesc)
150
178
151
- nmap,nred = 0 ,0
152
- progresschannel = RemoteChannel (()-> Channel {Tuple{Bool,Bool}} (
153
- ifelse (showprogress,2 num_workers_active,0 )))
154
- progressbar = Progress (2 num_workers_active,1 ,progressdesc)
155
-
156
- # Run the function on each processor and compute the reduction at each node
157
179
@sync begin
158
- for (rank,mypipe) in enumerate (branches)
159
- @async begin
160
- p = mypipe. p
180
+
181
+ for (ind,mypipe) in enumerate (branches)
182
+ p = mypipe. p
183
+ rank = ind - extrareducenodes
184
+ if rank > 0
161
185
iterable_on_proc = ProductSplit (iterators,num_workers_active,rank)
162
186
163
187
@spawnat p mapTreeNode (fmap,iterable_on_proc,rank,mypipe,
164
188
ifelse (showprogress,progresschannel,nothing ),
165
189
args... ;kwargs... )
190
+
191
+ @spawnat p reduceTreeNode (freduce,rank,mypipe,ord,
192
+ ifelse (showprogress,progresschannel,nothing ))
193
+ else
166
194
@spawnat p reduceTreeNode (freduce,rank,mypipe,ord,
167
195
ifelse (showprogress,progresschannel,nothing ))
168
196
end
169
197
end
170
-
198
+
171
199
if showprogress
172
- for i = 1 : 2 num_workers_active
173
- mapdone,reddone = take! (progresschannel)
174
- nmap += mapdone
175
- nred += reddone
176
200
177
- next! (progressbar;showvalues= [(:map ,nmap),(:reduce ,nred)])
201
+ mapdone,reducedone = 0 ,0
202
+
203
+ for i = 1 : Nprogress
204
+ mapflag,redflag,rank = take! (progresschannel)
205
+ # both flags are false in case of an error
206
+ mapflag || redflag || break
207
+
208
+ mapdone += mapflag
209
+ reducedone += redflag
210
+
211
+ if mapdone != Nmaptotal && reducedone != Nreducetotal
212
+ showvalues = [
213
+ (:map ,string (mapdone)* " /" * string (Nmaptotal)),
214
+ (:reduce ,string (reducedone)* " /" * string (Nreducetotal))
215
+ ]
216
+
217
+ elseif reducedone != Nreducetotal
218
+ showvalues = [
219
+ (:reduce ,string (reducedone)* " /" * string (Nreducetotal))
220
+ ]
221
+ else
222
+ showvalues = []
223
+ end
224
+
225
+ next! (progressbar;showvalues= showvalues)
178
226
end
179
- finish! (progressbar)
180
227
end
181
228
end
182
229
183
- return_unless_error (topnode (tree,branches))
230
+ return_unless_error (topbranch (tree,branches))
184
231
end
185
232
186
233
"""
@@ -227,14 +274,6 @@ julia> pmapreduce_commutative(x->ones(2), x->hcat(x...), 1:4)
227
274
1.0 1.0 1.0 1.0
228
275
1.0 1.0 1.0 1.0
229
276
230
- julia> pmapreduce_commutative(x->(sleep(myid());ones(2)), x->hcat(x...), 1:4, showprogress=true, progressdesc="Progress : ")
231
- Progress : 100%|████████████████████████████████████████| Time: 0:00:05
232
- map: 4
233
- reduce: 4
234
- 2×4 Array{Float64,2}:
235
- 1.0 1.0 1.0 1.0
236
- 1.0 1.0 1.0 1.0
237
-
238
277
julia> pmapreduce_commutative(x->ones(2), Vector{Int64}, x->hcat(x...), Matrix{Int64}, 1:4)
239
278
2×4 Array{Int64,2}:
240
279
1 1 1 1
@@ -243,12 +282,12 @@ julia> pmapreduce_commutative(x->ones(2), Vector{Int64}, x->hcat(x...), Matrix{I
243
282
244
283
See also: [`pmapreduce_commutative_elementwise`](@ref), [`pmapreduce`](@ref), [`pmapsum`](@ref)
245
284
"""
246
- function pmapreduce_commutative (fmap:: Function ,:: Type{Tmap} ,
247
- freduce:: Function ,:: Type{Tred} ,iterators:: Tuple ,args... ;
248
- kwargs... ) where {Tmap,Tred}
285
+ function pmapreduce_commutative (fmap:: Function ,Tmap :: Type ,
286
+ freduce:: Function ,Tred :: Type ,iterators:: Tuple ,args... ;
287
+ kwargs... )
249
288
250
289
tree,branches = createbranchchannels (Tmap,Tred,iterators,
251
- SequentialBinaryTree )
290
+ SegmentedSequentialBinaryTree )
252
291
pmapreduceworkers (fmap,freduce,iterators,tree,
253
292
branches,Unsorted (),args... ;kwargs... )
254
293
end
@@ -259,8 +298,9 @@ function pmapreduce_commutative(fmap::Function,freduce::Function,
259
298
pmapreduce_commutative (fmap,Any,freduce,Any,iterators,args... ;kwargs... )
260
299
end
261
300
262
- function pmapreduce_commutative (fmap:: Function ,:: Type{Tmap} ,freduce:: Function ,:: Type{Tred} ,
263
- iterable,args... ;kwargs... ) where {Tmap,Tred}
301
+ function pmapreduce_commutative (fmap:: Function ,Tmap:: Type ,
302
+ freduce:: Function ,Tred:: Type ,
303
+ iterable,args... ;kwargs... )
264
304
pmapreduce_commutative (fmap,Tmap,freduce,Tred,(iterable,),args... ;kwargs... )
265
305
end
266
306
@@ -310,22 +350,16 @@ julia> pmapreduce_commutative_elementwise(x->x^2,prod,1:5)
310
350
julia> pmapreduce_commutative_elementwise((x,y)->x+y,sum,(1:2,1:2))
311
351
12
312
352
313
- julia> pmapreduce_commutative_elementwise(x->(sleep(myid());x^2), prod, 1:5, showprogress=true, progressdesc = "Progress : ")
314
- Progress : 100%|██████████████████████████████████████| Time: 0:00:05
315
- map: 4
316
- reduce: 4
317
- 14400
318
-
319
353
julia> pmapreduce_commutative_elementwise(x->x^2,Int,prod,Float64,1:5)
320
354
14400.0
321
355
```
322
356
323
357
See also: [`pmapsum_commutative_elementwise`](@ref), [`pmapreduce_commutative`](@ref)
324
358
"""
325
- function pmapreduce_commutative_elementwise (fmap:: Function ,:: Type{Tmap} ,
326
- freduce:: Function ,:: Type{Tred} ,iterable,args... ;
359
+ function pmapreduce_commutative_elementwise (fmap:: Function ,Tmap :: Type ,
360
+ freduce:: Function ,Tred :: Type ,iterable,args... ;
327
361
showprogress:: Bool = false , progressdesc = " Progress in pmapreduce : " ,
328
- kwargs... ) where {Tmap,Tred}
362
+ kwargs... )
329
363
330
364
pmapreduce_commutative (
331
365
plist-> freduce ((fmap (x... ,args... ;kwargs... ) for x in plist)),
@@ -380,14 +414,6 @@ julia> pmapsum(x->ones(2), 1:4)
380
414
4.0
381
415
4.0
382
416
383
- julia> pmapsum(x->(sleep(myid());ones(2)), 1:4, showprogress=true, progressdesc = "Progress : ")
384
- Progress : 100%|███████████████████████████████| Time: 0:00:05
385
- map: 4
386
- reduce: 4
387
- 2-element Array{Float64,1}:
388
- 4.0
389
- 4.0
390
-
391
417
julia> pmapsum(x->ones(2), Vector{Int64}, 1:4)
392
418
2-element Array{Int64,1}:
393
419
4
@@ -396,7 +422,7 @@ julia> pmapsum(x->ones(2), Vector{Int64}, 1:4)
396
422
397
423
See also: [`pmapreduce`](@ref), [`pmapreduce_commutative`](@ref)
398
424
"""
399
- function pmapsum (fmap:: Function ,:: Type{T} ,iterable,args... ;kwargs... ) where {T}
425
+ function pmapsum (fmap:: Function ,T :: Type ,iterable,args... ;kwargs... )
400
426
pmapreduce_commutative (fmap,T,sum,T,iterable,args... ;
401
427
progressdesc = " Progress in pmapsum : " ,kwargs... )
402
428
end
@@ -439,21 +465,15 @@ julia> pmapsum_elementwise(x->x^2,1:200)
439
465
julia> pmapsum_elementwise((x,y)-> x+y, (1:5,1:2))
440
466
45
441
467
442
- julia> pmapsum_elementwise(x->(sleep(myid());x^2), 1:5, showprogress=true, progressdesc = "Progress : ")
443
- Progress : 100%|███████████████████████████████████████| Time: 0:00:05
444
- map: 4
445
- reduce: 4
446
- 55
447
-
448
468
julia> pmapsum_elementwise(x->x^2, Float64, 1:5)
449
469
55.0
450
470
```
451
471
452
472
See also: [`pmapreduce_commutative_elementwise`](@ref), [`pmapsum`](@ref)
453
473
"""
454
- function pmapsum_elementwise (fmap:: Function ,:: Type{T} ,iterable,args... ;
474
+ function pmapsum_elementwise (fmap:: Function ,T :: Type ,iterable,args... ;
455
475
showprogress:: Bool = false , progressdesc = " Progress in pmapsum : " ,
456
- kwargs... ) where {T}
476
+ kwargs... )
457
477
458
478
pmapsum (plist-> sum (x-> fmap (x... ,args... ;kwargs... ),plist),T,iterable,
459
479
showprogress = showprogress, progressdesc = progressdesc)
@@ -510,14 +530,6 @@ julia> pmapreduce(x->ones(2).*myid(), x->hcat(x...), 1:4)
510
530
2.0 3.0 4.0 5.0
511
531
2.0 3.0 4.0 5.0
512
532
513
- julia> pmapreduce(x->(sleep(myid());ones(2).*myid()), x->hcat(x...), 1:4, showprogress=true, progressdesc="Progress : ")
514
- Progress : 100%|██████████████████████████████████████| Time: 0:00:05
515
- map: 4
516
- reduce: 4
517
- 2×4 Array{Float64,2}:
518
- 2.0 3.0 4.0 5.0
519
- 2.0 3.0 4.0 5.0
520
-
521
533
julia> pmapreduce(x->ones(2).*myid(), Vector{Int64}, x->hcat(x...), Matrix{Int64}, 1:4)
522
534
2×4 Array{Int64,2}:
523
535
2 3 4 5
@@ -526,8 +538,8 @@ julia> pmapreduce(x->ones(2).*myid(), Vector{Int64}, x->hcat(x...), Matrix{Int64
526
538
527
539
See also: [`pmapreduce_commutative`](@ref), [`pmapsum`](@ref)
528
540
"""
529
- function pmapreduce (fmap:: Function ,:: Type{Tmap} ,freduce:: Function ,:: Type{Tred} ,
530
- iterators:: Tuple ,args... ;kwargs... ) where {Tmap,Tred}
541
+ function pmapreduce (fmap:: Function ,Tmap :: Type ,freduce:: Function ,Tred :: Type ,
542
+ iterators:: Tuple ,args... ;kwargs... )
531
543
532
544
tree,branches = createbranchchannels (pval{Tmap},pval{Tred},
533
545
iterators,OrderedBinaryTree)
@@ -541,8 +553,8 @@ function pmapreduce(fmap::Function,freduce::Function,iterators::Tuple,args...;
541
553
pmapreduce (fmap,Any,freduce,Any,iterators,args... ;kwargs... )
542
554
end
543
555
544
- function pmapreduce (fmap:: Function ,:: Type{Tmap} ,freduce:: Function ,:: Type{Tred} ,
545
- iterable,args... ;kwargs... ) where {Tmap,Tred}
556
+ function pmapreduce (fmap:: Function ,Tmap :: Type ,freduce:: Function ,Tred :: Type ,
557
+ iterable,args... ;kwargs... )
546
558
547
559
pmapreduce (fmap,Tmap,freduce,Tred,(iterable,),args... ;kwargs... )
548
560
end
0 commit comments