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.13 by dl, Sat Sep 12 18:37:19 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;
13  
14      static final double EPSILON = 0.0001;  // convergence criterion
15  
16 <    public static void main(String[] args) {
16 >    public static void main(String[] args) throws Exception {
17          int n = 2048;
18          int steps = 1000;
19          try {
# 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 <        
31 <        ForkJoinPool fjp = new ForkJoinPool();
30 >
31 >        //        ForkJoinPool fjp = new ForkJoinPool();
32 >        ForkJoinPool fjp = ForkJoinPool.commonPool();
33 >
34 >        //        int granularity = (n * n / fjp.getParallelism()) / 2;
35          int granularity = n * n / fjp.getParallelism();
36          dimGran = (int)(Math.sqrt(granularity));
37 <      
37 >
38          // allocate enough space for edges
39          int dim = n+2;
40          int ncells = dim * dim;
41          double[][] a = new double[dim][dim];
42          double[][] b = new double[dim][dim];
43          // Initialize interiors to small value
44 <        double smallVal = 1.0/dim;
44 >        double smallVal = 1.0/dim;
45          for (int i = 1; i < dim-1; ++i) {
46              for (int j = 1; j < dim-1; ++j)
47                  a[i][j] = smallVal;
48          }
49 <        int nreps = 3;
49 >        int nreps = 10;
50          for (int rep = 0; rep < nreps; ++rep) {
51              // Fill all edges with 1's.
52              for (int k = 0; k < dim; ++k) {
# Line 51 | Line 58 | public class FJPhaserJacobi {
58              Driver driver = new Driver(a, b, 1, n, 1, n, steps);
59              long startTime = System.currentTimeMillis();
60              fjp.invoke(driver);
61 <            
61 >
62              long time = System.currentTimeMillis() - startTime;
63              double secs = ((double)time) / 1000.0;
64 <            
64 >
65              System.out.println("Compute Time: " + secs);
66              System.out.println(fjp);
67 <
67 >            Thread.sleep(1000);
68          }
62        
69      }
70  
71      static class Segment extends CyclicAction {
72          double[][] A; // matrix to get old values from
73          double[][] B; // matrix to put new values into
74          // indices of current submatrix
75 <        final int loRow;  
75 >        final int loRow;
76          final int hiRow;
77          final int loCol;
78          final int hiCol;
79          volatile double maxDiff; // maximum difference between old and new values
80  
81 <        Segment(double[][] A, double[][] B,
81 >        Segment(double[][] A, double[][] B,
82                  int loRow, int hiRow,
83 <                int loCol, int hiCol,
83 >                int loCol, int hiCol,
84                  Phaser br) {
85              super(br);
86              this.A = A;   this.B = B;
# Line 104 | Line 110 | public class FJPhaserJacobi {
110  
111              return md;
112          }
113 <        
113 >
114      }
115  
116      static class MyPhaser extends Phaser {
# Line 115 | Line 121 | public class FJPhaserJacobi {
121          }
122      }
123  
124 <    static class Driver extends RecursiveAction {
124 >    static class Driver extends RecursiveAction {
125          double[][] A; // matrix to get old values from
126          double[][] B; // matrix to put new values into
127          final int loRow;   // indices of current submatrix
# Line 123 | Line 129 | public class FJPhaserJacobi {
129          final int loCol;
130          final int hiCol;
131          final int steps;
132 <        Driver(double[][] mat1, double[][] mat2,
132 >        Driver(double[][] mat1, double[][] mat2,
133                 int firstRow, int lastRow,
134                 int firstCol, int lastCol,
135                 int steps) {
136 <            
136 >
137              this.A = mat1;   this.B = mat2;
138              this.loRow = firstRow; this.hiRow = lastRow;
139              this.loCol = firstCol; this.hiCol = lastCol;
# Line 137 | Line 143 | public class FJPhaserJacobi {
143          public void compute() {
144              int rows = hiRow - loRow + 1;
145              int cols = hiCol - loCol + 1;
146 <            int rblocks = (int)(Math.round((float)rows / dimGran));
147 <            int cblocks = (int)(Math.round((float)cols / dimGran));
148 <            
146 >            int rblocks = Math.round((float)rows / dimGran);
147 >            int cblocks = Math.round((float)cols / dimGran);
148 >
149              int n = rblocks * cblocks;
150 <            
150 >
151              System.out.println("Using " + n + " segments");
152 <            
152 >
153              Segment[] segs = new Segment[n];
154              Phaser barrier = new MyPhaser(steps);
155              int k = 0;
# Line 151 | Line 157 | public class FJPhaserJacobi {
157                  int lr = loRow + i * dimGran;
158                  int hr = lr + dimGran;
159                  if (i == rblocks-1) hr = hiRow;
160 <                
160 >
161                  for (int j = 0; j < cblocks; ++j) {
162                      int lc = loCol + j * dimGran;
163                      int hc = lc + dimGran;
164                      if (j == cblocks-1) hc = hiCol;
165 <                    
165 >
166                      segs[k] = new Segment(A, B, lr, hr, lc, hc, barrier);
167                      ++k;
168                  }
# Line 171 | Line 177 | public class FJPhaserJacobi {
177          }
178      }
179   }
174

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines