ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/jsr166y/LongCumulateDemo.java
Revision: 1.5
Committed: Sun Nov 1 21:50:49 2009 UTC (14 years, 7 months ago) by dl
Branch: MAIN
CVS Tags: HEAD
Changes since 1.4: +0 -0 lines
State: FILE REMOVED
Log Message:
move tests for unshipped classes

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