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