ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DynamicAsyncFib.java
Revision: 1.6
Committed: Wed Jul 4 20:07:01 2012 UTC (11 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +0 -2 lines
Log Message:
trailing newlines

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.4 * http://creativecommons.org/publicdomain/zero/1.0/
5 dl 1.1 */
6    
7     import java.util.concurrent.*;
8    
9 jsr166 1.5 public final class DynamicAsyncFib extends BinaryAsyncAction {
10 dl 1.1 int number;
11    
12 jsr166 1.2 public DynamicAsyncFib(int n) {
13 dl 1.1 this.number = n;
14     }
15    
16     public final boolean exec() {
17     DynamicAsyncFib f = this;
18     int n = f.number;
19     while (n > 1 && getSurplusQueuedTaskCount() <= 3) {
20     DynamicAsyncFib l = new DynamicAsyncFib(--n);
21     DynamicAsyncFib r = new DynamicAsyncFib(n - 1);
22     f.linkSubtasks(l, r);
23     r.fork();
24     f = l;
25     }
26     f.number = seqFib(n);
27     f.complete();
28     return false;
29     }
30    
31     protected void onComplete(BinaryAsyncAction x, BinaryAsyncAction y) {
32     number = ((DynamicAsyncFib)x).number + ((DynamicAsyncFib)y).number;
33     }
34    
35     static long lastStealCount;
36    
37     public static void main(String[] args) throws Exception {
38     int procs = 0;
39     int num = 45;
40     try {
41     if (args.length > 0)
42     procs = Integer.parseInt(args[0]);
43     if (args.length > 1)
44     num = Integer.parseInt(args[1]);
45     }
46     catch (Exception e) {
47     System.out.println("Usage: java DynamicAsyncFib <threads> <number>]");
48     return;
49     }
50    
51     for (int reps = 0; reps < 2; ++reps) {
52 jsr166 1.3 ForkJoinPool g = (procs == 0) ? new ForkJoinPool() :
53 dl 1.1 new ForkJoinPool(procs);
54     lastStealCount = g.getStealCount();
55     for (int i = 0; i < 20; ++i) {
56     test(g, num);
57     // Thread.sleep(1000);
58     }
59     System.out.println(g);
60     g.shutdown();
61     }
62     }
63    
64     /** for time conversion */
65     static final long NPS = (1000L * 1000 * 1000);
66    
67     static void test(ForkJoinPool g, int num) throws Exception {
68     int ps = g.getParallelism();
69     long start = System.nanoTime();
70     DynamicAsyncFib f = new DynamicAsyncFib(num);
71     g.invoke(f);
72     long time = System.nanoTime() - start;
73     double secs = ((double)time) / NPS;
74     long number = f.number;
75     System.out.print("DAFib " + num + " = " + number);
76     System.out.printf("\tTime: %9.3f", secs);
77     long sc = g.getStealCount();
78     long ns = sc - lastStealCount;
79     lastStealCount = sc;
80     System.out.printf(" Steals/t: %5d", ns/ps);
81     System.out.printf(" Workers: %8d", g.getPoolSize());
82     System.out.println();
83     }
84    
85     // Sequential version for arguments less than threshold
86     static final int seqFib(int n) { // unroll left only
87     int r = 1;
88     do {
89     int m = n - 2;
90     r += m <= 1 ? m : seqFib(m);
91     } while (--n > 1);
92     return r;
93     }
94     }