Skip to content

Commit df69e16

Browse files
committed
type assertion in reduction
1 parent 8a52f82 commit df69e16

File tree

4 files changed

+446
-306
lines changed

4 files changed

+446
-306
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "ParallelUtilities"
22
uuid = "fad6cfc8-4f83-11e9-06cc-151124046ad0"
33
authors = ["Jishnu Bhattacharya <[email protected]>"]
4-
version = "0.3.3"
4+
version = "0.4.0"
55

66
[deps]
77
Distributed = "8ba89e20-285c-5b6f-9357-94700520ee1b"

README.md

Lines changed: 81 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,56 @@ pkg> add ParallelUtilities
1515
julia> using ParallelUtilities
1616
```
1717

18+
# Exported functions
19+
20+
* `pmap`-related functions
21+
* `pmapreduce`
22+
* `pmapreduce_commutative`
23+
* `pmapsum`
24+
* `pmapreduce_elementwise`
25+
* `pmapsum_elementwise`
26+
* Functions to evenly split a Tuple of ranges
27+
* `evenlyscatterproduct`
28+
* `nworkersactive`
29+
* `workersactive`
30+
* `workerrank`
31+
* `whichproc`
32+
* `procrange_recast`
33+
* `localindex`
34+
* `procid_and_localindex`
35+
* `extremadims`
36+
* `extrema_commonlastdim`
37+
* Utility functions to query the cluster
38+
* `gethostnames`
39+
* `nodenames`
40+
* `nprocs_node`
41+
42+
# Quick start
43+
44+
```julia
45+
julia> addprocs(2)
46+
2-element Array{Int64,1}:
47+
2
48+
3
49+
50+
julia> @everywhere using ParallelUtilities
51+
52+
julia> pmapreduce(x->ones(2).*myid(),x->hcat(x...),1:nworkers())
53+
2×2 Array{Float64,2}:
54+
2.0 3.0
55+
2.0 3.0
56+
57+
julia> pmapreduce_commutative(x->ones(2).*myid(),sum,1:nworkers())
58+
2-element Array{Float64,1}:
59+
5.0
60+
5.0
61+
62+
julia> pmapsum(x->ones(2).*myid(),1:nworkers())
63+
2-element Array{Float64,1}:
64+
5.0
65+
5.0
66+
```
67+
1868
# Usage
1969

2070
The package splits up a collection of ranges into subparts of roughly equal length, so that all the cores are approximately equally loaded. This is best understood using an example: let's say that we have a function `f` that is defined as
@@ -148,6 +198,36 @@ julia> pmapsum(x->ones(2).*myid(),1:nworkers())
148198
5.0
149199
```
150200

201+
It is possible to specify the return types of the map and reduce operations in these functions. If they are not specified they are inferred using `Base.return_types`. To specify the return types use the following variants:
202+
203+
```julia
204+
julia> pmapreduce(x->ones(2).*myid(),Vector{Float64},x->hcat(x...),Matrix{Float64},1:nworkers())
205+
2×2 Array{Float64,2}:
206+
2.0 3.0
207+
2.0 3.0
208+
209+
julia> pmapsum(x->ones(2).*myid(),Vector{Float64},1:nworkers())
210+
2-element Array{Float64,1}:
211+
5.0
212+
5.0
213+
```
214+
215+
Specifying the types would lead to a type coercion if possible, or an error if a conversion is not possible. This might help in asserting the correctness of the result obtained. For example:
216+
217+
```julia
218+
# The result is converted from Vector{Float64} to Vector{Int}.
219+
# Conversion works as the numbers are integers
220+
julia> pmapsum(x->ones(2).*myid(),Vector{Int},1:nworkers())
221+
2-element Array{Int64,1}:
222+
5
223+
5
224+
225+
# Conversion fails here as the numbers aren't integers
226+
julia> pmapsum(x->rand(2),Vector{Int},1:nworkers())
227+
ERROR: On worker 2:
228+
InexactError: Int64(0.7742577217010362)
229+
```
230+
151231
## ProductSplit
152232

153233
In the above examples we have talked about the tasks being distributed approximately equally among the workers without going into details about the distribution, which is what we describe here. The package provides an iterator `ProductSplit` that lists that ranges of parameters that would be passed on to each core. This may equivalently be achieved using an
@@ -183,7 +263,7 @@ julia> Tuple(length(ProductSplit((xrange,yrange,zrange),10,i)) for i=1:10)
183263
(4, 4, 4, 4, 4, 4, 3, 3, 3, 3)
184264
```
185265

186-
The object can be generated through the function `evenlyscatterproduct` using the same signature
266+
The object may be generated through the function `evenlyscatterproduct` using the same signature
187267

188268
```julia
189269
julia> evenlyscatterproduct((xrange,yrange,zrange),10,4)

0 commit comments

Comments
 (0)