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); }
Long countOccurrencesInParallel(Folder folder, String searchedWord) { return forkJoinPool.invoke(new FolderSearchTask(folder, searchedWord)); }