ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/CCJacobi.java
Revision: 1.4
Committed: Thu Jan 15 18:34:18 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +0 -5 lines
Log Message:
delete extraneous 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 CCJacobi {
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 CountedCompleter<Void> {
81 // maximum difference between old and new values
82 double maxDiff;
83 MatrixTree(CountedCompleter<?> p, int c) { super(p, c); }
84 }
85
86 static final class LeafNode extends MatrixTree {
87 final double[][] A; // matrix to get old values from
88 final double[][] B; // matrix to put new values into
89
90 // indices of current submatrix
91 final int loRow; final int hiRow;
92 final int loCol; final int hiCol;
93
94 int steps = 0; // track even/odd steps
95
96 LeafNode(CountedCompleter<?> p,
97 double[][] A, double[][] B,
98 int loRow, int hiRow,
99 int loCol, int hiCol) {
100 super(p, 0);
101 this.A = A; this.B = B;
102 this.loRow = loRow; this.hiRow = hiRow;
103 this.loCol = loCol; this.hiCol = hiCol;
104 }
105
106 public final void compute() {
107 boolean AtoB = (steps++ & 1) == 0;
108 double[][] a = AtoB ? A : B;
109 double[][] b = AtoB ? B : A;
110
111 double md = 0.0; // local for computing max diff
112
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];
120 if (diff < 0) diff = -diff;
121 if (diff > md) md = diff;
122 }
123 }
124
125 maxDiff = md;
126 tryComplete();
127 }
128 }
129
130 static final class FourNode extends MatrixTree {
131 MatrixTree q1;
132 MatrixTree q2;
133 MatrixTree q3;
134 MatrixTree q4;
135 FourNode(CountedCompleter<?> p) {
136 super(p, 3);
137 }
138
139 public void onCompletion(CountedCompleter<?> caller) {
140 double md = q1.maxDiff, m;
141 if ((m = q2.maxDiff) > md)
142 md = m;
143 if ((m = q3.maxDiff) > md)
144 md = m;
145 if ((m = q4.maxDiff) > md)
146 md = m;
147 maxDiff = md;
148 setPendingCount(3);
149 }
150
151 public final void compute() {
152 q4.fork();
153 q3.fork();
154 q2.fork();
155 q1.compute();
156 }
157 }
158
159 static final class TwoNode extends MatrixTree {
160 MatrixTree q1;
161 MatrixTree q2;
162
163 TwoNode(CountedCompleter<?> p) {
164 super(p, 1);
165 }
166
167 public void onCompletion(CountedCompleter<?> caller) {
168 double md = q1.maxDiff, m;
169 if ((m = q2.maxDiff) > md)
170 md = m;
171 maxDiff = md;
172 setPendingCount(1);
173 }
174
175 public final void compute() {
176 q2.fork();
177 q1.compute();
178 }
179
180 }
181
182 static final class Driver extends RecursiveAction {
183 MatrixTree mat;
184 double[][] A; double[][] B;
185 int firstRow; int lastRow;
186 int firstCol; int lastCol;
187 final int steps;
188 final int leafs;
189 int nleaf;
190
191 Driver(double[][] A, double[][] B,
192 int firstRow, int lastRow,
193 int firstCol, int lastCol,
194 int steps, int leafs) {
195 this.A = A;
196 this.B = B;
197 this.firstRow = firstRow;
198 this.firstCol = firstCol;
199 this.lastRow = lastRow;
200 this.lastCol = lastCol;
201 this.steps = steps;
202 this.leafs = leafs;
203 mat = build(null, A, B, firstRow, lastRow, firstCol, lastCol, leafs);
204 System.out.println("Using " + nleaf + " segments");
205
206 }
207
208 MatrixTree build(MatrixTree p,
209 double[][] a, double[][] b,
210 int lr, int hr, int lc, int hc, int leafs) {
211 int rows = (hr - lr + 1);
212 int cols = (hc - lc + 1);
213
214 int mr = (lr + hr) >>> 1; // midpoints
215 int mc = (lc + hc) >>> 1;
216
217 int hrows = (mr - lr + 1);
218 int hcols = (mc - lc + 1);
219
220 if (rows * cols <= leafs) {
221 ++nleaf;
222 return new LeafNode(p, a, b, lr, hr, lc, hc);
223 }
224 else if (hrows * hcols >= leafs) {
225 FourNode q = new FourNode(p);
226 q.q1 = build(q, a, b, lr, mr, lc, mc, leafs);
227 q.q2 = build(q, a, b, lr, mr, mc+1, hc, leafs);
228 q.q3 = build(q, a, b, mr+1, hr, lc, mc, leafs);
229 q.q4 = build(q, a, b, mr+1, hr, mc+1, hc, leafs);
230 return q;
231 }
232 else if (cols >= rows) {
233 TwoNode q = new TwoNode(p);
234 q.q1 = build(q, a, b, lr, hr, lc, mc, leafs);
235 q.q2 = build(q, a, b, lr, hr, mc+1, hc, leafs);
236 return q;
237 }
238 else {
239 TwoNode q = new TwoNode(p);
240 q.q1 = build(q, a, b, lr, mr, lc, hc, leafs);
241 q.q2 = build(q, a, b, mr+1, hr, lc, hc, leafs);
242 return q;
243 }
244 }
245
246 static void doCompute(MatrixTree m, int s) {
247 for (int i = 0; i < s; ++i) {
248 m.setPendingCount(3);
249 m.invoke();
250 m.reinitialize();
251 }
252 }
253
254 public void compute() {
255 doCompute(mat, steps);
256 double md = mat.maxDiff;
257 System.out.println("max diff after " + steps + " steps = " + md);
258 }
259 }
260 }