ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/FJPhaserJacobi.java
Revision: 1.12
Committed: Mon Aug 10 03:13:33 2015 UTC (8 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.11: +0 -2 lines
Log Message:
delete unwanted blank lines

File Contents

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