ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Fib.java
Revision: 1.8
Committed: Sat Sep 12 18:52:19 2015 UTC (8 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.7: +4 -8 lines
Log Message:
Use commonPool

File Contents

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