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.8 by jsr166, Mon Nov 16 04:16:43 2009 UTC vs.
Revision 1.16 by jsr166, Thu Jan 15 18:34:19 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 java.util.*;
# Line 9 | Line 9 | import java.util.concurrent.*;
9  
10   public final class DynamicLeftSpineFib extends RecursiveAction {
11  
12    // 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 26 | Line 25 | public final class DynamicLeftSpineFib e
25              return;
26          }
27  
29
28          for (int reps = 0; reps < 2; ++reps) {
29 <            ForkJoinPool g = procs == 0? new ForkJoinPool() :
29 >            ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
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();
37 +            if (!g.awaitTermination(10, TimeUnit.SECONDS))
38 +                throw new Error();
39 +            Thread.sleep(1000);
40          }
41      }
42  
# Line 57 | Line 58 | public final class DynamicLeftSpineFib e
58          System.out.println();
59      }
60  
60
61      // Initialized with argument; replaced with result
62      int number;
63      DynamicLeftSpineFib next;
# Line 69 | Line 69 | public final class DynamicLeftSpineFib e
69      int getAnswer() {
70          return number;
71      }
72 +    public void compute() {
73 +        number = fib(number);
74 +    }
75  
76 <    public final void compute() {
77 <        int n = number;
78 <        if (n > 1) {
79 <            int r = 0;
80 <            DynamicLeftSpineFib rt = null;
81 <            while (getSurplusQueuedTaskCount() <= 3) {
82 <                int m = n - 2;
83 <                n -= 1;
84 <                if (m <= 1) {
85 <                    r += m;
86 <                    if (n > 1) {
87 <                        r += n - 2;
88 <                        n -= 1;
86 <                    }
87 <                    break;
76 >    static final int fib(int n) {
77 >        if (n <= 1)
78 >            return n;
79 >        int r = 0;
80 >        DynamicLeftSpineFib rt = null;
81 >        while (getSurplusQueuedTaskCount() <= 3) {
82 >            int m = n - 2;
83 >            n -= 1;
84 >            if (m <= 1) {
85 >                r += m;
86 >                if (n > 1) {
87 >                    r += n - 2;
88 >                    n -= 1;
89                  }
90 <                (rt = new DynamicLeftSpineFib(m, rt)).fork();
90 >                break;
91              }
92 <            r += seqFib(n);
93 <            while (rt != null) {
94 <                if (rt.tryUnfork()) rt.compute(); else rt.helpJoin();
92 >            (rt = new DynamicLeftSpineFib(m, rt)).fork();
93 >        }
94 >        if (n <= 1)
95 >            r += n;
96 >        else
97 >            r += seqFib(n - 2) + fib(n - 1);
98 >        if (rt != null)
99 >            r += collectRights(rt);
100 >        return r;
101 >    }
102 >
103 >    static final int collectRights(DynamicLeftSpineFib rt) {
104 >        int r = 0;
105 >        while (rt != null) {
106 >            DynamicLeftSpineFib rn = rt.next;
107 >            rt.next = null;
108 >            if (rt.tryUnfork())
109 >                r += fib(rt.number);
110 >            else {
111 >                rt.join();
112                  r += rt.number;
95                rt = rt.next;
113              }
114 <            number = r;
114 >            rt = rn;
115          }
116 +        return r;
117      }
118  
119      // Sequential version for arguments less than threshold
120 <    static int seqFib(int n) {
121 <        if (n <= 1)
122 <            return n;
123 <        else
124 <            return seqFib(n-1) + seqFib(n-2);
120 >    static final int seqFib(int n) { // unroll left only
121 >        int r = 1;
122 >        do {
123 >            int m = n - 2;
124 >            r += m <= 1 ? m : seqFib(m);
125 >        } while (--n > 1);
126 >        return r;
127      }
128  
129   }
110

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines