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

# Content
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
21 catch (Exception e) {
22 System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>");
23 return;
24 }
25
26 ForkJoinPool fjp = new ForkJoinPool();
27 // int granularity = (n * n / fjp.getParallelism()) / 2;
28 int granularity = n * n / fjp.getParallelism();
29 dimGran = (int)(Math.sqrt(granularity));
30
31 // 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 double smallVal = 1.0/dim;
38 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
55 long time = System.currentTimeMillis() - startTime;
56 double secs = ((double)time) / 1000.0;
57
58 System.out.println("Compute Time: " + secs);
59 System.out.println(fjp);
60
61 }
62
63 }
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 final int loRow;
70 final int hiRow;
71 final int loCol;
72 final int hiCol;
73 volatile double maxDiff; // maximum difference between old and new values
74
75 Segment(double[][] A, double[][] B,
76 int loRow, int hiRow,
77 int loCol, int hiCol,
78 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
108 }
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 static class Driver extends RecursiveAction {
119 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 Driver(double[][] mat1, double[][] mat2,
127 int firstRow, int lastRow,
128 int firstCol, int lastCol,
129 int steps) {
130
131 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 int rblocks = (int)(Math.round((float)rows / dimGran));
141 int cblocks = (int)(Math.round((float)cols / dimGran));
142
143 int n = rblocks * cblocks;
144
145 System.out.println("Using " + n + " segments");
146
147 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
155 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
160 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