-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_tutorial.jl
74 lines (64 loc) · 2.02 KB
/
parallel_tutorial.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# Lisätään Julialle koneen ytimet käyttöön
# addprocs(CPU_CORES - 1)
####################
# TIME IT!
function timeit()
@everywhere k = 486
@everywhere n = 360
@everywhere R_alku = 0.12
a = Array(Float64,n,k)
q = SharedArray(Float64, n, k)
@time advection_shared!(q)
@time testi_p(k::Int64,n::Int64,a)
#println(q == a)
end
# This function retuns the (irange,jrange) indexes assigned to this worker
# It splits the q::SharedArray into chunks of columns for workers
@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
# This function retuns the (irange,jrange) indexes assigned to this worker
# It splits the q::SharedArray into chunks of rows for workers
@everywhere function myrangerows(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,1),nchunks+1)]
splits[idx]+1:splits[idx+1], 1:size(q,2)
end
# Here's the kernel
@everywhere function advection_chunk!(q, irange, jrange)
@show (irange, jrange) # display so we can see what's happening
for j in jrange, i in irange
q[i,j] = R_alku * (rand(89:105)/100)
end
q
end
# Here's a convenience wrapper for a SharedArray implementation
@everywhere advection_shared_chunk!(q) = advection_chunk!(q, myrange(q)...)
function advection_shared!(q)
@sync begin
for p in procs(q)
@async remotecall_wait(advection_shared_chunk!, p, q)
end
end
q
end
@everywhere function testi_p(k::Int64,n::Int64,a_shared)
@simd for j in range(1,k)
@simd for i in range(1,n)
@fastmath @inbounds a_shared[i,j] = R_alku * (rand(89:105)/100)
end
end
return a_shared
end