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

# 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     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 jsr166 1.7 LeafNode(double[][] A, double[][] B,
110 dl 1.1 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 jsr166 1.7 public void compute() {
118 dl 1.1 boolean AtoB = (steps++ & 1) == 0;
119 jsr166 1.9 double[][] a = AtoB ? A : B;
120     double[][] b = AtoB ? B : A;
121 dl 1.1
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 jsr166 1.7
130 dl 1.1 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 jsr166 1.7 FourNode(MatrixTree q1, MatrixTree q2,
146 dl 1.1 MatrixTree q3, MatrixTree q4) {
147     this.q1 = q1; this.q2 = q2; this.q3 = q3; this.q4 = q4;
148     }
149    
150 jsr166 1.7 public void compute() {
151 dl 1.1 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 jsr166 1.7
162 dl 1.1 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 jsr166 1.7 public void compute() {
171 dl 1.1 q2.fork();
172     maxDiff = q2.joinAndReinitialize(q1.directCompute());
173     }
174 jsr166 1.7
175 dl 1.1 }
176 jsr166 1.7
177 dl 1.1 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 jsr166 1.7 Driver(double[][] A, double[][] B,
187 dl 1.1 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 jsr166 1.7
211 dl 1.1 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 jsr166 1.7
232 dl 1.1 }
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     }