如何使用remotecall_fetch调用函数(Julia - parallel)

时间:2017-07-20 03:55:46

标签: parallel-processing julia

我想在进程2上运行一个简单的函数。所以我定义了这样的函数:

julia> f(x,y) = x+y
f (generic function with 1 method)

然后我想在进程2上执行此操作,但是我收到了错误:

julia> remotecall_fetch(f,2,1,1)
ERROR: On worker 2:
UndefVarError: #f not defined
deserialize_datatype at ./serialize.jl:969
handle_deserialize at ./serialize.jl:674
deserialize at ./serialize.jl:634
handle_deserialize at ./serialize.jl:681
deserialize_msg at ./distributed/messages.jl:98
message_handler_loop at ./distributed/process_messages.jl:161
process_tcp_streams at ./distributed/process_messages.jl:118
#99 at ./event.jl:73
Stacktrace:
 [1] #remotecall_fetch#141(::Array{Any,1}, ::Function, ::Function, ::Base.Distributed.Worker, ::Int64, ::Vararg{Int64,N} where N) at ./distributed/remotecall.jl:354
 [2] remotecall_fetch(::Function, ::Base.Distributed.Worker, ::Int64, ::Vararg{Int64,N} where N) at ./distributed/remotecall.jl:346
 [3] #remotecall_fetch#144(::Array{Any,1}, ::Function, ::Function, ::Int64, ::Int64, ::Vararg{Int64,N} where N) at ./distributed/remotecall.jl:367
 [4] remotecall_fetch(::Function, ::Int64, ::Int64, ::Vararg{Int64,N} where N) at ./distributed/remotecall.jl:367

我知道我们可以定义这样的函数:

julia> @everywhere f(x,y)=x+y

然后我们可以得到结果:

julia> remotecall_fetch(f,2,3,4)
7

实际上我不知道如何在Includeusing

的所有流程或部分流程中定义我的功能

1 个答案:

答案 0 :(得分:3)

@everywhere是要使用的正确宏。对于模块,只需执行@everywhere using MyModule,模块MyModule中的所有导出函数都可用于所有工作进程。