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. |
13 |
> |
* fibonacci(0) = 0; |
14 |
> |
* fibonacci(1) = 1. |
15 |
|
* </pre> |
16 |
|
**/ |
17 |
|
|
21 |
|
static int sequentialThreshold; |
22 |
|
|
23 |
|
static long lastStealCount; |
24 |
< |
|
24 |
> |
|
25 |
|
public static void main(String[] args) throws Exception { |
26 |
|
int procs = 0; |
27 |
|
int num = 45; |
31 |
|
procs = Integer.parseInt(args[0]); |
32 |
|
if (args.length > 1) |
33 |
|
num = Integer.parseInt(args[1]); |
34 |
< |
if (args.length > 2) |
34 |
> |
if (args.length > 2) |
35 |
|
sequentialThreshold = Integer.parseInt(args[2]); |
36 |
|
} |
37 |
|
catch (Exception e) { |
40 |
|
} |
41 |
|
|
42 |
|
for (int reps = 0; reps < 2; ++reps) { |
43 |
< |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
43 |
> |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
44 |
|
new ForkJoinPool(procs); |
45 |
|
lastStealCount = g.getStealCount(); |
46 |
|
for (int i = 0; i < 20; ++i) { |
72 |
|
long result = f.getAnswer(); |
73 |
|
System.out.print("Fib " + num + " = " + result); |
74 |
|
System.out.printf("\tTime: %7.3f", secs); |
75 |
< |
|
75 |
> |
|
76 |
|
long sc = g.getStealCount(); |
77 |
|
long ns = sc - lastStealCount; |
78 |
|
lastStealCount = sc; |
99 |
|
else { |
100 |
|
Fib f1 = new Fib(n - 1); |
101 |
|
Fib f2 = new Fib(n - 2); |
102 |
< |
// forkJoin(f1, f2); |
103 |
< |
invokeAll(f1, f2); |
102 |
> |
// forkJoin(f1, f2); |
103 |
> |
invokeAll(f1, f2); |
104 |
|
number = f1.number + f2.number; |
105 |
|
} |
106 |
|
} |