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

Comparing jsr166/src/test/loops/FJPhaserJacobi.java (file contents):
Revision 1.6 by jsr166, Tue Nov 3 01:04:02 2009 UTC vs.
Revision 1.7 by dl, Sun Sep 19 12:55:37 2010 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
5 */
6
1   // Barrier version of Jacobi iteration
2  
3   import java.util.concurrent.*;
10 //import jsr166y.*;
4  
5   public class FJPhaserJacobi {
6  
# Line 24 | Line 17 | public class FJPhaserJacobi {
17              if (args.length > 1)
18                  steps = Integer.parseInt(args[1]);
19          }
20 <
20 >        
21          catch (Exception e) {
22 <            System.out.println("Usage: java FJPhaserJacobi <matrix size> <max steps>");
22 >            System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>");
23              return;
24          }
25 <
25 >        
26          ForkJoinPool fjp = new ForkJoinPool();
27 +        //        int granularity = (n * n / fjp.getParallelism()) / 2;
28          int granularity = n * n / fjp.getParallelism();
29 <        dimGran = (int) Math.sqrt(granularity);
30 <
29 >        dimGran = (int)(Math.sqrt(granularity));
30 >      
31          // allocate enough space for edges
32          int dim = n+2;
33          int ncells = dim * dim;
34          double[][] a = new double[dim][dim];
35          double[][] b = new double[dim][dim];
36          // Initialize interiors to small value
37 <        double smallVal = 1.0/dim;
37 >        double smallVal = 1.0/dim;
38          for (int i = 1; i < dim-1; ++i) {
39              for (int j = 1; j < dim-1; ++j)
40                  a[i][j] = smallVal;
# Line 57 | Line 51 | public class FJPhaserJacobi {
51              Driver driver = new Driver(a, b, 1, n, 1, n, steps);
52              long startTime = System.currentTimeMillis();
53              fjp.invoke(driver);
54 <
54 >            
55              long time = System.currentTimeMillis() - startTime;
56 <            double secs = (double) time / 1000.0;
57 <
56 >            double secs = ((double)time) / 1000.0;
57 >            
58              System.out.println("Compute Time: " + secs);
59              System.out.println(fjp);
60  
61          }
62 <
62 >        
63      }
64  
65      static class Segment extends CyclicAction {
66          double[][] A; // matrix to get old values from
67          double[][] B; // matrix to put new values into
68          // indices of current submatrix
69 <        final int loRow;
69 >        final int loRow;  
70          final int hiRow;
71          final int loCol;
72          final int hiCol;
73          volatile double maxDiff; // maximum difference between old and new values
74  
75 <        Segment(double[][] A, double[][] B,
75 >        Segment(double[][] A, double[][] B,
76                  int loRow, int hiRow,
77 <                int loCol, int hiCol,
77 >                int loCol, int hiCol,
78                  Phaser br) {
79              super(br);
80              this.A = A;   this.B = B;
# Line 110 | Line 104 | public class FJPhaserJacobi {
104  
105              return md;
106          }
107 <
107 >        
108      }
109  
110      static class MyPhaser extends Phaser {
# Line 121 | Line 115 | public class FJPhaserJacobi {
115          }
116      }
117  
118 <    static class Driver extends RecursiveAction {
118 >    static class Driver extends RecursiveAction {
119          double[][] A; // matrix to get old values from
120          double[][] B; // matrix to put new values into
121          final int loRow;   // indices of current submatrix
# Line 129 | Line 123 | public class FJPhaserJacobi {
123          final int loCol;
124          final int hiCol;
125          final int steps;
126 <        Driver(double[][] mat1, double[][] mat2,
126 >        Driver(double[][] mat1, double[][] mat2,
127                 int firstRow, int lastRow,
128                 int firstCol, int lastCol,
129                 int steps) {
130 <
130 >            
131              this.A = mat1;   this.B = mat2;
132              this.loRow = firstRow; this.hiRow = lastRow;
133              this.loCol = firstCol; this.hiCol = lastCol;
# Line 143 | Line 137 | public class FJPhaserJacobi {
137          public void compute() {
138              int rows = hiRow - loRow + 1;
139              int cols = hiCol - loCol + 1;
140 <            int rblocks = Math.round((float) rows / dimGran);
141 <            int cblocks = Math.round((float) cols / dimGran);
142 <
140 >            int rblocks = (int)(Math.round((float)rows / dimGran));
141 >            int cblocks = (int)(Math.round((float)cols / dimGran));
142 >            
143              int n = rblocks * cblocks;
144 <
144 >            
145              System.out.println("Using " + n + " segments");
146 <
146 >            
147              Segment[] segs = new Segment[n];
148              Phaser barrier = new MyPhaser(steps);
149              int k = 0;
# Line 157 | Line 151 | public class FJPhaserJacobi {
151                  int lr = loRow + i * dimGran;
152                  int hr = lr + dimGran;
153                  if (i == rblocks-1) hr = hiRow;
154 <
154 >                
155                  for (int j = 0; j < cblocks; ++j) {
156                      int lc = loCol + j * dimGran;
157                      int hc = lc + dimGran;
158                      if (j == cblocks-1) hc = hiCol;
159 <
159 >                    
160                      segs[k] = new Segment(A, B, lr, hr, lc, hc, barrier);
161                      ++k;
162                  }
# Line 177 | Line 171 | public class FJPhaserJacobi {
171          }
172      }
173   }
174 +

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines