1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
//import jsr166y.*; |
7 |
|
import java.util.concurrent.*; |
8 |
|
|
9 |
|
/** |
10 |
< |
* Sample program using Gaussian Quadrature for numerical integration. |
10 |
> |
* Sample program using Guassian Quadrature for numerical integration. |
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. |
25 |
|
static String forkArg = "dynamic"; |
26 |
|
|
27 |
|
// the function to integrate |
28 |
< |
static double computeFunction(double x) { |
28 |
> |
static double computeFunction(double x) { |
29 |
|
return (x * x + 1.0) * x; |
30 |
|
} |
31 |
|
|
39 |
|
static final int calls = 263479047; |
40 |
|
|
41 |
|
public static void main(String[] args) throws Exception { |
42 |
< |
int procs = Runtime.getRuntime().availableProcessors(); |
42 |
> |
int procs = 0; |
43 |
|
|
44 |
|
try { |
45 |
|
if (args.length > 0) |
53 |
|
} |
54 |
|
} |
55 |
|
catch (Exception e) { |
56 |
< |
System.out.println("Usage: java Integrate threads <s[erial] | d[ynamic] | f[ork] - default d>"); |
56 |
> |
System.out.println("Usage: java Integrate3 threads <s[erial] | d[ynamic] | f[ork] - default d>"); |
57 |
|
return; |
58 |
|
} |
59 |
+ |
oneTest(procs); |
60 |
+ |
oneTest(procs); |
61 |
+ |
oneTest(procs); |
62 |
+ |
} |
63 |
|
|
64 |
< |
ForkJoinPool g = new ForkJoinPool(procs); |
64 |
> |
static void oneTest(int procs) { |
65 |
> |
ForkJoinPool g = (procs == 0) ? new ForkJoinPool() : |
66 |
> |
new ForkJoinPool(procs); |
67 |
> |
System.out.println("Number of procs=" + g.getParallelism()); |
68 |
|
System.out.println("Integrating from " + start + " to " + end + |
69 |
|
" forkPolicy = " + forkArg); |
70 |
|
long lastTime = System.nanoTime(); |
71 |
< |
for (int i = 0; i < 10; ++i) { |
71 |
> |
for (int i = 0; i < 20; ++i) { |
72 |
|
double a; |
73 |
|
if (forkPolicy == SERIAL) |
74 |
|
a = SQuad.computeArea(g, start, end); |
77 |
|
else |
78 |
|
a = DQuad.computeArea(g, start, end); |
79 |
|
long now = System.nanoTime(); |
80 |
< |
double s = ((double) (now - lastTime)) / NPS; |
80 |
> |
double s = ((double)(now - lastTime))/NPS; |
81 |
|
lastTime = now; |
82 |
|
System.out.printf("Calls/sec: %12d", (long) (calls / s)); |
83 |
|
System.out.printf(" Time: %7.3f", s); |
84 |
< |
System.out.printf(" Area: %12.1f", a); |
84 |
> |
System.out.printf(" Threads: %5d", g.getPoolSize()); |
85 |
> |
// System.out.printf(" Area: %12.1f", a); |
86 |
|
System.out.println(); |
87 |
|
} |
88 |
|
System.out.println(g); |
168 |
|
q.fork(); |
169 |
|
ar = recEval(c, r, fc, fr, ar); |
170 |
|
if (!q.tryUnfork()) { |
171 |
< |
q.quietlyHelpJoin(); |
171 |
> |
q.quietlyJoin(); |
172 |
|
return ar + q.area; |
173 |
|
} |
174 |
|
return ar + recEval(l, c, fl, fc, al); |
216 |
|
(q = new DQuad(l, c, al)).fork(); |
217 |
|
ar = recEval(c, r, fc, fr, ar); |
218 |
|
if (q != null && !q.tryUnfork()) { |
219 |
< |
q.quietlyHelpJoin(); |
219 |
> |
q.quietlyJoin(); |
220 |
|
return ar + q.area; |
221 |
|
} |
222 |
|
return ar + recEval(l, c, fl, fc, al); |