Parallel Programming in Julia

I follow the docs for concurrent programming in julia, and for my mind, which thinks like openMP or MPI, I find the design choice pretty strange.

I have an application in which I want the data to be distributed between processes, and then I want to tell each process to apply some operation to any data that is assigned to it, but I don’t see a way to do this in Julia. Here is an example

julia> r = remotecall(2, rand, 2) RemoteRef{Channel{Any}}(2,1,30) julia> fetch(r) 2-element Array{Float64,1}: 0.733308 0.45227 

so process 2 lives in a random array with 2 elements. I can apply some function to this array through

 julia> remotecall_fetch(2, getindex, r, 1) 0.7333080770447185 

but why it doesn’t work if I use a function that should change the vector, like

 julia> remotecall_fetch(2, setindex!, r, 1,1) ERROR: On worker 2: MethodError: `setindex!` has no method matching setindex!(::RemoteRef{Channel{Any}}, ::Int64, ::Int64) in anonymous at multi.jl:892 in run_work_thunk at multi.jl:645 [inlined code] from multi.jl:892 in anonymous at task.jl:63 in remotecall_fetch at multi.jl:731 in remotecall_fetch at multi.jl:734 

I don’t quite know how to describe it, but it seems that workers can only return β€œnew” things. I do not see how I can send some variables and a function to the worker, and the function will change the variables in place. In the above example, I would like the array to work in one process, and ideally, I could say that this process performs some operations on this array. After completing all the operations, I could get the results, etc.

+7
parallel-processing mpi julia-lang
source share
2 answers

I think you can achieve this with the @spawnat macro:

 julia> addprocs(2) 2-element Array{Int64,1}: 2 3 julia> r = remotecall(2, rand, 2) RemoteRef{Channel{Any}}(2,1,3) julia> fetch(r) 2-element Array{Float64,1}: 0.149753 0.687653 julia> remotecall_fetch(2, getindex, r, 1) 0.14975250913699378 julia> @spawnat 2 setindex!(fetch(r), 320.0, 1) RemoteRef{Channel{Any}}(2,1,6) julia> fetch(r) 2-element Array{Float64,1}: 320.0 0.687653 julia> @spawnat 2 setindex!(fetch(r), 950.0, 2) RemoteRef{Channel{Any}}(2,1,8) julia> fetch(r) 2-element Array{Float64,1}: 320.0 950.0 

But with remotecall_fetch , it looks like the returned array is really a copy:

 julia> remotecall_fetch(2, setindex!, fetch(r), 878.99, 1) 2-element Array{Float64,1}: 878.99 950.0 julia> remotecall_fetch(2, setindex!, fetch(r), 232.99, 2) 2-element Array{Float64,1}: 320.0 232.99 julia> fetch(r) 2-element Array{Float64,1}: 320.0 950.0 

using: Julia Version 0.4.3

+4
source share

You may find Distributed Arrays useful based on a description of your need.

-one
source share

All Articles