编程中需要用到Julia的并行计算,学习过程中发现先前定义的SharedArray被修改后,重新调用时总提示work 2中出现边界错误,不知大家能否给些建议。下面是根据原程序中元胞自动机思想编写的一段简化代码,其中next_L和L都是SharedArray,且next_L由L决定。
#test.jl#
#run by "julia test.jl" in Windows PowerShell#
using Distributed
addprocs(4-nprocs())
@everywhere using SharedArrays
@everywhere const gridSize = 10
@everywhere const deltaTime = 0.05
function mainFunction()
totalTime = 0.0
L = SharedArray{Int64}(rand(1:100, gridSize, gridSize))
next_L = SharedArray{Int64}(gridSize,gridSize)
#========#
try
while totalTime <= 200.0
@show totalTime
t = 0.0
while t <= 1.0
@sync @distributed for j in 1:gridSize
for i in 1:gridSize
next_L[i, j] = rand(1:10)*L[i, j]
end
end
#========#
L = deepcopy(next_L)
t += deltaTime
end
totalTime += t
end
finally
rmprocs(2,3,4)
end
end
#========#
mainFunction()
# 运行环境如下:
Julia Version 1.4.0
Commit b8e9a9ecc6 (2020-03-21 16:36 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-8.0.1 (ORCJIT, skylake)
#========#
[c52e3926] Atom v0.12.9
[6e4b80f9] BenchmarkTools v0.5.0
[336ed68f] CSV v0.5.26
[a93c6f00] DataFrames v0.20.2
[31a5f54b] Debugger v0.6.4
[7073ff75] IJulia v1.21.1
[916415d5] Images v0.22.0
[e5e0dc1b] Juno v0.8.1
[91a5bcdd] Plots v0.29.7
[d330b81b] PyPlot v2.8.2
[ee283ea6] Rebugger v0.3.3
[295af30f] Revise v2.5.4
[276daf66] SpecialFunctions v0.10.0
[8bb1440f] DelimitedFiles
[9a3f8284] Random
# 最终运行结果如下:
ERROR: LoadError: TaskFailedException:
On worker 2:
BoundsError: attempt to access 0×0 Array{Int64,2} at index [1]
getindex at .\array.jl:787 [inlined]
getindex at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\SharedArrays\src\SharedArrays.jl:508 [inlined]
_getindex at .\abstractarray.jl:1003 [inlined]
getindex at .\abstractarray.jl:980
macro expansion at C:\Users\lx_ne\Desktop\test.jl:21 [inlined]
#3 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Distributed\src\macros.jl:302
#158 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Distributed\src\macros.jl:87
#101 at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Distributed\src\process_messages.jl:290
run_work_thunk at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Distributed\src\process_messages.jl:79
run_work_thunk at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.4\Distributed\src\process_messages.jl:88
#94 at .\task.jl:358
...and 2 more exception(s).
Stacktrace:
[1] sync_end(::Array{Any,1}) at .\task.jl:316
[2] (::Distributed.var"#157#159"{var"#3#4"{SharedArray{Int64,2}},UnitRange{Int64}})() at .\task.jl:335
Stacktrace:
[1] sync_end(::Array{Any,1}) at .\task.jl:316
[2] macro expansion at .\task.jl:335 [inlined]
[3] mainFunction() at C:\Users\lx_ne\Desktop\test.jl:19
[4] top-level scope at C:\Users\lx_ne\Desktop\test.jl:36
[5] include(::Module, ::String) at .\Base.jl:377
[6] exec_options(::Base.JLOptions) at .\client.jl:288
[7] _start() at .\client.jl:484
in expression starting at C:\Users\lx_ne\Desktop\test.jl:36
’‘’