Julia并行处理单独的模块

时间:2016-11-03 14:48:46

标签: parallel-processing julia

我正在尝试编写一个简单的程序,在单独的模块中使用代码进行并行处理。我在两个单独的文件中有以下代码:

main.jl

push!(LOAD_PATH, ".")

#using other  # This doesn't work either

importall other

np = 4
b = [Bounds(k, k+8) for k in 1:np]
fut = Array{Future}(np)

for k = 1:np
    fut[k] = @spawn center(b[k])
end

for k = 1:np
    xc = fetch(fut[k])
    println("Center for k ", k, " is ",  xc)
end

other.jl

@everywhere module other

export Bounds
export center

@everywhere type Bounds
    x1::Int
    x2::Int
end

@everywhere function center(bound::Bounds)
    return (bound.x1 + bound.x2) / 2
end 

end

当我使用“julia main.jl”运行单个进程时,它运行时没有错误,但如果我尝试使用“julia -p4 main.jl”添加进程,则会收到以下错误。看起来可能其他进程无法看到other.jl中的代码,但我在所有正确的位置都有@everywhere宏。有什么问题?

ERROR: LoadError: LoadError: On worker 2:
UndefVarError: ##5#7 not defined
 in deserialize_datatype at ./serialize.jl:823
 in handle_deserialize at ./serialize.jl:571
 in deserialize_msg at ./multi.jl:120
 in message_handler_loop at ./multi.jl:1317
 in process_tcp_streams at ./multi.jl:1276
 in #618 at ./event.jl:68
 in #remotecall_fetch#606(::Array{Any,1}, ::Function, ::Function, ::Base.Worker) at ./multi.jl:1070
 in remotecall_fetch(::Function, ::Base.Worker) at ./multi.jl:1062
 in #remotecall_fetch#609(::Array{Any,1}, ::Function, ::Function, ::Int64) at ./multi.jl:1080
 in remotecall_fetch(::Function, ::Int64) at ./multi.jl:1080
 in (::other.##6#8)() at ./multi.jl:1959

...and 3 other exceptions.

 in sync_end() at ./task.jl:311
 in macro expansion; at ./multi.jl:1968 [inlined]
 in anonymous at ./<missing>:?
 in eval(::Module, ::Any) at ./boot.jl:234
 in (::##1#3)() at ./multi.jl:1957
 in sync_end() at ./task.jl:311
 in macro expansion; at ./multi.jl:1968 [inlined]
 in anonymous at ./<missing>:?
 in include_from_node1(::String) at ./loading.jl:488
 in eval(::Module, ::Any) at ./boot.jl:234
 in require(::Symbol) at ./loading.jl:409
 in include_from_node1(::String) at ./loading.jl:488
 in process_options(::Base.JLOptions) at ./client.jl:262
 in _start() at ./client.jl:318
while loading /mnt/mint320/home/bmaier/BillHome/Programs/Julia/parallel/modules/other.jl, in expression starting on line 1
while loading /mnt/mint320/home/bmaier/BillHome/Programs/Julia/parallel/modules/main.jl, in expression starting on line 5

1 个答案:

答案 0 :(得分:1)

在main.jl中尝试@everywhere importall other

这将在所有当前工作人员上加载other。除了正常的脚本或模块之外,other不需要任何其他内容,因此您不需要在other.jl中使用@everywhere