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 FibTask extends RecursiveTask<Integer> { |
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) ? new ForkJoinPool() : |
43 |
new ForkJoinPool(procs); |
44 |
lastStealCount = g.getStealCount(); |
45 |
for (int i = 0; i < 20; ++i) { |
46 |
test(g, num); |
47 |
// Thread.sleep(1000); |
48 |
} |
49 |
System.out.println(g); |
50 |
g.shutdown(); |
51 |
} |
52 |
} |
53 |
|
54 |
|
55 |
/** for time conversion */ |
56 |
static final long NPS = (1000L * 1000 * 1000); |
57 |
|
58 |
static void test(ForkJoinPool g, int num) throws Exception { |
59 |
int ps = g.getParallelism(); |
60 |
long start = System.nanoTime(); |
61 |
int result = g.invoke(new FibTask(num)); |
62 |
long time = System.nanoTime() - start; |
63 |
double secs = ((double)time) / NPS; |
64 |
System.out.print("FibTask " + num + " = " + result); |
65 |
System.out.printf("\tTime: %7.3f", secs); |
66 |
|
67 |
long sc = g.getStealCount(); |
68 |
long ns = sc - lastStealCount; |
69 |
lastStealCount = sc; |
70 |
System.out.printf(" Steals/t: %5d", ns/ps); |
71 |
System.out.printf(" Workers: %5d", g.getPoolSize()); |
72 |
System.out.println(); |
73 |
} |
74 |
|
75 |
|
76 |
// Initialized with argument; replaced with result |
77 |
int number; |
78 |
|
79 |
FibTask(int n) { number = n; } |
80 |
|
81 |
public Integer compute() { |
82 |
int n = number; |
83 |
|
84 |
// Handle base cases: |
85 |
if (n <= 1) { |
86 |
return n; |
87 |
} |
88 |
// Use sequential code for small problems: |
89 |
else if (n <= sequentialThreshold) { |
90 |
return seqFib(n); |
91 |
} |
92 |
// Otherwise use recursive parallel decomposition: |
93 |
else { |
94 |
FibTask f1 = new FibTask(n - 1); |
95 |
f1.fork(); |
96 |
FibTask f2 = new FibTask(n - 2); |
97 |
return f2.compute() + f1.join(); |
98 |
} |
99 |
} |
100 |
|
101 |
// Sequential version for arguments less than threshold |
102 |
static final int seqFib(int n) { // unroll left only |
103 |
int r = 1; |
104 |
do { |
105 |
int m = n - 2; |
106 |
r += m <= 1 ? m : seqFib(m); |
107 |
} while (--n > 1); |
108 |
return r; |
109 |
} |
110 |
} |