Julia 中的共享数组使用 [英] Shared array usage in Julia

查看:20
本文介绍了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.

推荐答案

虽然底层数据是共享给所有worker的,但是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 + 总和(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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆