我想弄清楚如何與Julia一起使用並行計算。文檔看起來很棒,即使對於像我這樣從未使用過並行計算的人(並且不理解文檔背後的大多數概念)也是如此。如何與Julia語言並行運行函數?
只需提一下:我正在使用Ubuntu的PC上工作。它有一個4核心處理器。
要運行我在下面說明我打電話的朱莉婭終端作爲代碼:
$ julia -p 4
我的文檔here以下。我現在面臨的一些問題與this section
我試圖運行下面的代碼段描述的例子:
@everywhere advection_shared_chunk!(q, u) = advection_chunk!(q, u, myrange(q)..., 1:size(q,3)-1)
function advection_shared!(q, u)
@sync begin
for p in procs(q)
@async remotecall_wait(advection_shared_chunk!, p, q, u)
end
end
q
end
q = SharedArray(Float64, (500,500,500))
u = SharedArray(Float64, (500,500,500))
#Run once to JIT-compile
advection_shared!(q,u)
但我面臨着以下錯誤:
ERROR: MethodError: `remotecall_wait` has no method matching remotecall_wait(::Function, ::Int64, ::SharedArray{Float64,3}, ::SharedArray{Float64,3})
Closest candidates are:
remotecall_wait(::LocalProcess, ::Any, ::Any...)
remotecall_wait(::Base.Worker, ::Any, ::Any...)
remotecall_wait(::Integer, ::Any, ::Any...)
in anonymous at task.jl:447
...and 3 other exceptions.
in sync_end at ./task.jl:413
[inlined code] from task.jl:422
in advection_shared! at none:2
我是什麼在這裏做錯了嗎?據我所知,我只是在文檔中重現這個例子......或者不是?
感謝您的幫助,
感謝@Daniel阿恩特,你發現的伎倆!我正在查看文檔:http://docs.julialang.org/en/latest/manual/parallel-computing/我認爲它應該是Julia 0.4.x(迄今爲止最新的穩定版本)的一個,但它似乎與Julia 0.5.x相關(所有最新版本版本)。
我做了您所建議的更改(更改了順序並添加了缺少的功能),並且所有工作都像魅力一樣。我將離開這裏的更新代碼
# Here's the kernel
@everywhere function advection_chunk!(q, u, irange, jrange, trange)
@show (irange, jrange, trange) # display so we can see what's happening
for t in trange, j in jrange, i in irange
q[i,j,t+1] = q[i,j,t] + u[i,j,t]
end
q
end
# This function retuns the (irange,jrange) indexes assigned to this worker
@everywhere function myrange(q::SharedArray)
idx = indexpids(q)
if idx == 0
# This worker is not assigned a piece
return 1:0, 1:0
end
nchunks = length(procs(q))
splits = [round(Int, s) for s in linspace(0,size(q,2),nchunks+1)]
1:size(q,1), splits[idx]+1:splits[idx+1]
end
@everywhere advection_shared_chunk!(q, u) = advection_chunk!(q, u, myrange(q)..., 1:size(q,3)-1)
function advection_shared!(q, u)
@sync begin
for p in procs(q)
@async remotecall_wait(p, advection_shared_chunk!, q, u)
end
end
q
end
q = SharedArray(Float64, (500,500,500))
u = SharedArray(Float64, (500,500,500))
#Run once to JIT-compile
advection_shared!(q,u)
完成!