ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/extra166y/FilterDemo.java
Revision: 1.1
Committed: Sun Nov 1 22:00:35 2009 UTC (14 years, 5 months ago) by dl
Branch: MAIN
Log Message:
Move tests for extra166y

File Contents

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