【问题标题】:Parallel processing in JuliaJulia 中的并行处理
【发布时间】:2018-07-03 08:57:06
【问题描述】:

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

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 ......我需要在同一个数组上运行它们。

非常感谢任何帮助

【问题讨论】:

    标签: parallel-processing julia


    【解决方案1】:

    您的代码丢失 @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)。

    【讨论】:

      猜你喜欢
      • 2014-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多