ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DynamicAsyncFib.java
Revision: 1.8
Committed: Sat Sep 12 18:59:49 2015 UTC (8 years, 7 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Changes since 1.7: +1 -1 lines
Log Message:
whitespace

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