Skip to content

Commit 9694de7

Browse files
authored
Optimize remotecall pattern used in distributed loading (#21745)
* Optimize remotecall pattern used in distributed loading
1 parent 4dbfe4b commit 9694de7

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

base/loading.jl

+25-14
Original file line numberDiff line numberDiff line change
@@ -172,19 +172,25 @@ function _require_from_serialized(node::Int, mod::Symbol, path_to_try::String, t
172172
end
173173
restored = _include_from_serialized(content)
174174
isa(restored, Exception) && return restored
175-
others = filter(x -> x != myid(), procs())
176-
refs = Any[
177-
(p, @spawnat(p,
178-
let m = try
179-
_include_from_serialized(content)
180-
catch ex
181-
isa(ex, Exception) ? ex : ErrorException(string(ex))
175+
176+
results = sizehint!(Vector{Tuple{Int,Any}}(), nprocs())
177+
@sync for p in procs()
178+
if p != myid()
179+
@async begin
180+
result = remotecall_fetch(p) do
181+
let m = try
182+
_include_from_serialized(content)
183+
catch ex
184+
isa(ex, Exception) ? ex : ErrorException(string(ex))
185+
end
186+
isa(m, Exception) ? m : nothing
182187
end
183-
isa(m, Exception) ? m : nothing
184-
end))
185-
for p in others ]
186-
for (id, ref) in refs
187-
m = fetch(ref)
188+
end
189+
push!(results, (p, result))
190+
end
191+
end
192+
end
193+
for (id, m) in results
188194
if m !== nothing
189195
warn("Node state is inconsistent: node $id failed to load cache from $path_to_try. Got:")
190196
warn(m, prefix="WARNING: ")
@@ -460,8 +466,13 @@ function _require(mod::Symbol)
460466
eval(Main, :(Base.include_from_node1($path)))
461467

462468
# broadcast top-level import/using from node 1 (only)
463-
refs = Any[ @spawnat p eval(Main, :(Base.include_from_node1($path))) for p in filter(x -> x != 1, procs()) ]
464-
for r in refs; wait(r); end
469+
@sync begin
470+
for p in filter(x -> x != 1, procs())
471+
@async remotecall_fetch(p) do
472+
eval(Main, :(Base.include_from_node1($path); nothing))
473+
end
474+
end
475+
end
465476
else
466477
eval(Main, :(Base.include_from_node1($path)))
467478
end

0 commit comments

Comments
 (0)