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.9 by jsr166, Thu Jan 15 18:34:19 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/publicdomain/zero/1.0/
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 94 | Line 99 | public class ThreadPhaserJacobi {
99              barrier.register();
100          }
101  
97
102          public void run() {
103              try {
104                  double[][] a = A;
105                  double[][] b = B;
106 <        
106 >
107                  for (int i = 0; i < steps; ++i) {
108                      maxDiff = update(a, b);
109                      if (barrier.awaitAdvance(barrier.arrive()) < 0)
# Line 107 | Line 111 | public class ThreadPhaserJacobi {
111                      double[][] tmp = a; a = b; b = tmp;
112                  }
113              }
114 <            catch(Exception ex) {
114 >            catch (Exception ex) {
115                  ex.printStackTrace();
116                  return;
117              }
# Line 130 | Line 134 | public class ThreadPhaserJacobi {
134  
135              return md;
136          }
137 <        
137 >
138      }
135        
139  
140 <    static class Driver {
140 >    static class Driver {
141          double[][] A; // matrix to get old values from
142          double[][] B; // matrix to put new values into
143  
# Line 145 | Line 148 | public class ThreadPhaserJacobi {
148          final int steps;
149          Segment[] allSegments;
150  
151 <        Driver(double[][] mat1, double[][] mat2,
151 >        Driver(double[][] mat1, double[][] mat2,
152                 int firstRow, int lastRow,
153                 int firstCol, int lastCol,
154                 int steps) {
155 <      
155 >
156              this.A = mat1;   this.B = mat2;
157              this.loRow = firstRow; this.hiRow = lastRow;
158              this.loCol = firstCol; this.hiCol = lastCol;
# Line 157 | Line 160 | public class ThreadPhaserJacobi {
160  
161              int rows = hiRow - loRow + 1;
162              int cols = hiCol - loCol + 1;
163 <            int rblocks = (int)(Math.round((float)rows / dimGran));
164 <            int cblocks = (int)(Math.round((float)cols / dimGran));
165 <            
163 >            int rblocks = Math.round((float) rows / dimGran);
164 >            int cblocks = Math.round((float) cols / dimGran);
165 >
166              int n = rblocks * cblocks;
167 <            
167 >
168              Segment[] segs = new Segment[n];
169              Phaser barrier = new Phaser();
170              int k = 0;
# Line 169 | Line 172 | public class ThreadPhaserJacobi {
172                  int lr = loRow + i * dimGran;
173                  int hr = lr + dimGran;
174                  if (i == rblocks-1) hr = hiRow;
175 <                
175 >
176                  for (int j = 0; j < cblocks; ++j) {
177                      int lc = loCol + j * dimGran;
178                      int hc = lc + dimGran;
179                      if (j == cblocks-1) hc = hiCol;
180 <                    
180 >
181                      segs[k] = new Segment(A, B, lr, hr, lc, hc, steps, barrier);
182                      ++k;
183                  }
# Line 201 | Line 204 | public class ThreadPhaserJacobi {
204              System.out.println("Max diff after " + steps + " steps = " + maxd);
205          }
206      }
204
205
207   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines