Skip to content

Commit 821f983

Browse files
committed
@async sendto, @sync gather
1 parent dcde823 commit 821f983

File tree

2 files changed

+13
-9
lines changed

2 files changed

+13
-9
lines changed

.github/workflows/CI.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
strategy:
1212
matrix:
1313
version:
14-
- '1.5.0'
14+
- '1.11.6'
1515
os:
1616
- ubuntu-latest
1717
- macOS-latest

src/ParallelOperations.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ export
2424
# point-to-point
2525

2626
function sendto(p::Int, expr, data, mod::Module = Main)
27-
@sync @spawnat(p, Core.eval(mod, Expr(:(=), expr, data)))
27+
@async @spawnat(p, Core.eval(mod, Expr(:(=), expr, data)))
2828
end
2929

3030
function sendto(p::Int, mod::Module = Main; args...)
31-
@sync for (nm, val) in args
32-
@spawnat(p, Core.eval(mod, Expr(:(=), nm, val)))
33-
end
31+
data = Dict(nm => val for (nm, val) in args)
32+
@async @spawnat(p, Core.eval(mod, Expr(:(=), :parallel_data, data)))
3433
end
3534

3635
function sendto(p::Int, f::Function, expr, mod::Module = Main; args = ())
@@ -80,13 +79,13 @@ end
8079
# broadcast
8180

8281
function bcast(pids::Array, expr, data, mod::Module = Main)
83-
for p in pids
82+
@sync for p in pids
8483
sendto(p, expr, data, mod)
8584
end
8685
end
8786

8887
function bcast(pids::Array, mod::Module = Main; args...)
89-
for p in pids
88+
@sync for p in pids
9089
sendto(p, mod; args...)
9190
end
9291
end
@@ -113,7 +112,7 @@ end
113112

114113
function scatterto(pids::Array, data::Array, expr, mod::Module = Main)
115114
if length(data) == length(pids)
116-
for i in eachindex(pids)
115+
@sync for i in eachindex(pids)
117116
@inbounds sendto(pids[i], expr, data[i], mod)
118117
end
119118
else
@@ -135,7 +134,12 @@ end
135134
# Gather
136135

137136
function gather(pids::Array, expr, mod::Module = Main)
138-
return [fetch(@spawnat(p, Core.eval(mod, expr))) for p in pids]
137+
# return [fetch(@spawnat(p, Core.eval(mod, expr))) for p in pids]
138+
results = Vector{Any}(undef, length(pids))
139+
@sync for (i, p) in enumerate(pids)
140+
@async results[i] = fetch(@spawnat(p, Core.eval(mod, expr)))
141+
end
142+
return results
139143
end
140144

141145
macro gather(pids, expr, mod::Symbol = :Main)

0 commit comments

Comments
 (0)