Julia:在并行上下文中为worker创建局部变量

时间:2014-07-23 07:57:43

标签: parallel-processing julia

在worker只需要存储非共享数据的情况下使用DistributedArrays似乎过于复杂。我想做

r=remotecall(2,a=Float64[])
remotecall(2,setindex!,a,5,10) #Error

r=remotecall(2,zeros,10)
remotecall(2,setindex!,r,5,10) #Error.

我想为每个worker执行此操作,然后在异步上下文中访问该数组。执行一些计算,然后获取结果。由于let behavior of async

,我不确定这是否可行

下面我做了一个简化的例子,我修改了the pmap example form the docs。 Ť

times=linspace(0.1,2.0,10) # times in secs representing different difficult computations
sort!(times,rev=true)

np = nprocs()
n = length(times)

#create local variables
for p=1:np
    if p != myid() || np == 1
        remotecall(p,stack = Float64p[]) #does not work
    end
end

@everywhere function fun(s)
    mid=myid()
    sleep(s)
    #s represents some computation save to local stack
    push!(stack,s)
end




#asynchronously do the computations
@everywhere i = 1
function nextidx()
    global i
    idx=i;
    i+=1;
    return idx;
end
@sync begin
    for p=1:np
        if p != myid() || np == 1
            @async begin
                j=1
                res=zeros(40);
                while true
                    idx = nextidx()
                    if idx > n
                        break
                    end
                    remotecall(fun, times[idx])
                end
            end
        end
    end
end

# collect the results of the computations
for p=1:np
    if p != myid() || np == 1
        tmpStack=fetch(p,stack)
        #do someting with the results
    end
end

1 个答案:

答案 0 :(得分:2)

当你修改工人的全局变量时使用'global'(例如,由@everywhere a = 3设置),你可以解决你的问题。查看下面的示例代码。

@everywhere a = 0
remotecall_fetch(2, ()->a)  # print 0

@everywhere function change_a(b)
    global a
    a = b
end

b = 10
remotecall_fetch(2, change_a, b) 
remotecall_fetch(2, ()->a)  # print 10