Julia中的共享数组用法 [英] Shared array usage in Julia
问题描述
我需要在多个工作人员上并行执行一项特定任务. 为此,我需要所有工作人员都可以访问存储数据的矩阵.
I need to parallelise a certain task over a number of workers. To that purpose I need all workers to have access to a matrix that stores the data.
我认为可以将数据矩阵实现为共享数组,以最大程度地减少数据移动.
I thought that the data matrix could be implemented as a Shared Array in order to minimise data movement.
为了让我开始使用共享数组,我正在尝试以下非常简单的示例,该示例使我想到意外的行为:
In order to get me started with Shared Arrays, I am trying the following very simple example which gives me, what I think is, unexpected behaviour:
julia -p 2
# the data matrix
D = SharedArray(Float64, 2, 3)
# initialise the data matrix with dummy values
for ii=1:length(D)
D[ii] = rand()
end
# Define some kind of dummy computation involving the shared array
f = x -> x + sum(D)
# call function on worker
@time fetch(@spawnat 2 f(1.0))
最后一条命令给我以下错误:
The last command gives me the following error:
ERROR: On worker 2:
UndefVarError: D not defined
in anonymous at none:1
in anonymous at multi.jl:1358
in anonymous at multi.jl:904
in run_work_thunk at multi.jl:645
in run_work_thunk at multi.jl:654
in anonymous at task.jl:58
in remotecall_fetch at multi.jl:731
in call_on_owner at multi.jl:777
in fetch at multi.jl:795
我认为共享阵列D对所有工作人员都应该可见? 我显然缺少基本的东西.预先感谢.
I thought that the Shared Array D should be visible to all workers? I am clearly missing something basic. Thanks in advance.
推荐答案
尽管基础数据已共享给所有工作程序,但D
的声明却没有.您仍然需要传递对D的引用,所以类似
Although the underlying data is shared to all workers, the declaration of D
is not. You will still need to pass in the reference to D, so something like
f = (x,SA) -> x + sum(SA)
@time fetch(@spawnat 2 f(1.0,D))
f = (x,SA) -> x + sum(SA)
@time fetch(@spawnat 2 f(1.0,D))
应该工作.您可以在主流程上更改D并使用相同的数据查看它是否确实存在:
should work. You can change D on the main process and see that it is infact using the same data:
julia> # call function on worker
@time fetch(@spawnat 2 f(1.0,D))
0.325254 seconds (225.62 k allocations: 9.701 MB, 5.88% gc time)
4.405613684678047
julia> D[1] += 1
1.2005544517241717
julia> # call function on worker
@time fetch(@spawnat 2 f(1.0,D))
0.004548 seconds (637 allocations: 45.490 KB)
5.405613684678047
这篇关于Julia中的共享数组用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!