7 |
|
// Jacobi iteration on a mesh. Based loosely on a Filaments demo |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
– |
//import jsr166y.*; |
10 |
|
|
11 |
|
public class FJJacobi { |
12 |
|
|
15 |
|
/** |
16 |
|
* The maximum number of matrix cells |
17 |
|
* at which to stop recursing down and instead directly update. |
18 |
< |
**/ |
20 |
< |
|
18 |
> |
*/ |
19 |
|
static final double EPSILON = 0.0001; // convergence criterion |
20 |
|
|
21 |
|
public static void main(String[] args) throws Exception { |
45 |
|
double[][] a = new double[dim][dim]; |
46 |
|
double[][] b = new double[dim][dim]; |
47 |
|
// Initialize interiors to small value |
48 |
< |
double smallVal = 1.0/dim; |
48 |
> |
double smallVal = EPSILON; // 1.0/dim; |
49 |
|
for (int i = 1; i < dim-1; ++i) { |
50 |
|
for (int j = 1; j < dim-1; ++j) |
51 |
|
a[i][j] = smallVal; |
52 |
|
} |
53 |
< |
|
54 |
< |
int nreps = 3; |
53 |
> |
// Fill all edges with 1's. |
54 |
> |
for (int k = 0; k < dim; ++k) { |
55 |
> |
a[k][0] = 1.0; |
56 |
> |
a[k][n+1] = 1.0; |
57 |
> |
a[0][k] = 1.0; |
58 |
> |
a[n+1][k] = 1.0; |
59 |
> |
b[k][0] = 1.0; |
60 |
> |
b[k][n+1] = 1.0; |
61 |
> |
b[0][k] = 1.0; |
62 |
> |
b[n+1][k] = 1.0; |
63 |
> |
} |
64 |
> |
int nreps = 10; |
65 |
|
for (int rep = 0; rep < nreps; ++rep) { |
58 |
– |
// Fill all edges with 1's. |
59 |
– |
for (int k = 0; k < dim; ++k) { |
60 |
– |
a[k][0] += 1.0; |
61 |
– |
a[k][n+1] += 1.0; |
62 |
– |
a[0][k] += 1.0; |
63 |
– |
a[n+1][k] += 1.0; |
64 |
– |
} |
66 |
|
Driver driver = new Driver(a, b, 1, n, 1, n, steps, granularity); |
67 |
|
|
68 |
|
long startTime = System.currentTimeMillis(); |
69 |
|
fjp.invoke(driver); |
70 |
|
|
71 |
|
long time = System.currentTimeMillis() - startTime; |
72 |
< |
double secs = (double) time / 1000.0; |
72 |
> |
double secs = ((double)time) / 1000.0; |
73 |
|
|
74 |
|
System.out.println("Compute Time: " + secs); |
74 |
– |
System.out.println("Workers: " + fjp.getPoolSize()); |
75 |
|
System.out.println(fjp); |
76 |
|
} |
77 |
|
} |
88 |
|
if (tryUnfork()) |
89 |
|
compute(); |
90 |
|
else { |
91 |
– |
// quietlyJoin(); |
91 |
|
quietlyJoin(); |
92 |
|
reinitialize(); |
93 |
|
} |
94 |
|
double m = maxDiff; |
95 |
< |
return (md > m) ? md : m; |
95 |
> |
return (md > m)? md : m; |
96 |
|
} |
97 |
|
|
98 |
|
} |
118 |
|
|
119 |
|
public void compute() { |
120 |
|
boolean AtoB = (steps++ & 1) == 0; |
121 |
< |
double[][] a = AtoB ? A : B; |
122 |
< |
double[][] b = AtoB ? B : A; |
121 |
> |
double[][] a = (AtoB)? A : B; |
122 |
> |
double[][] b = (AtoB)? B : A; |
123 |
|
|
124 |
|
double md = 0.0; // local for computing max diff |
125 |
|
|