Hello,
I have been having a lot of trouble figuring out how to write parallel code
in Julia. Consider this toy example of a serial program:
N = 5
for i = 1:N
for j = (i + p):N
println(i * j)
end
end
Now, suppose that "i * j" is an expensive operation, so I want to compute
those values in parallel and print them later. Here is my (failed) attempt
at doing that:
N = 5
tmp = SharedArray(Int, (N))
for i = 1:N
# ----------------------- #
# Compute tmp in parallel #
# ----------------------- #
@sync begin
@everywhere begin
p = myid()
np = nprocs()
for j = (i + p):np:N
tmp[j] = i * j
end
end
end
# --------------------- #
# Consume tmp in serial #
# --------------------- #
for j = (i + 1):N
println(tmp[j])
end
end
So, my idea is to have a shared array with N elements where I store some of
the "i * j" calculations in parallel. Once everyone is finished, I consume
the results and repeat the loop. However, I get an error saying that "i" is
not visible inside the @everywhere block:
julia> nprocs()
4
julia> foo_parallel()
exception on 1: ERROR: i not defined
in anonymous at /home/sigrid/Daniel/Science/VENUS/venus.jl:303
in eval at /build/buildd/julia-0.3.8-docfix/base/sysimg.jl:7
in anonymous at multi.jl:1310
in run_work_thunk at multi.jl:621
in run_work_thunk at multi.jl:630
in anonymous at task.jl:6
... many lines ...
So, apparently the workers in the @everywhere block cannot see outside
variables. What can I do? ... One thing I do not want to do is turn "tmp"
into an N x N matrix. That would make "tmp" very large for large N, and in
my real problem there are other outside variables that I want to use.
Help?
Cheers,
Daniel.