4 |
|
* Recursive task-based version of Fibonacci. Computes: |
5 |
|
* <pre> |
6 |
|
* Computes fibonacci(n) = fibonacci(n-1) + fibonacci(n-2); for n> 1 |
7 |
< |
* fibonacci(0) = 0; |
8 |
< |
* fibonacci(1) = 1. |
7 |
> |
* fibonacci(0) = 0; |
8 |
> |
* fibonacci(1) = 1. |
9 |
|
* </pre> |
10 |
< |
**/ |
11 |
< |
|
10 |
> |
*/ |
11 |
|
public final class FibTask extends RecursiveTask<Integer> { |
12 |
|
|
13 |
|
// Performance-tuning constant: |
14 |
|
static int sequentialThreshold; |
15 |
|
|
16 |
|
static long lastStealCount; |
17 |
< |
|
17 |
> |
|
18 |
|
public static void main(String[] args) throws Exception { |
19 |
|
int procs = 0; |
20 |
|
int num = 45; |
24 |
|
procs = Integer.parseInt(args[0]); |
25 |
|
if (args.length > 1) |
26 |
|
num = Integer.parseInt(args[1]); |
27 |
< |
if (args.length > 2) |
27 |
> |
if (args.length > 2) |
28 |
|
sequentialThreshold = Integer.parseInt(args[2]); |
29 |
|
} |
30 |
|
catch (Exception e) { |
33 |
|
} |
34 |
|
|
35 |
|
for (int reps = 0; reps < 2; ++reps) { |
36 |
< |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
36 |
> |
ForkJoinPool g = (procs == 0) ? new ForkJoinPool() : |
37 |
|
new ForkJoinPool(procs); |
38 |
|
lastStealCount = g.getStealCount(); |
39 |
|
for (int i = 0; i < 20; ++i) { |
57 |
|
double secs = ((double)time) / NPS; |
58 |
|
System.out.print("FibTask " + num + " = " + result); |
59 |
|
System.out.printf("\tTime: %7.3f", secs); |
60 |
< |
|
60 |
> |
|
61 |
|
long sc = g.getStealCount(); |
62 |
|
long ns = sc - lastStealCount; |
63 |
|
lastStealCount = sc; |
88 |
|
FibTask f1 = new FibTask(n - 1); |
89 |
|
f1.fork(); |
90 |
|
FibTask f2 = new FibTask(n - 2); |
91 |
< |
return f2.compute() + f1.join(); |
91 |
> |
return f2.compute() + f1.join(); |
92 |
|
} |
93 |
|
} |
94 |
|
|
102 |
|
return r; |
103 |
|
} |
104 |
|
} |
106 |
– |
|