ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/loops/ExchangeLoops.java
Revision: 1.1
Committed: Mon May 2 19:19:38 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Put misc performance tests into CVS

File Contents

# Content
1 /*
2 * @test
3 * @summary checks to make sure a pipeline of exchangers passes data.
4 */
5 /*
6 * Written by Doug Lea with assistance from members of JCP JSR-166
7 * Expert Group and released to the public domain. Use, modify, and
8 * redistribute this code in any way without acknowledgement.
9 */
10
11 import java.util.concurrent.*;
12
13 public class ExchangeLoops {
14 static final ExecutorService pool = Executors.newCachedThreadPool();
15 static boolean print = false;
16
17 static class Int {
18 public int value;
19 Int(int i) { value = i; }
20 }
21
22
23 public static void main(String[] args) throws Exception {
24 int maxStages = 100;
25 int iters = 100000;
26
27 if (args.length > 0)
28 maxStages = Integer.parseInt(args[0]);
29
30 print = false;
31 System.out.println("Warmup...");
32 oneRun(2, 100000);
33 print = true;
34
35 for (int i = 2; i <= maxStages; i += (i+1) >>> 1) {
36 System.out.print("Threads: " + i + "\t: ");
37 oneRun(i, iters);
38 }
39 pool.shutdown();
40 }
41
42 static class Stage implements Runnable {
43 final int iters;
44 final Exchanger<Int> left;
45 final Exchanger<Int> right;
46 final CyclicBarrier barrier;
47 volatile int result;
48 Stage (Exchanger<Int> left,
49 Exchanger<Int> right,
50 CyclicBarrier b, int iters) {
51 this.left = left;
52 this.right = right;
53 barrier = b;
54 this.iters = iters;
55 }
56
57 public void run() {
58 try {
59 barrier.await();
60 Int item = new Int(hashCode());
61 for (int i = 0; i < iters; ++i) {
62 if (left != null) {
63 item.value = LoopHelpers.compute1(item.value);
64 Int other = left.exchange(item);
65 if (other == item || other == null)
66 throw new Error("Failed Exchange");
67 item = other;
68
69 }
70 if (right != null) {
71 item.value = LoopHelpers.compute2(item.value);
72 Int other = right.exchange(item);
73 if (other == item || other == null)
74 throw new Error("Failed Exchange");
75 item = other;
76 }
77 }
78 barrier.await();
79
80 }
81 catch (Exception ie) {
82 ie.printStackTrace();
83 return;
84 }
85 }
86 }
87
88 static void oneRun(int nthreads, int iters) throws Exception {
89 LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer();
90 CyclicBarrier barrier = new CyclicBarrier(nthreads + 1, timer);
91 Exchanger<Int> l = null;
92 Exchanger<Int> r = new Exchanger<Int>();
93 for (int i = 0; i < nthreads; ++i) {
94 pool.execute(new Stage(l, r, barrier, iters));
95 l = r;
96 r = (i+2 < nthreads) ? new Exchanger<Int>() : null;
97 }
98 barrier.await();
99 barrier.await();
100 long time = timer.getTime();
101 if (print)
102 System.out.println(LoopHelpers.rightJustify(time / (iters * nthreads + iters * (nthreads-2))) + " ns per transfer");
103 }
104
105 }