ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DynamicLeftSpineFib.java
Revision: 1.15
Committed: Wed Dec 31 17:00:58 2014 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.14: +1 -3 lines
Log Message:
lexicographic import order

File Contents

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