12 |
|
* This version uses a simplified hardwired function. Inspired by a |
13 |
|
* <A href="http://www.cs.uga.edu/~dkl/filaments/dist.html"> |
14 |
|
* Filaments</A> demo program. |
15 |
< |
* |
15 |
> |
* |
16 |
|
*/ |
17 |
|
public final class Integrate { |
18 |
|
|
33 |
|
|
34 |
|
static final double start = 0.0; |
35 |
|
static final double end = 1536.0; |
36 |
< |
/* |
37 |
< |
* The number of recursive calls for |
36 |
> |
/* |
37 |
> |
* The number of recursive calls for |
38 |
|
* integrate from start to end. |
39 |
|
* (Empirically determined) |
40 |
|
*/ |
60 |
|
} |
61 |
|
|
62 |
|
ForkJoinPool g = new ForkJoinPool(procs); |
63 |
< |
System.out.println("Integrating from " + start + " to " + end + |
63 |
> |
System.out.println("Integrating from " + start + " to " + end + |
64 |
|
" forkPolicy = " + forkArg); |
65 |
|
long lastTime = System.nanoTime(); |
66 |
|
for (int i = 0; i < 10; ++i) { |
67 |
|
double a; |
68 |
|
if (forkPolicy == SERIAL) |
69 |
|
a = SQuad.computeArea(g, start, end); |
70 |
< |
else if (forkPolicy == FORK) |
70 |
> |
else if (forkPolicy == FORK) |
71 |
|
a = FQuad.computeArea(g, start, end); |
72 |
|
else |
73 |
|
a = DQuad.computeArea(g, start, end); |
95 |
|
final double left; // lower bound |
96 |
|
final double right; // upper bound |
97 |
|
double area; |
98 |
< |
|
98 |
> |
|
99 |
|
SQuad(double l, double r, double a) { |
100 |
|
this.left = l; this.right = r; this.area = a; |
101 |
|
} |
102 |
< |
|
102 |
> |
|
103 |
|
public final void compute() { |
104 |
|
double l = left; |
105 |
|
double r = right; |
106 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
107 |
|
} |
108 |
< |
|
108 |
> |
|
109 |
|
static final double recEval(double l, double r, double fl, |
110 |
|
double fr, double a) { |
111 |
|
double h = (r - l) * 0.5; |
112 |
|
double c = l + h; |
113 |
< |
double fc = (c * c + 1.0) * c; |
113 |
> |
double fc = (c * c + 1.0) * c; |
114 |
|
double hh = h * 0.5; |
115 |
< |
double al = (fl + fc) * hh; |
115 |
> |
double al = (fl + fc) * hh; |
116 |
|
double ar = (fr + fc) * hh; |
117 |
|
double alr = al + ar; |
118 |
|
if (Math.abs(alr - a) <= errorTolerance) |
136 |
|
final double left; // lower bound |
137 |
|
final double right; // upper bound |
138 |
|
double area; |
139 |
< |
|
139 |
> |
|
140 |
|
FQuad(double l, double r, double a) { |
141 |
|
this.left = l; this.right = r; this.area = a; |
142 |
|
} |
143 |
< |
|
143 |
> |
|
144 |
|
public final void compute() { |
145 |
|
double l = left; |
146 |
|
double r = right; |
147 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
148 |
|
} |
149 |
< |
|
149 |
> |
|
150 |
|
static final double recEval(double l, double r, double fl, |
151 |
|
double fr, double a) { |
152 |
|
double h = (r - l) * 0.5; |
153 |
|
double c = l + h; |
154 |
< |
double fc = (c * c + 1.0) * c; |
154 |
> |
double fc = (c * c + 1.0) * c; |
155 |
|
double hh = h * 0.5; |
156 |
< |
double al = (fl + fc) * hh; |
156 |
> |
double al = (fl + fc) * hh; |
157 |
|
double ar = (fr + fc) * hh; |
158 |
|
double alr = al + ar; |
159 |
|
if (Math.abs(alr - a) <= errorTolerance) |
172 |
|
|
173 |
|
// ........................... |
174 |
|
|
175 |
< |
// Version using on-demand Fork |
175 |
> |
// Version using on-demand Fork |
176 |
|
static final class DQuad extends RecursiveAction { |
177 |
|
static double computeArea(ForkJoinPool pool, double l, double r) { |
178 |
|
DQuad q = new DQuad(l, r, 0); |
183 |
|
final double left; // lower bound |
184 |
|
final double right; // upper bound |
185 |
|
double area; |
186 |
< |
|
186 |
> |
|
187 |
|
DQuad(double l, double r, double a) { |
188 |
|
this.left = l; this.right = r; this.area = a; |
189 |
|
} |
190 |
< |
|
190 |
> |
|
191 |
|
public final void compute() { |
192 |
|
double l = left; |
193 |
|
double r = right; |
194 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
195 |
|
} |
196 |
< |
|
196 |
> |
|
197 |
|
static final double recEval(double l, double r, double fl, |
198 |
|
double fr, double a) { |
199 |
|
double h = (r - l) * 0.5; |
200 |
|
double c = l + h; |
201 |
< |
double fc = (c * c + 1.0) * c; |
201 |
> |
double fc = (c * c + 1.0) * c; |
202 |
|
double hh = h * 0.5; |
203 |
< |
double al = (fl + fc) * hh; |
203 |
> |
double al = (fl + fc) * hh; |
204 |
|
double ar = (fr + fc) * hh; |
205 |
|
double alr = al + ar; |
206 |
|
if (Math.abs(alr - a) <= errorTolerance) |
219 |
|
} |
220 |
|
|
221 |
|
} |
222 |
– |
|
223 |
– |
|