ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DynamicLeftSpineFib.java
Revision: 1.16
Committed: Thu Jan 15 18:34:19 2015 UTC (9 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.15: +0 -2 lines
Log Message:
delete extraneous blank lines

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