朱莉娅中的并行处理

时间:2018-07-03 08:57:07

标签: parallel-processing julia

我正在尝试并行运行一个for循环,包括并行尝试一个eras模型。以下是代码:

function init_population(pop :: _population)
        addprocs(16)

    @sync @parallel for i in 1:pop.size
    @everywhere ran=sample(1:202,10,replace=false)
    @everywhere w=get_weights(ran)  ####keras model
    @everywhere gg=_genotype(ran,w)   ### composite type
    @everywhere m,v=get_mean_variance(gg)  ####func doing calculation
    @everywhere pp=_phenotype(m,v)    ### composite type
    @everywhere fitn=get_fitness(pp)   ####func doing calculation
    @everywhere new_guy = _individual(gg,pp,fitn)     ### composite type
    @everywhere push!(pop.individuals, new_guy)
end
return pop
end

我得到的错误:::

ERROR: LoadError: UndefVarError: sample not defined
eval at ./boot.jl:235
eval_ew_expr at ./distributed/macros.jl:116 [inlined]
#135 at ./distributed/remotecall.jl:319
run_work_thunk at ./distributed/process_messages.jl:56
#remotecall_fetch#140 at ./distributed/remotecall.jl:344
remotecall_fetch at ./distributed/remotecall.jl:344
#remotecall_fetch#144 at ./distributed/remotecall.jl:372
remotecall_fetch at ./distributed/remotecall.jl:372
#33 at ./distributed/macros.jl:102
#remotecall_fetch#140(::Array{Any,1}, ::Function, ::Function, ::Base.Distributed.LocalProcess, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:345
remotecall_fetch(::Function, ::Base.Distributed.LocalProcess, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:344
#remotecall_fetch#144(::Array{Any,1}, ::Function, ::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:372
remotecall_fetch(::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at ./distributed/remotecall.jl:372
(::##73#75)() at ./distributed/macros.jl:102
Stacktrace:
 [1] sync_end() at ./task.jl:287
 [2] macro expansion at ./distributed/macros.jl:112 [inlined]
 [3] evolutionary_loop(::_population) at ./untitled-75c3e04a7f530386f03caa1b6d061e62:372
 [4] include_string(::String, ::String) at ./loading.jl:522
 [5] include_string(::Module, ::String, ::String) at /Users/yash/.julia/v0.6/Compat/src/Compat.jl:88
 [6] (::Atom.##112#116{String,String})() at /Users/yash/.julia/v0.6/Atom/src/eval.jl:109
 [7] withpath(::Atom.##112#116{String,String}, ::Void) at /Users/yash/.julia/v0.6/CodeTools/src/utils.jl:30
 [8] withpath(::Function, ::String) at /Users/yash/.julia/v0.6/Atom/src/eval.jl:38
 [9] hideprompt(::Atom.##111#115{String,String}) at /Users/yash/.julia/v0.6/Atom/src/repl.jl:67
 [10] macro expansion at /Users/yash/.julia/v0.6/Atom/src/eval.jl:106 [inlined]
 [11] (::Atom.##110#114{Dict{String,Any}})() at ./task.jl:80
while loading untitled-75c3e04a7f530386f03caa1b6d061e62, in expression starting on line 395

我不确定如何进行远程呼叫及其工作方式。我基本上是在16个进程中运行for循环.pop.size = 100 ...,我需要在同一数组上运行它们。

非常感谢您的帮助

1 个答案:

答案 0 :(得分:2)

您的代码丢失 @everywhere using StatsBase 由于每个工作程序都是一个附加过程,因此模块StatsBase应该在所有工作程序中导入。

如果使用@parallel循环,则循环内既不需要@sync也不需要@everywhere@parallel只是在各个工作程序之间划分循环,并在每个工作程序上执行部分。根据您要执行的操作,您可能会缺少聚合器功能,因此通常是:

@parallel (my_agg_function) for i in 1:n
   # do something - job will be evenly split across workers
end 

也请考虑使用pmap代替@parallel

@everywhere对所有工作程序执行命令。在并行仿真中,它通常用于诸如初始化变量/仿真状态或导入库之类的事情。请注意,如果您希望跨工作人员发送数据,则可能要使用ParallelDataTransfer.jl

在函数内最后但并非最不重要的addprocs(16)通常不是一个好的模式-每次调用该函数时都会产生新的16个julia进程。改为使用-p命令行选项(例如,以julia -p 16命令启动Julia)。