-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New functions: - head_and_tail -- like take and rest but atomic - batchsplit -- like split, but aware of nworkers - generate -- shorthand for creating a Genertor - asyncgenerate -- generate using tasks - asyncmap -- map using tasks - pgenerate -- generate using tasks and workers. Reimplement pmap: pmap(f, c...) = collect(pgenerate(f, c...)) workerpool.jl - add length(pool::WorkerPool) - add remote(p::WorkerPool, f) new pmap.jl (to avoid circular dependancy between type definitions in multi.jl and workerpool.jl)
- Loading branch information
1 parent
f880834
commit 87a0dad
Showing
9 changed files
with
148 additions
and
101 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
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,89 @@ | ||
# This file is a part of Julia. License is MIT: http://julialang.org/license | ||
|
||
|
||
""" | ||
pgenerate([::WorkerPool,] f, c...) -> iterator | ||
Apply `f` to each element of `c` in parallel using available workers and tasks. | ||
For multiple collection arguments, apply f elementwise. | ||
Results are returned by the iterator as they become available. | ||
""" | ||
function pgenerate(p::WorkerPool, f, c) | ||
if length(p) == 0 | ||
return asyncgenerate(f, c) | ||
end | ||
batches = batchsplit(c, min_batch_count = length(p) * 3) | ||
return flatten(asyncgenerate(remote(p, b -> asyncmap(f, b)), batches)) | ||
end | ||
|
||
pgenerate(p::WorkerPool, f, c1, c...) = pgenerate(p, a->f(a...), zip(c1, c...)) | ||
|
||
pgenerate(f, c) = pgenerate(default_worker_pool(), f, c...) | ||
pgenerate(f, c1, c...) = pgenerate(a->f(a...), zip(c1, c...)) | ||
|
||
|
||
|
||
""" | ||
pmap([::WorkerPool,] f, c...) | ||
Transform collection `c` by applying `f` to each element using available | ||
workers and tasks. | ||
For multiple collection arguments, apply f elementwise. | ||
""" | ||
pmap(p::WorkerPool, f, c...) = collect(pgenerate(p, f, c...)) | ||
pmap(f, c...) = pmap(p, f, c...) | ||
|
||
function pmap(f, c...; err_retry=nothing, err_stop=nothing, pids=nothing) | ||
|
||
if err_retry != nothing | ||
depwarn("`err_retry` is deprecated, use `pmap(retry(f), c...)`.", :pmap) | ||
if err_retry == true | ||
f = retry(f) | ||
end | ||
end | ||
|
||
if err_stop != nothing | ||
depwarn("`err_stop` is deprecated, use `pmap(@catch(f), c...).", :pmap) | ||
if err_stop == false | ||
f = @catch(f) | ||
end | ||
end | ||
|
||
if pids == nothing | ||
p = default_worker_pool() | ||
else | ||
depwarn("`pids` is deprecated, use `pmap(::WorkerPool, f, c...).", :pmap) | ||
p = WorkerPool(pids) | ||
end | ||
|
||
return pmap(p, f, c...) | ||
end | ||
|
||
|
||
|
||
""" | ||
batchsplit(c; min_batch_count=0, max_batch_size=100) | ||
Split a collection into at least `min_batch_count` batches. | ||
Equivalent to `split(c, batch_size)` when `length(c) >> max_batch_size`. | ||
""" | ||
function batchsplit(c; min_batch_count=1, max_batch_size=100) | ||
|
||
# FIXME Use @require per https://github.com/JuliaLang/julia/pull/15495 | ||
@assert min_batch_count > 0 | ||
@assert max_batch_size > 1 | ||
|
||
# Split collection into batches, then peek at the first few batches... | ||
batches = split(c, max_batch_size) | ||
head, tail = head_and_tail(batches, min_batch_count) | ||
|
||
# If there are not enough batches, use a smaller batch size... | ||
if length(head) < min_batch_count | ||
head = vcat(head...) | ||
batch_size = max(1, div(length(head), min_batch_count)) | ||
return split(head, batch_size) | ||
end | ||
|
||
return flatten((head, tail)) | ||
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