1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
//import jsr166y.*; |
8 |
|
import java.util.concurrent.*; |
9 |
– |
import java.util.concurrent.TimeUnit; |
10 |
– |
|
9 |
|
|
10 |
|
/** |
11 |
|
* LU matrix decomposition demo |
12 |
|
* Based on those in Cilk and Hood |
13 |
< |
**/ |
16 |
< |
|
13 |
> |
*/ |
14 |
|
public final class LU { |
15 |
|
|
16 |
|
/** for time conversion */ |
17 |
|
static final long NPS = (1000L * 1000 * 1000); |
18 |
|
|
19 |
|
// granularity is hard-wired as compile-time constant here |
20 |
< |
static final int BLOCK_SIZE = 16; |
20 |
> |
static final int BLOCK_SIZE = 16; |
21 |
|
static final boolean CHECK = false; // set true to check answer |
22 |
|
|
23 |
|
public static void main(String[] args) throws Exception { |
26 |
|
|
27 |
|
int procs = 0; |
28 |
|
int n = 2048; |
29 |
< |
int runs = 5; |
29 |
> |
int runs = 32; |
30 |
|
try { |
31 |
|
if (args.length > 0) |
32 |
|
procs = Integer.parseInt(args[0]); |
33 |
|
if (args.length > 1) |
34 |
|
n = Integer.parseInt(args[1]); |
35 |
< |
if (args.length > 2) |
35 |
> |
if (args.length > 2) |
36 |
|
runs = Integer.parseInt(args[2]); |
37 |
|
} catch (Exception e) { |
38 |
|
System.out.println(usage); |
43 |
|
System.out.println(usage); |
44 |
|
return; |
45 |
|
} |
46 |
< |
ForkJoinPool pool = procs == 0? new ForkJoinPool() : |
46 |
> |
ForkJoinPool pool = (procs == 0) ? ForkJoinPool.commonPool() : |
47 |
|
new ForkJoinPool(procs); |
48 |
< |
System.out.println("procs: " + pool.getParallelism() + |
48 |
> |
System.out.println("procs: " + pool.getParallelism() + |
49 |
|
" n: " + n + " runs: " + runs); |
50 |
|
for (int run = 0; run < runs; ++run) { |
51 |
|
double[][] m = new double[n][n]; |
64 |
|
pool.invoke(new LowerUpper(n, M)); |
65 |
|
long time = System.nanoTime() - start; |
66 |
|
double secs = ((double)time) / NPS; |
67 |
< |
System.out.printf("\tTime: %7.3f\n", secs); |
67 |
> |
System.out.printf("Time: %7.3f ", secs); |
68 |
> |
if ((run & 3) == 3) System.out.println(); |
69 |
|
|
70 |
|
if (CHECK) check(m, copy, n); |
71 |
|
} |
73 |
|
pool.shutdown(); |
74 |
|
} |
75 |
|
|
78 |
– |
|
76 |
|
static void randomInit(double[][] M, int n) { |
77 |
|
java.util.Random rng = new java.util.Random(); |
78 |
|
for (int i = 0; i < n; ++i) |
103 |
|
System.out.println("Max difference = " + maxDiff); |
104 |
|
} |
105 |
|
|
109 |
– |
|
106 |
|
// Blocks record underlying matrix, and offsets into current block |
107 |
|
static final class Block { |
108 |
|
final double[][] m; |
157 |
|
Block W01 = new Block(W.m, W.loRow, W.loCol+h); |
158 |
|
Block W10 = new Block(W.m, W.loRow+h, W.loCol); |
159 |
|
Block W11 = new Block(W.m, W.loRow+h, W.loCol+h); |
160 |
< |
|
160 |
> |
|
161 |
|
Seq2 s3 = seq(new Schur(h, V10, W01, M11), |
162 |
|
new Schur(h, V11, W11, M11)); |
163 |
|
s3.fork(); |
215 |
|
Block L10 = new Block(L.m, L.loRow+h, L.loCol); |
216 |
|
Block L11 = new Block(L.m, L.loRow+h, L.loCol+h); |
217 |
|
|
218 |
< |
|
223 |
< |
Seq3 s1 = |
218 |
> |
Seq3 s1 = |
219 |
|
seq(new Lower(h, L00, M00), |
220 |
|
new Schur(h, L10, M00, M10), |
221 |
|
new Lower(h, L11, M10)); |
222 |
< |
Seq3 s2 = |
222 |
> |
Seq3 s2 = |
223 |
|
seq(new Lower(h, L00, M01), |
224 |
|
new Schur(h, L10, M01, M11), |
225 |
|
new Lower(h, L11, M11)); |
230 |
|
} |
231 |
|
} |
232 |
|
|
238 |
– |
|
233 |
|
static final class Upper extends RecursiveAction { |
234 |
|
final int size; |
235 |
|
final Block U; |
253 |
|
} |
254 |
|
} |
255 |
|
|
262 |
– |
|
256 |
|
public void compute() { |
257 |
|
if (size == BLOCK_SIZE) { |
258 |
|
upper(); |
270 |
|
Block U10 = new Block(U.m, U.loRow+h, U.loCol); |
271 |
|
Block U11 = new Block(U.m, U.loRow+h, U.loCol+h); |
272 |
|
|
273 |
< |
Seq3 s1 = |
273 |
> |
Seq3 s1 = |
274 |
|
seq(new Upper(h, U00, M00), |
275 |
|
new Schur(h, M00, U01, M01), |
276 |
|
new Upper(h, U11, M01)); |
277 |
< |
Seq3 s2 = |
277 |
> |
Seq3 s2 = |
278 |
|
seq(new Upper(h, U00, M10), |
279 |
|
new Schur(h, M10, U01, M11), |
280 |
|
new Upper(h, U11, M11)); |
284 |
|
} |
285 |
|
} |
286 |
|
} |
294 |
– |
|
287 |
|
|
288 |
|
static final class LowerUpper extends RecursiveAction { |
289 |
|
final int size; |
331 |
|
} |
332 |
|
} |
333 |
|
|
334 |
< |
static Seq2 seq(RecursiveAction task1, |
335 |
< |
RecursiveAction task2) { |
336 |
< |
return new Seq2(task1, task2); |
334 |
> |
static Seq2 seq(RecursiveAction task1, |
335 |
> |
RecursiveAction task2) { |
336 |
> |
return new Seq2(task1, task2); |
337 |
|
} |
338 |
|
|
339 |
|
static final class Seq2 extends RecursiveAction { |
349 |
|
} |
350 |
|
} |
351 |
|
|
352 |
< |
static Seq3 seq(RecursiveAction task1, |
352 |
> |
static Seq3 seq(RecursiveAction task1, |
353 |
|
RecursiveAction task2, |
354 |
< |
RecursiveAction task3) { |
355 |
< |
return new Seq3(task1, task2, task3); |
354 |
> |
RecursiveAction task3) { |
355 |
> |
return new Seq3(task1, task2, task3); |
356 |
|
} |
357 |
|
|
358 |
|
static final class Seq3 extends RecursiveAction { |
359 |
|
final RecursiveAction fst; |
360 |
|
final RecursiveAction snd; |
361 |
|
final RecursiveAction thr; |
362 |
< |
public Seq3(RecursiveAction task1, |
362 |
> |
public Seq3(RecursiveAction task1, |
363 |
|
RecursiveAction task2, |
364 |
|
RecursiveAction task3) { |
365 |
|
fst = task1; |
372 |
|
thr.invoke(); |
373 |
|
} |
374 |
|
} |
383 |
– |
|
384 |
– |
|
385 |
– |
|
375 |
|
} |
387 |
– |
|