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.4 by jsr166, Mon Nov 2 23:42:46 2009 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       **/
# Line 26 | Line 32 | public class ThreadPhaserJacobi {
32              if (args.length > 1)
33                  steps = Integer.parseInt(args[1]);
34          }
35 <      
35 >
36          catch (Exception e) {
37              System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>");
38              return;
# Line 41 | Line 47 | public class ThreadPhaserJacobi {
47          double[][] a = new double[dim][dim];
48          double[][] b = new double[dim][dim];
49          // Initialize interiors to small value
50 <        double smallVal = 1.0/dim;
50 >        double smallVal = 1.0/dim;
51          for (int i = 1; i < dim-1; ++i) {
52              for (int j = 1; j < dim-1; ++j)
53                  a[i][j] = smallVal;
54          }
55 <      
55 >
56          int nreps = 3;
57          for (int rep = 0; rep < nreps; ++rep) {
58              // Fill all edges with 1's.
# Line 59 | Line 65 | public class ThreadPhaserJacobi {
65              Driver driver = new Driver(a, b, 1, n, 1, n, steps);
66              long startTime = System.currentTimeMillis();
67              driver.compute();
68 <          
68 >
69              long time = System.currentTimeMillis() - startTime;
70              double secs = ((double)time) / 1000.0;
71 <          
71 >
72              System.out.println("Compute Time: " + secs);
73          }
74      }
# Line 73 | Line 79 | public class ThreadPhaserJacobi {
79          double[][] B; // matrix to put new values into
80  
81          // indices of current submatrix
82 <        final int loRow;  
82 >        final int loRow;
83          final int hiRow;
84          final int loCol;
85          final int hiCol;
# Line 81 | Line 87 | public class ThreadPhaserJacobi {
87          final Phaser barrier;
88          double maxDiff; // maximum difference between old and new values
89  
90 <        Segment(double[][] A, double[][] B,
90 >        Segment(double[][] A, double[][] B,
91                  int loRow, int hiRow,
92                  int loCol, int hiCol,
93                  int steps,
# Line 99 | Line 105 | public class ThreadPhaserJacobi {
105              try {
106                  double[][] a = A;
107                  double[][] b = B;
108 <        
108 >
109                  for (int i = 0; i < steps; ++i) {
110                      maxDiff = update(a, b);
111                      if (barrier.awaitAdvance(barrier.arrive()) < 0)
# Line 107 | Line 113 | public class ThreadPhaserJacobi {
113                      double[][] tmp = a; a = b; b = tmp;
114                  }
115              }
116 <            catch(Exception ex) {
116 >            catch (Exception ex) {
117                  ex.printStackTrace();
118                  return;
119              }
# Line 130 | Line 136 | public class ThreadPhaserJacobi {
136  
137              return md;
138          }
139 <        
139 >
140      }
135        
141  
142 <    static class Driver {
142 >
143 >    static class Driver {
144          double[][] A; // matrix to get old values from
145          double[][] B; // matrix to put new values into
146  
# Line 145 | Line 151 | public class ThreadPhaserJacobi {
151          final int steps;
152          Segment[] allSegments;
153  
154 <        Driver(double[][] mat1, double[][] mat2,
154 >        Driver(double[][] mat1, double[][] mat2,
155                 int firstRow, int lastRow,
156                 int firstCol, int lastCol,
157                 int steps) {
158 <      
158 >
159              this.A = mat1;   this.B = mat2;
160              this.loRow = firstRow; this.hiRow = lastRow;
161              this.loCol = firstCol; this.hiCol = lastCol;
# Line 159 | Line 165 | public class ThreadPhaserJacobi {
165              int cols = hiCol - loCol + 1;
166              int rblocks = (int)(Math.round((float)rows / dimGran));
167              int cblocks = (int)(Math.round((float)cols / dimGran));
168 <            
168 >
169              int n = rblocks * cblocks;
170 <            
170 >
171              Segment[] segs = new Segment[n];
172              Phaser barrier = new Phaser();
173              int k = 0;
# Line 169 | Line 175 | public class ThreadPhaserJacobi {
175                  int lr = loRow + i * dimGran;
176                  int hr = lr + dimGran;
177                  if (i == rblocks-1) hr = hiRow;
178 <                
178 >
179                  for (int j = 0; j < cblocks; ++j) {
180                      int lc = loCol + j * dimGran;
181                      int hc = lc + dimGran;
182                      if (j == cblocks-1) hc = hiCol;
183 <                    
183 >
184                      segs[k] = new Segment(A, B, lr, hr, lc, hc, steps, barrier);
185                      ++k;
186                  }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines