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.15 by jsr166, Wed Dec 31 17:00:58 2014 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  
28  
29          for (int reps = 0; reps < 2; ++reps) {
30 <            ForkJoinPool g = (procs == 0) ?
33 <                new ForkJoinPool() :
30 >            ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
31                  new ForkJoinPool(procs);
32              lastStealCount = g.getStealCount();
33 <            for (int i = 0; i < 10; ++i) {
33 >            for (int i = 0; i < 20; ++i) {
34                  test(g, num);
35              }
36              System.out.println(g);
37              g.shutdown();
38 +            if (!g.awaitTermination(10, TimeUnit.SECONDS))
39 +                throw new Error();
40 +            Thread.sleep(1000);
41          }
42      }
43  
# Line 47 | Line 47 | public final class DynamicLeftSpineFib e
47          DynamicLeftSpineFib f = new DynamicLeftSpineFib(num, null);
48          g.invoke(f);
49          long time = System.currentTimeMillis() - start;
50 <        double secs = (double) time / 1000.0;
50 >        double secs = ((double)time) / 1000.0;
51          long result = f.getAnswer();
52 <        System.out.print("DynamicLeftSpineFib " + num + " = " + result);
52 >        System.out.print("DLSFib " + num + " = " + result);
53          System.out.printf("\tTime: %7.3f", secs);
54          long sc = g.getStealCount();
55          long ns = sc - lastStealCount;
# Line 71 | Line 71 | public final class DynamicLeftSpineFib e
71      int getAnswer() {
72          return number;
73      }
74 +    public void compute() {
75 +        number = fib(number);
76 +    }
77  
78 <    public final void compute() {
79 <        int n = number;
80 <        if (n > 1) {
81 <            DynamicLeftSpineFib rt = null;
82 <            while (n > 1 && getSurplusQueuedTaskCount() <= 3) {
83 <                (rt = new DynamicLeftSpineFib(n - 2, rt)).fork();
84 <                n -= 1;
78 >    static final int fib(int n) {
79 >        if (n <= 1)
80 >            return n;
81 >        int r = 0;
82 >        DynamicLeftSpineFib rt = null;
83 >        while (getSurplusQueuedTaskCount() <= 3) {
84 >            int m = n - 2;
85 >            n -= 1;
86 >            if (m <= 1) {
87 >                r += m;
88 >                if (n > 1) {
89 >                    r += n - 2;
90 >                    n -= 1;
91 >                }
92 >                break;
93              }
94 <            int r = seqFib(n);
95 <            while (rt != null) {
96 <                if (rt.tryUnfork()) rt.compute(); else rt.helpJoin();
94 >            (rt = new DynamicLeftSpineFib(m, rt)).fork();
95 >        }
96 >        if (n <= 1)
97 >            r += n;
98 >        else
99 >            r += seqFib(n - 2) + fib(n - 1);
100 >        if (rt != null)
101 >            r += collectRights(rt);
102 >        return r;
103 >    }
104 >
105 >    static final int collectRights(DynamicLeftSpineFib rt) {
106 >        int r = 0;
107 >        while (rt != null) {
108 >            DynamicLeftSpineFib rn = rt.next;
109 >            rt.next = null;
110 >            if (rt.tryUnfork())
111 >                r += fib(rt.number);
112 >            else {
113 >                rt.join();
114                  r += rt.number;
87                rt = rt.next;
115              }
116 <            number = r;
116 >            rt = rn;
117          }
118 +        return r;
119      }
120  
121      // Sequential version for arguments less than threshold
122 <    static int seqFib(int n) {
123 <        if (n <= 1)
124 <            return n;
125 <        else
126 <            return seqFib(n-1) + seqFib(n-2);
122 >    static final int seqFib(int n) { // unroll left only
123 >        int r = 1;
124 >        do {
125 >            int m = n - 2;
126 >            r += m <= 1 ? m : seqFib(m);
127 >        } while (--n > 1);
128 >        return r;
129      }
130  
131   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines