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.14 by jsr166, Wed Jul 4 20:07:01 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.
14 < *
14 > *
15   */
16   public final class Integrate {
17  
# Line 27 | Line 26 | public final class Integrate {
26      static String forkArg = "dynamic";
27  
28      // the function to integrate
29 <    static double computeFunction(double x)  {
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
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();
43 >        int procs = 0;
44  
45          try {
46              if (args.length > 0)
# Line 58 | Line 57 | public final class Integrate {
57              System.out.println("Usage: java Integrate3 threads <s[erial] | d[ynamic] | f[ork] - default d>");
58              return;
59          }
60 +        oneTest(procs);
61 +        oneTest(procs);
62 +        oneTest(procs);
63 +    }
64  
65 <        ForkJoinPool g = new ForkJoinPool(procs);
66 <        System.out.println("Integrating from " + start + " to " + end +
65 >    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                             " forkPolicy = " + forkArg);
71          long lastTime = System.nanoTime();
72 <        for (int i = 0; i < 10; ++i) {
72 >        for (int i = 0; i < 20; ++i) {
73              double a;
74              if (forkPolicy == SERIAL)
75                  a = SQuad.computeArea(g, start, end);
76 <            else if (forkPolicy == FORK)
76 >            else if (forkPolicy == FORK)
77                  a = FQuad.computeArea(g, start, end);
78              else
79                  a = DQuad.computeArea(g, start, end);
# Line 76 | Line 82 | public final class Integrate {
82              lastTime = now;
83              System.out.printf("Calls/sec: %12d", (long) (calls / s));
84              System.out.printf(" Time: %7.3f", s);
85 <            System.out.printf(" Area: %12.1f", a);
85 >            System.out.printf(" Threads: %5d", g.getPoolSize());
86 >            //            System.out.printf(" Area: %12.1f", a);
87              System.out.println();
88          }
89          System.out.println(g);
# Line 95 | Line 102 | public final class Integrate {
102          final double left;       // lower bound
103          final double right;      // upper bound
104          double area;
105 <        
105 >
106          SQuad(double l, double r, double a) {
107              this.left = l; this.right = r; this.area = a;
108          }
109 <        
109 >
110          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 <        
115 >
116          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 <            double fc = (c * c + 1.0) * c;
120 >            double fc = (c * c + 1.0) * c;
121              double hh = h * 0.5;
122 <            double al = (fl + fc) * hh;
122 >            double al = (fl + fc) * hh;
123              double ar = (fr + fc) * hh;
124              double alr = al + ar;
125              if (Math.abs(alr - a) <= errorTolerance)
# Line 136 | Line 143 | public final class Integrate {
143          final double left;       // lower bound
144          final double right;      // upper bound
145          double area;
146 <        
146 >
147          FQuad(double l, double r, double a) {
148              this.left = l; this.right = r; this.area = a;
149          }
150 <        
150 >
151          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 <        
156 >
157          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 <            double fc = (c * c + 1.0) * c;
161 >            double fc = (c * c + 1.0) * c;
162              double hh = h * 0.5;
163 <            double al = (fl + fc) * hh;
163 >            double al = (fl + fc) * hh;
164              double ar = (fr + fc) * hh;
165              double alr = al + ar;
166              if (Math.abs(alr - a) <= errorTolerance)
# Line 162 | Line 169 | public final class Integrate {
169              q.fork();
170              ar = recEval(c, r, fc, fr, ar);
171              if (!q.tryUnfork()) {
172 <                q.quietlyHelpJoin();
172 >                q.quietlyJoin();
173                  return ar + q.area;
174              }
175              return ar + recEval(l, c, fl, fc, al);
# Line 172 | Line 179 | public final class Integrate {
179  
180      // ...........................
181  
182 <    // Version using on-demand Fork
182 >    // Version using on-demand Fork
183      static final class DQuad extends RecursiveAction {
184          static double computeArea(ForkJoinPool pool, double l, double r) {
185              DQuad q = new DQuad(l, r, 0);
# Line 183 | Line 190 | public final class Integrate {
190          final double left;       // lower bound
191          final double right;      // upper bound
192          double area;
193 <        
193 >
194          DQuad(double l, double r, double a) {
195              this.left = l; this.right = r; this.area = a;
196          }
197 <        
197 >
198          public final void compute() {
199              double l = left;
200              double r = right;
201              area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
202          }
203 <        
203 >
204          static final double recEval(double l, double r, double fl,
205                                      double fr, double a) {
206              double h = (r - l) * 0.5;
207              double c = l + h;
208 <            double fc = (c * c + 1.0) * c;
208 >            double fc = (c * c + 1.0) * c;
209              double hh = h * 0.5;
210 <            double al = (fl + fc) * hh;
210 >            double al = (fl + fc) * hh;
211              double ar = (fr + fc) * hh;
212              double alr = al + ar;
213              if (Math.abs(alr - a) <= errorTolerance)
# Line 210 | Line 217 | public final class Integrate {
217                  (q = new DQuad(l, c, al)).fork();
218              ar = recEval(c, r, fc, fr, ar);
219              if (q != null && !q.tryUnfork()) {
220 <                q.quietlyHelpJoin();
220 >                q.quietlyJoin();
221                  return ar + q.area;
222              }
223              return ar + recEval(l, c, fl, fc, al);
# Line 219 | Line 226 | public final class Integrate {
226      }
227  
228   }
222
223  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines