ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FJJacobi.java
Revision: 1.12
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +0 -6 lines
Log Message:
delete extraneous blank lines

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