8 |
|
import java.util.concurrent.*; |
9 |
|
|
10 |
|
/** |
11 |
< |
* Sample program using Guassian Quadrature for numerical integration. |
11 |
> |
* Sample program using Gaussian Quadrature for numerical integration. |
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 |
|
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 |
|
*/ |
54 |
|
} |
55 |
|
} |
56 |
|
catch (Exception e) { |
57 |
< |
System.out.println("Usage: java Integrate3 threads <s[erial] | d[ynamic] | f[ork] - default d>"); |
57 |
> |
System.out.println("Usage: java Integrate threads <s[erial] | d[ynamic] | f[ork] - default d>"); |
58 |
|
return; |
59 |
|
} |
60 |
|
|
61 |
|
ForkJoinPool g = new ForkJoinPool(procs); |
62 |
< |
System.out.println("Integrating from " + start + " to " + end + |
62 |
> |
System.out.println("Integrating from " + start + " to " + end + |
63 |
|
" forkPolicy = " + forkArg); |
64 |
|
long lastTime = System.nanoTime(); |
65 |
|
for (int i = 0; i < 10; ++i) { |
66 |
|
double a; |
67 |
|
if (forkPolicy == SERIAL) |
68 |
|
a = SQuad.computeArea(g, start, end); |
69 |
< |
else if (forkPolicy == FORK) |
69 |
> |
else if (forkPolicy == FORK) |
70 |
|
a = FQuad.computeArea(g, start, end); |
71 |
|
else |
72 |
|
a = DQuad.computeArea(g, start, end); |
73 |
|
long now = System.nanoTime(); |
74 |
< |
double s = ((double)(now - lastTime))/NPS; |
74 |
> |
double s = ((double) (now - lastTime)) / NPS; |
75 |
|
lastTime = now; |
76 |
|
System.out.printf("Calls/sec: %12d", (long) (calls / s)); |
77 |
|
System.out.printf(" Time: %7.3f", s); |
94 |
|
final double left; // lower bound |
95 |
|
final double right; // upper bound |
96 |
|
double area; |
97 |
< |
|
97 |
> |
|
98 |
|
SQuad(double l, double r, double a) { |
99 |
|
this.left = l; this.right = r; this.area = a; |
100 |
|
} |
101 |
< |
|
101 |
> |
|
102 |
|
public final void compute() { |
103 |
|
double l = left; |
104 |
|
double r = right; |
105 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
106 |
|
} |
107 |
< |
|
107 |
> |
|
108 |
|
static final double recEval(double l, double r, double fl, |
109 |
|
double fr, double a) { |
110 |
|
double h = (r - l) * 0.5; |
111 |
|
double c = l + h; |
112 |
< |
double fc = (c * c + 1.0) * c; |
112 |
> |
double fc = (c * c + 1.0) * c; |
113 |
|
double hh = h * 0.5; |
114 |
< |
double al = (fl + fc) * hh; |
114 |
> |
double al = (fl + fc) * hh; |
115 |
|
double ar = (fr + fc) * hh; |
116 |
|
double alr = al + ar; |
117 |
|
if (Math.abs(alr - a) <= errorTolerance) |
135 |
|
final double left; // lower bound |
136 |
|
final double right; // upper bound |
137 |
|
double area; |
138 |
< |
|
138 |
> |
|
139 |
|
FQuad(double l, double r, double a) { |
140 |
|
this.left = l; this.right = r; this.area = a; |
141 |
|
} |
142 |
< |
|
142 |
> |
|
143 |
|
public final void compute() { |
144 |
|
double l = left; |
145 |
|
double r = right; |
146 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
147 |
|
} |
148 |
< |
|
148 |
> |
|
149 |
|
static final double recEval(double l, double r, double fl, |
150 |
|
double fr, double a) { |
151 |
|
double h = (r - l) * 0.5; |
152 |
|
double c = l + h; |
153 |
< |
double fc = (c * c + 1.0) * c; |
153 |
> |
double fc = (c * c + 1.0) * c; |
154 |
|
double hh = h * 0.5; |
155 |
< |
double al = (fl + fc) * hh; |
155 |
> |
double al = (fl + fc) * hh; |
156 |
|
double ar = (fr + fc) * hh; |
157 |
|
double alr = al + ar; |
158 |
|
if (Math.abs(alr - a) <= errorTolerance) |
161 |
|
q.fork(); |
162 |
|
ar = recEval(c, r, fc, fr, ar); |
163 |
|
if (!q.tryUnfork()) { |
164 |
< |
q.quietlyHelpJoin(); |
164 |
> |
q.quietlyJoin(); |
165 |
|
return ar + q.area; |
166 |
|
} |
167 |
|
return ar + recEval(l, c, fl, fc, al); |
171 |
|
|
172 |
|
// ........................... |
173 |
|
|
174 |
< |
// Version using on-demand Fork |
174 |
> |
// Version using on-demand Fork |
175 |
|
static final class DQuad extends RecursiveAction { |
176 |
|
static double computeArea(ForkJoinPool pool, double l, double r) { |
177 |
|
DQuad q = new DQuad(l, r, 0); |
182 |
|
final double left; // lower bound |
183 |
|
final double right; // upper bound |
184 |
|
double area; |
185 |
< |
|
185 |
> |
|
186 |
|
DQuad(double l, double r, double a) { |
187 |
|
this.left = l; this.right = r; this.area = a; |
188 |
|
} |
189 |
< |
|
189 |
> |
|
190 |
|
public final void compute() { |
191 |
|
double l = left; |
192 |
|
double r = right; |
193 |
|
area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area); |
194 |
|
} |
195 |
< |
|
195 |
> |
|
196 |
|
static final double recEval(double l, double r, double fl, |
197 |
|
double fr, double a) { |
198 |
|
double h = (r - l) * 0.5; |
199 |
|
double c = l + h; |
200 |
< |
double fc = (c * c + 1.0) * c; |
200 |
> |
double fc = (c * c + 1.0) * c; |
201 |
|
double hh = h * 0.5; |
202 |
< |
double al = (fl + fc) * hh; |
202 |
> |
double al = (fl + fc) * hh; |
203 |
|
double ar = (fr + fc) * hh; |
204 |
|
double alr = al + ar; |
205 |
|
if (Math.abs(alr - a) <= errorTolerance) |
209 |
|
(q = new DQuad(l, c, al)).fork(); |
210 |
|
ar = recEval(c, r, fc, fr, ar); |
211 |
|
if (q != null && !q.tryUnfork()) { |
212 |
< |
q.quietlyHelpJoin(); |
212 |
> |
q.quietlyJoin(); |
213 |
|
return ar + q.area; |
214 |
|
} |
215 |
|
return ar + recEval(l, c, fl, fc, al); |
218 |
|
} |
219 |
|
|
220 |
|
} |
222 |
– |
|
223 |
– |
|