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 |
|
// Jacobi iteration on a mesh. Based loosely on a Filaments demo |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
– |
//import jsr166y.*; |
10 |
|
|
11 |
|
public class FJJacobi { |
12 |
|
|
13 |
< |
static final int DEFAULT_GRANULARITY = 4096; // 1024; |
13 |
> |
// static final int DEFAULT_GRANULARITY = 4096; |
14 |
> |
static final int DEFAULT_GRANULARITY = 256; |
15 |
|
|
16 |
|
/** |
17 |
|
* The maximum number of matrix cells |
18 |
|
* at which to stop recursing down and instead directly update. |
19 |
< |
**/ |
20 |
< |
|
19 |
> |
*/ |
20 |
|
static final double EPSILON = 0.0001; // convergence criterion |
21 |
|
|
22 |
|
public static void main(String[] args) throws Exception { |
38 |
|
return; |
39 |
|
} |
40 |
|
|
41 |
< |
ForkJoinPool fjp = new ForkJoinPool(); |
41 |
> |
// ForkJoinPool fjp = new ForkJoinPool(1); |
42 |
> |
ForkJoinPool fjp = ForkJoinPool.commonPool(); |
43 |
|
|
44 |
|
// allocate enough space for edges |
45 |
|
int dim = n+2; |
47 |
|
double[][] a = new double[dim][dim]; |
48 |
|
double[][] b = new double[dim][dim]; |
49 |
|
// Initialize interiors to small value |
50 |
< |
double smallVal = 1.0/dim; |
50 |
> |
double smallVal = EPSILON; // 1.0/dim; |
51 |
|
for (int i = 1; i < dim-1; ++i) { |
52 |
|
for (int j = 1; j < dim-1; ++j) |
53 |
|
a[i][j] = smallVal; |
54 |
|
} |
55 |
< |
|
56 |
< |
int nreps = 3; |
55 |
> |
// Fill all edges with 1's. |
56 |
> |
for (int k = 0; k < dim; ++k) { |
57 |
> |
a[k][0] = 1.0; |
58 |
> |
a[k][n+1] = 1.0; |
59 |
> |
a[0][k] = 1.0; |
60 |
> |
a[n+1][k] = 1.0; |
61 |
> |
b[k][0] = 1.0; |
62 |
> |
b[k][n+1] = 1.0; |
63 |
> |
b[0][k] = 1.0; |
64 |
> |
b[n+1][k] = 1.0; |
65 |
> |
} |
66 |
> |
int nreps = 10; |
67 |
|
for (int rep = 0; rep < nreps; ++rep) { |
58 |
– |
// Fill all edges with 1's. |
59 |
– |
for (int k = 0; k < dim; ++k) { |
60 |
– |
a[k][0] += 1.0; |
61 |
– |
a[k][n+1] += 1.0; |
62 |
– |
a[0][k] += 1.0; |
63 |
– |
a[n+1][k] += 1.0; |
64 |
– |
} |
68 |
|
Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity); |
69 |
|
|
70 |
|
long startTime = System.currentTimeMillis(); |
74 |
|
double secs = ((double)time) / 1000.0; |
75 |
|
|
76 |
|
System.out.println("Compute Time: " + secs); |
74 |
– |
System.out.println("Workers: " + fjp.getPoolSize()); |
77 |
|
System.out.println(fjp); |
78 |
+ |
Thread.sleep(1000); |
79 |
|
} |
80 |
|
} |
81 |
|
|
79 |
– |
|
82 |
|
abstract static class MatrixTree extends RecursiveAction { |
83 |
|
// maximum difference between old and new values |
84 |
|
double maxDiff; |
90 |
|
if (tryUnfork()) |
91 |
|
compute(); |
92 |
|
else { |
93 |
< |
// quietlyJoin(); |
92 |
< |
quietlyHelpJoin(); |
93 |
> |
quietlyJoin(); |
94 |
|
reinitialize(); |
95 |
|
} |
96 |
|
double m = maxDiff; |
97 |
|
return (md > m) ? md : m; |
98 |
|
} |
98 |
– |
|
99 |
|
} |
100 |
|
|
101 |
– |
|
101 |
|
static final class LeafNode extends MatrixTree { |
102 |
|
final double[][] A; // matrix to get old values from |
103 |
|
final double[][] B; // matrix to put new values into |
161 |
|
} |
162 |
|
} |
163 |
|
|
165 |
– |
|
164 |
|
static final class TwoNode extends MatrixTree { |
165 |
|
final MatrixTree q1; |
166 |
|
final MatrixTree q2; |
176 |
|
|
177 |
|
} |
178 |
|
|
181 |
– |
|
179 |
|
static final class Driver extends RecursiveAction { |
180 |
|
MatrixTree mat; |
181 |
|
double[][] A; double[][] B; |
247 |
|
System.out.println("max diff after " + steps + " steps = " + md); |
248 |
|
} |
249 |
|
} |
253 |
– |
|
254 |
– |
|
250 |
|
} |