38 |
|
return; |
39 |
|
} |
40 |
|
|
41 |
< |
ForkJoinPool fjp = new ForkJoinPool(); |
41 |
> |
ForkJoinPool fjp = ForkJoinPool.commonPool(); |
42 |
|
|
43 |
|
// allocate enough space for edges |
44 |
|
int dim = n+2; |
67 |
|
Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity); |
68 |
|
|
69 |
|
long startTime = System.currentTimeMillis(); |
70 |
< |
fjp.invoke(driver); |
70 |
> |
driver.invoke(); |
71 |
|
|
72 |
|
long time = System.currentTimeMillis() - startTime; |
73 |
|
double secs = ((double)time) / 1000.0; |
74 |
|
|
75 |
|
System.out.println("Compute Time: " + secs); |
76 |
|
System.out.println(fjp); |
77 |
+ |
Thread.sleep(1000); |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
< |
|
81 |
< |
abstract static class MatrixTree extends CountedCompleter { |
81 |
> |
abstract static class MatrixTree extends CountedCompleter<Void> { |
82 |
|
// maximum difference between old and new values |
83 |
|
double maxDiff; |
84 |
< |
MatrixTree(CountedCompleter p, int c) { super(p, c); } |
84 |
> |
MatrixTree(CountedCompleter<?> p, int c) { super(p, c); } |
85 |
|
} |
86 |
|
|
87 |
– |
|
87 |
|
static final class LeafNode extends MatrixTree { |
88 |
|
final double[][] A; // matrix to get old values from |
89 |
|
final double[][] B; // matrix to put new values into |
94 |
|
|
95 |
|
int steps = 0; // track even/odd steps |
96 |
|
|
97 |
< |
LeafNode(CountedCompleter p, |
97 |
> |
LeafNode(CountedCompleter<?> p, |
98 |
|
double[][] A, double[][] B, |
99 |
|
int loRow, int hiRow, |
100 |
|
int loCol, int hiCol) { |
110 |
|
double[][] b = AtoB ? B : A; |
111 |
|
|
112 |
|
double md = 0.0; // local for computing max diff |
114 |
– |
|
113 |
|
for (int i = loRow; i <= hiRow; ++i) { |
114 |
|
for (int j = loCol; j <= hiCol; ++j) { |
115 |
|
double v = 0.25 * (a[i-1][j] + a[i][j-1] + |
116 |
|
a[i+1][j] + a[i][j+1]); |
117 |
|
b[i][j] = v; |
118 |
< |
|
119 |
< |
double diff = v - a[i][j]; |
118 |
> |
double prev = a[i][j]; |
119 |
> |
double diff = v - prev; |
120 |
|
if (diff < 0) diff = -diff; |
121 |
|
if (diff > md) md = diff; |
122 |
|
} |
123 |
|
} |
124 |
|
|
125 |
|
maxDiff = md; |
126 |
< |
tryComplete(); |
126 |
> |
tryComplete(); |
127 |
|
} |
128 |
|
} |
129 |
|
|
132 |
|
MatrixTree q2; |
133 |
|
MatrixTree q3; |
134 |
|
MatrixTree q4; |
135 |
< |
FourNode(CountedCompleter p) { |
135 |
> |
FourNode(CountedCompleter<?> p) { |
136 |
|
super(p, 3); |
137 |
|
} |
138 |
|
|
139 |
< |
public void onCompletion(CountedCompleter caller) { |
139 |
> |
public void onCompletion(CountedCompleter<?> caller) { |
140 |
|
double md = q1.maxDiff, m; |
141 |
|
if ((m = q2.maxDiff) > md) |
142 |
|
md = m; |
156 |
|
} |
157 |
|
} |
158 |
|
|
161 |
– |
|
159 |
|
static final class TwoNode extends MatrixTree { |
160 |
|
MatrixTree q1; |
161 |
|
MatrixTree q2; |
162 |
|
|
163 |
< |
TwoNode(CountedCompleter p) { |
163 |
> |
TwoNode(CountedCompleter<?> p) { |
164 |
|
super(p, 1); |
165 |
|
} |
166 |
|
|
167 |
< |
public void onCompletion(CountedCompleter caller) { |
167 |
> |
public void onCompletion(CountedCompleter<?> caller) { |
168 |
|
double md = q1.maxDiff, m; |
169 |
|
if ((m = q2.maxDiff) > md) |
170 |
|
md = m; |
176 |
|
q2.fork(); |
177 |
|
q1.compute(); |
178 |
|
} |
182 |
– |
|
179 |
|
} |
180 |
|
|
181 |
|
static final class Driver extends RecursiveAction { |
201 |
|
this.leafs = leafs; |
202 |
|
mat = build(null, A, B, firstRow, lastRow, firstCol, lastCol, leafs); |
203 |
|
System.out.println("Using " + nleaf + " segments"); |
208 |
– |
|
204 |
|
} |
205 |
|
|
206 |
|
MatrixTree build(MatrixTree p, |
255 |
|
System.out.println("max diff after " + steps + " steps = " + md); |
256 |
|
} |
257 |
|
} |
263 |
– |
|
264 |
– |
|
258 |
|
} |