我在 DataFrame 上进行了大型模拟df,我试图将模拟结果并行化并将模拟结果保存在名为 的 DataFrame 中simulation_results。
并行化循环工作得很好。问题是,如果我要将结果存储在数组中,我会将其声明为SharedArray循环之前。我不知道如何声明simulation_results为“共享数据帧”,它对所有处理器来说都可用并且可以修改。
代码片段如下:
addprocs(length(Sys.cpu_info()))
@everywhere begin
using <required packages>
df = CSV.read("/path/data.csv", DataFrame)
simulation_results = similar(df, 0) #I need to declare this as shared and modifiable by all processors
nsims = 100000
end
@sync @distributed for sim in 1:nsims
nsim_result = similar(df, 0)
<the code which for one simulation stores the results in nsim_result >
append!(simulation_results, nsim_result)
end
Run Code Online (Sandbox Code Playgroud)
问题在于,由于simulation_results未声明为由处理器共享和可修改,因此在循环运行后,它基本上会生成一个空的 DataFrame,如@everywhere simulation_results = similar(df, 0) …