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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import jsr166y.*; |
16 |
|
/** |
17 |
|
* Sequential version, for performance comparison |
18 |
|
*/ |
19 |
< |
static<T> List<T> seqFilter(T[] list, |
19 |
> |
static<T> List<T> seqFilter(T[] list, |
20 |
|
Ops.Predicate<T> pred) { |
21 |
|
ArrayList<T> result = new ArrayList<T>(); |
22 |
|
int n = list.length; |
56 |
|
public static void main(String[] args) throws Exception { |
57 |
|
int n = 1 << 9; |
58 |
|
Rand[] array = new Rand[n]; |
59 |
< |
for (int i = 0; i < n; ++i) |
59 |
> |
for (int i = 0; i < n; ++i) |
60 |
|
array[i] = new Rand(i); |
61 |
|
final IsPrime pred = new IsPrime(); |
62 |
|
final NextRand nextRand = new NextRand(); |
78 |
|
elapsed = (double)(now - last) / NPS; |
79 |
|
last = now; |
80 |
|
System.out.printf("seq: %7.3f\n", elapsed); |
81 |
< |
// for (Rand r : array) r.next(); |
81 |
> |
// for (Rand r : array) r.next(); |
82 |
|
} |
83 |
|
int pass = 0; |
84 |
|
int ps = 2; |
85 |
< |
ForkJoinPool fjp = new ForkJoinPool(ps); |
85 |
> |
ForkJoinPool fjp = new ForkJoinPool(); |
86 |
|
ParallelArray<Rand> pa = ParallelArray.createUsingHandoff(array, fjp); |
87 |
|
for (;;) { |
88 |
|
last = System.nanoTime(); |
99 |
|
System.out.printf("ps %2d: %7.3f\n", ps, elapsed); |
100 |
|
} |
101 |
|
if (pass == 0) { |
102 |
< |
if (ps >= NCPU) |
102 |
> |
if (ps >= NCPU) |
103 |
|
pass = 1; |
104 |
|
else |
105 |
|
ps <<= 1; |
111 |
|
ps >>>= 1; |
112 |
|
} |
113 |
|
// pa.apply(nextRand); |
114 |
< |
fjp.setParallelism(ps); |
114 |
> |
// fjp.setParallelism(ps); |
115 |
|
} |
116 |
|
fjp.shutdownNow(); |
117 |
|
fjp.awaitTermination(1, TimeUnit.SECONDS); |
121 |
|
* Unsynchronized version of java.util.Random algorithm. |
122 |
|
*/ |
123 |
|
static final class Rand { |
124 |
< |
private final static long multiplier = 0x5DEECE66DL; |
125 |
< |
private final static long addend = 0xBL; |
126 |
< |
private final static long mask = (1L << 48) - 1; |
124 |
> |
private static final long multiplier = 0x5DEECE66DL; |
125 |
> |
private static final long addend = 0xBL; |
126 |
> |
private static final long mask = (1L << 48) - 1; |
127 |
|
private long seed; |
128 |
|
|
129 |
|
Rand(long s) { |
138 |
|
} |
139 |
|
|
140 |
|
public String toString() { |
141 |
< |
return String.valueOf(seed); |
141 |
> |
return String.valueOf(seed); |
142 |
|
} |
143 |
|
} |
144 |
|
|