1 /* 2 * Copyright (c) 2014 Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 package org.openjdk.bench.java.util.stream.tasks.PrimesSieve; 26 27 import org.openjdk.jmh.annotations.Benchmark; 28 import org.openjdk.jmh.annotations.BenchmarkMode; 29 import org.openjdk.jmh.annotations.Level; 30 import org.openjdk.jmh.annotations.Mode; 31 import org.openjdk.jmh.annotations.OutputTimeUnit; 32 import org.openjdk.jmh.annotations.Scope; 33 import org.openjdk.jmh.annotations.Setup; 34 import org.openjdk.jmh.annotations.State; 35 36 import java.util.Arrays; 37 import java.util.concurrent.RecursiveTask; 38 import java.util.concurrent.TimeUnit; 39 import java.util.function.BinaryOperator; 40 import java.util.function.Predicate; 41 42 /** 43 * Bulk scenario: filter out candidate primes. 44 * 45 * This test covers bulk infrastructure only. Refer to other tests for lambda-specific cases. 46 */ 47 @BenchmarkMode(Mode.Throughput) 48 @OutputTimeUnit(TimeUnit.SECONDS) 49 @State(Scope.Benchmark) 50 public class Bulk { 51 52 private PrimesSieveProblem problem; 53 54 @Setup(Level.Trial) 55 public void populateData(){ 56 problem = new PrimesSieveProblem(); 57 } 58 59 @Benchmark 60 public int hm_seq() { 61 int max = Integer.MIN_VALUE; 62 for (int d : problem.get()) { 63 if (PrimesSieveProblem.isNotDivisible(d, 2) 64 && PrimesSieveProblem.isNotDivisible(d, 3) 65 && PrimesSieveProblem.isNotDivisible(d, 5) 66 && PrimesSieveProblem.isNotDivisible(d, 7) 67 && PrimesSieveProblem.isNotDivisible(d, 11) 68 && PrimesSieveProblem.isNotDivisible(d, 13) 69 && PrimesSieveProblem.isNotDivisible(d, 17) 70 && PrimesSieveProblem.isNotDivisible(d, 19) 71 ) { 72 if (d > max) { 73 max = d; 74 } 75 } 76 } 77 return max; 78 } 79 80 @Benchmark 81 public int hm_par() { 82 return new FilterTask(problem.get()).invoke(); 83 } 84 85 @Benchmark 86 public int bulk_seq_inner() { 87 return Arrays.stream(problem.get()) 88 .filter(new FilterOp(2)) 89 .filter(new FilterOp(3)) 90 .filter(new FilterOp(5)) 91 .filter(new FilterOp(7)) 92 .filter(new FilterOp(11)) 93 .filter(new FilterOp(13)) 94 .filter(new FilterOp(17)) 95 .filter(new FilterOp(19)) 96 .reduce(Integer.MIN_VALUE, new ReduceOp()); 97 } 98 99 @Benchmark 100 public int bulk_par_inner() { 101 return Arrays.stream(problem.get()).parallel() 102 .filter(new FilterOp(2)) 103 .filter(new FilterOp(3)) 104 .filter(new FilterOp(5)) 105 .filter(new FilterOp(7)) 106 .filter(new FilterOp(11)) 107 .filter(new FilterOp(13)) 108 .filter(new FilterOp(17)) 109 .filter(new FilterOp(19)) 110 .reduce(Integer.MIN_VALUE, new ReduceOp()); 111 } 112 113 static class FilterOp implements Predicate<Integer> { 114 private final int divisor; 115 116 public FilterOp(int divisor) { 117 this.divisor = divisor; 118 } 119 120 @Override 121 public boolean test(Integer value) { 122 return PrimesSieveProblem.isNotDivisible(value, divisor); 123 } 124 } 125 126 static class ReduceOp implements BinaryOperator<Integer> { 127 @Override 128 public Integer apply(Integer left, Integer right) { 129 return (left > right) ? left : right; 130 } 131 } 132 133 static class FilterTask extends RecursiveTask<Integer> { 134 private static final int FORK_LIMIT = 1000; 135 final Integer[] data; 136 final int start, end; 137 138 FilterTask(Integer[] data) { 139 this(data, 0, data.length); 140 } 141 142 FilterTask(Integer[] data, int start, int end) { 143 this.data = data; 144 this.start = start; 145 this.end = end; 146 } 147 148 @Override 149 protected Integer compute() { 150 int size = end - start; 151 if (size > FORK_LIMIT) { 152 int mid = start + size / 2; 153 FilterTask t1 = new FilterTask(data, start, mid); 154 FilterTask t2 = new FilterTask(data, mid, end); 155 t1.fork(); 156 Integer r1 = t2.invoke(); 157 Integer r2 = t1.join(); 158 return (r1 > r2) ? r1 : r2; 159 } else { 160 int max = Integer.MIN_VALUE; 161 for (int i = start; i < end; i++) { 162 int d = data[i]; 163 if (PrimesSieveProblem.isNotDivisible(d, 2) 164 && PrimesSieveProblem.isNotDivisible(d, 3) 165 && PrimesSieveProblem.isNotDivisible(d, 5) 166 && PrimesSieveProblem.isNotDivisible(d, 7) 167 && PrimesSieveProblem.isNotDivisible(d, 11) 168 && PrimesSieveProblem.isNotDivisible(d, 13) 169 && PrimesSieveProblem.isNotDivisible(d, 17) 170 && PrimesSieveProblem.isNotDivisible(d, 19) 171 ) { 172 if (d > max) { 173 max = d; 174 } 175 } 176 } 177 return max; 178 } 179 } 180 } 181 182 }