ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Integrate.java
Revision: 1.18
Committed: Thu Jan 15 18:42:39 2015 UTC (9 years, 3 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.17: +1 -1 lines
Log Message:
typos

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 jsr166 1.13 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.concurrent.*;
8    
9     /**
10 jsr166 1.18 * Sample program using Gaussian Quadrature for numerical integration.
11 dl 1.1 * 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     */
15     public final class Integrate {
16    
17     static final double errorTolerance = 1.0e-12;
18     /** for time conversion */
19     static final long NPS = (1000L * 1000 * 1000);
20    
21     static final int SERIAL = -1;
22     static final int DYNAMIC = 0;
23     static final int FORK = 1;
24     static int forkPolicy = DYNAMIC;
25     static String forkArg = "dynamic";
26    
27 jsr166 1.16 /** the function to integrate */
28 jsr166 1.10 static double computeFunction(double x) {
29 dl 1.1 return (x * x + 1.0) * x;
30     }
31    
32     static final double start = 0.0;
33     static final double end = 1536.0;
34 jsr166 1.16
35     /**
36 jsr166 1.9 * The number of recursive calls for
37 dl 1.1 * 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 dl 1.8 int procs = 0;
44 dl 1.1
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 dl 1.8 System.out.println("Usage: java Integrate3 threads <s[erial] | d[ynamic] | f[ork] - default d>");
58 dl 1.1 return;
59     }
60 dl 1.8 oneTest(procs);
61     oneTest(procs);
62     oneTest(procs);
63     }
64 dl 1.1
65 dl 1.8 static void oneTest(int procs) {
66 jsr166 1.12 ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
67 dl 1.8 new ForkJoinPool(procs);
68     System.out.println("Number of procs=" + g.getParallelism());
69 jsr166 1.9 System.out.println("Integrating from " + start + " to " + end +
70 dl 1.1 " forkPolicy = " + forkArg);
71     long lastTime = System.nanoTime();
72 dl 1.8 for (int i = 0; i < 20; ++i) {
73 dl 1.1 double a;
74     if (forkPolicy == SERIAL)
75     a = SQuad.computeArea(g, start, end);
76 jsr166 1.9 else if (forkPolicy == FORK)
77 dl 1.1 a = FQuad.computeArea(g, start, end);
78     else
79     a = DQuad.computeArea(g, start, end);
80     long now = System.nanoTime();
81 dl 1.8 double s = ((double)(now - lastTime))/NPS;
82 dl 1.1 lastTime = now;
83     System.out.printf("Calls/sec: %12d", (long) (calls / s));
84     System.out.printf(" Time: %7.3f", s);
85 dl 1.8 System.out.printf(" Threads: %5d", g.getPoolSize());
86     // System.out.printf(" Area: %12.1f", a);
87 dl 1.1 System.out.println();
88     }
89     System.out.println(g);
90     g.shutdown();
91     }
92    
93     // Sequential version
94     static final class SQuad extends RecursiveAction {
95     static double computeArea(ForkJoinPool pool, double l, double r) {
96     SQuad q = new SQuad(l, r, 0);
97     pool.invoke(q);
98     return q.area;
99     }
100    
101     final double left; // lower bound
102     final double right; // upper bound
103     double area;
104 jsr166 1.9
105 dl 1.1 SQuad(double l, double r, double a) {
106     this.left = l; this.right = r; this.area = a;
107     }
108 jsr166 1.9
109 dl 1.1 public final void compute() {
110     double l = left;
111     double r = right;
112     area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
113     }
114 jsr166 1.9
115 dl 1.1 static final double recEval(double l, double r, double fl,
116     double fr, double a) {
117     double h = (r - l) * 0.5;
118     double c = l + h;
119 jsr166 1.9 double fc = (c * c + 1.0) * c;
120 dl 1.1 double hh = h * 0.5;
121 jsr166 1.9 double al = (fl + fc) * hh;
122 dl 1.1 double ar = (fr + fc) * hh;
123     double alr = al + ar;
124     if (Math.abs(alr - a) <= errorTolerance)
125     return alr;
126     else
127     return recEval(c, r, fc, fr, ar) + recEval(l, c, fl, fc, al);
128     }
129    
130     }
131    
132     //....................................
133    
134     // ForkJoin version
135     static final class FQuad extends RecursiveAction {
136     static double computeArea(ForkJoinPool pool, double l, double r) {
137     FQuad q = new FQuad(l, r, 0);
138     pool.invoke(q);
139     return q.area;
140     }
141    
142     final double left; // lower bound
143     final double right; // upper bound
144     double area;
145 jsr166 1.9
146 dl 1.1 FQuad(double l, double r, double a) {
147     this.left = l; this.right = r; this.area = a;
148     }
149 jsr166 1.9
150 dl 1.1 public final void compute() {
151     double l = left;
152     double r = right;
153     area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
154     }
155 jsr166 1.9
156 dl 1.1 static final double recEval(double l, double r, double fl,
157     double fr, double a) {
158     double h = (r - l) * 0.5;
159     double c = l + h;
160 jsr166 1.9 double fc = (c * c + 1.0) * c;
161 dl 1.1 double hh = h * 0.5;
162 jsr166 1.9 double al = (fl + fc) * hh;
163 dl 1.1 double ar = (fr + fc) * hh;
164     double alr = al + ar;
165     if (Math.abs(alr - a) <= errorTolerance)
166     return alr;
167     FQuad q = new FQuad(l, c, al);
168     q.fork();
169     ar = recEval(c, r, fc, fr, ar);
170     if (!q.tryUnfork()) {
171 dl 1.7 q.quietlyJoin();
172 dl 1.1 return ar + q.area;
173     }
174     return ar + recEval(l, c, fl, fc, al);
175     }
176    
177     }
178    
179     // ...........................
180    
181 jsr166 1.9 // Version using on-demand Fork
182 dl 1.1 static final class DQuad extends RecursiveAction {
183     static double computeArea(ForkJoinPool pool, double l, double r) {
184     DQuad q = new DQuad(l, r, 0);
185     pool.invoke(q);
186     return q.area;
187     }
188    
189     final double left; // lower bound
190     final double right; // upper bound
191     double area;
192 jsr166 1.9
193 dl 1.1 DQuad(double l, double r, double a) {
194     this.left = l; this.right = r; this.area = a;
195     }
196 jsr166 1.9
197 dl 1.1 public final void compute() {
198     double l = left;
199     double r = right;
200     area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
201     }
202 jsr166 1.9
203 dl 1.1 static final double recEval(double l, double r, double fl,
204     double fr, double a) {
205     double h = (r - l) * 0.5;
206     double c = l + h;
207 jsr166 1.9 double fc = (c * c + 1.0) * c;
208 dl 1.1 double hh = h * 0.5;
209 jsr166 1.9 double al = (fl + fc) * hh;
210 dl 1.1 double ar = (fr + fc) * hh;
211     double alr = al + ar;
212     if (Math.abs(alr - a) <= errorTolerance)
213     return alr;
214     DQuad q = null;
215     if (getSurplusQueuedTaskCount() <= 3)
216     (q = new DQuad(l, c, al)).fork();
217     ar = recEval(c, r, fc, fr, ar);
218     if (q != null && !q.tryUnfork()) {
219 dl 1.7 q.quietlyJoin();
220 dl 1.1 return ar + q.area;
221     }
222     return ar + recEval(l, c, fl, fc, al);
223     }
224    
225     }
226    
227     }