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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
– |
import java.util.*; |
7 |
|
import java.util.concurrent.*; |
8 |
+ |
import java.util.*; |
9 |
+ |
import java.util.concurrent.TimeUnit; |
10 |
+ |
//import java.util.concurrent.*; |
11 |
|
|
12 |
|
public final class DynamicLeftSpineFib extends RecursiveAction { |
13 |
|
|
12 |
– |
// Performance-tuning constant: |
14 |
|
static long lastStealCount; |
15 |
< |
|
15 |
> |
|
16 |
|
public static void main(String[] args) throws Exception { |
17 |
|
int procs = 0; |
18 |
< |
int num = 43; |
18 |
> |
int num = 45; |
19 |
|
try { |
20 |
|
if (args.length > 0) |
21 |
|
procs = Integer.parseInt(args[0]); |
27 |
|
return; |
28 |
|
} |
29 |
|
|
30 |
< |
|
30 |
> |
|
31 |
|
for (int reps = 0; reps < 2; ++reps) { |
32 |
< |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
32 |
> |
ForkJoinPool g = (procs == 0) ? new ForkJoinPool() : |
33 |
|
new ForkJoinPool(procs); |
34 |
|
lastStealCount = g.getStealCount(); |
35 |
< |
for (int i = 0; i < 10; ++i) { |
35 |
> |
for (int i = 0; i < 20; ++i) { |
36 |
|
test(g, num); |
37 |
|
} |
38 |
|
System.out.println(g); |
39 |
|
g.shutdown(); |
40 |
+ |
if (!g.awaitTermination(10, TimeUnit.SECONDS)) |
41 |
+ |
throw new Error(); |
42 |
+ |
Thread.sleep(1000); |
43 |
|
} |
44 |
|
} |
45 |
|
|
66 |
|
int number; |
67 |
|
DynamicLeftSpineFib next; |
68 |
|
|
69 |
< |
DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) { |
70 |
< |
number = n; next = nxt; |
69 |
> |
DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) { |
70 |
> |
number = n; next = nxt; |
71 |
|
} |
72 |
|
|
73 |
|
int getAnswer() { |
74 |
|
return number; |
75 |
|
} |
76 |
+ |
public void compute() { |
77 |
+ |
number = fib(number); |
78 |
+ |
} |
79 |
|
|
80 |
< |
public final void compute() { |
81 |
< |
int n = number; |
82 |
< |
if (n > 1) { |
83 |
< |
int r = 0; |
84 |
< |
DynamicLeftSpineFib rt = null; |
85 |
< |
while (getSurplusQueuedTaskCount() <= 3) { |
86 |
< |
int m = n - 2; |
87 |
< |
n -= 1; |
88 |
< |
if (m <= 1) { |
89 |
< |
r += m; |
90 |
< |
if (n > 1) { |
91 |
< |
r += n - 2; |
92 |
< |
n -= 1; |
86 |
< |
} |
87 |
< |
break; |
80 |
> |
static final int fib(int n) { |
81 |
> |
if (n <= 1) |
82 |
> |
return n; |
83 |
> |
int r = 0; |
84 |
> |
DynamicLeftSpineFib rt = null; |
85 |
> |
while (getSurplusQueuedTaskCount() <= 3) { |
86 |
> |
int m = n - 2; |
87 |
> |
n -= 1; |
88 |
> |
if (m <= 1) { |
89 |
> |
r += m; |
90 |
> |
if (n > 1) { |
91 |
> |
r += n - 2; |
92 |
> |
n -= 1; |
93 |
|
} |
94 |
< |
(rt = new DynamicLeftSpineFib(m, rt)).fork(); |
94 |
> |
break; |
95 |
|
} |
96 |
< |
r += seqFib(n); |
97 |
< |
while (rt != null) { |
98 |
< |
if (rt.tryUnfork()) rt.compute(); else rt.helpJoin(); |
96 |
> |
(rt = new DynamicLeftSpineFib(m, rt)).fork(); |
97 |
> |
} |
98 |
> |
if (n <= 1) |
99 |
> |
r += n; |
100 |
> |
else |
101 |
> |
r += seqFib(n - 2) + fib(n - 1); |
102 |
> |
if (rt != null) |
103 |
> |
r += collectRights(rt); |
104 |
> |
return r; |
105 |
> |
} |
106 |
> |
|
107 |
> |
static final int collectRights(DynamicLeftSpineFib rt) { |
108 |
> |
int r = 0; |
109 |
> |
while (rt != null) { |
110 |
> |
DynamicLeftSpineFib rn = rt.next; |
111 |
> |
rt.next = null; |
112 |
> |
if (rt.tryUnfork()) |
113 |
> |
r += fib(rt.number); |
114 |
> |
else { |
115 |
> |
rt.join(); |
116 |
|
r += rt.number; |
95 |
– |
rt = rt.next; |
117 |
|
} |
118 |
< |
number = r; |
118 |
> |
rt = rn; |
119 |
|
} |
120 |
+ |
return r; |
121 |
|
} |
122 |
|
|
123 |
|
// Sequential version for arguments less than threshold |
124 |
< |
static int seqFib(int n) { |
125 |
< |
if (n <= 1) |
126 |
< |
return n; |
127 |
< |
else |
128 |
< |
return seqFib(n-1) + seqFib(n-2); |
124 |
> |
static final int seqFib(int n) { // unroll left only |
125 |
> |
int r = 1; |
126 |
> |
do { |
127 |
> |
int m = n - 2; |
128 |
> |
r += m <= 1 ? m : seqFib(m); |
129 |
> |
} while (--n > 1); |
130 |
> |
return r; |
131 |
|
} |
108 |
– |
|
109 |
– |
} |
132 |
|
|
133 |
+ |
} |