Julia中并行for循环中的数据管理

时间:2015-03-06 16:47:11

标签: for-loop parallel-processing 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 in for-loop)保存到统计分析变量(在 for循环之前初始化)和显示结果(在for循环之后)。

我读过有关使用@spawnfetch()以及pmap()等功能的内容,但我不知道该如何处理。只使用for循环中的变量不起作用,因为每个proc都有自己的副本,因此for循环之后的统计分析变量的值只是零。

[编辑] 在Julia中,我使用include("script.jl")script(100000)来运行模拟,使用单个proc时没有问题。但是,当使用多个proc(例如使用addprocs(3))时,所有统计变量在for循环之后都为零 - 这是预期的。

1 个答案:

答案 0 :(得分:1)

似乎您希望并行化固有的串行操作,因为每个操作都与另一个操作的结果相关(在本例中为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

然后你可以找到问题的并行解决方案。