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.5 by jsr166, Tue Nov 3 00:02:00 2009 UTC

# Line 8 | Line 8
8   import java.util.concurrent.*;
9  
10   /**
11 < * Sample program using Guassian Quadrature for numerical integration.
11 > * Sample program using Gaussian Quadrature for numerical integration.
12   * 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 *
15   */
16   public final class Integrate {
17  
# Line 33 | Line 32 | public final class Integrate {
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       */
# Line 55 | Line 54 | public final class Integrate {
54              }
55          }
56          catch (Exception e) {
57 <            System.out.println("Usage: java Integrate3 threads <s[erial] | d[ynamic] | f[ork] - default d>");
57 >            System.out.println("Usage: java Integrate threads <s[erial] | d[ynamic] | f[ork] - default d>");
58              return;
59          }
60  
61          ForkJoinPool g = new ForkJoinPool(procs);
62 <        System.out.println("Integrating from " + start + " to " + end +
62 >        System.out.println("Integrating from " + start + " to " + end +
63                             " forkPolicy = " + forkArg);
64          long lastTime = System.nanoTime();
65          for (int i = 0; i < 10; ++i) {
66              double a;
67              if (forkPolicy == SERIAL)
68                  a = SQuad.computeArea(g, start, end);
69 <            else if (forkPolicy == FORK)
69 >            else if (forkPolicy == FORK)
70                  a = FQuad.computeArea(g, start, end);
71              else
72                  a = DQuad.computeArea(g, start, end);
# Line 95 | Line 94 | public final class Integrate {
94          final double left;       // lower bound
95          final double right;      // upper bound
96          double area;
97 <        
97 >
98          SQuad(double l, double r, double a) {
99              this.left = l; this.right = r; this.area = a;
100          }
101 <        
101 >
102          public final void compute() {
103              double l = left;
104              double r = right;
105              area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
106          }
107 <        
107 >
108          static final double recEval(double l, double r, double fl,
109                                      double fr, double a) {
110              double h = (r - l) * 0.5;
111              double c = l + h;
112 <            double fc = (c * c + 1.0) * c;
112 >            double fc = (c * c + 1.0) * c;
113              double hh = h * 0.5;
114 <            double al = (fl + fc) * hh;
114 >            double al = (fl + fc) * hh;
115              double ar = (fr + fc) * hh;
116              double alr = al + ar;
117              if (Math.abs(alr - a) <= errorTolerance)
# Line 136 | Line 135 | public final class Integrate {
135          final double left;       // lower bound
136          final double right;      // upper bound
137          double area;
138 <        
138 >
139          FQuad(double l, double r, double a) {
140              this.left = l; this.right = r; this.area = a;
141          }
142 <        
142 >
143          public final void compute() {
144              double l = left;
145              double r = right;
146              area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
147          }
148 <        
148 >
149          static final double recEval(double l, double r, double fl,
150                                      double fr, double a) {
151              double h = (r - l) * 0.5;
152              double c = l + h;
153 <            double fc = (c * c + 1.0) * c;
153 >            double fc = (c * c + 1.0) * c;
154              double hh = h * 0.5;
155 <            double al = (fl + fc) * hh;
155 >            double al = (fl + fc) * hh;
156              double ar = (fr + fc) * hh;
157              double alr = al + ar;
158              if (Math.abs(alr - a) <= errorTolerance)
# Line 172 | Line 171 | public final class Integrate {
171  
172      // ...........................
173  
174 <    // Version using on-demand Fork
174 >    // Version using on-demand Fork
175      static final class DQuad extends RecursiveAction {
176          static double computeArea(ForkJoinPool pool, double l, double r) {
177              DQuad q = new DQuad(l, r, 0);
# Line 183 | Line 182 | public final class Integrate {
182          final double left;       // lower bound
183          final double right;      // upper bound
184          double area;
185 <        
185 >
186          DQuad(double l, double r, double a) {
187              this.left = l; this.right = r; this.area = a;
188          }
189 <        
189 >
190          public final void compute() {
191              double l = left;
192              double r = right;
193              area = recEval(l, r, (l * l + 1.0) * l, (r * r + 1.0) * r, area);
194          }
195 <        
195 >
196          static final double recEval(double l, double r, double fl,
197                                      double fr, double a) {
198              double h = (r - l) * 0.5;
199              double c = l + h;
200 <            double fc = (c * c + 1.0) * c;
200 >            double fc = (c * c + 1.0) * c;
201              double hh = h * 0.5;
202 <            double al = (fl + fc) * hh;
202 >            double al = (fl + fc) * hh;
203              double ar = (fr + fc) * hh;
204              double alr = al + ar;
205              if (Math.abs(alr - a) <= errorTolerance)
# Line 219 | Line 218 | public final class Integrate {
218      }
219  
220   }
222
223  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines