Ail*_*rus 5 parallel-processing for-loop julia
我正在尝试使用Julia进行一些统计分析.代码由文件组成script.jl(例如数据的初始化)和algorithm.jl.
模拟的数量很大(至少100,000),因此使用并行处理是有意义的.
下面的代码只是一些伪代码来说明我的问题 -
function script(simulations::Int64)
# initialise input data
...
# initialise other variables for statistical analysis using zeros()
...
require("algorithm.jl")
@parallel for z = 1:simulations
  while true
    choices = algorithm(data);      
    if length(choices) == 0
      break
    else
      # process choices and pick one (which alters the data)
      ...
    end
  end
end
# display results of statistical analysis
...
end
和
function algorithm(data)
# actual algorithm
...
return choices;
end
例如,我想知道平均有多少选择,最常见的选择是什么,等等.为此,我需要将choices(在 for循环中)的一些数据保存到统计分析变量(在 for循环之前初始化)并显示结果(在 for循环之后).
我已经阅读过关于使用@spawn和fetch()和函数的内容,pmap()但我不确定如何继续.只使用for循环中的变量不起作用,因为每个proc都有自己的副本,因此for循环之后的统计分析变量的值将只是零.
[编辑]在朱莉娅,我使用include("script.jl")并script(100000)运行模拟,使用单个过程时没有问题.但是,当使用多个过程(例如使用addprocs(3))时,所有统计变量在for循环之后都是零 - 这是预期的.
看来您想要并行化本质上串行的操作,因为每个操作都与另一个操作的结果相关(在本例中data)。我想如果你可以实现上面的代码,例如:
@parallel (dosumethingwithdata) for z = 1:simulations
  while true
    choices = algorithm(data,z);      
    if length(choices) == 0
      break
    else
      # process choices and pick one (which alters the data)
      ...
    end
    data
  end
end
那么你可能会找到该问题的并行解决方案。