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.1 by dl, Fri Oct 23 19:57:06 2009 UTC vs.
Revision 1.9 by dl, Wed Jul 7 20:32:04 2010 UTC

# Line 1 | Line 1
1 < //import jsr166y.*;
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.*;
8   import java.util.concurrent.*;
9  
# Line 6 | Line 11 | public final class DynamicLeftSpineFib e
11  
12      // Performance-tuning constant:
13      static long lastStealCount;
14 <  
14 >
15      public static void main(String[] args) throws Exception {
16          int procs = 0;
17          int num = 43;
# Line 21 | Line 26 | public final class DynamicLeftSpineFib e
26              return;
27          }
28  
29 <        
29 >
30          for (int reps = 0; reps < 2; ++reps) {
31              ForkJoinPool g = procs == 0? new ForkJoinPool() :
32                  new ForkJoinPool(procs);
# Line 42 | Line 47 | public final class DynamicLeftSpineFib e
47          long time = System.currentTimeMillis() - start;
48          double secs = ((double)time) / 1000.0;
49          long result = f.getAnswer();
50 <        System.out.print("DynamicLeftSpineFib " + num + " = " + result);
50 >        System.out.print("DLSFib " + num + " = " + result);
51          System.out.printf("\tTime: %7.3f", secs);
52          long sc = g.getStealCount();
53          long ns = sc - lastStealCount;
# Line 57 | Line 62 | public final class DynamicLeftSpineFib e
62      int number;
63      DynamicLeftSpineFib next;
64  
65 <    DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) {
66 <        number = n; next = nxt;
65 >    DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) {
66 >        number = n; next = nxt;
67      }
68  
69      int getAnswer() {
# Line 68 | Line 73 | public final class DynamicLeftSpineFib e
73      public final void compute() {
74          int n = number;
75          if (n > 1) {
76 +            int r = 0;
77              DynamicLeftSpineFib rt = null;
78 <            while (n > 1 && getSurplusQueuedTaskCount() <= 3) {
79 <                (rt = new DynamicLeftSpineFib(n - 2, rt)).fork();
78 >            while (getSurplusQueuedTaskCount() <= 3) {
79 >                int m = n - 2;
80                  n -= 1;
81 +                if (m <= 1) {
82 +                    r += m;
83 +                    if (n > 1) {
84 +                        r += n - 2;
85 +                        n -= 1;
86 +                    }
87 +                    break;
88 +                }
89 +                (rt = new DynamicLeftSpineFib(m, rt)).fork();
90              }
91 <            int r = seqFib(n);
91 >            r += seqFib(n);
92              while (rt != null) {
93 <                if (rt.tryUnfork()) rt.compute(); else rt.helpJoin();
93 >                if (rt.tryUnfork()) rt.compute(); else rt.join();
94                  r += rt.number;
95                  rt = rt.next;
96              }
# Line 85 | Line 100 | public final class DynamicLeftSpineFib e
100  
101      // Sequential version for arguments less than threshold
102      static int seqFib(int n) {
103 <        if (n <= 1)
103 >        if (n <= 1)
104              return n;
105 <        else
105 >        else
106              return seqFib(n-1) + seqFib(n-2);
107      }
108 <    
108 >
109   }
110  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines