8 |
|
|
9 |
|
static final int nprocs = Runtime.getRuntime().availableProcessors(); |
10 |
|
|
11 |
< |
/** |
11 |
> |
/** |
12 |
|
* The maximum submatrix length (both row-wise and column-wise) |
13 |
|
* for any Segment |
14 |
|
**/ |
26 |
|
if (args.length > 1) |
27 |
|
steps = Integer.parseInt(args[1]); |
28 |
|
} |
29 |
< |
|
29 |
> |
|
30 |
|
catch (Exception e) { |
31 |
|
System.out.println("Usage: java ThreadPhaserJacobi <matrix size> <max steps>"); |
32 |
|
return; |
41 |
|
double[][] a = new double[dim][dim]; |
42 |
|
double[][] b = new double[dim][dim]; |
43 |
|
// Initialize interiors to small value |
44 |
< |
double smallVal = 1.0/dim; |
44 |
> |
double smallVal = 1.0/dim; |
45 |
|
for (int i = 1; i < dim-1; ++i) { |
46 |
|
for (int j = 1; j < dim-1; ++j) |
47 |
|
a[i][j] = smallVal; |
48 |
|
} |
49 |
< |
|
49 |
> |
|
50 |
|
int nreps = 3; |
51 |
|
for (int rep = 0; rep < nreps; ++rep) { |
52 |
|
// Fill all edges with 1's. |
59 |
|
Driver driver = new Driver(a, b, 1, n, 1, n, steps); |
60 |
|
long startTime = System.currentTimeMillis(); |
61 |
|
driver.compute(); |
62 |
< |
|
62 |
> |
|
63 |
|
long time = System.currentTimeMillis() - startTime; |
64 |
|
double secs = ((double)time) / 1000.0; |
65 |
< |
|
65 |
> |
|
66 |
|
System.out.println("Compute Time: " + secs); |
67 |
|
} |
68 |
|
} |
73 |
|
double[][] B; // matrix to put new values into |
74 |
|
|
75 |
|
// indices of current submatrix |
76 |
< |
final int loRow; |
76 |
> |
final int loRow; |
77 |
|
final int hiRow; |
78 |
|
final int loCol; |
79 |
|
final int hiCol; |
81 |
|
final Phaser barrier; |
82 |
|
double maxDiff; // maximum difference between old and new values |
83 |
|
|
84 |
< |
Segment(double[][] A, double[][] B, |
84 |
> |
Segment(double[][] A, double[][] B, |
85 |
|
int loRow, int hiRow, |
86 |
|
int loCol, int hiCol, |
87 |
|
int steps, |
99 |
|
try { |
100 |
|
double[][] a = A; |
101 |
|
double[][] b = B; |
102 |
< |
|
102 |
> |
|
103 |
|
for (int i = 0; i < steps; ++i) { |
104 |
|
maxDiff = update(a, b); |
105 |
|
if (barrier.awaitAdvance(barrier.arrive()) < 0) |
107 |
|
double[][] tmp = a; a = b; b = tmp; |
108 |
|
} |
109 |
|
} |
110 |
< |
catch(Exception ex) { |
110 |
> |
catch(Exception ex) { |
111 |
|
ex.printStackTrace(); |
112 |
|
return; |
113 |
|
} |
130 |
|
|
131 |
|
return md; |
132 |
|
} |
133 |
< |
|
133 |
> |
|
134 |
|
} |
135 |
– |
|
135 |
|
|
136 |
< |
static class Driver { |
136 |
> |
|
137 |
> |
static class Driver { |
138 |
|
double[][] A; // matrix to get old values from |
139 |
|
double[][] B; // matrix to put new values into |
140 |
|
|
145 |
|
final int steps; |
146 |
|
Segment[] allSegments; |
147 |
|
|
148 |
< |
Driver(double[][] mat1, double[][] mat2, |
148 |
> |
Driver(double[][] mat1, double[][] mat2, |
149 |
|
int firstRow, int lastRow, |
150 |
|
int firstCol, int lastCol, |
151 |
|
int steps) { |
152 |
< |
|
152 |
> |
|
153 |
|
this.A = mat1; this.B = mat2; |
154 |
|
this.loRow = firstRow; this.hiRow = lastRow; |
155 |
|
this.loCol = firstCol; this.hiCol = lastCol; |
159 |
|
int cols = hiCol - loCol + 1; |
160 |
|
int rblocks = (int)(Math.round((float)rows / dimGran)); |
161 |
|
int cblocks = (int)(Math.round((float)cols / dimGran)); |
162 |
< |
|
162 |
> |
|
163 |
|
int n = rblocks * cblocks; |
164 |
< |
|
164 |
> |
|
165 |
|
Segment[] segs = new Segment[n]; |
166 |
|
Phaser barrier = new Phaser(); |
167 |
|
int k = 0; |
169 |
|
int lr = loRow + i * dimGran; |
170 |
|
int hr = lr + dimGran; |
171 |
|
if (i == rblocks-1) hr = hiRow; |
172 |
< |
|
172 |
> |
|
173 |
|
for (int j = 0; j < cblocks; ++j) { |
174 |
|
int lc = loCol + j * dimGran; |
175 |
|
int hc = lc + dimGran; |
176 |
|
if (j == cblocks-1) hc = hiCol; |
177 |
< |
|
177 |
> |
|
178 |
|
segs[k] = new Segment(A, B, lr, hr, lc, hc, steps, barrier); |
179 |
|
++k; |
180 |
|
} |