ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FJPhaserJacobi.java
Revision: 1.7
Committed: Sun Sep 19 12:55:37 2010 UTC (13 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.6: +26 -31 lines
Log Message:
Add and update FJ and Queue tests

File Contents

# User Rev Content
1 dl 1.1 // Barrier version of Jacobi iteration
2    
3     import java.util.concurrent.*;
4    
5     public class FJPhaserJacobi {
6    
7     static int dimGran;
8    
9     static final double EPSILON = 0.0001; // convergence criterion
10    
11     public static void main(String[] args) {
12     int n = 2048;
13     int steps = 1000;
14     try {
15     if (args.length > 0)
16     n = Integer.parseInt(args[0]);
17     if (args.length > 1)
18     steps = Integer.parseInt(args[1]);
19     }
20 dl 1.7
21 dl 1.1 catch (Exception e) {
22 dl 1.7 System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>");
23 dl 1.1 return;
24     }
25 dl 1.7
26 dl 1.1 ForkJoinPool fjp = new ForkJoinPool();
27 dl 1.7 // int granularity = (n * n / fjp.getParallelism()) / 2;
28 dl 1.1 int granularity = n * n / fjp.getParallelism();
29 dl 1.7 dimGran = (int)(Math.sqrt(granularity));
30    
31 dl 1.1 // allocate enough space for edges
32     int dim = n+2;
33     int ncells = dim * dim;
34     double[][] a = new double[dim][dim];
35     double[][] b = new double[dim][dim];
36     // Initialize interiors to small value
37 dl 1.7 double smallVal = 1.0/dim;
38 dl 1.1 for (int i = 1; i < dim-1; ++i) {
39     for (int j = 1; j < dim-1; ++j)
40     a[i][j] = smallVal;
41     }
42     int nreps = 3;
43     for (int rep = 0; rep < nreps; ++rep) {
44     // Fill all edges with 1's.
45     for (int k = 0; k < dim; ++k) {
46     a[k][0] += 1.0;
47     a[k][n+1] += 1.0;
48     a[0][k] += 1.0;
49     a[n+1][k] += 1.0;
50     }
51     Driver driver = new Driver(a, b, 1, n, 1, n, steps);
52     long startTime = System.currentTimeMillis();
53     fjp.invoke(driver);
54 dl 1.7
55 dl 1.1 long time = System.currentTimeMillis() - startTime;
56 dl 1.7 double secs = ((double)time) / 1000.0;
57    
58 dl 1.1 System.out.println("Compute Time: " + secs);
59     System.out.println(fjp);
60    
61     }
62 dl 1.7
63 dl 1.1 }
64    
65     static class Segment extends CyclicAction {
66     double[][] A; // matrix to get old values from
67     double[][] B; // matrix to put new values into
68     // indices of current submatrix
69 dl 1.7 final int loRow;
70 dl 1.1 final int hiRow;
71     final int loCol;
72     final int hiCol;
73     volatile double maxDiff; // maximum difference between old and new values
74    
75 dl 1.7 Segment(double[][] A, double[][] B,
76 dl 1.1 int loRow, int hiRow,
77 dl 1.7 int loCol, int hiCol,
78 dl 1.1 Phaser br) {
79     super(br);
80     this.A = A; this.B = B;
81     this.loRow = loRow; this.hiRow = hiRow;
82     this.loCol = loCol; this.hiCol = hiCol;
83     }
84    
85     public void step() {
86     maxDiff = update(A, B);
87     double[][] tmp = A; A = B; B = tmp;
88     }
89    
90     double update(double[][] a, double[][] b) {
91     double md = 0.0; // local for computing max diff
92    
93     for (int i = loRow; i <= hiRow; ++i) {
94     for (int j = loCol; j <= hiCol; ++j) {
95     double v = 0.25 * (a[i-1][j] + a[i][j-1] +
96     a[i+1][j] + a[i][j+1]);
97     b[i][j] = v;
98    
99     double diff = v - a[i][j];
100     if (diff < 0) diff = -diff;
101     if (diff > md) md = diff;
102     }
103     }
104    
105     return md;
106     }
107 dl 1.7
108 dl 1.1 }
109    
110     static class MyPhaser extends Phaser {
111     final int max;
112     MyPhaser(int steps) { this.max = steps - 1; }
113     public boolean onAdvance(int phase, int registeredParties) {
114     return phase >= max || registeredParties <= 0;
115     }
116     }
117    
118 dl 1.7 static class Driver extends RecursiveAction {
119 dl 1.1 double[][] A; // matrix to get old values from
120     double[][] B; // matrix to put new values into
121     final int loRow; // indices of current submatrix
122     final int hiRow;
123     final int loCol;
124     final int hiCol;
125     final int steps;
126 dl 1.7 Driver(double[][] mat1, double[][] mat2,
127 dl 1.1 int firstRow, int lastRow,
128     int firstCol, int lastCol,
129     int steps) {
130 dl 1.7
131 dl 1.1 this.A = mat1; this.B = mat2;
132     this.loRow = firstRow; this.hiRow = lastRow;
133     this.loCol = firstCol; this.hiCol = lastCol;
134     this.steps = steps;
135     }
136    
137     public void compute() {
138     int rows = hiRow - loRow + 1;
139     int cols = hiCol - loCol + 1;
140 dl 1.7 int rblocks = (int)(Math.round((float)rows / dimGran));
141     int cblocks = (int)(Math.round((float)cols / dimGran));
142    
143 dl 1.1 int n = rblocks * cblocks;
144 dl 1.7
145 dl 1.1 System.out.println("Using " + n + " segments");
146 dl 1.7
147 dl 1.1 Segment[] segs = new Segment[n];
148     Phaser barrier = new MyPhaser(steps);
149     int k = 0;
150     for (int i = 0; i < rblocks; ++i) {
151     int lr = loRow + i * dimGran;
152     int hr = lr + dimGran;
153     if (i == rblocks-1) hr = hiRow;
154 dl 1.7
155 dl 1.1 for (int j = 0; j < cblocks; ++j) {
156     int lc = loCol + j * dimGran;
157     int hc = lc + dimGran;
158     if (j == cblocks-1) hc = hiCol;
159 dl 1.7
160 dl 1.1 segs[k] = new Segment(A, B, lr, hr, lc, hc, barrier);
161     ++k;
162     }
163     }
164     invokeAll(segs);
165     double maxd = 0;
166     for (k = 0; k < n; ++k) {
167     double md = segs[k].maxDiff;
168     if (md > maxd) maxd = md;
169     }
170     System.out.println("Max diff after " + steps + " steps = " + maxd);
171     }
172     }
173     }
174 dl 1.7