并行模拟写在同一个文件上 [英] Parallel simulations writing on the same file

查看:125
本文介绍了并行模拟写在同一个文件上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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

解决方案

以下是可以设置一组10000个独立模拟的一种简单示例使用 pmap()

  @everywhere函数模拟(i)
#我们在这里计算模拟结果。在这种情况下,我们只需返回
#模拟号和一个随机值
x = rand()
return(i,x)
end

x = pmap(模拟,1:10000)
#x是从所有模拟返回的元组数组
$ b $ showall(x)
#...或者我们可以写x一个文件或做其他事情

@everywhere 来确保 simulate()函数可用于所有进程,而不仅仅是一个进程。对于第二个参数中的每一个值,并行地为 pmap()调用 simulate()模拟()产生的所有结果数组。

I aim to run a 10,000 or so Julia-coded simulations in parallel (each simulation is independent of all the others) on a cluster. Each simulation has a single number to output (along with 3 columns of info about which simulation has produced this number). It therefore sounds a bit stupid to me to force each simulation to print on a separate file.

Can I safely ask all these simulations to write on the same file or might this cause a bug if two simulations happen to write on the file at the exact same time? What is the best solution?

解决方案

Here is a brief example of one way in which a set of 10000 independent simulations can be set up to run in parallel in Julia, using 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 is needed to ensure that the simulate() function is available to all processes rather than just one process. pmap() calls simulate() once for each of the values in the second parameter, in parallel, and returns an array of all the results produced by simulate().

这篇关于并行模拟写在同一个文件上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆