在同一文件上写入的并行模拟



我的目标是在一个集群上并行运行10,000个左右的Julia编码模拟(每个模拟都独立于所有其他模拟)。每个模拟都有一个要输出的数字(以及有关哪个模拟生成此数字的 3 列信息)。因此,强制每个模拟打印在单独的文件上对我来说听起来有点愚蠢。

我是否可以安全地要求所有这些模拟写入同一个文件,或者如果两个模拟碰巧同时写入文件,这可能会导致错误?最好的解决方案是什么?

下面是一个简短的示例,其中可以使用pmap()将一组 10000 个独立模拟设置为在 Julia 中并行运行:

@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()对第二个参数中的每个值并行调用simulate()一次,并返回 simulate() 生成的所有结果的数组。

最新更新