Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Distributed.jl-based parallelization of
reduce
- Loading branch information
Showing
10 changed files
with
103 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,8 @@ foldl | |
foreach | ||
mapreduce | ||
reduce | ||
dreduce | ||
dtransduce | ||
eduction | ||
map! | ||
copy! | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
""" | ||
dreduce(step, xform::Transducer, array; [init, simd, basesize, pool]) | ||
Distributed.jl-based parallelization of `reduce`. Input collection must | ||
be indexable. | ||
# Keyword Arguments | ||
- `pool::AbstractWorkerPool`: Passed to `Distributed.remotecall`. | ||
- `basesize::Integer`: A size of chunk in `itr` that is processed by | ||
each worker. Unlike `reduce`, this is `length(itr) ÷ nworkers()` | ||
so that inter-process communication is minimized. A smaller size may | ||
be required when computation time for processing each item can fluctuate | ||
a lot. | ||
- For other keyword arguments, see [`reduce`](@ref). | ||
# Examples | ||
```jldoctest | ||
julia> using Transducers | ||
using Distributed | ||
using BangBang | ||
julia> dreduce(append!!, Map(vcat), 1:3; init=Union{}[]) | ||
3-element Array{Int64,1}: | ||
1 | ||
2 | ||
3 | ||
``` | ||
""" | ||
dreduce(step, xform::Transducer, itr; init=MissingInit(), kwargs...) = | ||
unreduced(dtransduce(xform, Completing(step), init, itr); kwargs...) | ||
|
||
""" | ||
dtransduce(xform::Transducer, step, init, array; [simd, basesize, pool]) | ||
See [`dreduce`](@ref) and [`transduce`](@ref). | ||
""" | ||
function dtransduce( | ||
xform::Transducer, step, init, coll; | ||
simd::SIMDFlag = Val(false), | ||
basesize::Integer = length(coll) ÷ Distributed.nworkers(), | ||
pool::Distributed.AbstractWorkerPool = Distributed.default_worker_pool(), | ||
) | ||
@argcheck basesize > 0 | ||
load_me_everywhere() | ||
rf = maybe_usesimd(Reduction(xform, step), simd) | ||
futures = map(firstindex(coll):basesize:lastindex(coll)) do start | ||
Distributed.remotecall( | ||
foldl_nocomplete, | ||
pool, | ||
rf, | ||
init, | ||
coll[start:min(end, start - 1 + basesize)], | ||
) | ||
end | ||
# TODO: Cancel remote computation when there is a Reduced. | ||
results = map(fetch, futures) | ||
i = findfirst(isreduced, results) | ||
i === nothing || return results[i] | ||
c = foldl(results) do a, b | ||
combine(rf, a, b) | ||
end | ||
return complete(rf, c) | ||
end | ||
|
||
function load_me_everywhere() | ||
pkgid = Base.PkgId(@__MODULE__) | ||
@everywhere Base.require($pkgid) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
module TestDistributedReduce | ||
|
||
include("preamble.jl") | ||
using BangBang | ||
using Distributed | ||
|
||
if get(ENV, "CI", "false") == "true" | ||
addprocs(3) | ||
end | ||
@info "Testing with:" nworkers() | ||
|
||
@testset begin | ||
fname = gensym(:attach_pid) | ||
@everywhere $fname(x) = [(x, getpid())] | ||
fun = getproperty(Main, fname) | ||
|
||
pids = fetch.(map(id -> remotecall(getpid, id), workers())) | ||
xs = 1:10 | ||
ys = dreduce(append!!, Map(fun), xs; init=Union{}[]) | ||
@test first.(ys) == xs | ||
@test Set(last.(ys)) == Set(pids) | ||
end | ||
|
||
end # module |