ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FJJacobi.java
(Generate patch)

Comparing jsr166/src/test/loops/FJJacobi.java (file contents):
Revision 1.6 by dl, Sun Sep 19 12:55:36 2010 UTC vs.
Revision 1.10 by jsr166, Tue Mar 15 19:47:05 2011 UTC

# Line 1 | Line 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 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7   // Jacobi iteration on a mesh. Based loosely on a Filaments demo
# Line 12 | Line 12 | public class FJJacobi {
12  
13      static final int DEFAULT_GRANULARITY = 4096; // 1024;
14  
15 <    /**
16 <     * The maximum number of matrix cells
15 >    /**
16 >     * The maximum number of matrix cells
17       * at which to stop recursing down and instead directly update.
18 <     **/
19 <
18 >     */
19      static final double EPSILON = 0.0001;  // convergence criterion
20  
21      public static void main(String[] args) throws Exception {
22          int n = 2048;
23          int steps = 1000;
24          int granularity = DEFAULT_GRANULARITY;
25 <      
25 >
26          try {
27              if (args.length > 0)
28                  n = Integer.parseInt(args[0]);
29              if (args.length > 1)
30                  steps = Integer.parseInt(args[1]);
31 <            if (args.length > 2)
31 >            if (args.length > 2)
32                  granularity = Integer.parseInt(args[2]);
33          }
34 <      
34 >
35          catch (Exception e) {
36              System.out.println("Usage: java FJJacobi <matrix size> <max steps> [<leafcells>]");
37              return;
# Line 46 | Line 45 | public class FJJacobi {
45          double[][] a = new double[dim][dim];
46          double[][] b = new double[dim][dim];
47          // Initialize interiors to small value
48 <        double smallVal = EPSILON; // 1.0/dim;
48 >        double smallVal = EPSILON; // 1.0/dim;
49          for (int i = 1; i < dim-1; ++i) {
50              for (int j = 1; j < dim-1; ++j)
51                  a[i][j] = smallVal;
# Line 65 | Line 64 | public class FJJacobi {
64          int nreps = 10;
65          for (int rep = 0; rep < nreps; ++rep) {
66              Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity);
67 <      
67 >
68              long startTime = System.currentTimeMillis();
69              fjp.invoke(driver);
70 <            
70 >
71              long time = System.currentTimeMillis() - startTime;
72              double secs = ((double)time) / 1000.0;
73 <            
73 >
74              System.out.println("Compute Time: " + secs);
75              System.out.println(fjp);
76          }
# Line 80 | Line 79 | public class FJJacobi {
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();
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();
89 >                compute();
90              else {
91                  quietlyJoin();
92                  reinitialize();
93              }
94              double m = maxDiff;
95 <            return (md > m)? md : m;
95 >            return (md > m) ? md : m;
96          }
97  
98      }
# Line 109 | Line 108 | public class FJJacobi {
108  
109          int steps = 0; // track even/odd steps
110  
111 <        LeafNode(double[][] A, double[][] B,
111 >        LeafNode(double[][] A, double[][] B,
112                   int loRow, int hiRow,
113                   int loCol, int hiCol) {
114              this.A = A;   this.B = B;
# Line 117 | Line 116 | public class FJJacobi {
116              this.loCol = loCol; this.hiCol = hiCol;
117          }
118  
119 <        public void compute() {
119 >        public void compute() {
120              boolean AtoB = (steps++ & 1) == 0;
121 <            double[][] a = (AtoB)? A : B;
122 <            double[][] b = (AtoB)? B : A;
121 >            double[][] a = AtoB ? A : B;
122 >            double[][] b = AtoB ? B : A;
123  
124              double md = 0.0; // local for computing max diff
125  
# Line 129 | Line 128 | public class FJJacobi {
128                      double v = 0.25 * (a[i-1][j] + a[i][j-1] +
129                                         a[i+1][j] + a[i][j+1]);
130                      b[i][j] = v;
131 <                    
131 >
132                      double diff = v - a[i][j];
133                      if (diff < 0) diff = -diff;
134                      if (diff > md) md = diff;
# Line 145 | Line 144 | public class FJJacobi {
144          final MatrixTree q2;
145          final MatrixTree q3;
146          final MatrixTree q4;
147 <        FourNode(MatrixTree q1, MatrixTree q2,
147 >        FourNode(MatrixTree q1, MatrixTree q2,
148                   MatrixTree q3, MatrixTree q4) {
149              this.q1 = q1; this.q2 = q2; this.q3 = q3; this.q4 = q4;
150          }
151  
152 <        public void compute() {
152 >        public void compute() {
153              q4.fork();
154              q3.fork();
155              q2.fork();
# Line 161 | Line 160 | public class FJJacobi {
160              maxDiff = md;
161          }
162      }
163 <        
163 >
164  
165      static final class TwoNode extends MatrixTree {
166          final MatrixTree q1;
# Line 171 | Line 170 | public class FJJacobi {
170              this.q1 = q1; this.q2 = q2;
171          }
172  
173 <        public void compute() {
173 >        public void compute() {
174              q2.fork();
175              maxDiff = q2.joinAndReinitialize(q1.directCompute());
176          }
177 <      
177 >
178      }
179 <        
179 >
180  
181      static final class Driver extends RecursiveAction {
182          MatrixTree mat;
# Line 188 | Line 187 | public class FJJacobi {
187          final int leafs;
188          int nleaf;
189  
190 <        Driver(double[][] A, double[][] B,
190 >        Driver(double[][] A, double[][] B,
191                 int firstRow, int lastRow,
192                 int firstCol, int lastCol,
193                 int steps, int leafs) {
# Line 212 | Line 211 | public class FJJacobi {
211  
212              int mr = (lr + hr) >>> 1; // midpoints
213              int mc = (lc + hc) >>> 1;
214 <      
214 >
215              int hrows = (mr - lr + 1);
216              int hcols = (mc - lc + 1);
217  
# Line 233 | Line 232 | public class FJJacobi {
232              else {
233                  return new TwoNode(build(a, b, lr,   mr, lc, hc, leafs),
234                                     build(a, b, mr+1, hr, lc, hc, leafs));
235 <        
235 >
236              }
237          }
238  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines