Parallel processing Julia:在并行上下文中为worker创建局部变量
在工作人员只需要存储非共享数据的情况下使用DistributedArray似乎过于复杂。我想做什么Parallel processing Julia:在并行上下文中为worker创建局部变量,parallel-processing,julia,Parallel Processing,Julia,在工作人员只需要存储非共享数据的情况下使用DistributedArray似乎过于复杂。我想做什么 r=remotecall(2,a=Float64[]) remotecall(2,setindex!,a,5,10) #Error 或 我希望为每个工作线程执行此操作,然后在异步上下文中访问阵列。执行一些计算,然后获取结果。我不确定这是否可能,因为 下面我做了一个简化的例子,并对其进行了修改。T 通过在修改worker的全局变量时使用“global”(例如,由@everywhere a=3设置)
r=remotecall(2,a=Float64[])
remotecall(2,setindex!,a,5,10) #Error
或
我希望为每个工作线程执行此操作,然后在异步上下文中访问阵列。执行一些计算,然后获取结果。我不确定这是否可能,因为
下面我做了一个简化的例子,并对其进行了修改。T
通过在修改worker的全局变量时使用“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
如果所有工人使用相同的处理功能,为什么
pmap
不够?有两个原因1)如果函数具有随机运行时,pmap似乎效率低下2)可能是通过对每个工作进程的结果求和,然后在所有计算完成后对中间结果求和来进行聚合计算。而@parallel
对于你也是?我想有更多的控制权。例如,只让计算运行时间t。这在@parallel中是不可能的。
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
@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