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.10 by jsr166, Sat Feb 16 21:37:44 2013 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);
65  
66          }
67 <        
67 >
68      }
69  
70      static class Segment extends CyclicAction {
71          double[][] A; // matrix to get old values from
72          double[][] B; // matrix to put new values into
73          // indices of current submatrix
74 <        final int loRow;  
74 >        final int loRow;
75          final int hiRow;
76          final int loCol;
77          final int hiCol;
78          volatile double maxDiff; // maximum difference between old and new values
79  
80 <        Segment(double[][] A, double[][] B,
80 >        Segment(double[][] A, double[][] B,
81                  int loRow, int hiRow,
82 <                int loCol, int hiCol,
82 >                int loCol, int hiCol,
83                  Phaser br) {
84              super(br);
85              this.A = A;   this.B = B;
# Line 104 | Line 109 | public class FJPhaserJacobi {
109  
110              return md;
111          }
112 <        
112 >
113      }
114  
115      static class MyPhaser extends Phaser {
# Line 115 | Line 120 | public class FJPhaserJacobi {
120          }
121      }
122  
123 <    static class Driver extends RecursiveAction {
123 >    static class Driver extends RecursiveAction {
124          double[][] A; // matrix to get old values from
125          double[][] B; // matrix to put new values into
126          final int loRow;   // indices of current submatrix
# Line 123 | Line 128 | public class FJPhaserJacobi {
128          final int loCol;
129          final int hiCol;
130          final int steps;
131 <        Driver(double[][] mat1, double[][] mat2,
131 >        Driver(double[][] mat1, double[][] mat2,
132                 int firstRow, int lastRow,
133                 int firstCol, int lastCol,
134                 int steps) {
135 <            
135 >
136              this.A = mat1;   this.B = mat2;
137              this.loRow = firstRow; this.hiRow = lastRow;
138              this.loCol = firstCol; this.hiCol = lastCol;
# Line 139 | Line 144 | public class FJPhaserJacobi {
144              int cols = hiCol - loCol + 1;
145              int rblocks = (int)(Math.round((float)rows / dimGran));
146              int cblocks = (int)(Math.round((float)cols / dimGran));
147 <            
147 >
148              int n = rblocks * cblocks;
149 <            
149 >
150              System.out.println("Using " + n + " segments");
151 <            
151 >
152              Segment[] segs = new Segment[n];
153              Phaser barrier = new MyPhaser(steps);
154              int k = 0;
# Line 151 | Line 156 | public class FJPhaserJacobi {
156                  int lr = loRow + i * dimGran;
157                  int hr = lr + dimGran;
158                  if (i == rblocks-1) hr = hiRow;
159 <                
159 >
160                  for (int j = 0; j < cblocks; ++j) {
161                      int lc = loCol + j * dimGran;
162                      int hc = lc + dimGran;
163                      if (j == cblocks-1) hc = hiCol;
164 <                    
164 >
165                      segs[k] = new Segment(A, B, lr, hr, lc, hc, barrier);
166                      ++k;
167                  }
# Line 171 | Line 176 | public class FJPhaserJacobi {
176          }
177      }
178   }
174

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines