I adapted the pmap () implementation for my program to do some planning, and I have a question about the scope of variables in tasks. This is the implementation of Julia
function pmap(f, lst)
np = nprocs()
n = length(lst)
results = cell(n)
i = 1
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(p, f, lst[idx])
end
end
end
end
end
results
end
If I replaced the line idx = nextidx () with idx = x; i = i + 1 ;, each task updates its local copy of variable i. However, the variable i inside the nextidx () function is shared by all tasks. Why is this?
source
share