-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add pipeline interface and remove threading
A pipeline objects holds references to the channels and tasks involved in the execution. It further holds the configurations of each stage of the pipeline. Addresses #18 Opens #1 See https://github.com/JuliaLang/julia/issues/37706
- Loading branch information
1 parent
911cdec
commit 9799f38
Showing
13 changed files
with
278 additions
and
148 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,35 @@ | ||
module ParaReal | ||
|
||
import DiffEqBase | ||
import Base.Threads, | ||
DiffEqBase, | ||
Distributed | ||
|
||
using Base.Iterators: countfrom, repeat | ||
using Base.Threads: nthreads, @threads | ||
using Distributed: Future, | ||
RemoteChannel, | ||
procs, | ||
remotecall, | ||
workers, | ||
@fetchfrom, | ||
@spawnat | ||
using LinearAlgebra: norm | ||
using UnPack: @unpack | ||
|
||
export ParaRealAlgorithm | ||
|
||
const T = Base.Threads | ||
const D = Distributed | ||
|
||
include("types.jl") | ||
include("stages.jl") | ||
include("pipeline.jl") | ||
|
||
include("solution.jl") | ||
include("problem.jl") | ||
|
||
include("solve.jl") | ||
include("worker.jl") | ||
|
||
include("utils.jl") | ||
include("compat.jl") | ||
|
||
export ParaRealAlgorithm | ||
|
||
end # module |
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,84 @@ | ||
function init_pipeline(workers::Vector{Int}) | ||
conns = map(RemoteChannel, workers) | ||
nsteps = length(workers) | ||
results = RemoteChannel(() -> Channel(nsteps)) | ||
configs = Vector{StageConfig}(undef, nsteps) | ||
|
||
# Initialize first stages: | ||
for i in 1:nsteps-1 | ||
prev = conns[i] | ||
next = conns[i+1] | ||
configs[i] = StageConfig(step=i, | ||
nsteps=nsteps, | ||
prev=prev, | ||
next=next, | ||
results=results) | ||
end | ||
|
||
# Initialize final stage: | ||
prev = next = conns[nsteps] | ||
# Pass a `::ValueChannel` instead of `nothing` as to not trigger another | ||
# compilation. The value of `next` will never be accessed anyway. | ||
configs[nsteps] = StageConfig(step=nsteps, | ||
nsteps=nsteps, | ||
prev=prev, | ||
next=next, | ||
results=results) | ||
|
||
Pipeline(conns=conns, | ||
results=results, | ||
workers=workers, | ||
configs=configs) | ||
end | ||
|
||
function start_pipeline!(pipeline::Pipeline, prob, alg; kwargs...) | ||
is_pipeline_started(pipeline) && error("Pipeline already started") | ||
@unpack workers, configs = pipeline | ||
tasks = map(workers, configs) do w, c | ||
D.@spawnat w execute_stage(prob, alg, c; kwargs...) | ||
end | ||
pipeline.tasks = tasks | ||
nothing | ||
end | ||
|
||
function send_initial_value(pipeline::Pipeline, prob) | ||
u0 = initialvalue(prob) | ||
c = first(pipeline.conns) | ||
put!(c, u0) | ||
close(c) | ||
nothing | ||
end | ||
|
||
""" | ||
is_pipeline_started(pl::Pipeline) -> Bool | ||
Determine whether the stages of a pipeline have been started executing. | ||
""" | ||
is_pipeline_started(pl::Pipeline) = pl.tasks !== nothing | ||
|
||
""" | ||
is_pipeline_done(pl::Pipeline) -> Bool | ||
Determine whether all the stages of a pipeline have exited. | ||
""" | ||
is_pipeline_done(pl::Pipeline) = is_pipeline_started(pl) && all(isready, pl.tasks) | ||
|
||
""" | ||
is_pipeline_failed(pl::Pipeline) -> Bool) | ||
Determine whether some stage of a pipeline has exited because an exception was thrown. | ||
""" | ||
function is_pipeline_failed(pl::Pipeline) | ||
is_pipeline_started(pl) || return false | ||
@unpack tasks = pl | ||
for t in tasks | ||
isready(t) || continue | ||
try | ||
# should return nothing: | ||
fetch(t) | ||
catch | ||
return true | ||
end | ||
end | ||
return false | ||
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
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
Oops, something went wrong.