Matthew Bedford
Matthew Bedford

Reputation: 727

Julia - partially shared arrays with @parallel

I want to alter a shared array owned by only some of my processes:

julia> addprocs(4)
4-element Array{Int64,1}:
2
3
4
5

julia> s = SharedArray(Int, (100,), pids=[2,3]);
julia>for i in procs() println(remotecall_fetch(localindexes, i, s)) end
1:0
1:50
51:100
1:0
1:0

This works, but I want to be able to parallelize the loop:

julia> for i=1:100 s[i] = i end

This results in processes 4 and 5 terminating with a segfault:

julia> @parallel for i=1:100 s[i] = i end

Question: Why does this terminate the processes rather than throw an exception or split the loop only among the processes that share the array?

I expected this to work instead, but it does not fill the entire array:

julia> @parallel for i in localindexes(s) s[i] = i end

Each process updates the part of the array that is local to it, and since the array is shared, changes made by one process should be visible to all processes. Why is some of the array still unchanged?

Replacing @parallel with @everywhere gives the error that s is not defined on process 2. How can a process which owns part of the array be unaware of it?

I am so confused. What is the best way to parallelize this loop?

Upvotes: 1

Views: 435

Answers (1)

Kevin L. Keys
Kevin L. Keys

Reputation: 995

Will this do the trick?

@sync begin
    for i in procs(s) # <-- note loop over process IDs of SharedArray s!
        @async @spawnat i setindex!(s, i, localindexes(s))
    end
end

You may run into issues if the master process is called here; in that case, you can try building your own function modeled on the pmap example.

Upvotes: 0

Related Questions