ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/LeftSpineFib.java
Revision: 1.7
Committed: Sat Sep 12 19:06:29 2015 UTC (8 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.6: +4 -10 lines
Log Message:
Use commonPool

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