ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/DoubleAccumulatorLoops.java
Revision: 1.1
Committed: Tue Jan 27 06:57:36 2015 UTC (9 years, 2 months ago) by jsr166
Branch: MAIN
CVS Tags: HEAD
Log Message:
variant of LongAdderLoops

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 import java.util.concurrent.atomic.DoubleAccumulator;
8 import java.util.concurrent.Executors;
9 import java.util.concurrent.ExecutorService;
10 import java.util.concurrent.Phaser;
11
12 public class DoubleAccumulatorLoops {
13 public static void main(String[] args) {
14 final int NCPU = Runtime.getRuntime().availableProcessors();
15 int minThreads = 1;
16 int maxThreads = 2 * NCPU;
17 long iters = 300_000_000L;
18 nextArg: for (String arg : args) {
19 String[] fields = arg.split("=");
20 if (fields.length == 2) {
21 String prop = fields[0], val = fields[1];
22 switch (prop) {
23 case "threads":
24 minThreads = maxThreads = Integer.valueOf(val);
25 continue nextArg;
26 case "minThreads":
27 minThreads = Integer.valueOf(val);
28 continue nextArg;
29 case "maxThreads":
30 maxThreads = Integer.valueOf(val);
31 continue nextArg;
32 case "iters":
33 iters = Long.valueOf(val);
34 continue nextArg;
35 }
36 }
37 throw new Error("Usage: DoubleAccumulatorLoops minThreads=n maxThreads=n threads=n iters=n");
38 }
39
40 final ExecutorService pool = Executors.newCachedThreadPool();
41 for (int i = minThreads; i <= maxThreads; i += (i+1) >>> 1)
42 accumulatorTest(pool, i, iters);
43 pool.shutdown();
44 }
45
46 static void accumulatorTest(ExecutorService pool, int nthreads, long iters) {
47 System.out.print("DoubleAccumulator ");
48 Phaser phaser = new Phaser(nthreads + 1);
49 DoubleAccumulator a = new DoubleAccumulator(Double::max, Double.NEGATIVE_INFINITY);
50 for (int i = 0; i < nthreads; ++i)
51 pool.execute(new AccumulatorTask(a, phaser, iters));
52 report(nthreads, iters, timeTasks(phaser), a.get());
53 }
54
55 static void report(int nthreads, long iters, long time, double result) {
56 double secs = (double)time / (1000L * 1000 * 1000);
57 long rate = nthreads * iters * (1000L) / time;
58 System.out.printf("threads:%3d Time: %7.3fsec Iters per microsec: %4d\n",
59 nthreads, secs, rate);
60 }
61
62 static long timeTasks(Phaser phaser) {
63 phaser.arriveAndAwaitAdvance();
64 long start = System.nanoTime();
65 phaser.arriveAndAwaitAdvance();
66 phaser.arriveAndAwaitAdvance();
67 return System.nanoTime() - start;
68 }
69
70 static final class AccumulatorTask implements Runnable {
71 final DoubleAccumulator accumulator;
72 final Phaser phaser;
73 final long iters;
74 volatile double result;
75 AccumulatorTask(DoubleAccumulator accumulator, Phaser phaser, long iters) {
76 this.accumulator = accumulator;
77 this.phaser = phaser;
78 this.iters = iters;
79 }
80
81 public void run() {
82 phaser.arriveAndAwaitAdvance();
83 phaser.arriveAndAwaitAdvance();
84 DoubleAccumulator a = accumulator;
85 for (long i = 0; i < iters; ++i)
86 a.accumulate(2.0);
87 result = a.get();
88 phaser.arrive();
89 }
90 }
91
92 }