forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhpl.jl
395 lines (307 loc) · 10.1 KB
/
hpl.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
## Based on "Multi-Threading and One-Sided Communication in Parallel LU Factorization"
## http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.138.4361&rank=7
function hpl_seq(A::Matrix, b::Vector)
blocksize = 5
n = size(A,1)
A = [A b]
B_rows = linspace(0, n, div(n,blocksize)+1)
B_rows[end] = n
B_cols = [B_rows, [n+1]]
nB = length(B_rows)
depend = zeros(Bool, nB, nB) # In parallel, depend needs to be able to hold futures
## Small matrix case
if nB <= 1
x = A[1:n, 1:n] \ A[:,n+1]
return x
end
## Add a ghost row of dependencies to boostrap the computation
for j=1:nB; depend[1,j] = true; end
for i=1:(nB-1)
## Threads for panel factorizations
I = (B_rows[i]+1):B_rows[i+1]
#(depend[i+1,i], panel_p) = spawn(panel_factor_seq, I, depend[i,i])
(depend[i+1,i], panel_p) = panel_factor_seq(A, I, depend[i,i])
## Threads for trailing updates
for j=(i+1):nB
J = (B_cols[j]+1):B_cols[j+1]
#depend[i+1,j] = spawn(trailing_update_seq, I, J, panel_p, depend[i+1,i],depend[i,j])
depend[i+1,j] = trailing_update_seq(A, I, J, panel_p, depend[i+1,i],depend[i,j])
end
end
## Completion of the last diagonal block signals termination
#wait(depend[nB, nB])
## Solve the triangular system
x = triu(A[1:n,1:n]) \ A[:,n+1]
return x
end ## hpl()
### Panel factorization ###
function panel_factor_seq(A, I, col_dep)
n = size (A, 1)
## Enforce dependencies
#wait(col_dep)
## Factorize a panel
K = I[1]:n
panel_p = lufact!(sub(A, K, I))[:p] # Economy mode
## Panel permutation
panel_p = K[panel_p]
return (true, panel_p)
end ## panel_factor_seq()
### Trailing update ###
function trailing_update_seq(A, I, J, panel_p, row_dep, col_dep)
n = size (A, 1)
## Enforce dependencies
#wait(row_dep, col_dep)
## Apply permutation from pivoting
K = (I[end]+1):n
A[I[1]:n, J] = A[panel_p, J]
## Compute blocks of U
L = tril(A[I,I],-1) + eye(length(I))
A[I, J] = L \ A[I, J]
## Trailing submatrix update
if !isempty(K)
A[K,J] = A[K,J] - A[K,I]*A[I,J]
end
return true
end ## trailing_update_seq()
# This version is written for a shared memory implementation.
# The matrix A is local to the first Worker, which allocates work to other Workers
# All updates to A are carried out by the first Worker. Thus A is not distributed
hpl_par(A::Matrix, b::Vector) = hpl_par(A, b, max(1, div(max(size(A)),4)), true)
hpl_par(A::Matrix, b::Vector, bsize::Integer) = hpl_par(A, b, bsize, true)
function hpl_par(A::Matrix, b::Vector, blocksize::Integer, run_parallel::Bool)
n = size(A,1)
A = [A b]
if blocksize < 1
throw(ArgumentError("hpl_par: invalid blocksize: $blocksize < 1"))
end
B_rows = linspace(0, n, div(n,blocksize)+1)
B_rows[end] = n
B_cols = [B_rows, [n+1]]
nB = length(B_rows)
depend = cell(nB, nB)
## Small matrix case
if nB <= 1
x = A[1:n, 1:n] \ A[:,n+1]
return x
end
## Add a ghost row of dependencies to boostrap the computation
for j=1:nB; depend[1,j] = true; end
for i=2:nB, j=1:nB; depend[i,j] = false; end
for i=1:(nB-1)
#println("A=$A") #####
## Threads for panel factorizations
I = (B_rows[i]+1):B_rows[i+1]
K = I[1]:n
(A_KI, panel_p) = panel_factor_par(A[K,I], depend[i,i])
## Write the factorized panel back to A
A[K,I] = A_KI
## Panel permutation
panel_p = K[panel_p]
depend[i+1,i] = true
## Apply permutation from pivoting
J = (B_cols[i+1]+1):B_cols[nB+1]
A[K, J] = A[panel_p, J]
## Threads for trailing updates
#L_II = tril(A[I,I], -1) + eye(length(I))
L_II = tril(sub(A,I,I), -1) + eye(length(I))
K = (I[length(I)]+1):n
A_KI = A[K,I]
for j=(i+1):nB
J = (B_cols[j]+1):B_cols[j+1]
## Do the trailing update (Compute U, and DGEMM - all flops are here)
if run_parallel
A_IJ = A[I,J]
#A_KI = A[K,I]
A_KJ = A[K,J]
depend[i+1,j] = @spawn trailing_update_par(L_II, A_IJ, A_KI, A_KJ, depend[i+1,i], depend[i,j])
else
depend[i+1,j] = trailing_update_par(L_II, A[I,J], A[K,I], A[K,J], depend[i+1,i], depend[i,j])
end
end
# Wait for all trailing updates to complete, and write back to A
for j=(i+1):nB
J = (B_cols[j]+1):B_cols[j+1]
if run_parallel
(A_IJ, A_KJ) = fetch(depend[i+1,j])
else
(A_IJ, A_KJ) = depend[i+1,j]
end
A[I,J] = A_IJ
A[K,J] = A_KJ
depend[i+1,j] = true
end
end
## Completion of the last diagonal block signals termination
@assert depend[nB, nB]
## Solve the triangular system
x = triu(A[1:n,1:n]) \ A[:,n+1]
return x
end ## hpl()
### Panel factorization ###
function panel_factor_par(A_KI, col_dep)
@assert col_dep
## Factorize a panel
panel_p = lufact!(A_KI)[:p] # Economy mode
return (A_KI, panel_p)
end ## panel_factor_par()
### Trailing update ###
function trailing_update_par(L_II, A_IJ, A_KI, A_KJ, row_dep, col_dep)
@assert row_dep
@assert col_dep
## Compute blocks of U
A_IJ = L_II \ A_IJ
## Trailing submatrix update - All flops are here
if !isempty(A_KJ)
m, k = size(A_KI)
n = size(A_IJ,2)
blas_gemm('N','N',m,n,k,-1.0,A_KI,m,A_IJ,k,1.0,A_KJ,m)
#A_KJ = A_KJ - A_KI*A_IJ
end
return (A_IJ, A_KJ)
end ## trailing_update_par()
### using DArrays ###
function hpl_par2(A::Matrix, b::Vector)
n = size(A,1)
A = [A b]
C = distribute(A, 2)
nB = length(C.pmap)
## case if only one processor
if nB <= 1
x = A[1:n, 1:n] \ A[:,n+1]
return x
end
depend = Array(RemoteRef, nB, nB)
#pmap[i] is where block i's stuff is
#block i is dist[i] to dist[i+1]-1
for i = 1:nB
#println("C=$(convert(Array, C))") #####
##panel factorization
panel_p = remotecall_fetch(C.pmap[i], panel_factor_par2, C, i, n)
## Apply permutation from pivoting
for j = (i+1):nB
depend[i,j] = remotecall(C.pmap[j], permute, C, i, j, panel_p, n, false)
end
## Special case for last column
if i == nB
depend[nB,nB] = remotecall(C.pmap[nB], permute, C, i, nB+1, panel_p, n, true)
end
##Trailing updates
(i == nB) ? (I = (C.dist[i]):n) :
(I = (C.dist[i]):(C.dist[i+1]-1))
C_II = C[I,I]
L_II = tril(C_II, -1) + eye(length(I))
K = (I[length(I)]+1):n
if length(K) > 0
C_KI = C[K,I]
else
C_KI = zeros(0)
end
for j=(i+1):nB
dep = depend[i,j]
depend[j,i] = remotecall(C.pmap[j], trailing_update_par2, C, L_II, C_KI, i, j, n, false, dep)
end
## Special case for last column
if i == nB
dep = depend[nB,nB]
remotecall_fetch(C.pmap[nB], trailing_update_par2, C, L_II, C_KI, i, nB+1, n, true, dep)
else
#enforce dependencies for nonspecial case
for j=(i+1):nB
wait(depend[j,i])
end
end
end
A = convert(Array, C)
x = triu(A[1:n,1:n]) \ A[:,n+1]
end ## hpl_par2()
function panel_factor_par2(C, i, n)
(C.dist[i+1] == n+2) ? (I = (C.dist[i]):n) :
(I = (C.dist[i]):(C.dist[i+1]-1))
K = I[1]:n
C_KI = C[K,I]
#(C_KI, panel_p) = lu!(C_KI) #economy mode
panel_p = lu!(C_KI)[2]
C[K,I] = C_KI
return panel_p
end ##panel_factor_par2()
function permute(C, i, j, panel_p, n, flag)
if flag
K = (C.dist[i]):n
J = (n+1):(n+1)
C_KJ = C[K,J]
C_KJ = C_KJ[panel_p,:]
C[K,J] = C_KJ
else
K = (C.dist[i]):n
J = (C.dist[j]):(C.dist[j+1]-1)
C_KJ = C[K,J]
C_KJ = C_KJ[panel_p,:]
C[K,J] = C_KJ
end
end ##permute()
function trailing_update_par2(C, L_II, C_KI, i, j, n, flag, dep)
if isa(dep, RemoteRef); wait(dep); end
if flag
#(C.dist[i+1] == n+2) ? (I = (C.dist[i]):n) :
# (I = (C.dist[i]):(C.dist[i+1]-1))
I = C.dist[i]:n
J = (n+1):(n+1)
K = (I[length(I)]+1):n
C_IJ = C[I,J]
if length(K) > 0
C_KJ = C[K,J]
else
C_KJ = zeros(0)
end
## Compute blocks of U
C_IJ = L_II \ C_IJ
C[I,J] = C_IJ
else
#(C.dist[i+1] == n+2) ? (I = (C.dist[i]):n) :
# (I = (C.dist[i]):(C.dist[i+1]-1))
I = (C.dist[i]):(C.dist[i+1]-1)
J = (C.dist[j]):(C.dist[j+1]-1)
K = (I[length(I)]+1):n
C_IJ = C[I,J]
if length(K) > 0
C_KJ = C[K,J]
else
C_KJ = zeros(0)
end
## Compute blocks of U
C_IJ = L_II \ C_IJ
C[I,J] = C_IJ
## Trailing submatrix update - All flops are here
if !isempty(C_KJ)
cm, ck = size(C_KI)
cn = size(C_IJ,2)
blas_gemm('N','N',cm,cn,ck,-1.0,C_KI,cm,C_IJ,ck,1.0,C_KJ,cm)
#C_KJ = C_KJ - C_KI*C_IJ
C[K,J] = C_KJ
end
end
end ## trailing_update_par2()
## Test n*n matrix on np processors
## Prints 5 numbers that should be close to zero
function test(n, np)
A = rand(n,n); b = rand(n);
X = (@elapsed x = A \ b);
Y = (@elapsed y = hpl_par(A,b, max(1,div(n,np))));
Z = (@elapsed z = hpl_par2(A,b));
for i=1:(min(5,n))
print(z[i]-y[i], " ")
end
println()
return (X,Y,Z)
end
## test k times and collect average
function test(n,np,k)
sum1 = 0; sum2 = 0; sum3 = 0;
for i = 1:k
(X,Y,Z) = test(n,np)
sum1 += X
sum2 += Y
sum3 += Z
end
return (sum1/k, sum2/k, sum3/k)
end