xy = SharedArray{Float64}(200,2) #创建200个坐标为(0,0)的点
@everywhere function randomWalk(xy) #随机向四个方向运动
@distributed for i in 1:1000
for j in 1:200
direction = rand(["up","dowm","left","right"])
if direction == "up"
xy[j,1] = xy[j,1] + 1
end
if direction == "dowm"
xy[j,1] = xy[j,1] - 1
end
if direction == "left"
xy[j,2] = xy[j,2] - 1
end
if direction == "right"
xy[j,2] = xy[j,2] + 1
end
end
@everywhere using SharedArrays
xy = SharedArray{Float64}(10000, 2)
@everywhere movements = [
[0., 1.], # up
[0., -1.], # down
[1., 0.], # left
[-1., 0.] # right
]
@everywhere function customized_points(data::SharedArray)
idx = indexpids(data)
if idx == 0 # This worker is not assigned a piece
return 1:0, 1:0
end
nchunks = length(procs(data))
splits = [round(Int, s) for s in range(0, stop=size(data, 1), length=nchunks+1)] # 这里其实不用预分配,为了方便理解参照的文档写的
splits[idx]+1:splits[idx+1]
end
@everywhere function random_walk!(data)
for _ in 1:10000
for i in customized_points(data)
data[i, :] .+= rand(movements)
end
end
end
function random_walk_sync!(data)
@sync begin
for p in procs(data)
@async remotecall_wait(random_walk!, p, data)
end
end
end
Julia Version 1.1.0
Commit 80516ca202 (2019-01-21 21:24 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: Intel(R) Core(TM) i5-6300HQ CPU @ 2.30GHz
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-6.0.1 (ORCJIT, skylake)
下面上代码:
using Distributed
addprocs(4)
@everywhere using SharedArrays
using BenchmarkTools
@everywhere function randomWalk3d!(xy::SharedArray{Int64},time::Int64=1000)
axis=Dict("x"=>1,"y"=>2,"z"=>3) # 随机选坐标轴
for i in 1:time
for j in 1:size(xy)[1] # 对行循环
xy[j,rand(axis).second]+=rand([-1,1]) # 随机加一减一
end
end
end
function randomWalk3d_sync!(xy::SharedArray{Int64},time::Int64=1000)
@sync begin
for p in procs(xy)
@async remotecall_wait(randomWalk3d!,p,xy,10000)
end
end
end
xy=SharedArray{Int64}(200,3)
@benchmark randomWalk3d_sync!(xy)
结果
BenchmarkTools.Trial:
memory estimate: 18.69 KiB
allocs estimate: 369
--------------
minimum time: 272.681 ms (0.00% GC)
median time: 282.398 ms (0.00% GC)
mean time: 286.255 ms (0.00% GC)
maximum time: 313.312 ms (0.00% GC)
--------------
samples: 18
evals/sample: 1