ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FJJacobi.java
Revision: 1.13
Committed: Mon Aug 10 03:13:33 2015 UTC (8 years, 9 months ago) by jsr166
Branch: MAIN
Changes since 1.12: +0 -1 lines
Log Message:
delete unwanted blank lines

File Contents

# Content
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/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
11 public class FJJacobi {
12
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 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
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)
33 granularity = Integer.parseInt(args[2]);
34 }
35
36 catch (Exception e) {
37 System.out.println("Usage: java FJJacobi <matrix size> <max steps> [<leafcells>]");
38 return;
39 }
40
41 ForkJoinPool fjp = new ForkJoinPool();
42
43 // allocate enough space for edges
44 int dim = n+2;
45 int ncells = dim * dim;
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;
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 // 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) {
67 Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity);
68
69 long startTime = System.currentTimeMillis();
70 fjp.invoke(driver);
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 }
78 }
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();
85 return maxDiff;
86 }
87 public final double joinAndReinitialize(double md) {
88 if (tryUnfork())
89 compute();
90 else {
91 quietlyJoin();
92 reinitialize();
93 }
94 double m = maxDiff;
95 return (md > m) ? md : m;
96 }
97 }
98
99 static final class LeafNode extends MatrixTree {
100 final double[][] A; // matrix to get old values from
101 final double[][] B; // matrix to put new values into
102
103 // indices of current submatrix
104 final int loRow; final int hiRow;
105 final int loCol; final int hiCol;
106
107 int steps = 0; // track even/odd steps
108
109 LeafNode(double[][] A, double[][] B,
110 int loRow, int hiRow,
111 int loCol, int hiCol) {
112 this.A = A; this.B = B;
113 this.loRow = loRow; this.hiRow = hiRow;
114 this.loCol = loCol; this.hiCol = hiCol;
115 }
116
117 public void compute() {
118 boolean AtoB = (steps++ & 1) == 0;
119 double[][] a = AtoB ? A : B;
120 double[][] b = AtoB ? B : A;
121
122 double md = 0.0; // local for computing max diff
123
124 for (int i = loRow; i <= hiRow; ++i) {
125 for (int j = loCol; j <= hiCol; ++j) {
126 double v = 0.25 * (a[i-1][j] + a[i][j-1] +
127 a[i+1][j] + a[i][j+1]);
128 b[i][j] = v;
129
130 double diff = v - a[i][j];
131 if (diff < 0) diff = -diff;
132 if (diff > md) md = diff;
133 }
134 }
135
136 maxDiff = md;
137 }
138 }
139
140 static final class FourNode extends MatrixTree {
141 final MatrixTree q1;
142 final MatrixTree q2;
143 final MatrixTree q3;
144 final MatrixTree q4;
145 FourNode(MatrixTree q1, MatrixTree q2,
146 MatrixTree q3, MatrixTree q4) {
147 this.q1 = q1; this.q2 = q2; this.q3 = q3; this.q4 = q4;
148 }
149
150 public void compute() {
151 q4.fork();
152 q3.fork();
153 q2.fork();
154 double md = q1.directCompute();
155 md = q2.joinAndReinitialize(md);
156 md = q3.joinAndReinitialize(md);
157 md = q4.joinAndReinitialize(md);
158 maxDiff = md;
159 }
160 }
161
162 static final class TwoNode extends MatrixTree {
163 final MatrixTree q1;
164 final MatrixTree q2;
165
166 TwoNode(MatrixTree q1, MatrixTree q2) {
167 this.q1 = q1; this.q2 = q2;
168 }
169
170 public void compute() {
171 q2.fork();
172 maxDiff = q2.joinAndReinitialize(q1.directCompute());
173 }
174
175 }
176
177 static final class Driver extends RecursiveAction {
178 MatrixTree mat;
179 double[][] A; double[][] B;
180 int firstRow; int lastRow;
181 int firstCol; int lastCol;
182 final int steps;
183 final int leafs;
184 int nleaf;
185
186 Driver(double[][] A, double[][] B,
187 int firstRow, int lastRow,
188 int firstCol, int lastCol,
189 int steps, int leafs) {
190 this.A = A;
191 this.B = B;
192 this.firstRow = firstRow;
193 this.firstCol = firstCol;
194 this.lastRow = lastRow;
195 this.lastCol = lastCol;
196 this.steps = steps;
197 this.leafs = leafs;
198 mat = build(A, B, firstRow, lastRow, firstCol, lastCol, leafs);
199 System.out.println("Using " + nleaf + " segments");
200
201 }
202
203 MatrixTree build(double[][] a, double[][] b,
204 int lr, int hr, int lc, int hc, int leafs) {
205 int rows = (hr - lr + 1);
206 int cols = (hc - lc + 1);
207
208 int mr = (lr + hr) >>> 1; // midpoints
209 int mc = (lc + hc) >>> 1;
210
211 int hrows = (mr - lr + 1);
212 int hcols = (mc - lc + 1);
213
214 if (rows * cols <= leafs) {
215 ++nleaf;
216 return new LeafNode(a, b, lr, hr, lc, hc);
217 }
218 else if (hrows * hcols >= leafs) {
219 return new FourNode(build(a, b, lr, mr, lc, mc, leafs),
220 build(a, b, lr, mr, mc+1, hc, leafs),
221 build(a, b, mr+1, hr, lc, mc, leafs),
222 build(a, b, mr+1, hr, mc+1, hc, leafs));
223 }
224 else if (cols >= rows) {
225 return new TwoNode(build(a, b, lr, hr, lc, mc, leafs),
226 build(a, b, lr, hr, mc+1, hc, leafs));
227 }
228 else {
229 return new TwoNode(build(a, b, lr, mr, lc, hc, leafs),
230 build(a, b, mr+1, hr, lc, hc, leafs));
231
232 }
233 }
234
235 static void doCompute(MatrixTree m, int s) {
236 for (int i = 0; i < s; ++i) {
237 m.invoke();
238 m.reinitialize();
239 }
240 }
241
242 public void compute() {
243 doCompute(mat, steps);
244 double md = mat.maxDiff;
245 System.out.println("max diff after " + steps + " steps = " + md);
246 }
247 }
248 }