--- jsr166/src/test/loops/ThreadPhaserJacobi.java 2009/10/23 19:57:07 1.1 +++ jsr166/src/test/loops/ThreadPhaserJacobi.java 2009/11/02 23:55:36 1.5 @@ -1,3 +1,9 @@ +/* + * Written by Doug Lea with assistance from members of JCP JSR-166 + * Expert Group and released to the public domain, as explained at + * http://creativecommons.org/licenses/publicdomain + */ + // Barrier version of Jacobi iteration import java.util.*; @@ -8,7 +14,7 @@ public class ThreadPhaserJacobi { static final int nprocs = Runtime.getRuntime().availableProcessors(); - /** + /** * The maximum submatrix length (both row-wise and column-wise) * for any Segment **/ @@ -26,7 +32,7 @@ public class ThreadPhaserJacobi { if (args.length > 1) steps = Integer.parseInt(args[1]); } - + catch (Exception e) { System.out.println("Usage: java ThreadPhaserJacobi "); return; @@ -41,12 +47,12 @@ public class ThreadPhaserJacobi { double[][] a = new double[dim][dim]; double[][] b = new double[dim][dim]; // Initialize interiors to small value - double smallVal = 1.0/dim; + double smallVal = 1.0/dim; for (int i = 1; i < dim-1; ++i) { for (int j = 1; j < dim-1; ++j) a[i][j] = smallVal; } - + int nreps = 3; for (int rep = 0; rep < nreps; ++rep) { // Fill all edges with 1's. @@ -59,10 +65,10 @@ public class ThreadPhaserJacobi { Driver driver = new Driver(a, b, 1, n, 1, n, steps); long startTime = System.currentTimeMillis(); driver.compute(); - + long time = System.currentTimeMillis() - startTime; double secs = ((double)time) / 1000.0; - + System.out.println("Compute Time: " + secs); } } @@ -73,7 +79,7 @@ public class ThreadPhaserJacobi { double[][] B; // matrix to put new values into // indices of current submatrix - final int loRow; + final int loRow; final int hiRow; final int loCol; final int hiCol; @@ -81,7 +87,7 @@ public class ThreadPhaserJacobi { final Phaser barrier; double maxDiff; // maximum difference between old and new values - Segment(double[][] A, double[][] B, + Segment(double[][] A, double[][] B, int loRow, int hiRow, int loCol, int hiCol, int steps, @@ -99,7 +105,7 @@ public class ThreadPhaserJacobi { try { double[][] a = A; double[][] b = B; - + for (int i = 0; i < steps; ++i) { maxDiff = update(a, b); if (barrier.awaitAdvance(barrier.arrive()) < 0) @@ -107,7 +113,7 @@ public class ThreadPhaserJacobi { double[][] tmp = a; a = b; b = tmp; } } - catch(Exception ex) { + catch (Exception ex) { ex.printStackTrace(); return; } @@ -130,11 +136,11 @@ public class ThreadPhaserJacobi { return md; } - + } - - static class Driver { + + static class Driver { double[][] A; // matrix to get old values from double[][] B; // matrix to put new values into @@ -145,11 +151,11 @@ public class ThreadPhaserJacobi { final int steps; Segment[] allSegments; - Driver(double[][] mat1, double[][] mat2, + Driver(double[][] mat1, double[][] mat2, int firstRow, int lastRow, int firstCol, int lastCol, int steps) { - + this.A = mat1; this.B = mat2; this.loRow = firstRow; this.hiRow = lastRow; this.loCol = firstCol; this.hiCol = lastCol; @@ -157,11 +163,11 @@ public class ThreadPhaserJacobi { int rows = hiRow - loRow + 1; int cols = hiCol - loCol + 1; - int rblocks = (int)(Math.round((float)rows / dimGran)); - int cblocks = (int)(Math.round((float)cols / dimGran)); - + int rblocks = Math.round((float)rows / dimGran); + int cblocks = Math.round((float)cols / dimGran); + int n = rblocks * cblocks; - + Segment[] segs = new Segment[n]; Phaser barrier = new Phaser(); int k = 0; @@ -169,12 +175,12 @@ public class ThreadPhaserJacobi { int lr = loRow + i * dimGran; int hr = lr + dimGran; if (i == rblocks-1) hr = hiRow; - + for (int j = 0; j < cblocks; ++j) { int lc = loCol + j * dimGran; int hc = lc + dimGran; if (j == cblocks-1) hc = hiCol; - + segs[k] = new Segment(A, B, lr, hr, lc, hc, steps, barrier); ++k; }