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