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