ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/Fib.java
Revision: 1.9
Committed: Sat Sep 12 18:59:49 2015 UTC (8 years, 6 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.8: +1 -1 lines
Log Message:
whitespace

File Contents

# Content
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/publicdomain/zero/1.0/
5 */
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 * fibonacci(0) = 0;
14 * fibonacci(1) = 1.
15 * </pre>
16 */
17 public final class Fib extends RecursiveAction {
18
19 // Performance-tuning constant:
20 static int sequentialThreshold;
21
22 static long lastStealCount;
23
24 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 if (args.length > 2)
34 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 ForkJoinPool g = (procs == 0) ? ForkJoinPool.commonPool() :
43 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 if (g != ForkJoinPool.commonPool())
52 g.shutdown();
53 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
71 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 invokeAll(f1, f2);
97 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
112 }