Julia上的数据移动(并行)

时间:2017-07-20 05:58:35

标签: parallel-processing julia

我们在count_heads.jl中有一个函数:

function count_hands(n)
    c::Int=0
    for i=1:n
        c+=rand(Bool)
    end
    c
end

我们将julia作为./julia -p 2运行 我们想在不同的流程中计算ab,我们有:

julia> @everywhere include("count_hands.jl")
julia> a=@spawn count_hands(1000000000)
julia> b=@spawn count_hands(1000000000)
julia> fetch(a)+fetch(b)

1:我们如何确定我们在不同的流程中计算ab? 我知道我们可以使用@spawnat代替@spawn并选择进程数,但我看到了这段代码,我想知道我们如何确定这一点。

我们认为它是正确的,并且它们都在不同的过程中进行计算,每个count_hands(1000000000) ab在不同的过程中进行计算,然后它们在过程1中相加这是对的吗?

1 个答案:

答案 0 :(得分:1)

  

我们如何确定我们在不同的过程中计算a和b?

除非您使用@spawnat n并确保nprocs()大于或等于n,并且n不同,否则您无法使用。

  

这是对的吗?

是的,假设您已将@spawnat 1用于a。您可以通过重写函数进行测试,如下所示:

julia> @everywhere function count_hands(n)
           println("this process is $(myid())")
           c::Int=0
           for i=1:n
               c+=rand(Bool)
           end
           c
       end

julia> a = @spawnat 1 count_hands(1000)
this process is 1
Future(1, 1, 11, Nullable{Any}())

julia> b = @spawnat 2 count_hands(1000)
Future(2, 1, 12, Nullable{Any}())

julia>  From worker 2:  this process is 2
julia>

julia> fetch(a) + fetch(b)
1021