13 |
|
|
14 |
|
static final int DEFAULT_GRANULARITY = 4096; // 1024; |
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 |
|
|
24 |
|
int n = 2048; |
25 |
|
int steps = 1000; |
26 |
|
int granularity = DEFAULT_GRANULARITY; |
27 |
< |
|
27 |
> |
|
28 |
|
try { |
29 |
|
if (args.length > 0) |
30 |
|
n = Integer.parseInt(args[0]); |
31 |
|
if (args.length > 1) |
32 |
|
steps = Integer.parseInt(args[1]); |
33 |
< |
if (args.length > 2) |
33 |
> |
if (args.length > 2) |
34 |
|
granularity = Integer.parseInt(args[2]); |
35 |
|
} |
36 |
< |
|
36 |
> |
|
37 |
|
catch (Exception e) { |
38 |
|
System.out.println("Usage: java FJJacobi <matrix size> <max steps> [<leafcells>]"); |
39 |
|
return; |
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 = 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 |
< |
|
55 |
> |
|
56 |
|
int nreps = 3; |
57 |
|
for (int rep = 0; rep < nreps; ++rep) { |
58 |
|
// Fill all edges with 1's. |
63 |
|
a[n+1][k] += 1.0; |
64 |
|
} |
65 |
|
Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity); |
66 |
< |
|
66 |
> |
|
67 |
|
long startTime = System.currentTimeMillis(); |
68 |
|
fjp.invoke(driver); |
69 |
< |
|
69 |
> |
|
70 |
|
long time = System.currentTimeMillis() - startTime; |
71 |
|
double secs = ((double)time) / 1000.0; |
72 |
< |
|
72 |
> |
|
73 |
|
System.out.println("Compute Time: " + secs); |
74 |
|
System.out.println("Workers: " + fjp.getPoolSize()); |
75 |
|
System.out.println(fjp); |
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(); |
93 |
|
reinitialize(); |
94 |
|
} |
95 |
|
double m = maxDiff; |
96 |
< |
return (md > m)? md : m; |
96 |
> |
return (md > m) ? md : m; |
97 |
|
} |
98 |
|
|
99 |
|
} |
109 |
|
|
110 |
|
int steps = 0; // track even/odd steps |
111 |
|
|
112 |
< |
LeafNode(double[][] A, double[][] B, |
112 |
> |
LeafNode(double[][] A, double[][] B, |
113 |
|
int loRow, int hiRow, |
114 |
|
int loCol, int hiCol) { |
115 |
|
this.A = A; this.B = B; |
117 |
|
this.loCol = loCol; this.hiCol = hiCol; |
118 |
|
} |
119 |
|
|
120 |
< |
public void compute() { |
120 |
> |
public void compute() { |
121 |
|
boolean AtoB = (steps++ & 1) == 0; |
122 |
< |
double[][] a = (AtoB)? A : B; |
123 |
< |
double[][] b = (AtoB)? B : A; |
122 |
> |
double[][] a = AtoB ? A : B; |
123 |
> |
double[][] b = AtoB ? B : A; |
124 |
|
|
125 |
|
double md = 0.0; // local for computing max diff |
126 |
|
|
145 |
|
final MatrixTree q2; |
146 |
|
final MatrixTree q3; |
147 |
|
final MatrixTree q4; |
148 |
< |
FourNode(MatrixTree q1, MatrixTree q2, |
148 |
> |
FourNode(MatrixTree q1, MatrixTree q2, |
149 |
|
MatrixTree q3, MatrixTree q4) { |
150 |
|
this.q1 = q1; this.q2 = q2; this.q3 = q3; this.q4 = q4; |
151 |
|
} |
152 |
|
|
153 |
< |
public void compute() { |
153 |
> |
public void compute() { |
154 |
|
q4.fork(); |
155 |
|
q3.fork(); |
156 |
|
q2.fork(); |
161 |
|
maxDiff = md; |
162 |
|
} |
163 |
|
} |
164 |
< |
|
164 |
> |
|
165 |
|
|
166 |
|
static final class TwoNode extends MatrixTree { |
167 |
|
final MatrixTree q1; |
171 |
|
this.q1 = q1; this.q2 = q2; |
172 |
|
} |
173 |
|
|
174 |
< |
public void compute() { |
174 |
> |
public void compute() { |
175 |
|
q2.fork(); |
176 |
|
maxDiff = q2.joinAndReinitialize(q1.directCompute()); |
177 |
|
} |
178 |
< |
|
178 |
> |
|
179 |
|
} |
180 |
< |
|
180 |
> |
|
181 |
|
|
182 |
|
static final class Driver extends RecursiveAction { |
183 |
|
MatrixTree mat; |
188 |
|
final int leafs; |
189 |
|
int nleaf; |
190 |
|
|
191 |
< |
Driver(double[][] A, double[][] B, |
191 |
> |
Driver(double[][] A, double[][] B, |
192 |
|
int firstRow, int lastRow, |
193 |
|
int firstCol, int lastCol, |
194 |
|
int steps, int leafs) { |
212 |
|
|
213 |
|
int mr = (lr + hr) >>> 1; // midpoints |
214 |
|
int mc = (lc + hc) >>> 1; |
215 |
< |
|
215 |
> |
|
216 |
|
int hrows = (mr - lr + 1); |
217 |
|
int hcols = (mc - lc + 1); |
218 |
|
|
233 |
|
else { |
234 |
|
return new TwoNode(build(a, b, lr, mr, lc, hc, leafs), |
235 |
|
build(a, b, mr+1, hr, lc, hc, leafs)); |
236 |
< |
|
236 |
> |
|
237 |
|
} |
238 |
|
} |
239 |
|
|