-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for forcing the GC to run when using Distributed
- Loading branch information
Showing
6 changed files
with
98 additions
and
17 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,61 @@ | ||
distribute(term) = @spawnat(:any, term) | ||
distribute(term::Future) = term | ||
distribute(term::MPO) = distribute(ProjMPO(term)) | ||
|
||
# Functionality for distributed terms of a sum | ||
# onto seperate processes | ||
function distribute(terms::Vector) | ||
return map(term -> @spawnat(:any, term), terms) | ||
distribute(terms::Vector) = distribute.(terms) | ||
|
||
function DistributedSum(terms::Vector; executor_kwargs...) | ||
return FoldsSum(distribute(terms), SequentialEx(; executor_kwargs...)) | ||
end | ||
|
||
distribute(terms::Vector{Future}) = terms | ||
(term::Future)(v::ITensor) = product(term, v) | ||
|
||
function distribute(terms::Vector{MPO}) | ||
return distribute(ProjMPO.(terms)) | ||
function product(term::Future, v::ITensor) | ||
return product(() -> force_gc(), term, v) | ||
end | ||
|
||
function DistributedSum(terms::Vector; executor_kwargs...) | ||
return FoldsSum(distribute(terms), SequentialEx(; executor_kwargs...)) | ||
function product(callback, term::Future, v::ITensor) | ||
return @fetchfrom term.where begin | ||
res = fetch(term)(v) | ||
callback() | ||
return res | ||
end | ||
end | ||
|
||
function position!(term::Future, v::MPS, pos::Int) | ||
return @spawnat term.where position!(fetch(term), v, pos) | ||
return position!(() -> force_gc(), term, v, pos) | ||
end | ||
|
||
function product(term::Future, v::ITensor) | ||
return @fetchfrom term.where fetch(term)(v) | ||
function position!(callback, term::Future, v::MPS, pos::Int) | ||
return @spawnat term.where begin | ||
res = position!(fetch(term), v, pos) | ||
callback() | ||
return res | ||
end | ||
end | ||
(term::Future)(v::ITensor) = product(term, v) | ||
|
||
function noiseterm(term::Future, v::ITensor, dir::String) | ||
return @fetchfrom term.where noiseterm(fetch(term), v, dir) | ||
return noiseterm(() -> force_gc(), term, v, dir) | ||
end | ||
|
||
function noiseterm(callback, term::Future, v::ITensor, dir::String) | ||
return @fetchfrom term.where begin | ||
res = noiseterm(fetch(term), v, dir) | ||
callback() | ||
return res | ||
end | ||
end | ||
|
||
function disk(term::Future; disk_kwargs...) | ||
return @spawnat term.where disk(fetch(term); disk_kwargs...) | ||
return disk(() -> force_gc(), term; disk_kwargs...) | ||
end | ||
|
||
function disk(callback, term::Future; disk_kwargs...) | ||
return @spawnat term.where begin | ||
res = disk(fetch(term); disk_kwargs...) | ||
callback() | ||
return res | ||
end | ||
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,16 @@ | ||
# Default to 6 GB threshold to trigger GC | ||
default_gc_gb_threshold() = 6.0 | ||
const gc_gb_threshold = Ref(default_gc_gb_threshold()) | ||
get_gc_gb_threshold() = gc_gb_threshold[] | ||
function set_gc_gb_threshold!(gb_threshold) | ||
gc_gb_threshold[] = gb_threshold | ||
return nothing | ||
end | ||
|
||
# https://discourse.julialang.org/t/from-multithreading-to-distributed/101984/6 | ||
function force_gc(gb_threshold::Real=get_gc_gb_threshold()) | ||
if Sys.free_memory() < gb_threshold * 2^30 | ||
GC.gc() | ||
end | ||
return nothing | ||
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