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