ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Integrate.java
Revision: 1.8
Committed: Sun Sep 19 12:55:37 2010 UTC (13 years, 8 months ago) by dl
Branch: MAIN
Changes since 1.7: +40 -28 lines
Log Message:
Add and update FJ and Queue tests

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 java.util.concurrent.*;
8    
9     /**
10 dl 1.8 * Sample program using Guassian 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 dl 1.8 *
15 dl 1.1 */
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 dl 1.8 /*
36     * 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     ForkJoinPool g = procs == 0? new ForkJoinPool() :
67     new ForkJoinPool(procs);
68     System.out.println("Number of procs=" + g.getParallelism());
69     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 dl 1.8 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    
94     // Sequential version
95     static final class SQuad extends RecursiveAction {
96     static double computeArea(ForkJoinPool pool, double l, double r) {
97     SQuad q = new SQuad(l, r, 0);
98     pool.invoke(q);
99     return q.area;
100     }
101    
102     final double left; // lower bound
103     final double right; // upper bound
104     double area;
105 dl 1.8
106 dl 1.1 SQuad(double l, double r, double a) {
107     this.left = l; this.right = r; this.area = a;
108     }
109 dl 1.8
110 dl 1.1 public final void compute() {
111     double l = left;
112     double r = right;
113     area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
114     }
115 dl 1.8
116 dl 1.1 static final double recEval(double l, double r, double fl,
117     double fr, double a) {
118     double h = (r - l) * 0.5;
119     double c = l + h;
120 dl 1.8 double fc = (c * c + 1.0) * c;
121 dl 1.1 double hh = h * 0.5;
122 dl 1.8 double al = (fl + fc) * hh;
123 dl 1.1 double ar = (fr + fc) * hh;
124     double alr = al + ar;
125     if (Math.abs(alr - a) <= errorTolerance)
126     return alr;
127     else
128     return recEval(c, r, fc, fr, ar) + recEval(l, c, fl, fc, al);
129     }
130    
131     }
132    
133     //....................................
134    
135     // ForkJoin version
136     static final class FQuad extends RecursiveAction {
137     static double computeArea(ForkJoinPool pool, double l, double r) {
138     FQuad q = new FQuad(l, r, 0);
139     pool.invoke(q);
140     return q.area;
141     }
142    
143     final double left; // lower bound
144     final double right; // upper bound
145     double area;
146 dl 1.8
147 dl 1.1 FQuad(double l, double r, double a) {
148     this.left = l; this.right = r; this.area = a;
149     }
150 dl 1.8
151 dl 1.1 public final void compute() {
152     double l = left;
153     double r = right;
154     area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
155     }
156 dl 1.8
157 dl 1.1 static final double recEval(double l, double r, double fl,
158     double fr, double a) {
159     double h = (r - l) * 0.5;
160     double c = l + h;
161 dl 1.8 double fc = (c * c + 1.0) * c;
162 dl 1.1 double hh = h * 0.5;
163 dl 1.8 double al = (fl + fc) * hh;
164 dl 1.1 double ar = (fr + fc) * hh;
165     double alr = al + ar;
166     if (Math.abs(alr - a) <= errorTolerance)
167     return alr;
168     FQuad q = new FQuad(l, c, al);
169     q.fork();
170     ar = recEval(c, r, fc, fr, ar);
171     if (!q.tryUnfork()) {
172 dl 1.8 // q.quietlyHelpJoin();
173 dl 1.7 q.quietlyJoin();
174 dl 1.1 return ar + q.area;
175     }
176     return ar + recEval(l, c, fl, fc, al);
177     }
178    
179     }
180    
181     // ...........................
182    
183 dl 1.8 // Version using on-demand Fork
184 dl 1.1 static final class DQuad extends RecursiveAction {
185     static double computeArea(ForkJoinPool pool, double l, double r) {
186     DQuad q = new DQuad(l, r, 0);
187     pool.invoke(q);
188     return q.area;
189     }
190    
191     final double left; // lower bound
192     final double right; // upper bound
193     double area;
194 dl 1.8
195 dl 1.1 DQuad(double l, double r, double a) {
196     this.left = l; this.right = r; this.area = a;
197     }
198 dl 1.8
199 dl 1.1 public final void compute() {
200     double l = left;
201     double r = right;
202     area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
203     }
204 dl 1.8
205 dl 1.1 static final double recEval(double l, double r, double fl,
206     double fr, double a) {
207     double h = (r - l) * 0.5;
208     double c = l + h;
209 dl 1.8 double fc = (c * c + 1.0) * c;
210 dl 1.1 double hh = h * 0.5;
211 dl 1.8 double al = (fl + fc) * hh;
212 dl 1.1 double ar = (fr + fc) * hh;
213     double alr = al + ar;
214     if (Math.abs(alr - a) <= errorTolerance)
215     return alr;
216     DQuad q = null;
217     if (getSurplusQueuedTaskCount() <= 3)
218     (q = new DQuad(l, c, al)).fork();
219     ar = recEval(c, r, fc, fr, ar);
220     if (q != null && !q.tryUnfork()) {
221 dl 1.8 // q.quietlyHelpJoin();
222 dl 1.7 q.quietlyJoin();
223 dl 1.1 return ar + q.area;
224     }
225     return ar + recEval(l, c, fl, fc, al);
226     }
227    
228     }
229    
230     }
231 dl 1.8
232