ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/extra166y/LongCumulateDemo.java
Revision: 1.3
Committed: Thu Nov 25 12:38:35 2010 UTC (13 years, 6 months ago) by dl
Branch: MAIN
Changes since 1.2: +4 -4 lines
Log Message:
Misc test syncs

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     * http://creativecommons.org/licenses/publicdomain
5     */
6    
7    
8     import jsr166y.*;
9     import extra166y.*;
10     import static extra166y.Ops.*;
11     import java.util.*;
12     import java.util.concurrent.*;
13    
14     public class LongCumulateDemo {
15     static final int NCPU = Runtime.getRuntime().availableProcessors();
16     /** for time conversion */
17     static final long NPS = (1000L * 1000 * 1000);
18    
19     static final boolean reportSteals = false;
20    
21     public static void main(String[] args) throws Exception {
22     int n = 1 << 19;
23     int reps = 1 << 8;
24     int tests = 2;
25     long[] array = new long[n];
26     long last, now;
27     double elapsed;
28    
29     for (int j = 0; j < tests; ++j) {
30     seqFill(array);
31     last = System.nanoTime();
32     for (int k = 0; k < reps; ++k) {
33     seqCumulate(array);
34     if (j == 0 && k == 0)
35     check(array);
36     }
37     now = System.nanoTime();
38     elapsed = (double)(now - last) / (NPS);
39     System.out.printf("seq : %9.5f\n", elapsed);
40     }
41    
42     for (int sweeps = 0; sweeps < 2; ++sweeps) {
43     for (int i = 2; i <= NCPU; i <<= 1) {
44 dl 1.3 ForkJoinPool fjp = new ForkJoinPool(i);
45 dl 1.1 oneRun(fjp, array, i, reps, tests);
46 dl 1.3 fjp.shutdown();
47 dl 1.1 }
48     for (int i = NCPU; i >= 1; i >>>= 1) {
49 dl 1.3 ForkJoinPool fjp = new ForkJoinPool(i);
50 dl 1.1 oneRun(fjp, array, i, reps, tests);
51 dl 1.3 fjp.shutdown();
52 dl 1.1 }
53     }
54     }
55    
56 jsr166 1.2 static void oneRun(ForkJoinPool fjp,
57     long[] array, int nthreads,
58 dl 1.1 int reps, int tests) throws Exception {
59     ParallelLongArray pa = ParallelLongArray.createUsingHandoff(array, fjp);
60     long last, now;
61     long steals = fjp.getStealCount();
62     // long syncs = fjp.getSyncCount();
63     for (int j = 0; j < tests; ++j) {
64     pa.replaceWithValue(1);
65     last = System.nanoTime();
66     for (int k = 0; k < reps; ++k) {
67     pa.cumulateSum();
68     if (j == 0 && k == 0)
69     check(array);
70     }
71     now = System.nanoTime();
72     double elapsed = (double)(now - last) / (NPS);
73     last = now;
74     System.out.printf("ps %2d: %9.5f", nthreads, elapsed);
75     if (reportSteals) {
76     long sc = fjp.getStealCount();
77     long scount = (sc - steals) / reps;
78     steals = sc;
79     System.out.printf(" Steals:%6d", scount);
80     }
81     System.out.println();
82     }
83     Thread.sleep(100);
84     }
85    
86     static void check(long[] array) {
87     for (int i = 0; i < array.length; ++i) {
88     long sum = i + 1;
89     if (array[i] != sum) {
90     System.out.println("i: " + i + " sum: " + sum + " element:" + array[i]);
91     throw new Error();
92     }
93     }
94     }
95    
96     static void seqFill(long[] array) {
97     for (int i = 0; i < array.length; ++i)
98     array[i] = 1;
99     }
100    
101     static long seqCumulate(long[] array) {
102     long sum = 0;
103     for (int i = 0; i < array.length; ++i)
104     sum = array[i] += sum;
105     return sum;
106     }
107     }