I am new to Julia and studying Julia parallel computing recently.
I am still not clear about the accurate mechanism of Julia's parallelism including macros \@sync
and \@async
after I read the relevant documents.
The following is the pmap
function from the Julia v0.5 documentation:
function pmap(f, lst)
np = nprocs() # determine the number of processes available
n = length(lst)
results = Vector{Any}(n)
i = 1
# function to produce the next work item from the queue.
# in this case it's just an index.
nextidx() = (idx=i; i+=1; idx)
@sync begin
for p=1:np
if p != myid() || np == 1
@async begin
while true
idx = nextidx()
if idx > n
break
end
results[idx] = remotecall_fetch(f, p, lst[idx])
end
end
end
end
end
results
end
Is it possible for different two processors/workers call nextidx()
at the same time getting the same idx = j
? If yes, I feel results[j]
will be computed twice and result[j+1]
will not be computed.
Thanks very much.
More findings:
function f1()
i=1
nextidx()=(idx=i;sleep(1);i+=1;idx)
for p=1:2
@async begin
idx=nextidx()
println(idx)
end
end
end
f1()
The result is 1 1
.
Through this I find the time periods during which the two tasks call the function nextidx()
could overlap. So I feel that in the first code,
if np = 3
(i.e. two workers), and the length n
of lst
is very large, say 10^8,
it's possible for the tasks to get the same index.
It may happen just because of a coincidence in time, i.e., the two tasks take the expression idx = i
at almost the same time point, so the code is not stable.
Am I right?
\@sync
and\@async
. in thepmap
example that you cite, each process obtainsidx
fromnextidx()
separately from other processes. – Kevin L Keys