ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FJJacobi.java
Revision: 1.11
Committed: Mon Apr 9 13:18:06 2012 UTC (12 years, 1 month ago) by dl
Branch: MAIN
Changes since 1.10: +2 -1 lines
Log Message:
Add tests/demos for CountedCompleters

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.10 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
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 dl 1.11 // static final int DEFAULT_GRANULARITY = 4096;
14     static final int DEFAULT_GRANULARITY = 256;
15 dl 1.1
16 jsr166 1.7 /**
17     * The maximum number of matrix cells
18 dl 1.1 * at which to stop recursing down and instead directly update.
19 jsr166 1.8 */
20 dl 1.1 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 jsr166 1.7
27 dl 1.1 try {
28     if (args.length > 0)
29     n = Integer.parseInt(args[0]);
30     if (args.length > 1)
31     steps = Integer.parseInt(args[1]);
32 jsr166 1.7 if (args.length > 2)
33 dl 1.1 granularity = Integer.parseInt(args[2]);
34     }
35 jsr166 1.7
36 dl 1.1 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 jsr166 1.7 double smallVal = EPSILON; // 1.0/dim;
50 dl 1.1 for (int i = 1; i < dim-1; ++i) {
51     for (int j = 1; j < dim-1; ++j)
52     a[i][j] = smallVal;
53     }
54 dl 1.6 // 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 dl 1.1 for (int rep = 0; rep < nreps; ++rep) {
67     Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity);
68 jsr166 1.7
69 dl 1.1 long startTime = System.currentTimeMillis();
70     fjp.invoke(driver);
71 jsr166 1.7
72 dl 1.1 long time = System.currentTimeMillis() - startTime;
73 dl 1.6 double secs = ((double)time) / 1000.0;
74 jsr166 1.7
75 dl 1.1 System.out.println("Compute Time: " + secs);
76     System.out.println(fjp);
77     }
78     }
79    
80    
81     abstract static class MatrixTree extends RecursiveAction {
82     // maximum difference between old and new values
83 jsr166 1.7 double maxDiff;
84     public final double directCompute() {
85     compute();
86 dl 1.1 return maxDiff;
87     }
88     public final double joinAndReinitialize(double md) {
89     if (tryUnfork())
90 jsr166 1.7 compute();
91 dl 1.1 else {
92 dl 1.5 quietlyJoin();
93 dl 1.1 reinitialize();
94     }
95     double m = maxDiff;
96 jsr166 1.9 return (md > m) ? md : m;
97 dl 1.1 }
98    
99     }
100    
101    
102     static final class LeafNode extends MatrixTree {
103     final double[][] A; // matrix to get old values from
104     final double[][] B; // matrix to put new values into
105    
106     // indices of current submatrix
107     final int loRow; final int hiRow;
108     final int loCol; final int hiCol;
109    
110     int steps = 0; // track even/odd steps
111    
112 jsr166 1.7 LeafNode(double[][] A, double[][] B,
113 dl 1.1 int loRow, int hiRow,
114     int loCol, int hiCol) {
115     this.A = A; this.B = B;
116     this.loRow = loRow; this.hiRow = hiRow;
117     this.loCol = loCol; this.hiCol = hiCol;
118     }
119    
120 jsr166 1.7 public void compute() {
121 dl 1.1 boolean AtoB = (steps++ & 1) == 0;
122 jsr166 1.9 double[][] a = AtoB ? A : B;
123     double[][] b = AtoB ? B : A;
124 dl 1.1
125     double md = 0.0; // local for computing max diff
126    
127     for (int i = loRow; i <= hiRow; ++i) {
128     for (int j = loCol; j <= hiCol; ++j) {
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 jsr166 1.7
133 dl 1.1 double diff = v - a[i][j];
134     if (diff < 0) diff = -diff;
135     if (diff > md) md = diff;
136     }
137     }
138    
139     maxDiff = md;
140     }
141     }
142    
143     static final class FourNode extends MatrixTree {
144     final MatrixTree q1;
145     final MatrixTree q2;
146     final MatrixTree q3;
147     final MatrixTree q4;
148 jsr166 1.7 FourNode(MatrixTree q1, MatrixTree q2,
149 dl 1.1 MatrixTree q3, MatrixTree q4) {
150     this.q1 = q1; this.q2 = q2; this.q3 = q3; this.q4 = q4;
151     }
152    
153 jsr166 1.7 public void compute() {
154 dl 1.1 q4.fork();
155     q3.fork();
156     q2.fork();
157     double md = q1.directCompute();
158     md = q2.joinAndReinitialize(md);
159     md = q3.joinAndReinitialize(md);
160     md = q4.joinAndReinitialize(md);
161     maxDiff = md;
162     }
163     }
164 jsr166 1.7
165 dl 1.1
166     static final class TwoNode extends MatrixTree {
167     final MatrixTree q1;
168     final MatrixTree q2;
169    
170     TwoNode(MatrixTree q1, MatrixTree q2) {
171     this.q1 = q1; this.q2 = q2;
172     }
173    
174 jsr166 1.7 public void compute() {
175 dl 1.1 q2.fork();
176     maxDiff = q2.joinAndReinitialize(q1.directCompute());
177     }
178 jsr166 1.7
179 dl 1.1 }
180 jsr166 1.7
181 dl 1.1
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 jsr166 1.7 Driver(double[][] A, double[][] B,
192 dl 1.1 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(A, B, firstRow, lastRow, firstCol, lastCol, leafs);
204     System.out.println("Using " + nleaf + " segments");
205    
206     }
207    
208     MatrixTree build(double[][] a, double[][] b,
209     int lr, int hr, int lc, int hc, int leafs) {
210     int rows = (hr - lr + 1);
211     int cols = (hc - lc + 1);
212    
213     int mr = (lr + hr) >>> 1; // midpoints
214     int mc = (lc + hc) >>> 1;
215 jsr166 1.7
216 dl 1.1 int hrows = (mr - lr + 1);
217     int hcols = (mc - lc + 1);
218    
219     if (rows * cols <= leafs) {
220     ++nleaf;
221     return new LeafNode(a, b, lr, hr, lc, hc);
222     }
223     else if (hrows * hcols >= leafs) {
224     return new FourNode(build(a, b, lr, mr, lc, mc, leafs),
225     build(a, b, lr, mr, mc+1, hc, leafs),
226     build(a, b, mr+1, hr, lc, mc, leafs),
227     build(a, b, mr+1, hr, mc+1, hc, leafs));
228     }
229     else if (cols >= rows) {
230     return new TwoNode(build(a, b, lr, hr, lc, mc, leafs),
231     build(a, b, lr, hr, mc+1, hc, leafs));
232     }
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 jsr166 1.7
237 dl 1.1 }
238     }
239    
240     static void doCompute(MatrixTree m, int s) {
241     for (int i = 0; i < s; ++i) {
242     m.invoke();
243     m.reinitialize();
244     }
245     }
246    
247     public void compute() {
248     doCompute(mat, steps);
249     double md = mat.maxDiff;
250     System.out.println("max diff after " + steps + " steps = " + md);
251     }
252     }
253    
254    
255     }