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

Comparing jsr166/src/test/loops/ThreadPhaserJacobi.java (file contents):
Revision 1.1 by dl, Fri Oct 23 19:57:07 2009 UTC vs.
Revision 1.7 by jsr166, Sat Oct 16 16:22:57 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 +
7   // Barrier version of Jacobi iteration
8  
9   import java.util.*;
# Line 8 | Line 14 | public class ThreadPhaserJacobi {
14  
15      static final int nprocs = Runtime.getRuntime().availableProcessors();
16  
17 <    /**
17 >    /**
18       * The maximum submatrix length (both row-wise and column-wise)
19       * for any Segment
20 <     **/
15 <
20 >     */
21      static final double EPSILON = 0.0001;  // convergence criterion
22  
23      static int dimGran;
# Line 26 | Line 31 | public class ThreadPhaserJacobi {
31              if (args.length > 1)
32                  steps = Integer.parseInt(args[1]);
33          }
34 <      
34 >
35          catch (Exception e) {
36              System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>");
37              return;
38          }
39  
40          int granularity = n * n / nprocs;
41 <        dimGran = (int)(Math.sqrt(granularity));
41 >        dimGran = (int) Math.sqrt(granularity);
42  
43          // allocate enough space for edges
44          int dim = n+2;
# Line 41 | Line 46 | public class ThreadPhaserJacobi {
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 = 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 <      
54 >
55          int nreps = 3;
56          for (int rep = 0; rep < nreps; ++rep) {
57              // Fill all edges with 1's.
# Line 59 | Line 64 | public class ThreadPhaserJacobi {
64              Driver driver = new Driver(a, b, 1, n, 1, n, steps);
65              long startTime = System.currentTimeMillis();
66              driver.compute();
67 <          
67 >
68              long time = System.currentTimeMillis() - startTime;
69 <            double secs = ((double)time) / 1000.0;
70 <          
69 >            double secs = (double) time / 1000.0;
70 >
71              System.out.println("Compute Time: " + secs);
72          }
73      }
# Line 73 | Line 78 | public class ThreadPhaserJacobi {
78          double[][] B; // matrix to put new values into
79  
80          // indices of current submatrix
81 <        final int loRow;  
81 >        final int loRow;
82          final int hiRow;
83          final int loCol;
84          final int hiCol;
# Line 81 | Line 86 | public class ThreadPhaserJacobi {
86          final Phaser barrier;
87          double maxDiff; // maximum difference between old and new values
88  
89 <        Segment(double[][] A, double[][] B,
89 >        Segment(double[][] A, double[][] B,
90                  int loRow, int hiRow,
91                  int loCol, int hiCol,
92                  int steps,
# Line 99 | Line 104 | public class ThreadPhaserJacobi {
104              try {
105                  double[][] a = A;
106                  double[][] b = B;
107 <        
107 >
108                  for (int i = 0; i < steps; ++i) {
109                      maxDiff = update(a, b);
110                      if (barrier.awaitAdvance(barrier.arrive()) < 0)
# Line 107 | Line 112 | public class ThreadPhaserJacobi {
112                      double[][] tmp = a; a = b; b = tmp;
113                  }
114              }
115 <            catch(Exception ex) {
115 >            catch (Exception ex) {
116                  ex.printStackTrace();
117                  return;
118              }
# Line 130 | Line 135 | public class ThreadPhaserJacobi {
135  
136              return md;
137          }
138 <        
138 >
139      }
135        
140  
141 <    static class Driver {
141 >
142 >    static class Driver {
143          double[][] A; // matrix to get old values from
144          double[][] B; // matrix to put new values into
145  
# Line 145 | Line 150 | public class ThreadPhaserJacobi {
150          final int steps;
151          Segment[] allSegments;
152  
153 <        Driver(double[][] mat1, double[][] mat2,
153 >        Driver(double[][] mat1, double[][] mat2,
154                 int firstRow, int lastRow,
155                 int firstCol, int lastCol,
156                 int steps) {
157 <      
157 >
158              this.A = mat1;   this.B = mat2;
159              this.loRow = firstRow; this.hiRow = lastRow;
160              this.loCol = firstCol; this.hiCol = lastCol;
# Line 157 | Line 162 | public class ThreadPhaserJacobi {
162  
163              int rows = hiRow - loRow + 1;
164              int cols = hiCol - loCol + 1;
165 <            int rblocks = (int)(Math.round((float)rows / dimGran));
166 <            int cblocks = (int)(Math.round((float)cols / dimGran));
167 <            
165 >            int rblocks = Math.round((float) rows / dimGran);
166 >            int cblocks = Math.round((float) cols / dimGran);
167 >
168              int n = rblocks * cblocks;
169 <            
169 >
170              Segment[] segs = new Segment[n];
171              Phaser barrier = new Phaser();
172              int k = 0;
# Line 169 | Line 174 | public class ThreadPhaserJacobi {
174                  int lr = loRow + i * dimGran;
175                  int hr = lr + dimGran;
176                  if (i == rblocks-1) hr = hiRow;
177 <                
177 >
178                  for (int j = 0; j < cblocks; ++j) {
179                      int lc = loCol + j * dimGran;
180                      int hc = lc + dimGran;
181                      if (j == cblocks-1) hc = hiCol;
182 <                    
182 >
183                      segs[k] = new Segment(A, B, lr, hr, lc, hc, steps, barrier);
184                      ++k;
185                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines