-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
111 additions
and
34 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using FastIce.Utils | ||
using Profile | ||
|
||
function main() | ||
pipes = [Pipeline() for _ in 1:6] | ||
for iter in 1:100 | ||
for ip in eachindex(pipes) | ||
put!(pipes[ip]) do | ||
sleep(1/60) | ||
# println(" inside pipeline #$(ip)!") | ||
end | ||
end | ||
sleep(1/10) | ||
# println("outside, iter #$(iter)!") | ||
take!.(pipes) | ||
# println() | ||
end | ||
setdone!(pipe) | ||
return | ||
end | ||
|
||
main() |
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,35 @@ | ||
module Utils | ||
|
||
export remove_dim, insert_dim | ||
export Pipeline, setdone! | ||
export extrapolate! | ||
|
||
using FastIce.Fields | ||
using KernelAbstractions | ||
|
||
include("pipelines.jl") | ||
include("extrapolate.jl") | ||
|
||
# Returns a copy of the tuple `A` with element in position `D` removed | ||
@inline remove_dim(::Val{D}, A::NTuple{N}) where {D,N} = ntuple(I -> I < D ? A[I] : A[I+1], Val(N - 1)) | ||
@inline remove_dim(::Val{1}, I::NTuple{1}) = 1 | ||
|
||
# Same as `remove_dim`, but for `CartesianIndex` | ||
@inline remove_dim(dim, I::CartesianIndex) = remove_dim(dim, Tuple(I)) |> CartesianIndex | ||
|
||
# Returns a copy of tuple `A`, but inserts `i` into position `D` | ||
@inline insert_dim(::Val{D}, A::NTuple{N}, i) where {D, N} = ntuple(Val(N + 1)) do I | ||
if I < D | ||
A[I] | ||
elseif I == D | ||
i | ||
else | ||
A[I-1] | ||
end | ||
end | ||
|
||
# Same as `insert_dim`, but for `CartesianIndex` | ||
@inline insert_dim(dim, A::CartesianIndex, i) = insert_dim(dim, Tuple(A), i) |> CartesianIndex | ||
|
||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
mutable struct Pipeline | ||
@atomic done::Bool | ||
in::Channel | ||
out::Base.Event | ||
task::Task | ||
|
||
function Pipeline(; pre=nothing, post=nothing) | ||
in = Channel() | ||
out = Base.Event(true) | ||
this = new(false, in, out) | ||
|
||
this.task = Threads.@spawn begin | ||
isnothing(pre) || pre() | ||
try | ||
while !(@atomic this.done) | ||
work = take!(in) | ||
work() | ||
notify(out) | ||
end | ||
catch err | ||
@atomic this.done = true | ||
rethrow(err) | ||
finally | ||
isnothing(post) || post() | ||
end | ||
end | ||
errormonitor(this.task) | ||
return this | ||
end | ||
end | ||
|
||
setdone!(p::Pipeline) = @atomic p.done = true | ||
|
||
Base.isdone(p::Pipeline) = @atomic p.done | ||
|
||
function Base.put!(work::F, p::Pipeline) where {F} | ||
if !(@atomic p.done) | ||
put!(p.in, work) | ||
else | ||
error("Pipeline is not running") | ||
end | ||
return | ||
end | ||
|
||
function Base.take!(p::Pipeline) | ||
if !(@atomic p.done) | ||
wait(p.out) | ||
else | ||
error("Pipeline is not running") | ||
end | ||
return | ||
end |