ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Integrate.java
Revision: 1.4
Committed: Thu Oct 29 23:16:47 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.3: +1 -1 lines
Log Message:
fix usage message

File Contents

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