Julia:使用@parallel for Loop与SharedArray

时间:2018-01-08 00:30:21

标签: for-loop parallel-processing julia

我正在尝试在Julia中学习并行for循环,而我对以下示例代码的结果感到困惑:

addprocs(4)
@everywhere begin
N = 10
V = SharedArray{Int64,1}(N)
@sync @parallel for i = 1:N
    V[i] = i
    println(V[i])
end 
end

通过使用println,我试图确定哪个工作人员经历了哪一步。令人惊讶的是,我从上面的代码得到的是每个工作者都经历了整个迭代,直到最后一个worker(在我的情况下是worker 3)完成for循环:

From worker 4:  1
From worker 4:  2
From worker 4:  3
From worker 5:  1
From worker 5:  2
From worker 5:  3
From worker 5:  4
From worker 5:  5
From worker 5:  6
From worker 5:  4
From worker 5:  5
From worker 5:  6
From worker 2:  7
From worker 2:  8
From worker 2:  4
From worker 2:  5
From worker 2:  6
From worker 2:  9
From worker 2:  10
From worker 4:  1
From worker 4:  2
From worker 2:  7
From worker 2:  8
From worker 3:  9
From worker 5:  4
From worker 5:  5
From worker 5:  6
From worker 3:  10
From worker 3:  7
From worker 3:  8
From worker 3:  7
From worker 3:  8
From worker 3:  9
From worker 3:  10
From worker 2:  4
From worker 2:  5
From worker 2:  6
From worker 3:  7
From worker 3:  8
From worker 4:  3
From worker 4:  9
From worker 4:  10
From worker 4:  1
From worker 4:  2
From worker 4:  3
From worker 4:  1
From worker 4:  2
From worker 4:  3
From worker 5:  9
From worker 5:  10

这不是我对并行for循环的期望,因为我认为工作应该在工人之间分配然后组合在一起,而不是分别为每个工人完成。我的意思是,并行for循环的概念应该是每个工作者只经过2-3步吗?

我的代码有问题还是我误解了并行for循环的概念?

谢谢!

编辑:我刚才意识到与@everywhere有关。在我删除@everywhere后,事情按预期工作。

1 个答案:

答案 0 :(得分:2)

你告诉julia(通过@everywhere)在每个工作者和主持人进程上运行并行循环,因此你以平行的方式从1到10计数5次。 (检查在您输出的输出中,从1到10的每个数字恰好发生5次)

稍微详细一点:首先我们注意到进程总数为nprocs() == 5(一个"主机"和4个工作人员,检查workers())。 @everywhere告诉Julia在每个进程上运行begin end block的内容,因此在我们的示例中为5次。开始结束块的内容是"做一个并行循环,从1到10"计数。这正是发生的事情。

删除@everywhere时,只执行一个并行循环。这样你就可以得到你想要的东西:

julia> N = 10
       V = SharedArray{Int64,1}(N)
       @sync @parallel for i = 1:N
           V[i] = i
           println(V[i])
       end
        From worker 5:  9
        From worker 5:  10
        From worker 3:  4
        From worker 3:  5
        From worker 3:  6
        From worker 2:  1
        From worker 2:  2
        From worker 2:  3

建议阅读:https://docs.julialang.org/en/stable/manual/parallel-computing