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. |
2 |
> |
* Written by Bill Scherer and Doug Lea with assistance from members |
3 |
> |
* of JCP JSR-166 Expert Group and released to the public domain, as |
4 |
> |
* explained at http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import java.util.concurrent.*; |
8 |
+ |
import java.util.concurrent.atomic.*; |
9 |
+ |
import java.util.concurrent.locks.*; |
10 |
|
|
11 |
|
public class ExchangeLoops { |
12 |
< |
static final ExecutorService pool = Executors.newCachedThreadPool(); |
15 |
< |
static boolean print = false; |
12 |
> |
static final int NCPUS = Runtime.getRuntime().availableProcessors(); |
13 |
|
|
14 |
< |
static class Int { |
15 |
< |
public int value; |
19 |
< |
Int(int i) { value = i; } |
20 |
< |
} |
21 |
< |
|
14 |
> |
static final int DEFAULT_THREADS = NCPUS + 2; |
15 |
> |
static final long DEFAULT_TRIAL_MILLIS = 10000; |
16 |
|
|
17 |
|
public static void main(String[] args) throws Exception { |
18 |
< |
int maxStages = 100; |
19 |
< |
int iters = 100000; |
18 |
> |
int maxThreads = DEFAULT_THREADS; |
19 |
> |
long trialMillis = DEFAULT_TRIAL_MILLIS; |
20 |
> |
int nReps = 3; |
21 |
> |
|
22 |
> |
// Parse and check args |
23 |
> |
int argc = 0; |
24 |
> |
while (argc < args.length) { |
25 |
> |
String option = args[argc++]; |
26 |
> |
if (option.equals("-t")) |
27 |
> |
trialMillis = Integer.parseInt(args[argc]); |
28 |
> |
else if (option.equals("-r")) |
29 |
> |
nReps = Integer.parseInt(args[argc]); |
30 |
> |
else |
31 |
> |
maxThreads = Integer.parseInt(option); |
32 |
> |
argc++; |
33 |
> |
} |
34 |
|
|
35 |
< |
if (args.length > 0) |
36 |
< |
maxStages = Integer.parseInt(args[0]); |
35 |
> |
// Display runtime parameters |
36 |
> |
System.out.print("ExchangeTest"); |
37 |
> |
System.out.print(" -t " + trialMillis); |
38 |
> |
System.out.print(" -r " + nReps); |
39 |
> |
System.out.print(" max threads " + maxThreads); |
40 |
> |
System.out.println(); |
41 |
> |
long warmupTime = 2000; |
42 |
> |
long sleepTime = 100; |
43 |
> |
int nw = (maxThreads >= 3) ? 3 : 2; |
44 |
> |
|
45 |
> |
System.out.println("Warmups.."); |
46 |
> |
oneRun(3, warmupTime); |
47 |
> |
Thread.sleep(sleepTime); |
48 |
> |
|
49 |
> |
for (int i = maxThreads; i >= 2; i -= 1) { |
50 |
> |
oneRun(i, warmupTime++); |
51 |
> |
// System.gc(); |
52 |
> |
Thread.sleep(sleepTime); |
53 |
> |
} |
54 |
|
|
55 |
< |
print = false; |
56 |
< |
System.out.println("Warmup..."); |
57 |
< |
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 |
> |
for (int i = maxThreads; i >= 2; i -= 1) { |
57 |
> |
oneRun(i, warmupTime++); |
58 |
|
} |
59 |
+ |
*/ |
60 |
|
|
61 |
< |
public void run() { |
62 |
< |
try { |
63 |
< |
barrier.await(); |
64 |
< |
Int item = new Int(hashCode()); |
65 |
< |
for (int i = 0; i < iters; ++i) { |
66 |
< |
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 |
< |
|
61 |
> |
for (int j = 0; j < nReps; ++j) { |
62 |
> |
System.out.println("Trial: " + j); |
63 |
> |
for (int i = 2; i <= maxThreads; i += 2) { |
64 |
> |
oneRun(i, trialMillis); |
65 |
> |
// System.gc(); |
66 |
> |
Thread.sleep(sleepTime); |
67 |
|
} |
68 |
< |
catch (Exception ie) { |
69 |
< |
ie.printStackTrace(); |
70 |
< |
return; |
68 |
> |
for (int i = maxThreads; i >= 2; i -= 2) { |
69 |
> |
oneRun(i, trialMillis); |
70 |
> |
// System.gc(); |
71 |
> |
Thread.sleep(sleepTime); |
72 |
|
} |
73 |
+ |
Thread.sleep(sleepTime); |
74 |
|
} |
75 |
+ |
|
76 |
|
} |
77 |
|
|
78 |
< |
static void oneRun(int nthreads, int iters) throws Exception { |
79 |
< |
LoopHelpers.BarrierTimer timer = new LoopHelpers.BarrierTimer(); |
80 |
< |
CyclicBarrier barrier = new CyclicBarrier(nthreads + 1, timer); |
81 |
< |
Exchanger<Int> l = null; |
82 |
< |
Exchanger<Int> r = new Exchanger<Int>(); |
83 |
< |
for (int i = 0; i < nthreads; ++i) { |
84 |
< |
pool.execute(new Stage(l, r, barrier, iters)); |
85 |
< |
l = r; |
86 |
< |
r = (i+2 < nthreads) ? new Exchanger<Int>() : null; |
87 |
< |
} |
88 |
< |
barrier.await(); |
89 |
< |
barrier.await(); |
90 |
< |
long time = timer.getTime(); |
91 |
< |
if (print) |
92 |
< |
System.out.println(LoopHelpers.rightJustify(time / (iters * nthreads + iters * (nthreads-2))) + " ns per transfer"); |
78 |
> |
static void oneRun(int nThreads, long trialMillis) throws Exception { |
79 |
> |
System.out.printf("%4d threads", nThreads); |
80 |
> |
Exchanger x = new Exchanger(); |
81 |
> |
Runner[] runners = new Runner[nThreads]; |
82 |
> |
Thread[] threads = new Thread[nThreads]; |
83 |
> |
for (int i = 0; i < nThreads; ++i) { |
84 |
> |
runners[i] = new Runner(x); |
85 |
> |
threads[i] = new Thread(runners[i]); |
86 |
> |
// int h = System.identityHashCode(threads[i]); |
87 |
> |
// h ^= h << 1; |
88 |
> |
// h ^= h >>> 3; |
89 |
> |
// h ^= h << 10; |
90 |
> |
// System.out.printf("%10x\n", h); |
91 |
> |
} |
92 |
> |
|
93 |
> |
long startTime = System.nanoTime(); |
94 |
> |
for (int i = 0; i < nThreads; ++i) { |
95 |
> |
threads[i].start(); |
96 |
> |
} |
97 |
> |
Thread.sleep(trialMillis); |
98 |
> |
for (int i = 0; i < nThreads; ++i) |
99 |
> |
threads[i].interrupt(); |
100 |
> |
long elapsed = System.nanoTime() - startTime; |
101 |
> |
for (int i = 0; i < nThreads; ++i) |
102 |
> |
threads[i].join(); |
103 |
> |
int iters = 1; |
104 |
> |
// System.out.println(); |
105 |
> |
for (int i = 0; i < nThreads; ++i) { |
106 |
> |
int ipr = runners[i].iters; |
107 |
> |
// System.out.println(ipr); |
108 |
> |
iters += ipr; |
109 |
> |
} |
110 |
> |
long rate = iters * 1000L * 1000L * 1000L / elapsed; |
111 |
> |
long npt = elapsed / iters; |
112 |
> |
System.out.printf("%9dms", elapsed / (1000L * 1000L)); |
113 |
> |
System.out.printf("%9d it/s ", rate); |
114 |
> |
System.out.printf("%9d ns/it", npt); |
115 |
> |
System.out.println(); |
116 |
> |
// x.printStats(); |
117 |
|
} |
118 |
|
|
119 |
+ |
static final class Runner implements Runnable { |
120 |
+ |
final Exchanger exchanger; |
121 |
+ |
final Object mine = new Integer(2688); |
122 |
+ |
volatile int iters; |
123 |
+ |
Runner(Exchanger x) { this.exchanger = x; } |
124 |
+ |
|
125 |
+ |
public void run() { |
126 |
+ |
Exchanger x = exchanger; |
127 |
+ |
Object m = mine; |
128 |
+ |
int i = 0; |
129 |
+ |
try { |
130 |
+ |
for (;;) { |
131 |
+ |
Object e = x.exchange(m); |
132 |
+ |
if (e == null || e == m) |
133 |
+ |
throw new Error(); |
134 |
+ |
m = e; |
135 |
+ |
++i; |
136 |
+ |
} |
137 |
+ |
} catch (InterruptedException ie) { |
138 |
+ |
iters = i; |
139 |
+ |
} |
140 |
+ |
} |
141 |
+ |
} |
142 |
|
} |