ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/extra166y/DoubleCumulateDemo.java
Revision: 1.4
Committed: Tue Mar 15 19:47:04 2011 UTC (13 years, 1 month ago) by jsr166
Branch: MAIN
CVS Tags: release-1_7_0, HEAD
Changes since 1.3: +1 -1 lines
Log Message:
Update Creative Commons license URL in legal notices

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