ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LeftSpineFib.java
Revision: 1.6
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +0 -2 lines
Log Message:
delete extraneous blank lines

File Contents

# User Rev Content
1 jsr166 1.5 /*
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/publicdomain/zero/1.0/
5     */
6    
7 dl 1.1 import java.util.*;
8     import java.util.concurrent.*;
9    
10     public final class LeftSpineFib extends RecursiveAction {
11    
12     // Performance-tuning constant:
13     static int sequentialThreshold;
14     static long lastStealCount;
15 jsr166 1.2
16 dl 1.1 public static void main(String[] args) throws Exception {
17     int procs = 0;
18     int num = 45;
19     sequentialThreshold = 2;
20     try {
21     if (args.length > 0)
22     procs = Integer.parseInt(args[0]);
23     if (args.length > 1)
24     num = Integer.parseInt(args[1]);
25 jsr166 1.2 if (args.length > 2)
26 dl 1.1 sequentialThreshold = Integer.parseInt(args[2]);
27     }
28     catch (Exception e) {
29     System.out.println("Usage: java LeftSpineFib <threads> <number> [<sequntialThreshold>]");
30     return;
31     }
32    
33     for (int reps = 0; reps < 2; ++reps) {
34 jsr166 1.3 ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
35 dl 1.1 new ForkJoinPool(procs);
36     // g.setMaintainsParallelism(false);
37     lastStealCount = g.getStealCount();
38     for (int i = 0; i < 20; ++i) {
39     test(g, num);
40     Thread.sleep(50);
41     }
42     System.out.println(g);
43     g.shutdown();
44     if (!g.awaitTermination(8, TimeUnit.SECONDS)) {
45     System.out.println(g);
46     throw new Error();
47     }
48     g = null;
49     // System.gc();
50     Thread.sleep(500);
51     }
52     }
53    
54     static void test(ForkJoinPool g, int num) throws Exception {
55     int ps = g.getParallelism();
56     long start = System.currentTimeMillis();
57     LeftSpineFib f = new LeftSpineFib(num, null);
58     g.invoke(f);
59     long time = System.currentTimeMillis() - start;
60     double secs = ((double)time) / 1000.0;
61     long result = f.getAnswer();
62     System.out.print("JLSFib " + num + " = " + result);
63     System.out.printf("\tTime: %7.3f", secs);
64     long sc = g.getStealCount();
65     long ns = sc - lastStealCount;
66     lastStealCount = sc;
67     System.out.printf(" Steals/t: %5d", ns/ps);
68     // System.out.printf(" Workers: %8d", g.getRunningThreadCount());
69     System.out.printf(" Workers: %8d", g.getPoolSize());
70     System.out.println();
71     }
72    
73     // Initialized with argument; replaced with result
74     int number;
75     LeftSpineFib next;
76    
77     LeftSpineFib(int n, LeftSpineFib nxt) { number = n; next = nxt; }
78    
79     int getAnswer() {
80     return number;
81     }
82    
83     public final void compute() {
84     int n = number;
85     if (n > 1) {
86     LeftSpineFib rt = null;
87     int r = 0;
88     while (n > sequentialThreshold) {
89     int m = n - 2;
90     if (m <= 1)
91     r += m;
92     else
93     (rt = new LeftSpineFib(m, rt)).fork();
94     n -= 1;
95     }
96     r += n <= 1 ? n : seqFib(n);
97     if (rt != null)
98     r += collectRights(rt);
99     number = r;
100     }
101     }
102    
103     static final int collectRights(LeftSpineFib rt) {
104     int r = 0;
105     while (rt != null) {
106     LeftSpineFib rn = rt.next;
107     rt.next = null;
108     if (rt.tryUnfork()) rt.compute(); else rt.join();
109     // rt.join();
110     r += rt.number;
111     rt = rn;
112     }
113     return r;
114     }
115    
116     // Sequential version for arguments less than threshold
117     static final int seqFib(int n) { // unroll left only
118     int r = 1;
119     do {
120     int m = n - 2;
121     r += m <= 1 ? m : seqFib(m);
122     } while (--n > 1);
123     return r;
124     }
125    
126     }