12 |
|
|
13 |
|
static final int DEFAULT_GRANULARITY = 4096; // 1024; |
14 |
|
|
15 |
< |
/** |
16 |
< |
* The maximum number of matrix cells |
15 |
> |
/** |
16 |
> |
* The maximum number of matrix cells |
17 |
|
* at which to stop recursing down and instead directly update. |
18 |
|
**/ |
19 |
|
|
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 = EPSILON; // 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; |
65 |
|
int nreps = 10; |
66 |
|
for (int rep = 0; rep < nreps; ++rep) { |
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); |
76 |
|
System.out.println(fjp); |
77 |
|
} |
80 |
|
|
81 |
|
abstract static class MatrixTree extends RecursiveAction { |
82 |
|
// maximum difference between old and new values |
83 |
< |
double maxDiff; |
84 |
< |
public final double directCompute() { |
85 |
< |
compute(); |
83 |
> |
double maxDiff; |
84 |
> |
public final double directCompute() { |
85 |
> |
compute(); |
86 |
|
return maxDiff; |
87 |
|
} |
88 |
|
public final double joinAndReinitialize(double md) { |
89 |
|
if (tryUnfork()) |
90 |
< |
compute(); |
90 |
> |
compute(); |
91 |
|
else { |
92 |
|
quietlyJoin(); |
93 |
|
reinitialize(); |
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; |
129 |
|
double v = 0.25 * (a[i-1][j] + a[i][j-1] + |
130 |
|
a[i+1][j] + a[i][j+1]); |
131 |
|
b[i][j] = v; |
132 |
< |
|
132 |
> |
|
133 |
|
double diff = v - a[i][j]; |
134 |
|
if (diff < 0) diff = -diff; |
135 |
|
if (diff > md) md = diff; |
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 |
|
|