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.14 by dl, Sat Sep 12 18:34:50 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
# Line 10 | Line 10 | import java.util.concurrent.*;
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 <     **/
19 <
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;
39          }
40  
41 <        ForkJoinPool fjp = new ForkJoinPool();
41 >        //        ForkJoinPool fjp = new ForkJoinPool(1);
42 >        ForkJoinPool fjp = ForkJoinPool.commonPool();
43  
44          // allocate enough space for edges
45          int dim = n+2;
# Line 46 | Line 47 | public class FJJacobi {
47          double[][] a = new double[dim][dim];
48          double[][] b = new double[dim][dim];
49          // Initialize interiors to small value
50 <        double smallVal = EPSILON; // 1.0/dim;
50 >        double smallVal = EPSILON; // 1.0/dim;
51          for (int i = 1; i < dim-1; ++i) {
52              for (int j = 1; j < dim-1; ++j)
53                  a[i][j] = smallVal;
# Line 65 | Line 66 | public class FJJacobi {
66          int nreps = 10;
67          for (int rep = 0; rep < nreps; ++rep) {
68              Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity);
69 <      
69 >
70              long startTime = System.currentTimeMillis();
71              fjp.invoke(driver);
72 <            
72 >
73              long time = System.currentTimeMillis() - startTime;
74              double secs = ((double)time) / 1000.0;
75 <            
75 >
76              System.out.println("Compute Time: " + secs);
77              System.out.println(fjp);
78 +            Thread.sleep(1000);
79          }
80      }
81  
80
82      abstract static class MatrixTree extends RecursiveAction {
83          // maximum difference between old and new values
84 <        double maxDiff;
85 <        public final double directCompute() {
86 <            compute();
84 >        double maxDiff;
85 >        public final double directCompute() {
86 >            compute();
87              return maxDiff;
88          }
89          public final double joinAndReinitialize(double md) {
90              if (tryUnfork())
91 <                compute();
91 >                compute();
92              else {
93                  quietlyJoin();
94                  reinitialize();
95              }
96              double m = maxDiff;
97 <            return (md > m)? md : m;
97 >            return (md > m) ? md : m;
98          }
98
99      }
100  
101
101      static final class LeafNode extends MatrixTree {
102          final double[][] A; // matrix to get old values from
103          final double[][] B; // matrix to put new values into
# 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      }
164        
163  
164      static final class TwoNode extends MatrixTree {
165          final MatrixTree q1;
# Line 171 | Line 169 | public class FJJacobi {
169              this.q1 = q1; this.q2 = q2;
170          }
171  
172 <        public void compute() {
172 >        public void compute() {
173              q2.fork();
174              maxDiff = q2.joinAndReinitialize(q1.directCompute());
175          }
176 <      
176 >
177      }
180        
178  
179      static final class Driver extends RecursiveAction {
180          MatrixTree mat;
# Line 188 | Line 185 | public class FJJacobi {
185          final int leafs;
186          int nleaf;
187  
188 <        Driver(double[][] A, double[][] B,
188 >        Driver(double[][] A, double[][] B,
189                 int firstRow, int lastRow,
190                 int firstCol, int lastCol,
191                 int steps, int leafs) {
# Line 212 | Line 209 | public class FJJacobi {
209  
210              int mr = (lr + hr) >>> 1; // midpoints
211              int mc = (lc + hc) >>> 1;
212 <      
212 >
213              int hrows = (mr - lr + 1);
214              int hcols = (mc - lc + 1);
215  
# Line 233 | Line 230 | public class FJJacobi {
230              else {
231                  return new TwoNode(build(a, b, lr,   mr, lc, hc, leafs),
232                                     build(a, b, mr+1, hr, lc, hc, leafs));
233 <        
233 >
234              }
235          }
236  
# Line 250 | Line 247 | public class FJJacobi {
247              System.out.println("max diff after " + steps + " steps = " + md);
248          }
249      }
253
254
250   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines