Пример #1
0
 public static void main(String[] args) throws Exception {
   int n = 1 << 18;
   int reps = 1 << 8;
   Rand[] array = new Rand[n];
   for (int i = 0; i < n; ++i) array[i] = new Rand(i + 1);
   ForkJoinPool fjp = new ForkJoinPool(1);
   ParallelArray<Rand> pa = ParallelArray.createUsingHandoff(array, fjp);
   final GetNext getNext = new GetNext();
   final Accum accum = new Accum();
   final Long zero = Long.valueOf(0);
   long last, now;
   double elapsed;
   for (int j = 0; j < 2; ++j) {
     long rseed = rng.nextLong();
     resetSeeds(array, rseed);
     long seqsum = 0;
     last = System.nanoTime();
     for (int k = 0; k < reps; ++k) {
       seqsum += seqMapReduce(array, getNext, accum, zero);
       Rand tmp = array[k];
       array[k] = array[n - k - 1];
       array[n - k - 1] = tmp;
     }
     now = System.nanoTime();
     elapsed = (double) (now - last) / NPS;
     last = now;
     System.out.printf("sequential:    %7.3f\n", elapsed);
     for (int i = 2; i <= NCPU; i <<= 1) {
       resetSeeds(array, rseed);
       long sum = 0;
       fjp.setPoolSize(i);
       last = System.nanoTime();
       for (int k = 0; k < reps; ++k) {
         sum += pa.withMapping(getNext).reduce(accum, zero);
         Rand tmp = array[k];
         array[k] = array[n - k - 1];
         array[n - k - 1] = tmp;
       }
       now = System.nanoTime();
       elapsed = (double) (now - last) / NPS;
       last = now;
       System.out.printf("poolSize %3d:  %7.3f\n", i, elapsed);
       if (sum != seqsum) throw new Error("checksum");
     }
     for (int i = NCPU; i >= 1; i >>>= 1) {
       resetSeeds(array, rseed);
       long sum = 0;
       fjp.setPoolSize(i);
       last = System.nanoTime();
       for (int k = 0; k < reps; ++k) {
         sum += pa.withMapping(getNext).reduce(accum, zero);
         Rand tmp = array[k];
         array[k] = array[n - k - 1];
         array[n - k - 1] = tmp;
       }
       now = System.nanoTime();
       elapsed = (double) (now - last) / NPS;
       last = now;
       System.out.printf("poolSize %3d:  %7.3f\n", i, elapsed);
       if (sum != seqsum) throw new Error("checksum");
     }
   }
   fjp.shutdownNow();
   fjp.awaitTermination(1, TimeUnit.SECONDS);
   Thread.sleep(100);
 }
 /** new list contains all elements of initializing array */
 public void testConstructor2() {
   Integer[] ints = new Integer[SIZE];
   for (int i = 0; i < SIZE - 1; ++i) ints[i] = new Integer(i);
   List a = ParallelArray.createUsingHandoff(ints, ParallelArray.defaultExecutor()).asList();
   for (int i = 0; i < SIZE; ++i) assertEquals(ints[i], a.get(i));
 }