我的目标是在集群上并行运行 10,000 个左右的 Julia 编码模拟(每个模拟独立于所有其他模拟)。每个模拟都有一个要输出的数字(以及有关哪个模拟产生该数字的 3 列信息)。因此,强制每个模拟打印在单独的文件上对我来说听起来有点愚蠢。
我可以安全地要求所有这些模拟写入同一个文件,或者如果两个模拟恰好同时写入文件,这可能会导致错误吗?最好的解决方案是什么?
下面是一个简单的示例,说明了一种方法,可以将一组 10000 个独立模拟设置为在 Julia 中并行运行,使用pmap()
:
@everywhere function simulate(i)
# we compute the simulation results here. In this case we just return
# the simulation number and a random value
x = rand()
return (i,x)
end
x = pmap(simulate,1:10000)
# x is the array of tuples returned from all the simulations
showall(x)
# ... or we could write x to a file or do something else with it
@everywhere
需要确保simulate()
该功能适用于所有进程,而不仅仅是一个进程。pmap()
calls simulate()
并行地对第二个参数中的每个值执行一次,并返回由simulate()
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)