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/licenses/publicdomain |
5 |
+ |
*/ |
6 |
+ |
|
7 |
|
// Barrier version of Jacobi iteration |
8 |
|
|
9 |
|
import java.util.*; |
14 |
|
|
15 |
|
static final int nprocs = Runtime.getRuntime().availableProcessors(); |
16 |
|
|
17 |
< |
/** |
17 |
> |
/** |
18 |
|
* The maximum submatrix length (both row-wise and column-wise) |
19 |
|
* for any Segment |
20 |
|
**/ |
32 |
|
if (args.length > 1) |
33 |
|
steps = Integer.parseInt(args[1]); |
34 |
|
} |
35 |
< |
|
35 |
> |
|
36 |
|
catch (Exception e) { |
37 |
|
System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>"); |
38 |
|
return; |
39 |
|
} |
40 |
|
|
41 |
|
int granularity = n * n / nprocs; |
42 |
< |
dimGran = (int)(Math.sqrt(granularity)); |
42 |
> |
dimGran = (int) Math.sqrt(granularity); |
43 |
|
|
44 |
|
// allocate enough space for edges |
45 |
|
int dim = n+2; |
47 |
|
double[][] a = new double[dim][dim]; |
48 |
|
double[][] b = new double[dim][dim]; |
49 |
|
// Initialize interiors to small value |
50 |
< |
double smallVal = 1.0/dim; |
50 |
> |
double smallVal = 1.0/dim; |
51 |
|
for (int i = 1; i < dim-1; ++i) { |
52 |
|
for (int j = 1; j < dim-1; ++j) |
53 |
|
a[i][j] = smallVal; |
54 |
|
} |
55 |
< |
|
55 |
> |
|
56 |
|
int nreps = 3; |
57 |
|
for (int rep = 0; rep < nreps; ++rep) { |
58 |
|
// Fill all edges with 1's. |
65 |
|
Driver driver = new Driver(a, b, 1, n, 1, n, steps); |
66 |
|
long startTime = System.currentTimeMillis(); |
67 |
|
driver.compute(); |
68 |
< |
|
68 |
> |
|
69 |
|
long time = System.currentTimeMillis() - startTime; |
70 |
< |
double secs = ((double)time) / 1000.0; |
71 |
< |
|
70 |
> |
double secs = (double) time / 1000.0; |
71 |
> |
|
72 |
|
System.out.println("Compute Time: " + secs); |
73 |
|
} |
74 |
|
} |
79 |
|
double[][] B; // matrix to put new values into |
80 |
|
|
81 |
|
// indices of current submatrix |
82 |
< |
final int loRow; |
82 |
> |
final int loRow; |
83 |
|
final int hiRow; |
84 |
|
final int loCol; |
85 |
|
final int hiCol; |
87 |
|
final Phaser barrier; |
88 |
|
double maxDiff; // maximum difference between old and new values |
89 |
|
|
90 |
< |
Segment(double[][] A, double[][] B, |
90 |
> |
Segment(double[][] A, double[][] B, |
91 |
|
int loRow, int hiRow, |
92 |
|
int loCol, int hiCol, |
93 |
|
int steps, |
105 |
|
try { |
106 |
|
double[][] a = A; |
107 |
|
double[][] b = B; |
108 |
< |
|
108 |
> |
|
109 |
|
for (int i = 0; i < steps; ++i) { |
110 |
|
maxDiff = update(a, b); |
111 |
|
if (barrier.awaitAdvance(barrier.arrive()) < 0) |
113 |
|
double[][] tmp = a; a = b; b = tmp; |
114 |
|
} |
115 |
|
} |
116 |
< |
catch(Exception ex) { |
116 |
> |
catch (Exception ex) { |
117 |
|
ex.printStackTrace(); |
118 |
|
return; |
119 |
|
} |
136 |
|
|
137 |
|
return md; |
138 |
|
} |
139 |
< |
|
139 |
> |
|
140 |
|
} |
135 |
– |
|
141 |
|
|
142 |
< |
static class Driver { |
142 |
> |
|
143 |
> |
static class Driver { |
144 |
|
double[][] A; // matrix to get old values from |
145 |
|
double[][] B; // matrix to put new values into |
146 |
|
|
151 |
|
final int steps; |
152 |
|
Segment[] allSegments; |
153 |
|
|
154 |
< |
Driver(double[][] mat1, double[][] mat2, |
154 |
> |
Driver(double[][] mat1, double[][] mat2, |
155 |
|
int firstRow, int lastRow, |
156 |
|
int firstCol, int lastCol, |
157 |
|
int steps) { |
158 |
< |
|
158 |
> |
|
159 |
|
this.A = mat1; this.B = mat2; |
160 |
|
this.loRow = firstRow; this.hiRow = lastRow; |
161 |
|
this.loCol = firstCol; this.hiCol = lastCol; |
163 |
|
|
164 |
|
int rows = hiRow - loRow + 1; |
165 |
|
int cols = hiCol - loCol + 1; |
166 |
< |
int rblocks = (int)(Math.round((float)rows / dimGran)); |
167 |
< |
int cblocks = (int)(Math.round((float)cols / dimGran)); |
168 |
< |
|
166 |
> |
int rblocks = Math.round((float) rows / dimGran); |
167 |
> |
int cblocks = Math.round((float) cols / dimGran); |
168 |
> |
|
169 |
|
int n = rblocks * cblocks; |
170 |
< |
|
170 |
> |
|
171 |
|
Segment[] segs = new Segment[n]; |
172 |
|
Phaser barrier = new Phaser(); |
173 |
|
int k = 0; |
175 |
|
int lr = loRow + i * dimGran; |
176 |
|
int hr = lr + dimGran; |
177 |
|
if (i == rblocks-1) hr = hiRow; |
178 |
< |
|
178 |
> |
|
179 |
|
for (int j = 0; j < cblocks; ++j) { |
180 |
|
int lc = loCol + j * dimGran; |
181 |
|
int hc = lc + dimGran; |
182 |
|
if (j == cblocks-1) hc = hiCol; |
183 |
< |
|
183 |
> |
|
184 |
|
segs[k] = new Segment(A, B, lr, hr, lc, hc, steps, barrier); |
185 |
|
++k; |
186 |
|
} |