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 |
|
8 |
static long lastStealCount; |
9 |
|
10 |
public static void main(String[] args) throws Exception { |
11 |
int procs = 0; |
12 |
int num = 45; |
13 |
try { |
14 |
if (args.length > 0) |
15 |
procs = Integer.parseInt(args[0]); |
16 |
if (args.length > 1) |
17 |
num = Integer.parseInt(args[1]); |
18 |
} |
19 |
catch (Exception e) { |
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() : |
27 |
new ForkJoinPool(procs); |
28 |
lastStealCount = g.getStealCount(); |
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 |
|
40 |
static void test(ForkJoinPool g, int num) throws Exception { |
41 |
int ps = g.getParallelism(); |
42 |
long start = System.currentTimeMillis(); |
43 |
DynamicLeftSpineFib f = new DynamicLeftSpineFib(num, null); |
44 |
g.invoke(f); |
45 |
long time = System.currentTimeMillis() - start; |
46 |
double secs = ((double)time) / 1000.0; |
47 |
long result = f.getAnswer(); |
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; |
52 |
lastStealCount = sc; |
53 |
System.out.printf(" Steals/t: %5d", ns/ps); |
54 |
System.out.printf(" Workers: %8d", g.getPoolSize()); |
55 |
System.out.println(); |
56 |
} |
57 |
|
58 |
|
59 |
// Initialized with argument; replaced with result |
60 |
int number; |
61 |
DynamicLeftSpineFib next; |
62 |
|
63 |
DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) { |
64 |
number = n; next = nxt; |
65 |
} |
66 |
|
67 |
int getAnswer() { |
68 |
return number; |
69 |
} |
70 |
public void compute() { |
71 |
number = fib(number); |
72 |
} |
73 |
|
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 |
(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; |
111 |
} |
112 |
rt = rn; |
113 |
} |
114 |
return r; |
115 |
} |
116 |
|
117 |
// Sequential version for arguments less than threshold |
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 |
} |
128 |
|