1 |
– |
//import jsr166y.*; |
2 |
– |
import java.util.*; |
1 |
|
import java.util.concurrent.*; |
2 |
+ |
import java.util.*; |
3 |
+ |
import java.util.concurrent.TimeUnit; |
4 |
+ |
//import java.util.concurrent.*; |
5 |
|
|
6 |
|
public final class DynamicLeftSpineFib extends RecursiveAction { |
7 |
|
|
7 |
– |
// Performance-tuning constant: |
8 |
|
static long lastStealCount; |
9 |
|
|
10 |
|
public static void main(String[] args) throws Exception { |
11 |
|
int procs = 0; |
12 |
< |
int num = 43; |
12 |
> |
int num = 45; |
13 |
|
try { |
14 |
|
if (args.length > 0) |
15 |
|
procs = Integer.parseInt(args[0]); |
17 |
|
num = Integer.parseInt(args[1]); |
18 |
|
} |
19 |
|
catch (Exception e) { |
20 |
< |
System.out.println("Usage: java DynamicLeftSpineFib <threads> <number> [<sequentialThreshold>]"); |
20 |
> |
System.out.println("Usage: java DynamicLeftSpineFib <threads> <number> [<sequntialThreshold>]"); |
21 |
|
return; |
22 |
|
} |
23 |
|
|
24 |
|
|
25 |
|
for (int reps = 0; reps < 2; ++reps) { |
26 |
< |
ForkJoinPool g = procs == 0? new ForkJoinPool() : |
26 |
> |
ForkJoinPool g = (procs == 0) ? new ForkJoinPool() : |
27 |
|
new ForkJoinPool(procs); |
28 |
|
lastStealCount = g.getStealCount(); |
29 |
< |
for (int i = 0; i < 10; ++i) { |
29 |
> |
for (int i = 0; i < 20; ++i) { |
30 |
|
test(g, num); |
31 |
|
} |
32 |
|
System.out.println(g); |
33 |
|
g.shutdown(); |
34 |
+ |
if (!g.awaitTermination(10, TimeUnit.SECONDS)) |
35 |
+ |
throw new Error(); |
36 |
+ |
Thread.sleep(1000); |
37 |
|
} |
38 |
|
} |
39 |
|
|
45 |
|
long time = System.currentTimeMillis() - start; |
46 |
|
double secs = ((double)time) / 1000.0; |
47 |
|
long result = f.getAnswer(); |
48 |
< |
System.out.print("DynamicLeftSpineFib " + num + " = " + result); |
48 |
> |
System.out.print("DLSFib " + num + " = " + result); |
49 |
|
System.out.printf("\tTime: %7.3f", secs); |
50 |
|
long sc = g.getStealCount(); |
51 |
|
long ns = sc - lastStealCount; |
67 |
|
int getAnswer() { |
68 |
|
return number; |
69 |
|
} |
70 |
+ |
public void compute() { |
71 |
+ |
number = fib(number); |
72 |
+ |
} |
73 |
|
|
74 |
< |
public final void compute() { |
75 |
< |
int n = number; |
76 |
< |
if (n > 1) { |
77 |
< |
DynamicLeftSpineFib rt = null; |
78 |
< |
while (n > 1 && getSurplusQueuedTaskCount() <= 3) { |
79 |
< |
(rt = new DynamicLeftSpineFib(n - 2, rt)).fork(); |
80 |
< |
n -= 1; |
74 |
> |
static final int fib(int n) { |
75 |
> |
if (n <= 1) |
76 |
> |
return n; |
77 |
> |
int r = 0; |
78 |
> |
DynamicLeftSpineFib rt = null; |
79 |
> |
while (getSurplusQueuedTaskCount() <= 3) { |
80 |
> |
int m = n - 2; |
81 |
> |
n -= 1; |
82 |
> |
if (m <= 1) { |
83 |
> |
r += m; |
84 |
> |
if (n > 1) { |
85 |
> |
r += n - 2; |
86 |
> |
n -= 1; |
87 |
> |
} |
88 |
> |
break; |
89 |
|
} |
90 |
< |
int r = seqFib(n); |
91 |
< |
while (rt != null) { |
92 |
< |
if (rt.tryUnfork()) rt.compute(); else rt.helpJoin(); |
90 |
> |
(rt = new DynamicLeftSpineFib(m, rt)).fork(); |
91 |
> |
} |
92 |
> |
if (n <= 1) |
93 |
> |
r += n; |
94 |
> |
else |
95 |
> |
r += seqFib(n - 2) + fib(n - 1); |
96 |
> |
if (rt != null) |
97 |
> |
r += collectRights(rt); |
98 |
> |
return r; |
99 |
> |
} |
100 |
> |
|
101 |
> |
static final int collectRights(DynamicLeftSpineFib rt) { |
102 |
> |
int r = 0; |
103 |
> |
while (rt != null) { |
104 |
> |
DynamicLeftSpineFib rn = rt.next; |
105 |
> |
rt.next = null; |
106 |
> |
if (rt.tryUnfork()) |
107 |
> |
r += fib(rt.number); |
108 |
> |
else { |
109 |
> |
rt.join(); |
110 |
|
r += rt.number; |
80 |
– |
rt = rt.next; |
111 |
|
} |
112 |
< |
number = r; |
112 |
> |
rt = rn; |
113 |
|
} |
114 |
+ |
return r; |
115 |
|
} |
116 |
|
|
117 |
|
// Sequential version for arguments less than threshold |
118 |
< |
static int seqFib(int n) { |
119 |
< |
if (n <= 1) |
120 |
< |
return n; |
121 |
< |
else |
122 |
< |
return seqFib(n-1) + seqFib(n-2); |
118 |
> |
static final int seqFib(int n) { // unroll left only |
119 |
> |
int r = 1; |
120 |
> |
do { |
121 |
> |
int m = n - 2; |
122 |
> |
r += m <= 1 ? m : seqFib(m); |
123 |
> |
} while (--n > 1); |
124 |
> |
return r; |
125 |
|
} |
126 |
|
|
127 |
|
} |