You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Parallel mapreduce and other helpful functions for HPC, meant primarily for embarassingly parallel operations that often require one to split up a list of tasks into subsections that can be processed on individual cores.
where each parameter takes up values in a range, and we would like to sample the entire parameter space. As an example, we choose the ranges to be
77
55
78
56
```julia
79
-
julia> xrange,yrange,zrange =1:3,2:4,3:6# ranges should be strictly increasing
57
+
julia> xrange,yrange,zrange =1:3,2:4,3:6# ranges should be strictly increasing
80
58
```
81
59
82
60
There are a total of 36 possible `(x,y,z)` combinations possible given these ranges. Let's say that we would like to split the evaluation of the function over 10 processors. We describe the simple way to evaluate this and then explain how this is achieved.
@@ -111,33 +89,16 @@ Secondly, the iterator is passed to the function in batches and not elementwise,
111
89
As an example we demonstrate how to evaluate the function `f` for the ranges of parameters listed above:
112
90
113
91
```julia
114
-
julia> p =pmapbatch_elementwise(f,(xrange,yrange,zrange));
92
+
julia> p =pmapbatch_elementwise(f,(xrange,yrange,zrange));
julia> p ==map(f,vec(collect(Iterators.product(xrange,yrange,zrange))))
121
-
true
122
-
123
-
# pmapbatch_elementwise produces the same result as pmap, although the internals are different
124
-
julia>pmapbatch_elementwise(x->x^2,1:3)
125
-
3-element Array{Int64,1}:
126
-
1
127
-
4
128
-
9
129
-
130
-
julia>pmap(x->x^2,1:3)
131
-
3-element Array{Int64,1}:
132
-
1
133
-
4
134
-
9
135
96
```
136
97
137
98
There is also a function `pmapbatch` that deals with batches of parameters that are passed to each processor, and `pmap_elementwise` calls this function under the hood to process the parameters one by one. We may use this directly as well if we need the entire batch for some reason (eg. reading values off a disk, which needs to be done once for the entire set and not for every parameter). As an example we demonstrate how to obtain the same result as above using `pmapbatch`:
138
99
139
100
```julia
140
-
julia> p =pmapbatch(x->[f(i...) for i in x],(xrange,yrange,zrange));
101
+
julia> p =pmapbatch(x->[f(i...) for i in x],(xrange,yrange,zrange));
@@ -149,22 +110,22 @@ Often a parallel execution is followed by a reduction (eg. a sum over the result
149
110
150
111
As an example, to sum up a list of numbers in parallel we may call
151
112
```julia
152
-
julia>pmapsum_elementwise(identity,1:1000)
113
+
julia>pmapsum_elementwise(identity,1:1000)
153
114
500500
154
115
```
155
116
156
117
Here the mapped function is taken to by `identity` which just returns its argument. To sum the squares of the numbers in a list we may use
157
118
158
119
```julia
159
-
julia>pmapsum_elementwise(x->x^2,1:1000)
120
+
julia>pmapsum_elementwise(x->x^2,1:1000)
160
121
333833500
161
122
```
162
123
163
124
We may choose an arbitrary reduction operator in the function `pmapreduce` and `pmapreduce_commutative`, and the elementwise function `pmapreduce_commutative_elementwise`. The reductions are carried out as a binary tree across all workers.
It is possible to specify the return types of the map and reduce operations in these functions. To specify the return types use the following variants:
202
163
203
164
```julia
204
-
# Signature is pmapreduce(fmap,Tmap,freduce,Treduce,iterators)
where `processor_rank` takes up values in `1:number_of_processors`. Note that this is different from MPI where the rank starts from 0. For example, we check the tasks that are passed on to the processor number 4:
Another useful function is `whichproc` that returns the rank of the processor a specific set of parameters will be on, given the total number of processors. This is also computed using a binary search.
0 commit comments