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.1 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.12 by jsr166, Mon Aug 10 03:13:33 2015 UTC

# Line 1 | Line 1
1 < // Barrier version of Jacobi iteration
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/publicdomain/zero/1.0/
5 > */
6  
7   import java.util.concurrent.*;
4 //import jsr166y.*;
8  
9 + /** Barrier version of Jacobi iteration */
10   public class FJPhaserJacobi {
11  
12      static int dimGran;
# Line 18 | Line 22 | public class FJPhaserJacobi {
22              if (args.length > 1)
23                  steps = Integer.parseInt(args[1]);
24          }
25 <        
25 >
26          catch (Exception e) {
27              System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>");
28              return;
29          }
30 <        
30 >
31          ForkJoinPool fjp = new ForkJoinPool();
32 +        //        int granularity = (n * n / fjp.getParallelism()) / 2;
33          int granularity = n * n / fjp.getParallelism();
34          dimGran = (int)(Math.sqrt(granularity));
35 <      
35 >
36          // allocate enough space for edges
37          int dim = n+2;
38          int ncells = dim * dim;
39          double[][] a = new double[dim][dim];
40          double[][] b = new double[dim][dim];
41          // Initialize interiors to small value
42 <        double smallVal = 1.0/dim;
42 >        double smallVal = 1.0/dim;
43          for (int i = 1; i < dim-1; ++i) {
44              for (int j = 1; j < dim-1; ++j)
45                  a[i][j] = smallVal;
# Line 51 | Line 56 | public class FJPhaserJacobi {
56              Driver driver = new Driver(a, b, 1, n, 1, n, steps);
57              long startTime = System.currentTimeMillis();
58              fjp.invoke(driver);
59 <            
59 >
60              long time = System.currentTimeMillis() - startTime;
61              double secs = ((double)time) / 1000.0;
62 <            
62 >
63              System.out.println("Compute Time: " + secs);
64              System.out.println(fjp);
60
65          }
62        
66      }
67  
68      static class Segment extends CyclicAction {
69          double[][] A; // matrix to get old values from
70          double[][] B; // matrix to put new values into
71          // indices of current submatrix
72 <        final int loRow;  
72 >        final int loRow;
73          final int hiRow;
74          final int loCol;
75          final int hiCol;
76          volatile double maxDiff; // maximum difference between old and new values
77  
78 <        Segment(double[][] A, double[][] B,
78 >        Segment(double[][] A, double[][] B,
79                  int loRow, int hiRow,
80 <                int loCol, int hiCol,
80 >                int loCol, int hiCol,
81                  Phaser br) {
82              super(br);
83              this.A = A;   this.B = B;
# Line 104 | Line 107 | public class FJPhaserJacobi {
107  
108              return md;
109          }
110 <        
110 >
111      }
112  
113      static class MyPhaser extends Phaser {
# Line 115 | Line 118 | public class FJPhaserJacobi {
118          }
119      }
120  
121 <    static class Driver extends RecursiveAction {
121 >    static class Driver extends RecursiveAction {
122          double[][] A; // matrix to get old values from
123          double[][] B; // matrix to put new values into
124          final int loRow;   // indices of current submatrix
# Line 123 | Line 126 | public class FJPhaserJacobi {
126          final int loCol;
127          final int hiCol;
128          final int steps;
129 <        Driver(double[][] mat1, double[][] mat2,
129 >        Driver(double[][] mat1, double[][] mat2,
130                 int firstRow, int lastRow,
131                 int firstCol, int lastCol,
132                 int steps) {
133 <            
133 >
134              this.A = mat1;   this.B = mat2;
135              this.loRow = firstRow; this.hiRow = lastRow;
136              this.loCol = firstCol; this.hiCol = lastCol;
# Line 137 | Line 140 | public class FJPhaserJacobi {
140          public void compute() {
141              int rows = hiRow - loRow + 1;
142              int cols = hiCol - loCol + 1;
143 <            int rblocks = (int)(Math.round((float)rows / dimGran));
144 <            int cblocks = (int)(Math.round((float)cols / dimGran));
145 <            
143 >            int rblocks = Math.round((float)rows / dimGran);
144 >            int cblocks = Math.round((float)cols / dimGran);
145 >
146              int n = rblocks * cblocks;
147 <            
147 >
148              System.out.println("Using " + n + " segments");
149 <            
149 >
150              Segment[] segs = new Segment[n];
151              Phaser barrier = new MyPhaser(steps);
152              int k = 0;
# Line 151 | Line 154 | public class FJPhaserJacobi {
154                  int lr = loRow + i * dimGran;
155                  int hr = lr + dimGran;
156                  if (i == rblocks-1) hr = hiRow;
157 <                
157 >
158                  for (int j = 0; j < cblocks; ++j) {
159                      int lc = loCol + j * dimGran;
160                      int hc = lc + dimGran;
161                      if (j == cblocks-1) hc = hiCol;
162 <                    
162 >
163                      segs[k] = new Segment(A, B, lr, hr, lc, hc, barrier);
164                      ++k;
165                  }
# Line 171 | Line 174 | public class FJPhaserJacobi {
174          }
175      }
176   }
174

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines