Skip to content

Commit

Permalink
refcounting
Browse files Browse the repository at this point in the history
  • Loading branch information
Shashi Gowda committed Jan 29, 2018
1 parent 8a55b52 commit 5c79554
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
38 changes: 38 additions & 0 deletions src/array/darray.jl
Expand Up @@ -99,6 +99,44 @@ mutable struct DArray{T,N,F} <: ArrayOp{T, N}
subdomains::AbstractArray{ArrayDomain{N}, N}
chunks::AbstractArray{Union{Chunk,Thunk}, N}
concat::F
freed::Bool
function DArray{T,N,F}(domain, subdomains, chunks, concat) where {T, N,F}
A = new(domain, subdomains, chunks, concat, false)
refcount_chunks(A.chunks)
finalizer(A, free!)
A
end
end

function refcount_chunks(chunks)
for c in chunks
if c isa Chunk{<:Any, DRef}
# increment refcount on the master node
addrefcount(c.handle, 1)
elseif c isa Thunk
refcount_chunks(c.inputs)
end
end
end

function free_chunks(chunks)
@sync for c in chunks
if c isa Chunk{<:Any, DRef}
# increment refcount on the master node
cnt = addrefcount(c.handle, -1)
cnt <= 0 && @async free!(c.handle)
elseif c isa Thunk
free_chunks(c.inputs)
end
end
end

function free!(x::DArray)
if !x.freed
@schedule Dagger.free_chunks(x.chunks)
x.freed = true
end
nothing
end

# mainly for backwards-compatibility
Expand Down
16 changes: 16 additions & 0 deletions src/chunks.jl
Expand Up @@ -107,6 +107,7 @@ function free!(s::Chunk{X, DRef}; force=true, cache=false) where X
end
end
free!(x; force=true,cache=false) = x # catch-all for non-chunks
free!(x::DRef) = pooldelete(x)

function savechunk(data, dir, f)
sz = open(joinpath(dir, f), "w") do io
Expand All @@ -116,6 +117,21 @@ function savechunk(data, dir, f)
Chunk(typeof(data), domain(data), FileRef(f, sz), true)
end


const refcount = Dict{MemPool.DRef, Int}()

function addrefcount(r::DRef, x)
refcount[r] = getrefcount(r)+x
end

function getrefcount(r::DRef)
if haskey(refcount, r)
return refcount[r]
else
0
end
end

Base.@deprecate_binding AbstractPart Union{Chunk, Thunk}
Base.@deprecate_binding Part Chunk
Base.@deprecate parts(args...) chunks(args...)
Expand Down

0 comments on commit 5c79554

Please sign in to comment.