ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Integrate.java
(Generate patch)

Comparing jsr166/src/test/loops/Integrate.java (file contents):
Revision 1.1 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.15 by jsr166, Sun Oct 21 06:14:12 2012 UTC

# Line 1 | Line 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
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   */
6  
7 //import jsr166y.*;
7   import java.util.concurrent.*;
8  
9   /**
# Line 12 | Line 11 | import java.util.concurrent.*;
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.
15 *
14   */
15   public final class Integrate {
16  
# Line 27 | Line 25 | public final class Integrate {
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  
32      static final double start = 0.0;
33      static final double end = 1536.0;
34 <    /*
35 <     * The number of recursive calls for
34 >    /*
35 >     * The number of recursive calls for
36       * integrate from start to end.
37       * (Empirically determined)
38       */
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)
# Line 58 | Line 56 | public final class Integrate {
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);
65 <        System.out.println("Integrating from " + start + " to " + end +
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);
75 <            else if (forkPolicy == FORK)
75 >            else if (forkPolicy == FORK)
76                  a = FQuad.computeArea(g, start, end);
77              else
78                  a = DQuad.computeArea(g, start, end);
# Line 76 | Line 81 | public final class Integrate {
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);
# Line 95 | Line 101 | public final class Integrate {
101          final double left;       // lower bound
102          final double right;      // upper bound
103          double area;
104 <        
104 >
105          SQuad(double l, double r, double a) {
106              this.left = l; this.right = r; this.area = a;
107          }
108 <        
108 >
109          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 <        
114 >
115          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 <            double fc = (c * c + 1.0) * c;
119 >            double fc = (c * c + 1.0) * c;
120              double hh = h * 0.5;
121 <            double al = (fl + fc) * hh;
121 >            double al = (fl + fc) * hh;
122              double ar = (fr + fc) * hh;
123              double alr = al + ar;
124              if (Math.abs(alr - a) <= errorTolerance)
# Line 136 | Line 142 | public final class Integrate {
142          final double left;       // lower bound
143          final double right;      // upper bound
144          double area;
145 <        
145 >
146          FQuad(double l, double r, double a) {
147              this.left = l; this.right = r; this.area = a;
148          }
149 <        
149 >
150          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 <        
155 >
156          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 <            double fc = (c * c + 1.0) * c;
160 >            double fc = (c * c + 1.0) * c;
161              double hh = h * 0.5;
162 <            double al = (fl + fc) * hh;
162 >            double al = (fl + fc) * hh;
163              double ar = (fr + fc) * hh;
164              double alr = al + ar;
165              if (Math.abs(alr - a) <= errorTolerance)
# Line 162 | Line 168 | public final class Integrate {
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);
# Line 172 | Line 178 | public final class Integrate {
178  
179      // ...........................
180  
181 <    // Version using on-demand Fork
181 >    // Version using on-demand Fork
182      static final class DQuad extends RecursiveAction {
183          static double computeArea(ForkJoinPool pool, double l, double r) {
184              DQuad q = new DQuad(l, r, 0);
# Line 183 | Line 189 | public final class Integrate {
189          final double left;       // lower bound
190          final double right;      // upper bound
191          double area;
192 <        
192 >
193          DQuad(double l, double r, double a) {
194              this.left = l; this.right = r; this.area = a;
195          }
196 <        
196 >
197          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 <        
202 >
203          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 <            double fc = (c * c + 1.0) * c;
207 >            double fc = (c * c + 1.0) * c;
208              double hh = h * 0.5;
209 <            double al = (fl + fc) * hh;
209 >            double al = (fl + fc) * hh;
210              double ar = (fr + fc) * hh;
211              double alr = al + ar;
212              if (Math.abs(alr - a) <= errorTolerance)
# Line 210 | Line 216 | public final class Integrate {
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);
# Line 219 | Line 225 | public final class Integrate {
225      }
226  
227   }
222
223  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines