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.9 by dl, Wed Jul 7 20:32:04 2010 UTC vs.
Revision 1.10 by dl, Sun Sep 19 12:55:36 2010 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
5 */
6
7 import java.util.*;
1   import java.util.concurrent.*;
2 + import java.util.*;
3 + import java.util.concurrent.TimeUnit;
4 + //import java.util.concurrent.*;
5  
6   public final class DynamicLeftSpineFib extends RecursiveAction {
7  
12    // Performance-tuning constant:
8      static long lastStealCount;
9 <
9 >  
10      public static void main(String[] args) throws Exception {
11          int procs = 0;
12 <        int num = 43;
12 >        int num = 45;
13          try {
14              if (args.length > 0)
15                  procs = Integer.parseInt(args[0]);
# Line 26 | Line 21 | public final class DynamicLeftSpineFib e
21              return;
22          }
23  
24 <
24 >        
25          for (int reps = 0; reps < 2; ++reps) {
26              ForkJoinPool g = procs == 0? new ForkJoinPool() :
27                  new ForkJoinPool(procs);
28              lastStealCount = g.getStealCount();
29 <            for (int i = 0; i < 10; ++i) {
29 >            for (int i = 0; i < 20; ++i) {
30                  test(g, num);
31              }
32              System.out.println(g);
33              g.shutdown();
34 +            if (!g.awaitTermination(10, TimeUnit.SECONDS))
35 +                throw new Error();
36 +            Thread.sleep(1000);
37          }
38      }
39  
# Line 62 | Line 60 | public final class DynamicLeftSpineFib e
60      int number;
61      DynamicLeftSpineFib next;
62  
63 <    DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) {
64 <        number = n; next = nxt;
63 >    DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) {
64 >        number = n; next = nxt;
65      }
66  
67      int getAnswer() {
68          return number;
69      }
70 +    public void compute() {
71 +        number = fib(number);
72 +    }
73  
74 <    public final void compute() {
75 <        int n = number;
76 <        if (n > 1) {
77 <            int r = 0;
78 <            DynamicLeftSpineFib rt = null;
79 <            while (getSurplusQueuedTaskCount() <= 3) {
80 <                int m = n - 2;
81 <                n -= 1;
82 <                if (m <= 1) {
83 <                    r += m;
84 <                    if (n > 1) {
85 <                        r += n - 2;
86 <                        n -= 1;
86 <                    }
87 <                    break;
74 >    static final int fib(int n) {
75 >        if (n <= 1)
76 >            return n;
77 >        int r = 0;
78 >        DynamicLeftSpineFib rt = null;
79 >        while (getSurplusQueuedTaskCount() <= 3) {
80 >            int m = n - 2;
81 >            n -= 1;
82 >            if (m <= 1) {
83 >                r += m;
84 >                if (n > 1) {
85 >                    r += n - 2;
86 >                    n -= 1;
87                  }
88 <                (rt = new DynamicLeftSpineFib(m, rt)).fork();
88 >                break;
89              }
90 <            r += seqFib(n);
91 <            while (rt != null) {
92 <                if (rt.tryUnfork()) rt.compute(); else rt.join();
90 >            (rt = new DynamicLeftSpineFib(m, rt)).fork();
91 >        }
92 >        if (n <= 1)
93 >            r += n;
94 >        else
95 >            r += seqFib(n - 2) + fib(n - 1);
96 >        if (rt != null)
97 >            r += collectRights(rt);
98 >        return r;
99 >    }
100 >    
101 >    static final int collectRights(DynamicLeftSpineFib rt) {
102 >        int r = 0;
103 >        while (rt != null) {
104 >            DynamicLeftSpineFib rn = rt.next;
105 >            rt.next = null;
106 >            if (rt.tryUnfork())
107 >                r += fib(rt.number);
108 >            else {
109 >                rt.join();
110                  r += rt.number;
95                rt = rt.next;
111              }
112 <            number = r;
112 >            rt = rn;
113          }
114 +        return r;
115      }
116  
117      // Sequential version for arguments less than threshold
118 <    static int seqFib(int n) {
119 <        if (n <= 1)
120 <            return n;
121 <        else
122 <            return seqFib(n-1) + seqFib(n-2);
118 >    static final int seqFib(int n) { // unroll left only
119 >        int r = 1;
120 >        do {
121 >            int m = n - 2;
122 >            r += m <= 1 ? m : seqFib(m);
123 >        } while (--n > 1);
124 >        return r;
125      }
126  
127   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines