1 |
dl |
1.4 |
/* |
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 |
dl |
1.1 |
import java.util.*; |
8 |
|
|
import java.util.concurrent.*; |
9 |
|
|
|
10 |
|
|
public final class DynamicLeftSpineFib extends RecursiveAction { |
11 |
|
|
|
12 |
|
|
// Performance-tuning constant: |
13 |
|
|
static long lastStealCount; |
14 |
jsr166 |
1.8 |
|
15 |
dl |
1.1 |
public static void main(String[] args) throws Exception { |
16 |
|
|
int procs = 0; |
17 |
|
|
int num = 43; |
18 |
|
|
try { |
19 |
|
|
if (args.length > 0) |
20 |
|
|
procs = Integer.parseInt(args[0]); |
21 |
|
|
if (args.length > 1) |
22 |
|
|
num = Integer.parseInt(args[1]); |
23 |
|
|
} |
24 |
|
|
catch (Exception e) { |
25 |
dl |
1.7 |
System.out.println("Usage: java DynamicLeftSpineFib <threads> <number> [<sequntialThreshold>]"); |
26 |
dl |
1.1 |
return; |
27 |
|
|
} |
28 |
|
|
|
29 |
jsr166 |
1.8 |
|
30 |
dl |
1.1 |
for (int reps = 0; reps < 2; ++reps) { |
31 |
dl |
1.7 |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
32 |
dl |
1.1 |
new ForkJoinPool(procs); |
33 |
|
|
lastStealCount = g.getStealCount(); |
34 |
|
|
for (int i = 0; i < 10; ++i) { |
35 |
|
|
test(g, num); |
36 |
|
|
} |
37 |
|
|
System.out.println(g); |
38 |
|
|
g.shutdown(); |
39 |
|
|
} |
40 |
|
|
} |
41 |
|
|
|
42 |
|
|
static void test(ForkJoinPool g, int num) throws Exception { |
43 |
|
|
int ps = g.getParallelism(); |
44 |
|
|
long start = System.currentTimeMillis(); |
45 |
|
|
DynamicLeftSpineFib f = new DynamicLeftSpineFib(num, null); |
46 |
|
|
g.invoke(f); |
47 |
|
|
long time = System.currentTimeMillis() - start; |
48 |
dl |
1.7 |
double secs = ((double)time) / 1000.0; |
49 |
dl |
1.1 |
long result = f.getAnswer(); |
50 |
dl |
1.7 |
System.out.print("DLSFib " + num + " = " + result); |
51 |
dl |
1.1 |
System.out.printf("\tTime: %7.3f", secs); |
52 |
|
|
long sc = g.getStealCount(); |
53 |
|
|
long ns = sc - lastStealCount; |
54 |
|
|
lastStealCount = sc; |
55 |
|
|
System.out.printf(" Steals/t: %5d", ns/ps); |
56 |
|
|
System.out.printf(" Workers: %8d", g.getPoolSize()); |
57 |
|
|
System.out.println(); |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
|
61 |
|
|
// Initialized with argument; replaced with result |
62 |
|
|
int number; |
63 |
|
|
DynamicLeftSpineFib next; |
64 |
|
|
|
65 |
jsr166 |
1.8 |
DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) { |
66 |
|
|
number = n; next = nxt; |
67 |
dl |
1.1 |
} |
68 |
|
|
|
69 |
|
|
int getAnswer() { |
70 |
|
|
return number; |
71 |
|
|
} |
72 |
|
|
|
73 |
|
|
public final void compute() { |
74 |
|
|
int n = number; |
75 |
|
|
if (n > 1) { |
76 |
dl |
1.7 |
int r = 0; |
77 |
dl |
1.1 |
DynamicLeftSpineFib rt = null; |
78 |
dl |
1.7 |
while (getSurplusQueuedTaskCount() <= 3) { |
79 |
|
|
int m = n - 2; |
80 |
dl |
1.1 |
n -= 1; |
81 |
dl |
1.7 |
if (m <= 1) { |
82 |
|
|
r += m; |
83 |
|
|
if (n > 1) { |
84 |
|
|
r += n - 2; |
85 |
|
|
n -= 1; |
86 |
|
|
} |
87 |
|
|
break; |
88 |
|
|
} |
89 |
|
|
(rt = new DynamicLeftSpineFib(m, rt)).fork(); |
90 |
dl |
1.1 |
} |
91 |
dl |
1.7 |
r += seqFib(n); |
92 |
dl |
1.1 |
while (rt != null) { |
93 |
|
|
if (rt.tryUnfork()) rt.compute(); else rt.helpJoin(); |
94 |
|
|
r += rt.number; |
95 |
|
|
rt = rt.next; |
96 |
|
|
} |
97 |
|
|
number = r; |
98 |
|
|
} |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
// Sequential version for arguments less than threshold |
102 |
|
|
static int seqFib(int n) { |
103 |
jsr166 |
1.8 |
if (n <= 1) |
104 |
dl |
1.1 |
return n; |
105 |
jsr166 |
1.8 |
else |
106 |
dl |
1.1 |
return seqFib(n-1) + seqFib(n-2); |
107 |
|
|
} |
108 |
jsr166 |
1.8 |
|
109 |
dl |
1.7 |
} |
110 |
jsr166 |
1.2 |
|