ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/TieredPhaserLoops.java
Revision: 1.5
Committed: Mon Nov 16 04:16:43 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +1 -1 lines
Log Message:
whitespace

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 import java.util.*;
8 import java.util.concurrent.*;
9 //import jsr166y.*;
10
11 /*
12 * Based loosely on Java Grande Forum barrierBench
13 */
14
15 public class TieredPhaserLoops {
16 static final int NCPUS = Runtime.getRuntime().availableProcessors();
17 static final ExecutorService pool = Executors.newCachedThreadPool();
18 static final int FIRST_SIZE = 10000;
19 static final int LAST_SIZE = 1000000;
20 /** for time conversion */
21 static final long NPS = (1000L * 1000 * 1000);
22
23 static int tasksPerPhaser = Math.max(NCPUS / 8, 4);
24
25 static void build(Runnable[] actions, int sz, int lo, int hi, Phaser b) {
26 if (hi - lo > tasksPerPhaser) {
27 for (int i = lo; i < hi; i += tasksPerPhaser) {
28 int j = Math.min(i + tasksPerPhaser, hi);
29 build(actions, sz, i, j, new Phaser(b));
30 }
31 } else {
32 for (int i = lo; i < hi; ++i)
33 actions[i] = new PhaserAction(i, b, sz);
34 }
35 }
36
37
38 static final class PhaserAction implements Runnable {
39 final int id;
40 final int size;
41 final Phaser phaser;
42 public PhaserAction(int id, Phaser b, int size) {
43 this.id = id;
44 this.phaser = b;
45 this.size = size;
46 phaser.register();
47 }
48
49
50 public void run() {
51 int n = size;
52 Phaser b = phaser;
53 for (int i = 0; i < n; ++i)
54 b.arriveAndAwaitAdvance();
55 }
56 }
57
58 public static void main(String[] args) throws Exception {
59 int nthreads = NCPUS;
60 if (args.length > 0)
61 nthreads = Integer.parseInt(args[0]);
62 if (args.length > 1)
63 tasksPerPhaser = Integer.parseInt(args[1]);
64
65 System.out.printf("Max %d Threads, %d tasks per phaser\n", nthreads, tasksPerPhaser);
66
67 for (int k = 2; k <= nthreads; k *= 2) {
68 for (int size = FIRST_SIZE; size <= LAST_SIZE; size *= 10) {
69 long startTime = System.nanoTime();
70
71 Runnable[] actions = new Runnable [k];
72 build(actions, size, 0, k, new Phaser());
73 Future[] futures = new Future[k];
74 for (int i = 0; i < k; ++i) {
75 futures[i] = pool.submit(actions[i]);
76 }
77 for (int i = 0; i < k; ++i) {
78 futures[i].get();
79 }
80 long elapsed = System.nanoTime() - startTime;
81 long bs = (NPS * size) / elapsed;
82 System.out.printf("%4d Threads %8d iters: %11d barriers/sec\n",
83 k, size, bs);
84 }
85 }
86 pool.shutdown();
87 }
88
89 }