Julia中的并行编程

Lin*_*don 8 parallel-processing mpi julia

我一直在关注julia中的并行编程的文档,而我的想法就像openMP或MPI一样,我发现设计选择很奇怪.

我有一个应用程序,我希望数据在进程之间分配,然后我想告诉每个进程将一些操作应用于它分配的任何数据,但我没有看到在Julia中这样做的方法.这是一个例子

julia> r = remotecall(2, rand, 2)
RemoteRef{Channel{Any}}(2,1,30)

julia> fetch(r)
2-element Array{Float64,1}:
 0.733308
 0.45227 
Run Code Online (Sandbox Code Playgroud)

所以在进程2中存在一个包含2个元素的随机数组.我可以通过这个数组应用一些函数

julia> remotecall_fetch(2, getindex, r, 1)
0.7333080770447185
Run Code Online (Sandbox Code Playgroud)

但是,如果我应用一个应该改变向量的函数,为什么它不起作用,如:

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
Run Code Online (Sandbox Code Playgroud)

我不太清楚如何描述它,但似乎工人只能回归"新"事物.我不知道如何将一些变量和函数发送给worker并让函数修改变量.在上面的例子中,我希望数组存在于一个进程中,理想情况下我能够告诉该进程在该数组上执行某些操作.完成所有操作后,我可以获取结果等.

Gom*_*ero 4

我认为你可以通过宏来实现这一点@spawnat

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
Run Code Online (Sandbox Code Playgroud)

但是对于remotecall_fetch,看起来返回的数组实际上是一个副本:

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
Run Code Online (Sandbox Code Playgroud)

和:Julia Version 0.4.3