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 |
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 { |
23 |
|
int n = 2048; |
24 |
|
int steps = 1000; |
25 |
|
int granularity = DEFAULT_GRANULARITY; |
26 |
< |
|
26 |
> |
|
27 |
|
try { |
28 |
|
if (args.length > 0) |
29 |
|
n = Integer.parseInt(args[0]); |
30 |
|
if (args.length > 1) |
31 |
|
steps = Integer.parseInt(args[1]); |
32 |
< |
if (args.length > 2) |
32 |
> |
if (args.length > 2) |
33 |
|
granularity = Integer.parseInt(args[2]); |
34 |
|
} |
35 |
< |
|
35 |
> |
|
36 |
|
catch (Exception e) { |
37 |
|
System.out.println("Usage: java FJJacobi <matrix size> <max steps> [<leafcells>]"); |
38 |
|
return; |
46 |
|
double[][] a = new double[dim][dim]; |
47 |
|
double[][] b = new double[dim][dim]; |
48 |
|
// Initialize interiors to small value |
49 |
< |
double smallVal = 1.0/dim; |
49 |
> |
double smallVal = EPSILON; // 1.0/dim; |
50 |
|
for (int i = 1; i < dim-1; ++i) { |
51 |
|
for (int j = 1; j < dim-1; ++j) |
52 |
|
a[i][j] = smallVal; |
53 |
|
} |
54 |
< |
|
55 |
< |
int nreps = 3; |
54 |
> |
// Fill all edges with 1's. |
55 |
> |
for (int k = 0; k < dim; ++k) { |
56 |
> |
a[k][0] = 1.0; |
57 |
> |
a[k][n+1] = 1.0; |
58 |
> |
a[0][k] = 1.0; |
59 |
> |
a[n+1][k] = 1.0; |
60 |
> |
b[k][0] = 1.0; |
61 |
> |
b[k][n+1] = 1.0; |
62 |
> |
b[0][k] = 1.0; |
63 |
> |
b[n+1][k] = 1.0; |
64 |
> |
} |
65 |
> |
int nreps = 10; |
66 |
|
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 |
– |
} |
67 |
|
Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity); |
68 |
< |
|
68 |
> |
|
69 |
|
long startTime = System.currentTimeMillis(); |
70 |
|
fjp.invoke(driver); |
71 |
< |
|
71 |
> |
|
72 |
|
long time = System.currentTimeMillis() - startTime; |
73 |
|
double secs = ((double)time) / 1000.0; |
74 |
< |
|
74 |
> |
|
75 |
|
System.out.println("Compute Time: " + secs); |
74 |
– |
System.out.println("Workers: " + fjp.getPoolSize()); |
76 |
|
System.out.println(fjp); |
77 |
|
} |
78 |
|
} |
79 |
|
|
79 |
– |
|
80 |
|
abstract static class MatrixTree extends RecursiveAction { |
81 |
|
// maximum difference between old and new values |
82 |
< |
double maxDiff; |
83 |
< |
public final double directCompute() { |
84 |
< |
compute(); |
82 |
> |
double maxDiff; |
83 |
> |
public final double directCompute() { |
84 |
> |
compute(); |
85 |
|
return maxDiff; |
86 |
|
} |
87 |
|
public final double joinAndReinitialize(double md) { |
88 |
|
if (tryUnfork()) |
89 |
< |
compute(); |
89 |
> |
compute(); |
90 |
|
else { |
91 |
< |
// quietlyJoin(); |
92 |
< |
quietlyHelpJoin(); |
91 |
> |
quietlyJoin(); |
92 |
|
reinitialize(); |
93 |
|
} |
94 |
|
double m = maxDiff; |
95 |
< |
return (md > m)? md : m; |
95 |
> |
return (md > m) ? md : m; |
96 |
|
} |
97 |
|
|
98 |
|
} |
99 |
|
|
101 |
– |
|
100 |
|
static final class LeafNode extends MatrixTree { |
101 |
|
final double[][] A; // matrix to get old values from |
102 |
|
final double[][] B; // matrix to put new values into |
107 |
|
|
108 |
|
int steps = 0; // track even/odd steps |
109 |
|
|
110 |
< |
LeafNode(double[][] A, double[][] B, |
110 |
> |
LeafNode(double[][] A, double[][] B, |
111 |
|
int loRow, int hiRow, |
112 |
|
int loCol, int hiCol) { |
113 |
|
this.A = A; this.B = B; |
115 |
|
this.loCol = loCol; this.hiCol = hiCol; |
116 |
|
} |
117 |
|
|
118 |
< |
public void compute() { |
118 |
> |
public void compute() { |
119 |
|
boolean AtoB = (steps++ & 1) == 0; |
120 |
< |
double[][] a = (AtoB)? A : B; |
121 |
< |
double[][] b = (AtoB)? B : A; |
120 |
> |
double[][] a = AtoB ? A : B; |
121 |
> |
double[][] b = AtoB ? B : A; |
122 |
|
|
123 |
|
double md = 0.0; // local for computing max diff |
124 |
|
|
143 |
|
final MatrixTree q2; |
144 |
|
final MatrixTree q3; |
145 |
|
final MatrixTree q4; |
146 |
< |
FourNode(MatrixTree q1, MatrixTree q2, |
146 |
> |
FourNode(MatrixTree q1, MatrixTree q2, |
147 |
|
MatrixTree q3, MatrixTree q4) { |
148 |
|
this.q1 = q1; this.q2 = q2; this.q3 = q3; this.q4 = q4; |
149 |
|
} |
150 |
|
|
151 |
< |
public void compute() { |
151 |
> |
public void compute() { |
152 |
|
q4.fork(); |
153 |
|
q3.fork(); |
154 |
|
q2.fork(); |
159 |
|
maxDiff = md; |
160 |
|
} |
161 |
|
} |
164 |
– |
|
162 |
|
|
163 |
|
static final class TwoNode extends MatrixTree { |
164 |
|
final MatrixTree q1; |
168 |
|
this.q1 = q1; this.q2 = q2; |
169 |
|
} |
170 |
|
|
171 |
< |
public void compute() { |
171 |
> |
public void compute() { |
172 |
|
q2.fork(); |
173 |
|
maxDiff = q2.joinAndReinitialize(q1.directCompute()); |
174 |
|
} |
175 |
< |
|
175 |
> |
|
176 |
|
} |
180 |
– |
|
177 |
|
|
178 |
|
static final class Driver extends RecursiveAction { |
179 |
|
MatrixTree mat; |
184 |
|
final int leafs; |
185 |
|
int nleaf; |
186 |
|
|
187 |
< |
Driver(double[][] A, double[][] B, |
187 |
> |
Driver(double[][] A, double[][] B, |
188 |
|
int firstRow, int lastRow, |
189 |
|
int firstCol, int lastCol, |
190 |
|
int steps, int leafs) { |
208 |
|
|
209 |
|
int mr = (lr + hr) >>> 1; // midpoints |
210 |
|
int mc = (lc + hc) >>> 1; |
211 |
< |
|
211 |
> |
|
212 |
|
int hrows = (mr - lr + 1); |
213 |
|
int hcols = (mc - lc + 1); |
214 |
|
|
229 |
|
else { |
230 |
|
return new TwoNode(build(a, b, lr, mr, lc, hc, leafs), |
231 |
|
build(a, b, mr+1, hr, lc, hc, leafs)); |
232 |
< |
|
232 |
> |
|
233 |
|
} |
234 |
|
} |
235 |
|
|
246 |
|
System.out.println("max diff after " + steps + " steps = " + md); |
247 |
|
} |
248 |
|
} |
253 |
– |
|
254 |
– |
|
249 |
|
} |