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 jsr166y.forkjoin.*; |
8 |
import java.util.*; |
9 |
import java.util.concurrent.*; |
10 |
|
11 |
|
12 |
public class FilterDemo { |
13 |
static final int NCPU = Runtime.getRuntime().availableProcessors(); |
14 |
|
15 |
/** |
16 |
* Sequential version, for performance comparison |
17 |
*/ |
18 |
static<T> List<T> seqFilter(T[] list, |
19 |
Ops.Predicate<T> pred) { |
20 |
ArrayList<T> result = new ArrayList<T>(); |
21 |
int n = list.length; |
22 |
for (int i = 0; i < n; ++i) { |
23 |
T x = list[i]; |
24 |
if (pred.op(x)) |
25 |
result.add(x); |
26 |
} |
27 |
return result; |
28 |
} |
29 |
|
30 |
/** |
31 |
* Slow/dumb prime check |
32 |
*/ |
33 |
static final class IsPrime implements Ops.Predicate<Rand> { |
34 |
public boolean op(Rand r) { |
35 |
long n = r.seed; |
36 |
if ((n & 1) == 0) |
37 |
return false; |
38 |
int bound = (int)(Math.sqrt(n)); |
39 |
for (int i = 3; i <= bound; i += 2) |
40 |
if (n % i == 0) |
41 |
return false; |
42 |
return true; |
43 |
} |
44 |
} |
45 |
|
46 |
/** for time conversion */ |
47 |
static final long NPS = (1000L * 1000 * 1000); |
48 |
|
49 |
static final class NextRand implements Ops.Procedure<Rand> { |
50 |
public void op(Rand r) { |
51 |
r.next(); |
52 |
} |
53 |
} |
54 |
|
55 |
public static void main(String[] args) throws Exception { |
56 |
int n = 1 << 9; |
57 |
Rand[] array = new Rand[n]; |
58 |
for (int i = 0; i < n; ++i) |
59 |
array[i] = new Rand(i); |
60 |
final IsPrime pred = new IsPrime(); |
61 |
final NextRand nextRand = new NextRand(); |
62 |
long last, now; |
63 |
double elapsed; |
64 |
last = System.nanoTime(); |
65 |
List<Rand> seqResult = null; |
66 |
int resultLength = -1; |
67 |
boolean checked = false; |
68 |
for (int k = 0; k < 2; ++k) { |
69 |
List<Rand> result = seqFilter(array, pred); |
70 |
now = System.nanoTime(); |
71 |
if (resultLength < 0) { |
72 |
resultLength = result.size(); |
73 |
seqResult = result; |
74 |
} |
75 |
else if (resultLength != result.size()) |
76 |
throw new Error("wrong result size"); |
77 |
elapsed = (double)(now - last) / NPS; |
78 |
last = now; |
79 |
System.out.printf("seq: %7.3f\n", elapsed); |
80 |
// for (Rand r : array) r.next(); |
81 |
} |
82 |
int pass = 0; |
83 |
int ps = 2; |
84 |
ForkJoinPool fjp = new ForkJoinPool(ps); |
85 |
ParallelArray<Rand> pa = ParallelArray.createUsingHandoff(array, fjp); |
86 |
for (;;) { |
87 |
last = System.nanoTime(); |
88 |
for (int k = 0; k < 4; ++k) { |
89 |
List<Rand> result = pa.withFilter(pred).all().asList(); |
90 |
now = System.nanoTime(); |
91 |
if (!checked) { |
92 |
checked = true; |
93 |
if (!result.equals(seqResult)) |
94 |
throw new Error("res" + result + " seq" + seqResult); |
95 |
} |
96 |
elapsed = (double)(now - last) / NPS; |
97 |
last = now; |
98 |
System.out.printf("ps %2d: %7.3f\n", ps, elapsed); |
99 |
} |
100 |
if (pass == 0) { |
101 |
if (ps >= NCPU) |
102 |
pass = 1; |
103 |
else |
104 |
ps <<= 1; |
105 |
} |
106 |
else { |
107 |
if (ps == 1) |
108 |
break; |
109 |
else |
110 |
ps >>>= 1; |
111 |
} |
112 |
// pa.apply(nextRand); |
113 |
fjp.setPoolSize(ps); |
114 |
} |
115 |
fjp.shutdownNow(); |
116 |
fjp.awaitTermination(1, TimeUnit.SECONDS); |
117 |
} |
118 |
|
119 |
/** |
120 |
* Unsynchronized version of java.util.Random algorithm. |
121 |
*/ |
122 |
static final class Rand { |
123 |
private final static long multiplier = 0x5DEECE66DL; |
124 |
private final static long addend = 0xBL; |
125 |
private final static long mask = (1L << 48) - 1; |
126 |
private long seed; |
127 |
|
128 |
Rand(long s) { |
129 |
seed = s; |
130 |
next(); |
131 |
next(); |
132 |
} |
133 |
public int next() { |
134 |
long nextseed = (seed * multiplier + addend) & mask; |
135 |
seed = nextseed; |
136 |
return ((int)(nextseed >>> 17)) & 0x7FFFFFFF; |
137 |
} |
138 |
|
139 |
public String toString() { |
140 |
return String.valueOf(seed); |
141 |
} |
142 |
} |
143 |
|
144 |
} |