11 |
|
* This version uses a simplified hardwired function. Inspired by a |
12 |
|
* <A href="http://www.cs.uga.edu/~dkl/filaments/dist.html"> |
13 |
|
* Filaments</A> demo program. |
14 |
< |
* |
14 |
> |
* |
15 |
|
*/ |
16 |
|
public final class Integrate { |
17 |
|
|
32 |
|
|
33 |
|
static final double start = 0.0; |
34 |
|
static final double end = 1536.0; |
35 |
< |
/* |
36 |
< |
* The number of recursive calls for |
35 |
> |
/* |
36 |
> |
* The number of recursive calls for |
37 |
|
* integrate from start to end. |
38 |
|
* (Empirically determined) |
39 |
|
*/ |
63 |
|
} |
64 |
|
|
65 |
|
static void oneTest(int procs) { |
66 |
< |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
66 |
> |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
67 |
|
new ForkJoinPool(procs); |
68 |
|
System.out.println("Number of procs=" + g.getParallelism()); |
69 |
< |
System.out.println("Integrating from " + start + " to " + end + |
69 |
> |
System.out.println("Integrating from " + start + " to " + end + |
70 |
|
" forkPolicy = " + forkArg); |
71 |
|
long lastTime = System.nanoTime(); |
72 |
|
for (int i = 0; i < 20; ++i) { |
73 |
|
double a; |
74 |
|
if (forkPolicy == SERIAL) |
75 |
|
a = SQuad.computeArea(g, start, end); |
76 |
< |
else if (forkPolicy == FORK) |
76 |
> |
else if (forkPolicy == FORK) |
77 |
|
a = FQuad.computeArea(g, start, end); |
78 |
|
else |
79 |
|
a = DQuad.computeArea(g, start, end); |
102 |
|
final double left; // lower bound |
103 |
|
final double right; // upper bound |
104 |
|
double area; |
105 |
< |
|
105 |
> |
|
106 |
|
SQuad(double l, double r, double a) { |
107 |
|
this.left = l; this.right = r; this.area = a; |
108 |
|
} |
109 |
< |
|
109 |
> |
|
110 |
|
public final void compute() { |
111 |
|
double l = left; |
112 |
|
double r = right; |
113 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
114 |
|
} |
115 |
< |
|
115 |
> |
|
116 |
|
static final double recEval(double l, double r, double fl, |
117 |
|
double fr, double a) { |
118 |
|
double h = (r - l) * 0.5; |
119 |
|
double c = l + h; |
120 |
< |
double fc = (c * c + 1.0) * c; |
120 |
> |
double fc = (c * c + 1.0) * c; |
121 |
|
double hh = h * 0.5; |
122 |
< |
double al = (fl + fc) * hh; |
122 |
> |
double al = (fl + fc) * hh; |
123 |
|
double ar = (fr + fc) * hh; |
124 |
|
double alr = al + ar; |
125 |
|
if (Math.abs(alr - a) <= errorTolerance) |
143 |
|
final double left; // lower bound |
144 |
|
final double right; // upper bound |
145 |
|
double area; |
146 |
< |
|
146 |
> |
|
147 |
|
FQuad(double l, double r, double a) { |
148 |
|
this.left = l; this.right = r; this.area = a; |
149 |
|
} |
150 |
< |
|
150 |
> |
|
151 |
|
public final void compute() { |
152 |
|
double l = left; |
153 |
|
double r = right; |
154 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
155 |
|
} |
156 |
< |
|
156 |
> |
|
157 |
|
static final double recEval(double l, double r, double fl, |
158 |
|
double fr, double a) { |
159 |
|
double h = (r - l) * 0.5; |
160 |
|
double c = l + h; |
161 |
< |
double fc = (c * c + 1.0) * c; |
161 |
> |
double fc = (c * c + 1.0) * c; |
162 |
|
double hh = h * 0.5; |
163 |
< |
double al = (fl + fc) * hh; |
163 |
> |
double al = (fl + fc) * hh; |
164 |
|
double ar = (fr + fc) * hh; |
165 |
|
double alr = al + ar; |
166 |
|
if (Math.abs(alr - a) <= errorTolerance) |
180 |
|
|
181 |
|
// ........................... |
182 |
|
|
183 |
< |
// Version using on-demand Fork |
183 |
> |
// Version using on-demand Fork |
184 |
|
static final class DQuad extends RecursiveAction { |
185 |
|
static double computeArea(ForkJoinPool pool, double l, double r) { |
186 |
|
DQuad q = new DQuad(l, r, 0); |
191 |
|
final double left; // lower bound |
192 |
|
final double right; // upper bound |
193 |
|
double area; |
194 |
< |
|
194 |
> |
|
195 |
|
DQuad(double l, double r, double a) { |
196 |
|
this.left = l; this.right = r; this.area = a; |
197 |
|
} |
198 |
< |
|
198 |
> |
|
199 |
|
public final void compute() { |
200 |
|
double l = left; |
201 |
|
double r = right; |
202 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
203 |
|
} |
204 |
< |
|
204 |
> |
|
205 |
|
static final double recEval(double l, double r, double fl, |
206 |
|
double fr, double a) { |
207 |
|
double h = (r - l) * 0.5; |
208 |
|
double c = l + h; |
209 |
< |
double fc = (c * c + 1.0) * c; |
209 |
> |
double fc = (c * c + 1.0) * c; |
210 |
|
double hh = h * 0.5; |
211 |
< |
double al = (fl + fc) * hh; |
211 |
> |
double al = (fl + fc) * hh; |
212 |
|
double ar = (fr + fc) * hh; |
213 |
|
double alr = al + ar; |
214 |
|
if (Math.abs(alr - a) <= errorTolerance) |
229 |
|
|
230 |
|
} |
231 |
|
|
232 |
< |
|
232 |
> |
|