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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines