I’m in a very similar situation. For the sake of simplicity, I’d like to not make my code in to a module, and I want one function in the file to call the other in a parpool/pmap fashion.
I thought an easy way to do this would be to simply include the file with @everywhere
, but this has proved to be tricky. My current attempt is failing.
I have a file named pmap_test.jl
:
# pmap_test.jl
function do_the_work(x)
x * 2
sleep(1)
end
function do_the_pmap()
num_workers = 8
wpool = WorkerPool(addprocs(num_workers)) # make worker pool
# make all functions in this file available to all workers
@everywhere the_source_file = realpath(Base.source_path())
@everywhere include(the_source_file)
# use pmap with wpool
pmap(wpool, do_the_work, collect(1:num_workers*3))
# attempt to remove worker procs associated with wpool
rmprocs(wpool.workers)
end
When I try to include this file and call do_the_pmap
from a notebook, I get the following:
On worker 2:
SystemError: realpath: No error
#systemerror#44 at .\error.jl:64 [inlined]
systemerror at .\error.jl:64
realpath at .\path.jl:287
eval at .\boot.jl:235
eval_ew_expr at .\distributed\macros.jl:116
#106 at .\distributed\process_messages.jl:268 [inlined]
run_work_thunk at .\distributed\process_messages.jl:56
macro expansion at .\distributed\process_messages.jl:268 [inlined]
#105 at .\event.jl:73
#remotecall_fetch#141(::Array{Any,1}, ::Function, ::Function, ::Base.Distributed.Worker, ::Expr, ::Vararg{Expr,N} where N) at .\distributed\remotecall.jl:354
remotecall_fetch(::Function, ::Base.Distributed.Worker, ::Expr, ::Vararg{Expr,N} where N) at .\distributed\remotecall.jl:346
#remotecall_fetch#144(::Array{Any,1}, ::Function, ::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at .\distributed\remotecall.jl:367
remotecall_fetch(::Function, ::Int64, ::Expr, ::Vararg{Expr,N} where N) at .\distributed\remotecall.jl:367
(::##25#29)() at .\distributed\macros.jl:102
...and 40 more exception(s).
Stacktrace:
[1] sync_end() at .\task.jl:287
[2] macro expansion at .\distributed\macros.jl:112 [inlined]
[3] do_the_pmap() at pmap_test.jl:13
Is there a straightforward way to include a simple .jl file with functions and make those functions usable on all workers?