ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Integrate.java
Revision: 1.5
Committed: Tue Nov 3 00:02:00 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +0 -1 lines
Log Message:
whitespace

File Contents

# Content
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
5 */
6
7 //import jsr166y.*;
8 import java.util.concurrent.*;
9
10 /**
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 */
16 public final class Integrate {
17
18 static final double errorTolerance = 1.0e-12;
19 /** for time conversion */
20 static final long NPS = (1000L * 1000 * 1000);
21
22 static final int SERIAL = -1;
23 static final int DYNAMIC = 0;
24 static final int FORK = 1;
25 static int forkPolicy = DYNAMIC;
26 static String forkArg = "dynamic";
27
28 // the function to integrate
29 static double computeFunction(double x) {
30 return (x * x + 1.0) * x;
31 }
32
33 static final double start = 0.0;
34 static final double end = 1536.0;
35 /*
36 * The number of recursive calls for
37 * integrate from start to end.
38 * (Empirically determined)
39 */
40 static final int calls = 263479047;
41
42 public static void main(String[] args) throws Exception {
43 int procs = Runtime.getRuntime().availableProcessors();
44
45 try {
46 if (args.length > 0)
47 procs = Integer.parseInt(args[0]);
48 if (args.length > 1) {
49 forkArg = args[1];
50 if (forkArg.startsWith("s"))
51 forkPolicy = SERIAL;
52 else if (forkArg.startsWith("f"))
53 forkPolicy = FORK;
54 }
55 }
56 catch (Exception e) {
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 +
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)
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;
75 lastTime = now;
76 System.out.printf("Calls/sec: %12d", (long) (calls / s));
77 System.out.printf(" Time: %7.3f", s);
78 System.out.printf(" Area: %12.1f", a);
79 System.out.println();
80 }
81 System.out.println(g);
82 g.shutdown();
83 }
84
85
86 // Sequential version
87 static final class SQuad extends RecursiveAction {
88 static double computeArea(ForkJoinPool pool, double l, double r) {
89 SQuad q = new SQuad(l, r, 0);
90 pool.invoke(q);
91 return q.area;
92 }
93
94 final double left; // lower bound
95 final double right; // upper bound
96 double area;
97
98 SQuad(double l, double r, double a) {
99 this.left = l; this.right = r; this.area = a;
100 }
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
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;
113 double hh = h * 0.5;
114 double al = (fl + fc) * hh;
115 double ar = (fr + fc) * hh;
116 double alr = al + ar;
117 if (Math.abs(alr - a) <= errorTolerance)
118 return alr;
119 else
120 return recEval(c, r, fc, fr, ar) + recEval(l, c, fl, fc, al);
121 }
122
123 }
124
125 //....................................
126
127 // ForkJoin version
128 static final class FQuad extends RecursiveAction {
129 static double computeArea(ForkJoinPool pool, double l, double r) {
130 FQuad q = new FQuad(l, r, 0);
131 pool.invoke(q);
132 return q.area;
133 }
134
135 final double left; // lower bound
136 final double right; // upper bound
137 double area;
138
139 FQuad(double l, double r, double a) {
140 this.left = l; this.right = r; this.area = a;
141 }
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
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;
154 double hh = h * 0.5;
155 double al = (fl + fc) * hh;
156 double ar = (fr + fc) * hh;
157 double alr = al + ar;
158 if (Math.abs(alr - a) <= errorTolerance)
159 return alr;
160 FQuad q = new FQuad(l, c, al);
161 q.fork();
162 ar = recEval(c, r, fc, fr, ar);
163 if (!q.tryUnfork()) {
164 q.quietlyHelpJoin();
165 return ar + q.area;
166 }
167 return ar + recEval(l, c, fl, fc, al);
168 }
169
170 }
171
172 // ...........................
173
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);
178 pool.invoke(q);
179 return q.area;
180 }
181
182 final double left; // lower bound
183 final double right; // upper bound
184 double area;
185
186 DQuad(double l, double r, double a) {
187 this.left = l; this.right = r; this.area = a;
188 }
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
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;
201 double hh = h * 0.5;
202 double al = (fl + fc) * hh;
203 double ar = (fr + fc) * hh;
204 double alr = al + ar;
205 if (Math.abs(alr - a) <= errorTolerance)
206 return alr;
207 DQuad q = null;
208 if (getSurplusQueuedTaskCount() <= 3)
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();
213 return ar + q.area;
214 }
215 return ar + recEval(l, c, fl, fc, al);
216 }
217
218 }
219
220 }