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

Comparing jsr166/src/test/loops/FJSums.java (file contents):
Revision 1.1 by dl, Sun Sep 19 12:55:37 2010 UTC vs.
Revision 1.8 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 java.util.*;
# Line 14 | Line 14 | public class FJSums {
14      static final long NPS = (1000L * 1000 * 1000);
15      static int THRESHOLD;
16  
17 <    public static void main (String[] args) throws Exception {
17 >    public static void main(String[] args) throws Exception {
18          int procs = 0;
19          int n = 1 << 25;
20          int reps = 10;
# Line 24 | Line 24 | public class FJSums {
24              if (args.length > 1)
25                  n = Integer.parseInt(args[1]);
26              if (args.length > 2)
27 <                reps = Integer.parseInt(args[1]);
27 >                reps = Integer.parseInt(args[2]);
28          }
29          catch (Exception e) {
30              System.out.println("Usage: java FJSums threads n reps");
31              return;
32          }
33 <        ForkJoinPool g = procs == 0? new ForkJoinPool() :
33 >        ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
34              new ForkJoinPool(procs);
35          System.out.println("Number of procs=" + g.getParallelism());
36          // for now hardwire Cumulate threshold to 8 * #CPUs leaf tasks
37          THRESHOLD = 1 + ((n + 7) >>> 3) / g.getParallelism();
38 <        
38 >
39          long[] a = new long[n];
40          for (int i = 0; i < n; ++i)
41              a[i] = i;
# Line 46 | Line 46 | public class FJSums {
46              long ss = seqSum(a, 0, n);
47              double elapsed = elapsedTime(last);
48              System.out.printf("sum = %24d  time:  %7.3f\n", ss, elapsed);
49 <            if (ss != expected)
49 >            if (ss != expected)
50                  throw new Error("expected " + expected + " != " + ss);
51          }
52          for (int i = 0; i < reps; ++i) {
# Line 57 | Line 57 | public class FJSums {
57              long ss = s.result;
58              double elapsed = elapsedTime(last);
59              System.out.printf("sum = %24d  time:  %7.3f\n", ss, elapsed);
60 <            if (i == 0 && ss != expected)
60 >            if (i == 0 && ss != expected)
61                  throw new Error("expected " + expected + " != " + ss);
62              System.out.print("Cum: ");
63              last = System.nanoTime();
# Line 78 | Line 78 | public class FJSums {
78  
79      static long seqSum(long[] array, int l, int h) {
80          long sum = 0;
81 <        for (int i = l; i < h; ++i)
81 >        for (int i = l; i < h; ++i)
82              sum += array[i];
83          return sum;
84      }
# Line 89 | Line 89 | public class FJSums {
89              array[i] = sum += array[i];
90          return sum;
91      }
92 <    
92 >
93      /**
94       * Adapted from Applyer demo in RecursiveAction docs
95       */
# Line 102 | Line 102 | public class FJSums {
102              this.array = array; this.lo = lo; this.hi = hi;
103              this.next = next;
104          }
105 <        
105 >
106          protected void compute() {
107              int l = lo;
108              int h = hi;
# Line 157 | Line 157 | public class FJSums {
157       * internal nodes, it becomes true when one child is cumulated.
158       * When second child finishes cumulating, it then moves up tree,
159       * executing complete() at the root.
160     *
160       */
161      static final class Cumulater extends ForkJoinTask<Void> {
162          static final short CUMULATE = (short)1;
# Line 255 | Line 254 | public class FJSums {
254                          par.out = par.left.out + par.right.out;
255                          int refork =
256                              ((pb & CUMULATE) == 0 &&
257 <                             par.lo == 0)? CUMULATE : 0;
257 >                             par.lo == 0) ? CUMULATE : 0;
258                          int nextPhase = pb|cb|refork;
259                          if (pb == nextPhase ||
260                              phaseUpdater.compareAndSet(par, pb, nextPhase)) {
# Line 276 | Line 275 | public class FJSums {
275      }
276  
277   }
279
280

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines