并行模拟写在同一个文件上 [英] Parallel simulations writing on the same file
问题描述
我可以安全地要求所有这些模拟写在同一个文件上吗?如果两个模拟恰好同时在文件上写入,会导致一个错误?什么是最好的解决方案?
以下是可以设置一组10000个独立模拟的一种简单示例使用 pmap()
:
@everywhere函数模拟(i)
#我们在这里计算模拟结果。在这种情况下,我们只需返回
#模拟号和一个随机值
x = rand()
return(i,x)
end
x = pmap(模拟,1:10000)
#x是从所有模拟返回的元组数组
$ b $ showall(x)
#...或者我们可以写x一个文件或做其他事情
@everywhere $ c $需要c>来确保
调用 simulate()
函数可用于所有进程,而不仅仅是一个进程。对于第二个参数中的每一个值,并行地为 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屋!