ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/extra166y/LongCumulateDemo.java
Revision: 1.4
Committed: Tue Mar 15 19:47:04 2011 UTC (13 years, 2 months 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 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 ForkJoinPool fjp = new ForkJoinPool(i);
45 oneRun(fjp, array, i, reps, tests);
46 fjp.shutdown();
47 }
48 for (int i = NCPU; i >= 1; i >>>= 1) {
49 ForkJoinPool fjp = new ForkJoinPool(i);
50 oneRun(fjp, array, i, reps, tests);
51 fjp.shutdown();
52 }
53 }
54 }
55
56 static void oneRun(ForkJoinPool fjp,
57 long[] array, int nthreads,
58 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 }