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

Comparing jsr166/src/test/loops/DynamicLeftSpineFib.java (file contents):
Revision 1.6 by jsr166, Tue Nov 3 01:04:02 2009 UTC vs.
Revision 1.18 by jsr166, Sat Sep 12 19:09:00 2015 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.*;
8   import java.util.concurrent.*;
9  
10   public final class DynamicLeftSpineFib extends RecursiveAction {
11  
13    // Performance-tuning constant:
12      static long lastStealCount;
13  
14      public static void main(String[] args) throws Exception {
15          int procs = 0;
16 <        int num = 43;
16 >        int num = 45;
17          try {
18              if (args.length > 0)
19                  procs = Integer.parseInt(args[0]);
# Line 23 | Line 21 | public final class DynamicLeftSpineFib e
21                  num = Integer.parseInt(args[1]);
22          }
23          catch (Exception e) {
24 <            System.out.println("Usage: java DynamicLeftSpineFib <threads> <number> [<sequentialThreshold>]");
24 >            System.out.println("Usage: java DynamicLeftSpineFib <threads> <number> [<sequntialThreshold>]");
25              return;
26          }
27  
30
28          for (int reps = 0; reps < 2; ++reps) {
29 <            ForkJoinPool g = (procs == 0) ?
33 <                new ForkJoinPool() :
29 >            ForkJoinPool g = (procs == 0) ? ForkJoinPool.commonPool() :
30                  new ForkJoinPool(procs);
31              lastStealCount = g.getStealCount();
32 <            for (int i = 0; i < 10; ++i) {
32 >            for (int i = 0; i < 20; ++i) {
33                  test(g, num);
34              }
35              System.out.println(g);
36 <            g.shutdown();
36 >            if (g != ForkJoinPool.commonPool()) {
37 >                g.shutdown();
38 >                if (!g.awaitTermination(10, TimeUnit.SECONDS))
39 >                    throw new Error();
40 >            }
41 >            Thread.sleep(1000);
42          }
43      }
44  
# Line 47 | Line 48 | public final class DynamicLeftSpineFib e
48          DynamicLeftSpineFib f = new DynamicLeftSpineFib(num, null);
49          g.invoke(f);
50          long time = System.currentTimeMillis() - start;
51 <        double secs = (double) time / 1000.0;
51 >        double secs = ((double)time) / 1000.0;
52          long result = f.getAnswer();
53 <        System.out.print("DynamicLeftSpineFib " + num + " = " + result);
53 >        System.out.print("DLSFib " + num + " = " + result);
54          System.out.printf("\tTime: %7.3f", secs);
55          long sc = g.getStealCount();
56          long ns = sc - lastStealCount;
# Line 59 | Line 60 | public final class DynamicLeftSpineFib e
60          System.out.println();
61      }
62  
62
63      // Initialized with argument; replaced with result
64      int number;
65      DynamicLeftSpineFib next;
# Line 72 | Line 72 | public final class DynamicLeftSpineFib e
72          return number;
73      }
74  
75 <    public final void compute() {
76 <        int n = number;
77 <        if (n > 1) {
78 <            DynamicLeftSpineFib rt = null;
79 <            while (n > 1 && getSurplusQueuedTaskCount() <= 3) {
80 <                (rt = new DynamicLeftSpineFib(n - 2, rt)).fork();
81 <                n -= 1;
82 <            }
83 <            int r = seqFib(n);
84 <            while (rt != null) {
85 <                if (rt.tryUnfork()) rt.compute(); else rt.helpJoin();
75 >    public void compute() {
76 >        number = fib(number);
77 >    }
78 >
79 >    static final int fib(int n) {
80 >        if (n <= 1)
81 >            return n;
82 >        int r = 0;
83 >        DynamicLeftSpineFib rt = null;
84 >        while (getSurplusQueuedTaskCount() <= 3) {
85 >            int m = n - 2;
86 >            if (m <= 1)
87 >                r += m;
88 >            else
89 >                (rt = new DynamicLeftSpineFib(m, rt)).fork();
90 >            if (--n <= 1)
91 >                break;
92 >        }
93 >        r += n <= 1 ? n : seqFib(n);
94 >        if (rt != null)
95 >            r += collectRights(rt);
96 >        return r;
97 >    }
98 >
99 >    static final int collectRights(DynamicLeftSpineFib rt) {
100 >        int r = 0;
101 >        while (rt != null) {
102 >            DynamicLeftSpineFib rn = rt.next;
103 >            rt.next = null;
104 >            if (rt.tryUnfork())
105 >                r += fib(rt.number);
106 >            else {
107 >                rt.join();
108                  r += rt.number;
87                rt = rt.next;
109              }
110 <            number = r;
110 >            rt = rn;
111          }
112 +        return r;
113      }
114  
115 <    // Sequential version for arguments less than threshold
116 <    static int seqFib(int n) {
117 <        if (n <= 1)
118 <            return n;
119 <        else
120 <            return seqFib(n-1) + seqFib(n-2);
115 >    /** Sequential version for arguments less than threshold */
116 >    static final int seqFib(int n) { // unroll left only
117 >        int r = 1;
118 >        do {
119 >            int m = n - 2;
120 >            r += m <= 1 ? m : seqFib(m);
121 >        } while (--n > 1);
122 >        return r;
123      }
124  
125   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines