Skip to content

chunks: Allow weak Chunk references in Thunk args #364

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/chunks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,16 @@ function savechunk(data, dir, f)
Chunk{typeof(data),typeof(fr),typeof(proc),typeof(scope)}(typeof(data), domain(data), fr, proc, scope, true)
end

struct WeakChunk
x::WeakRef
end
WeakChunk(c::Chunk) = WeakChunk(WeakRef(c))
unwrap_weak(c::WeakChunk) = c.x.value
function unwrap_weak_checked(c::WeakChunk)
c = unwrap_weak(c)
@assert c !== nothing
return c
end

Base.@deprecate_binding AbstractPart Union{Chunk, Thunk}
Base.@deprecate_binding Part Chunk
Expand Down
2 changes: 1 addition & 1 deletion src/sch/Sch.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Statistics: mean
import Random: randperm

import ..Dagger
import ..Dagger: Context, Processor, Thunk, WeakThunk, ThunkFuture, ThunkFailedException, Chunk, OSProc, AnyScope
import ..Dagger: Context, Processor, Thunk, WeakThunk, ThunkFuture, ThunkFailedException, Chunk, WeakChunk, OSProc, AnyScope
import ..Dagger: order, dependents, noffspring, istask, inputs, unwrap_weak_checked, affinity, tochunk, timespan_start, timespan_finish, procs, move, chunktype, processor, default_enabled, get_processors, get_parent, execute!, rmprocs!, addprocs!, thunk_processor, constrain, cputhreadtime

const OneToMany = Dict{Thunk, Set{Thunk}}
Expand Down
5 changes: 4 additions & 1 deletion src/sch/eager.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ function eager_thunk()
added_future, future, uid, ref, f, args, opts = take!(EAGER_THUNK_CHAN)
# preserve inputs until they enter the scheduler
tid = GC.@preserve args begin
_args = map(x->x isa Dagger.EagerThunk ? ThunkID(EAGER_ID_MAP[x.uid], x.thunk_ref) : x, args)
_args = map(x->x isa Dagger.EagerThunk ? ThunkID(EAGER_ID_MAP[x.uid], x.thunk_ref) :
x isa Dagger.Chunk ? WeakChunk(x) :
x,
args)
add_thunk!(f, h, _args...; future=future, ref=ref, opts...)
end
EAGER_ID_MAP[uid] = tid.id
Expand Down
2 changes: 1 addition & 1 deletion src/sch/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ function reschedule_inputs!(state, thunk, seen=Set{Thunk}())
input in seen && continue

# Unseen
if istask(input) || (input isa Chunk)
if istask(input) || isa(input, Chunk)
push!(get!(()->Set{Thunk}(), state.waiting_data, input), thunk)
end
istask(input) || continue
Expand Down