1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.numbers.examples.jmh.core;
18
19 import java.util.concurrent.TimeUnit;
20 import java.util.function.ToDoubleFunction;
21
22 import org.apache.commons.rng.UniformRandomProvider;
23 import org.apache.commons.rng.simple.RandomSource;
24 import org.openjdk.jmh.annotations.Benchmark;
25 import org.openjdk.jmh.annotations.BenchmarkMode;
26 import org.openjdk.jmh.annotations.Fork;
27 import org.openjdk.jmh.annotations.Measurement;
28 import org.openjdk.jmh.annotations.Mode;
29 import org.openjdk.jmh.annotations.OutputTimeUnit;
30 import org.openjdk.jmh.annotations.Param;
31 import org.openjdk.jmh.annotations.Scope;
32 import org.openjdk.jmh.annotations.Setup;
33 import org.openjdk.jmh.annotations.State;
34 import org.openjdk.jmh.annotations.Warmup;
35 import org.openjdk.jmh.infra.Blackhole;
36
37
38
39
40 @BenchmarkMode(Mode.AverageTime)
41 @OutputTimeUnit(TimeUnit.NANOSECONDS)
42 @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
43 @Measurement(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS)
44 @State(Scope.Benchmark)
45 @Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"})
46 public class EuclideanNormAlgorithmPerformance {
47
48
49 private static final String LOW = "low";
50
51
52 private static final String MID = "mid";
53
54
55 private static final String HIGH = "high";
56
57
58 private static final String FULL = "full";
59
60
61
62 @State(Scope.Benchmark)
63 public static class VectorArrayInput {
64
65
66 @Param("100000")
67 private int samples;
68
69
70 @Param("100")
71 private int vectorLength;
72
73
74 @Param({LOW, MID, HIGH, FULL})
75 private String type;
76
77
78 private double[][] vectors;
79
80
81
82
83 public double[][] getVectors() {
84 return vectors;
85 }
86
87
88
89 @Setup
90 public void createVectors() {
91 final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_1024_PP.create();
92
93 int minExp;
94 int maxExp;
95
96 switch (type) {
97 case LOW:
98 minExp = -530;
99 maxExp = -510;
100 break;
101 case MID:
102 minExp = -10;
103 maxExp = +10;
104 break;
105 case HIGH:
106 minExp = +510;
107 maxExp = +530;
108 break;
109 default:
110 throw new IllegalArgumentException("Invalid vector type: " + type);
111 }
112
113 vectors = new double[samples][];
114 for (int i = 0; i < vectors.length; ++i) {
115 vectors[i] = DoubleUtils.randomArray(vectorLength, minExp, maxExp, rng);
116 }
117 }
118 }
119
120
121
122
123
124
125 private static void eval(final ToDoubleFunction<double[]> fn, final VectorArrayInput input,
126 final Blackhole bh) {
127 final double[][] vectors = input.getVectors();
128 for (int i = 0; i < vectors.length; ++i) {
129 bh.consume(fn.applyAsDouble(vectors[i]));
130 }
131 }
132
133
134
135
136
137 @Benchmark
138 public void exact(final VectorArrayInput input, final Blackhole bh) {
139 eval(new EuclideanNormAlgorithms.Exact(), input, bh);
140 }
141
142
143
144
145
146 @Benchmark
147 public void direct(final VectorArrayInput input, final Blackhole bh) {
148 eval(new EuclideanNormAlgorithms.Direct(), input, bh);
149 }
150
151
152
153
154
155 @Benchmark
156 public void enorm(final VectorArrayInput input, final Blackhole bh) {
157 eval(new EuclideanNormAlgorithms.Enorm(), input, bh);
158 }
159
160
161
162
163
164 @Benchmark
165 public void enormMod(final VectorArrayInput input, final Blackhole bh) {
166 eval(new EuclideanNormAlgorithms.EnormMod(), input, bh);
167 }
168
169
170
171
172
173 @Benchmark
174 public void enormModKahan(final VectorArrayInput input, final Blackhole bh) {
175 eval(new EuclideanNormAlgorithms.EnormModKahan(), input, bh);
176 }
177
178
179
180
181
182 @Benchmark
183 public void enormModExt(final VectorArrayInput input, final Blackhole bh) {
184 eval(new EuclideanNormAlgorithms.EnormModExt(), input, bh);
185 }
186
187
188
189
190
191 @Benchmark
192 public void extLinear(final VectorArrayInput input, final Blackhole bh) {
193 eval(new EuclideanNormAlgorithms.ExtendedPrecisionLinearCombination(), input, bh);
194 }
195
196
197
198
199
200 @Benchmark
201 public void extLinearMod(final VectorArrayInput input, final Blackhole bh) {
202 eval(new EuclideanNormAlgorithms.ExtendedPrecisionLinearCombinationMod(), input, bh);
203 }
204
205
206
207
208
209
210 @Benchmark
211 public void extLinearSinglePass(final VectorArrayInput input, final Blackhole bh) {
212 eval(new EuclideanNormAlgorithms.ExtendedPrecisionLinearCombinationSinglePass(), input, bh);
213 }
214
215
216
217
218
219
220 @Benchmark
221 public void extLinearSqrt2(final VectorArrayInput input, final Blackhole bh) {
222 eval(new EuclideanNormAlgorithms.ExtendedPrecisionLinearCombinationSqrt2(), input, bh);
223 }
224 }