Parallel processing Julia中的并行处理
我正在尝试并行运行一个for循环,其中包括并行运行一个eras模型。代码如下:Parallel processing Julia中的并行处理,parallel-processing,julia,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
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…我需要在同一个数组中运行它们
非常感谢您的帮助您的代码丢失了
@无处不在使用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而不是<代码> @并行< /代码> ./P>
@everywhere
在所有工作人员中执行命令。在并行仿真中,它通常用于初始化变量/仿真状态或导入库。请注意,如果您希望跨工作人员发送数据,您可能希望使用ParallelDataTransfer.jl
最后但并非最不重要的一点是,函数内部的addprocs(16)
通常不是一个好模式-每次调用函数时都会产生新的16julia
进程。改用-p
命令行选项(例如,使用Julia-p16
命令启动Julia)。您的代码缺失
@无处不在使用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而不是<代码> @并行< /代码> ./P>
@everywhere
在所有工作人员中执行命令。在并行仿真中,它通常用于初始化变量/仿真状态或导入库。请注意,如果您希望跨工作人员发送数据,您可能希望使用ParallelDataTransfer.jl
最后但并非最不重要的一点是,函数内部的addprocs(16)
通常不是一个好模式-每次调用函数时都会产生新的16julia
进程。改用-p
命令行选项(例如,使用Julia-p16
命令启动Julia)