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.1 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.13 by jsr166, Mon Aug 10 03:13:33 2015 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
8  
9   import java.util.concurrent.*;
10 //import jsr166y.*;
10  
11   public class FJJacobi {
12  
13 <    static final int DEFAULT_GRANULARITY = 4096; // 1024;
13 >    //    static final int DEFAULT_GRANULARITY = 4096;
14 >    static final int DEFAULT_GRANULARITY = 256;
15  
16 <    /**
17 <     * The maximum number of matrix cells
16 >    /**
17 >     * The maximum number of matrix cells
18       * at which to stop recursing down and instead directly update.
19 <     **/
20 <
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 <      
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)
32 >            if (args.length > 2)
33                  granularity = Integer.parseInt(args[2]);
34          }
35 <      
35 >
36          catch (Exception e) {
37              System.out.println("Usage: java FJJacobi <matrix size> <max steps> [<leafcells>]");
38              return;
# Line 47 | Line 46 | public class FJJacobi {
46          double[][] a = new double[dim][dim];
47          double[][] b = new double[dim][dim];
48          // Initialize interiors to small value
49 <        double smallVal = 1.0/dim;
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 <        
55 <        int nreps = 3;
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) {
58            // Fill all edges with 1's.
59            for (int k = 0; k < dim; ++k) {
60                a[k][0] += 1.0;
61                a[k][n+1] += 1.0;
62                a[0][k] += 1.0;
63                a[n+1][k] += 1.0;
64            }
67              Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity);
68 <      
68 >
69              long startTime = System.currentTimeMillis();
70              fjp.invoke(driver);
71 <            
71 >
72              long time = System.currentTimeMillis() - startTime;
73              double secs = ((double)time) / 1000.0;
74 <            
74 >
75              System.out.println("Compute Time: " + secs);
74            System.out.println("Workers: " + fjp.getPoolSize());
76              System.out.println(fjp);
77          }
78      }
79  
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 <                quietlyHelpJoin();
91 >                quietlyJoin();
92                  reinitialize();
93              }
94              double m = maxDiff;
95 <            return (md > m)? md : m;
95 >            return (md > m) ? md : m;
96          }
98
97      }
98  
101
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
# Line 109 | Line 106 | public class FJJacobi {
106  
107          int steps = 0; // track even/odd steps
108  
109 <        LeafNode(double[][] A, double[][] B,
109 >        LeafNode(double[][] A, double[][] B,
110                   int loRow, int hiRow,
111                   int loCol, int hiCol) {
112              this.A = A;   this.B = B;
# Line 117 | Line 114 | public class FJJacobi {
114              this.loCol = loCol; this.hiCol = hiCol;
115          }
116  
117 <        public void compute() {
117 >        public void compute() {
118              boolean AtoB = (steps++ & 1) == 0;
119 <            double[][] a = (AtoB)? A : B;
120 <            double[][] b = (AtoB)? B : A;
119 >            double[][] a = AtoB ? A : B;
120 >            double[][] b = AtoB ? B : A;
121  
122              double md = 0.0; // local for computing max diff
123  
# Line 145 | Line 142 | public class FJJacobi {
142          final MatrixTree q2;
143          final MatrixTree q3;
144          final MatrixTree q4;
145 <        FourNode(MatrixTree q1, MatrixTree q2,
145 >        FourNode(MatrixTree q1, MatrixTree q2,
146                   MatrixTree q3, MatrixTree q4) {
147              this.q1 = q1; this.q2 = q2; this.q3 = q3; this.q4 = q4;
148          }
149  
150 <        public void compute() {
150 >        public void compute() {
151              q4.fork();
152              q3.fork();
153              q2.fork();
# Line 161 | Line 158 | public class FJJacobi {
158              maxDiff = md;
159          }
160      }
164        
161  
162      static final class TwoNode extends MatrixTree {
163          final MatrixTree q1;
# Line 171 | Line 167 | public class FJJacobi {
167              this.q1 = q1; this.q2 = q2;
168          }
169  
170 <        public void compute() {
170 >        public void compute() {
171              q2.fork();
172              maxDiff = q2.joinAndReinitialize(q1.directCompute());
173          }
174 <      
174 >
175      }
180        
176  
177      static final class Driver extends RecursiveAction {
178          MatrixTree mat;
# Line 188 | Line 183 | public class FJJacobi {
183          final int leafs;
184          int nleaf;
185  
186 <        Driver(double[][] A, double[][] B,
186 >        Driver(double[][] A, double[][] B,
187                 int firstRow, int lastRow,
188                 int firstCol, int lastCol,
189                 int steps, int leafs) {
# Line 212 | Line 207 | public class FJJacobi {
207  
208              int mr = (lr + hr) >>> 1; // midpoints
209              int mc = (lc + hc) >>> 1;
210 <      
210 >
211              int hrows = (mr - lr + 1);
212              int hcols = (mc - lc + 1);
213  
# Line 233 | Line 228 | public class FJJacobi {
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 <        
231 >
232              }
233          }
234  
# Line 250 | Line 245 | public class FJJacobi {
245              System.out.println("max diff after " + steps + " steps = " + md);
246          }
247      }
253
254
248   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines