Mos*_*shi 6 parallel-processing dataframe julia julia-dataframe
我在 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)
.
非常感谢对此的任何帮助!谢谢!
Julia 中的分布式计算模式比您想要做的要简单得多。
\n您的代码应该或多或少看起来像这样:
\ndf = CSV.read("/path/data.csv", DataFrame)\n\n@everywhere using <required packages>\n\n\nsimulation_results = @distributed (append!) for sim in 1:nsims\n <the code which for one simulation stores the results in nsim_result >\n nsim_result\nend\n
Run Code Online (Sandbox Code Playgroud)\n请注意,您不需要df
在 Julia 集群中的每个进程上加载,因为@distributed
这将确保它是可读的。您也不需要,@sync
因为在我的代码中您将使用聚合器函数(append!
)。
一个最小的工作示例(运行addprocs(4)
):
@everywhere using Distributed, DataFrames\ndf = DataFrame(a=1:5,b=rand())\n
Run Code Online (Sandbox Code Playgroud)\n现在的结果是:
\njulia> @distributed (append!) for i in 2:5\n DataFrame(bsum=sum(df.b[1:myid()]),c=myid())\n end\n4\xc3\x972 DataFrame\n Row \xe2\x94\x82 bsum c\n \xe2\x94\x82 Float64 Int64\n\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xbc\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\n 1 \xe2\x94\x82 0.518127 2\n 2 \xe2\x94\x82 0.777191 3\n 3 \xe2\x94\x82 1.03625 4\n 4 \xe2\x94\x82 1.29532 5\n
Run Code Online (Sandbox Code Playgroud)\n
归档时间: |
|
查看次数: |
433 次 |
最近记录: |