Julia 中 @distributed 宏下的混淆范围规则
Posted
技术标签:
【中文标题】Julia 中 @distributed 宏下的混淆范围规则【英文标题】:Confusing scoping rules under @distributed macro in julia 【发布时间】:2021-11-07 07:27:44 【问题描述】:我正在尝试在 Julia 中并行化代码,但遇到了一个奇怪的范围问题。将局部变量传递给 @distributed
for 循环中的函数时,我不理解范围规则。执行以下代码时,您会得到预期的行为
using Distributed
addprocs(4)
@everywhere function k(x)
println("x = ", x)
return x
end
sumk = 0
sumk += @distributed (+) for i in 2:nprocs()
k(myid())
end
println("sumk = ", sumk)
运行此代码给出
From worker 4: x = 4
From worker 2: x = 2
From worker 3: x = 3
From worker 5: x = 5
14
sumk = 14
现在,我稍微修改一下代码
using Distributed
addprocs(4)
@everywhere function k(x)
println("x = ", x)
return x
end
@everywhere x = myid()
sumk = 0
sumk += @distributed (+) for i in 2:nprocs()
k(x)
end
println("sumk = ", sumk)
在执行时会给出以下结果:
From worker 2: x = 1
From worker 4: x = 1
From worker 5: x = 1
From worker 3: x = 1
4
sumk = 4
在这里,我不明白为什么 myid()
在本地工作,但 x
仅取自进程 1。
感谢您的帮助。
【问题讨论】:
【参考方案1】:看起来@everywhere
宏的右侧是在本地计算的。
你可以这样做:
remote_do.( Ref(()->global x = myid()), workers())
现在:
@distributed (+) for i in 2:nprocs()
k(x)
end
From worker 3: x = 3
From worker 2: x = 2
From worker 5: x = 5
From worker 4: x = 4
14
【讨论】:
以上是关于Julia 中 @distributed 宏下的混淆范围规则的主要内容,如果未能解决你的问题,请参考以下文章