ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DynamicLeftSpineFib.java
Revision: 1.3
Committed: Thu Oct 29 23:14:43 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.2: +1 -1 lines
Log Message:
typos

File Contents

# Content
1 //import jsr166y.*;
2 import java.util.*;
3 import java.util.concurrent.*;
4
5 public final class DynamicLeftSpineFib extends RecursiveAction {
6
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;
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> [<sequentialThreshold>]");
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 < 10; ++i) {
30 test(g, num);
31 }
32 System.out.println(g);
33 g.shutdown();
34 }
35 }
36
37 static void test(ForkJoinPool g, int num) throws Exception {
38 int ps = g.getParallelism();
39 long start = System.currentTimeMillis();
40 DynamicLeftSpineFib f = new DynamicLeftSpineFib(num, null);
41 g.invoke(f);
42 long time = System.currentTimeMillis() - start;
43 double secs = ((double)time) / 1000.0;
44 long result = f.getAnswer();
45 System.out.print("DynamicLeftSpineFib " + num + " = " + result);
46 System.out.printf("\tTime: %7.3f", secs);
47 long sc = g.getStealCount();
48 long ns = sc - lastStealCount;
49 lastStealCount = sc;
50 System.out.printf(" Steals/t: %5d", ns/ps);
51 System.out.printf(" Workers: %8d", g.getPoolSize());
52 System.out.println();
53 }
54
55
56 // Initialized with argument; replaced with result
57 int number;
58 DynamicLeftSpineFib next;
59
60 DynamicLeftSpineFib(int n, DynamicLeftSpineFib nxt) {
61 number = n; next = nxt;
62 }
63
64 int getAnswer() {
65 return number;
66 }
67
68 public final void compute() {
69 int n = number;
70 if (n > 1) {
71 DynamicLeftSpineFib rt = null;
72 while (n > 1 && getSurplusQueuedTaskCount() <= 3) {
73 (rt = new DynamicLeftSpineFib(n - 2, rt)).fork();
74 n -= 1;
75 }
76 int r = seqFib(n);
77 while (rt != null) {
78 if (rt.tryUnfork()) rt.compute(); else rt.helpJoin();
79 r += rt.number;
80 rt = rt.next;
81 }
82 number = r;
83 }
84 }
85
86 // Sequential version for arguments less than threshold
87 static int seqFib(int n) {
88 if (n <= 1)
89 return n;
90 else
91 return seqFib(n-1) + seqFib(n-2);
92 }
93
94 }