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 { |
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) ? new ForkJoinPool() : |
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]; |
72 |
|
pool.shutdown(); |
73 |
|
} |
74 |
|
|
78 |
– |
|
75 |
|
static void randomInit(double[][] M, int n) { |
76 |
|
java.util.Random rng = new java.util.Random(); |
77 |
|
for (int i = 0; i < n; ++i) |
102 |
|
System.out.println("Max difference = " + maxDiff); |
103 |
|
} |
104 |
|
|
109 |
– |
|
105 |
|
// Blocks record underlying matrix, and offsets into current block |
106 |
|
static final class Block { |
107 |
|
final double[][] m; |
156 |
|
Block W01 = new Block(W.m, W.loRow, W.loCol+h); |
157 |
|
Block W10 = new Block(W.m, W.loRow+h, W.loCol); |
158 |
|
Block W11 = new Block(W.m, W.loRow+h, W.loCol+h); |
159 |
< |
|
159 |
> |
|
160 |
|
Seq2 s3 = seq(new Schur(h, V10, W01, M11), |
161 |
|
new Schur(h, V11, W11, M11)); |
162 |
|
s3.fork(); |
214 |
|
Block L10 = new Block(L.m, L.loRow+h, L.loCol); |
215 |
|
Block L11 = new Block(L.m, L.loRow+h, L.loCol+h); |
216 |
|
|
217 |
< |
|
223 |
< |
Seq3 s1 = |
217 |
> |
Seq3 s1 = |
218 |
|
seq(new Lower(h, L00, M00), |
219 |
|
new Schur(h, L10, M00, M10), |
220 |
|
new Lower(h, L11, M10)); |
221 |
< |
Seq3 s2 = |
221 |
> |
Seq3 s2 = |
222 |
|
seq(new Lower(h, L00, M01), |
223 |
|
new Schur(h, L10, M01, M11), |
224 |
|
new Lower(h, L11, M11)); |
229 |
|
} |
230 |
|
} |
231 |
|
|
238 |
– |
|
232 |
|
static final class Upper extends RecursiveAction { |
233 |
|
final int size; |
234 |
|
final Block U; |
252 |
|
} |
253 |
|
} |
254 |
|
|
262 |
– |
|
255 |
|
public void compute() { |
256 |
|
if (size == BLOCK_SIZE) { |
257 |
|
upper(); |
269 |
|
Block U10 = new Block(U.m, U.loRow+h, U.loCol); |
270 |
|
Block U11 = new Block(U.m, U.loRow+h, U.loCol+h); |
271 |
|
|
272 |
< |
Seq3 s1 = |
272 |
> |
Seq3 s1 = |
273 |
|
seq(new Upper(h, U00, M00), |
274 |
|
new Schur(h, M00, U01, M01), |
275 |
|
new Upper(h, U11, M01)); |
276 |
< |
Seq3 s2 = |
276 |
> |
Seq3 s2 = |
277 |
|
seq(new Upper(h, U00, M10), |
278 |
|
new Schur(h, M10, U01, M11), |
279 |
|
new Upper(h, U11, M11)); |
283 |
|
} |
284 |
|
} |
285 |
|
} |
294 |
– |
|
286 |
|
|
287 |
|
static final class LowerUpper extends RecursiveAction { |
288 |
|
final int size; |
330 |
|
} |
331 |
|
} |
332 |
|
|
333 |
< |
static Seq2 seq(RecursiveAction task1, |
334 |
< |
RecursiveAction task2) { |
335 |
< |
return new Seq2(task1, task2); |
333 |
> |
static Seq2 seq(RecursiveAction task1, |
334 |
> |
RecursiveAction task2) { |
335 |
> |
return new Seq2(task1, task2); |
336 |
|
} |
337 |
|
|
338 |
|
static final class Seq2 extends RecursiveAction { |
348 |
|
} |
349 |
|
} |
350 |
|
|
351 |
< |
static Seq3 seq(RecursiveAction task1, |
351 |
> |
static Seq3 seq(RecursiveAction task1, |
352 |
|
RecursiveAction task2, |
353 |
< |
RecursiveAction task3) { |
354 |
< |
return new Seq3(task1, task2, task3); |
353 |
> |
RecursiveAction task3) { |
354 |
> |
return new Seq3(task1, task2, task3); |
355 |
|
} |
356 |
|
|
357 |
|
static final class Seq3 extends RecursiveAction { |
358 |
|
final RecursiveAction fst; |
359 |
|
final RecursiveAction snd; |
360 |
|
final RecursiveAction thr; |
361 |
< |
public Seq3(RecursiveAction task1, |
361 |
> |
public Seq3(RecursiveAction task1, |
362 |
|
RecursiveAction task2, |
363 |
|
RecursiveAction task3) { |
364 |
|
fst = task1; |
371 |
|
thr.invoke(); |
372 |
|
} |
373 |
|
} |
383 |
– |
|
384 |
– |
|
385 |
– |
|
374 |
|
} |
387 |
– |
|