This is an automated email from the ASF dual-hosted git repository. aherbert pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-numbers.git
commit ec99b95b6a1dee4aaaf0001c10d56cf444aa9c7c Author: Alex Herbert <[email protected]> AuthorDate: Fri Nov 20 22:27:44 2026 +0000 NUMBERS-220: Update the DD, Sum and Norm JMH benchmarks to compare FMA use DDPerformance: The scaled power benchmark used a single fixed x and n. Use random values in +/- [0.5, 2) with n sampled uniformly from ranges of the exponent ("lower:upper"), as for the pow benchmark. Remove BigDecimal and Dfp from the default scaled power implementations and document their limits on the exponent. Shuffle the power function data before each iteration. The (DD, exponent) cases are unchanged. With a large number of values the branch predictor cannot learn the branches of the power function over the sequence of calls. The objects are reallocated in the new order to avoid random memory access. Add a seed to the DD operator and power data, and to the Sum and Norm data (previously seeded from the current time, or unseeded), so the same data can be used between JVM runs, e.g. with -XX:+UseFMA and -XX:-UseFMA. Add property numbers.core.version (default ${project.version}) for the core dependency and its test-jar. This allows the same benchmark code to compare performance with a released version, e.g. mvn package -Pexamples-jmh -Dnumbers.core.version=1.3 -Duberjar.name=examples-jmh-1.3 SDD: Correct the rescale comment in the scaled power functions. Generated-by: Claude Code (claude-opus-5-5) --- commons-numbers-examples/examples-jmh/pom.xml | 13 ++ .../numbers/examples/jmh/core/DDPerformance.java | 191 ++++++++++++++++----- .../numbers/examples/jmh/core/NormPerformance.java | 9 +- .../commons/numbers/examples/jmh/core/SDD.java | 4 +- .../numbers/examples/jmh/core/SumPerformance.java | 14 +- 5 files changed, 182 insertions(+), 49 deletions(-) diff --git a/commons-numbers-examples/examples-jmh/pom.xml b/commons-numbers-examples/examples-jmh/pom.xml index 945225fd..4e26412a 100644 --- a/commons-numbers-examples/examples-jmh/pom.xml +++ b/commons-numbers-examples/examples-jmh/pom.xml @@ -41,14 +41,17 @@ <artifactId>commons-numbers-complex</artifactId> </dependency> + <!-- The core version can be changed to benchmark a released version (see numbers.core.version) --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-numbers-core</artifactId> + <version>${numbers.core.version}</version> </dependency> <!-- Required for DD performance benchmark --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-numbers-core</artifactId> + <version>${numbers.core.version}</version> <type>test-jar</type> <scope>compile</scope> </dependency> @@ -110,6 +113,13 @@ <!-- JMH Benchmark related properties: version, name of the benchmarking uber jar. --> <jmh.version>1.37</jmh.version> <uberjar.name>examples-jmh</uberjar.name> + <!-- Version of commons-numbers-core. This can be set to a released version to compare + performance of core with the current code using the same benchmark code, e.g. + mvn package -Pexamples-jmh -Dnumbers.core.version=1.3 -Duberjar.name=examples-jmh-1.3 + The released core (and its test-jar) is used by all modules in the uber jar. + The benchmark code must only use API available in the released version; benchmarks + of other modules that require newer core functionality may fail at runtime. --> + <numbers.core.version>${project.version}</numbers.core.version> <project.mainClass>org.openjdk.jmh.Main</project.mainClass> <!-- Disable analysis for benchmarking code. --> <pmd.skip>true</pmd.skip> @@ -242,6 +252,9 @@ java -jar target/examples-jmh.jar -h java -jar target/examples-jmh.jar ComplexPerformance -rf json -rff out.json java -jar target/examples-jmh.jar ComplexPerformance.*cos*$ -p type=cis + + Build using a released version of commons-numbers-core: + mvn package -Pexamples-jmh -Dnumbers.core.version=1.3 -Duberjar.name=examples-jmh-1.3 --> <id>examples-jmh</id> <build> diff --git a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/DDPerformance.java b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/DDPerformance.java index 8cefe66a..12301ce5 100644 --- a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/DDPerformance.java +++ b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/DDPerformance.java @@ -26,7 +26,7 @@ package org.apache.commons.numbers.examples.jmh.core; import java.math.BigDecimal; import java.math.MathContext; -import java.util.concurrent.ThreadLocalRandom; +import java.util.Arrays; import java.util.concurrent.TimeUnit; import java.util.function.BiFunction; import java.util.stream.IntStream; @@ -36,6 +36,7 @@ import org.apache.commons.numbers.core.DD; import org.apache.commons.numbers.core.DDExt; import org.apache.commons.numbers.core.DDMath; import org.apache.commons.rng.UniformRandomProvider; +import org.apache.commons.rng.sampling.ArraySampler; import org.apache.commons.rng.simple.RandomSource; import org.openjdk.jmh.annotations.Benchmark; import org.openjdk.jmh.annotations.BenchmarkMode; @@ -336,12 +337,18 @@ public class DDPerformance { /** * Contains the data to compute the double-double operations. + * + * <p>Samples are drawn using a fixed seed once per trial. This allows comparison + * of the same data between JVM runs, e.g. with different JVM options. */ @State(Scope.Benchmark) public static class OperatorData { /** The sample size. */ @Param({"1000"}) private int n; + /** The seed for the random samples. */ + @Param({"12345"}) + private long seed; /** The data. */ private DD[] data; @@ -369,20 +376,21 @@ public class DDPerformance { /** * Create the data. */ - @Setup(Level.Iteration) + @Setup public void setup() { - data = createData(n); - data2 = createData(n); + final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create(seed); + data = createData(rng, n); + data2 = createData(rng, n); } /** * Creates data where the high part is approximately uniform in the range [-1, 1). * + * @param rng Source of randomness. * @param n sample size * @return the data */ - static DD[] createData(int n) { - UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create(); + static DD[] createData(UniformRandomProvider rng, int n) { return IntStream.range(0, n) .mapToObj(i -> makeSignedDoubleDouble(rng)) .toArray(DD[]::new); @@ -504,6 +512,9 @@ public class DDPerformance { /** * Contains the data to computes the power function {@code (x, xx)^n}. + * + * <p>Samples are drawn using a fixed seed once per trial. The order of the samples + * is shuffled before each iteration (see {@link #shuffle(DDInt[], UniformRandomProvider)}). */ @State(Scope.Benchmark) public static class PowSample { @@ -518,9 +529,14 @@ public class DDPerformance { /** The upper limit on n. */ @Param({"1022"}) private int upper; + /** The seed for the random samples. */ + @Param({"12345"}) + private long seed; /** The data. */ private DDInt[] data; + /** Source of randomness to shuffle the data. */ + private UniformRandomProvider rng; /** * Gets the data. @@ -551,8 +567,32 @@ public class DDPerformance { if (!Double.isFinite(Math.pow(2, upper))) { throw new IllegalStateException("upper overflow 2^" + upper); } - UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create(); - data = IntStream.range(0, values) + data = createPowData(seed, values, lower, upper); + rng = createShuffleSource(seed); + } + + /** + * Shuffle the data. + */ + @Setup(Level.Iteration) + public void reorder() { + data = shuffle(data, rng); + } + + /** + * Creates the data for the power function {@code (x, xx)^n}. + * The value {@code (x, xx)} is in {@code +/- [0.5, 2)} and + * {@code n} is uniform in {@code [lower, upper)}. + * + * @param seed Seed for the random samples. + * @param values Number of values. + * @param lower Lower limit on n (inclusive). + * @param upper Upper limit on n (exclusive). + * @return the data + */ + static DDInt[] createPowData(long seed, int values, int lower, int upper) { + final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_128_PP.create(seed); + return IntStream.range(0, values) .mapToObj(i -> { // DD in +/- [1, 2) DD dd = makeSignedNormalDoubleDouble(rng); @@ -563,6 +603,42 @@ public class DDPerformance { return new DDInt(dd, rng.nextInt(lower, upper)); }).toArray(DDInt[]::new); } + + /** + * Creates the source of randomness to shuffle the data. This uses a different + * seed from the data so the shuffle is independent of the data generation. + * The sequence of shuffles is the same for each trial. + * + * @param seed Seed for the random samples. + * @return the source of randomness + */ + static UniformRandomProvider createShuffleSource(long seed) { + return RandomSource.XO_RO_SHI_RO_128_PP.create(~seed); + } + + /** + * Shuffle the data. The cases (DD, exponent) are the same; their order changes. + * + * <p>A random order for each iteration prevents the branch predictor learning the + * branches of the power function over the sequence of calls in the benchmark, when the + * number of values is large enough that the sequence is not learned within an iteration. + * + * <p>The objects are reallocated in the new order to avoid random memory access to + * the data. + * + * @param data Data. + * @param rng Source of randomness. + * @return the shuffled data + */ + static DDInt[] shuffle(DDInt[] data, UniformRandomProvider rng) { + ArraySampler.shuffle(rng, data); + // Copy each DD to allocate it next to its DDInt. + // DD is created by operations. Double negation is exact for all values + // (including non-finite and -0.0). + return Arrays.stream(data) + .map(d -> new DDInt(d.getX().negate().negate(), d.getN())) + .toArray(DDInt[]::new); + } } /** @@ -623,59 +699,94 @@ public class DDPerformance { * This method returns result with a separate scale and supports powers beyond the range * of a {@code double}. * - * <p>Data have been taken from the test cases in the {@code o.a.c.statistics.ext} package - * as these do not overflow BigDecimal using exponents in the range [50000000, 60000000). + * <p>The value {@code (x, xx)} is in {@code +/- [0.5, 2)} and {@code n} is uniform in + * a range {@code [lower, upper)}. The cost of the power function increases with the + * number of bits in {@code n}. Each range should cover a narrow band of bit lengths + * so the result is representative of that band. + * + * <p>The default ranges include small powers and powers used in the + * {@code o.a.c.statistics.ext} package test cases (exponents in the range + * [50000000, 60000000)). + * + * <p>Samples are drawn using a fixed seed once per trial. The order of the samples + * is shuffled before each iteration (see + * {@link PowSample#shuffle(DDInt[], UniformRandomProvider)}). */ @State(Scope.Benchmark) public static class PowScaledSample { - /** The power exponent. This should be below the limit of BigDecimal (0 through 999999999). - * Note that BigDecimal is too slow for larger powers for micro-benchmarking as run times - * can be in seconds. */ - @Param({"1000", "10000"}) - private int n; - /** The high part of the value. */ - @Param({"1.4146512942500389", - //"1.4092258370859025" - }) - private double x; + /** The number of values. */ + @Param({"500"}) + private int values; + /** The range of the power exponent: {@code lower:upper}. The lower limit is inclusive, + * the upper limit is exclusive. */ + @Param({"2:64", "512:2048", "1048576:2097152", "50000000:60000000"}) + private String range; + /** The seed for the random samples. */ + @Param({"12345"}) + private long seed; - /** The value. */ - private DD dd; + /** The data. */ + private DDInt[] data; + /** Source of randomness to shuffle the data. */ + private UniformRandomProvider rng; /** - * @return (x, xx) + * Gets the data. + * + * @return the data */ - DD getDD() { - return dd; + public DDInt[] getData() { + return data; } /** - * @return n + * Create the data. */ - int getN() { - return n; + @Setup + public void setup() { + final String[] limits = range.split(":"); + final int lower = limits.length == 2 ? Integer.parseInt(limits[0].trim()) : 0; + final int upper = limits.length == 2 ? Integer.parseInt(limits[1].trim()) : 0; + if (lower >= upper) { + throw new IllegalStateException("invalid range: " + range); + } + data = PowSample.createPowData(seed, values, lower, upper); + rng = PowSample.createShuffleSource(seed); } /** - * Create the data. + * Shuffle the data. */ @Setup(Level.Iteration) - public void setup() { - // Create a random set of round-off bits. - // The roundoff must be < 0.5 ULP of the value. - // Generate using +/- [0.25, 0.5) ULP. - final double xx = 0.25 * Math.ulp(x) * makeSignedNormalDouble(ThreadLocalRandom.current().nextLong()); - dd = DD.ofSum(x, xx); + public void reorder() { + data = PowSample.shuffle(data, rng); } } /** * Contains the scaled power operation. + * + * <p>The implementations {@code BigDecimal} and {@code Dfp} (using + * {@link MathContext#DECIMAL128} precision) are supported but not part of the default + * configuration as their limits restrict the range of the power exponent. Approximate + * limits for {@code (x, xx)} in {@code +/- [0.5, 2)} are: + * + * <ul> + * <li>{@code BigDecimal}: {@code |n| <= 999999999}; larger {@code |n|} raises an + * {@link ArithmeticException}. The result does not overflow for this range of {@code x}. + * <li>{@code Dfp}: the result overflows to infinity (or underflows to zero) when + * {@code |x^n|} is outside approximately 10<sup>+/-131072</sup>, i.e. + * {@code |n|} above approximately 435000 for {@code |x|} close to 0.5 or 2, + * and approximately 870000 for {@code |x|} close to {@code sqrt(2)} or {@code sqrt(0.5)}. + * </ul> + * + * <p>Use these implementations with a suitable range, e.g. + * {@code -pimplementation=BigDecimal,Dfp -prange=512:2048}. */ @State(Scope.Benchmark) public static class PowScaledMethod { /** The implementation of the function. */ - @Param({IMP_POW_SCALED, IMP_ACCURATE_POW_SCALED, IMP_SIMPLE_POW_SCALED, "BigDecimal", "Dfp"}) + @Param({IMP_POW_SCALED, IMP_ACCURATE_POW_SCALED, IMP_SIMPLE_POW_SCALED}) private String implementation; /** The function. */ @@ -876,14 +987,14 @@ public class DDPerformance { } /** - * Benchmark a sample of the KS function. + * Benchmark a sample of the scaled power function. * * @param method Test method. * @param data Test data. - * @return the result + * @param bh Data sink. */ @Benchmark - public Object powScaled(PowScaledMethod method, PowScaledSample data) { - return method.getFunction().apply(data.getDD(), data.getN()); + public void powScaled(PowScaledMethod method, PowScaledSample data, Blackhole bh) { + apply(method.getFunction(), data.getData(), bh); } } diff --git a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/NormPerformance.java b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/NormPerformance.java index 402f120d..e7bc4321 100644 --- a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/NormPerformance.java +++ b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/NormPerformance.java @@ -47,10 +47,17 @@ import org.openjdk.jmh.infra.Blackhole; public class NormPerformance { /** Class providing input vectors for benchmarks. + * + * <p>Samples are drawn using a fixed seed once per trial. This allows comparison + * of the same data between JVM runs, e.g. with different JVM options. */ @State(Scope.Benchmark) public static class VectorArrayInput { + /** The seed for the random samples. */ + @Param({"12345"}) + private long seed; + /** Number of vector samples. */ @Param("100000") private int samples; @@ -81,7 +88,7 @@ public class NormPerformance { */ @Setup public void createVectors() { - final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_1024_PP.create(); + final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_1024_PP.create(seed); vectors = new double[samples][]; for (int i = 0; i < vectors.length; ++i) { diff --git a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SDD.java b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SDD.java index 85e72f33..12d1c0c3 100644 --- a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SDD.java +++ b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SDD.java @@ -1293,7 +1293,7 @@ final class SDD { u = v + (f0 * b1 + f1 * b0); f0 = w + u; f1 = fastTwoSumLow(w, u, f0); - // Avoid rescale as x2 is in [1, 2) + // Avoid rescale as (b0, b1) is in [1, 2) } } @@ -1524,7 +1524,7 @@ final class SDD { f0 = norm3(s0, s1, s2, s3, f); f1 = f.hi; f2 = f.lo; - // Avoid rescale as x2 is in [1, 2) + // Avoid rescale as (b0, b1) is in [1, 2) } } diff --git a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SumPerformance.java b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SumPerformance.java index cd4c45e5..f1bb7d74 100644 --- a/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SumPerformance.java +++ b/commons-numbers-examples/examples-jmh/src/main/java/org/apache/commons/numbers/examples/jmh/core/SumPerformance.java @@ -46,17 +46,19 @@ import org.openjdk.jmh.infra.Blackhole; @State(Scope.Benchmark) @Fork(value = 1, jvmArgs = {"-server", "-Xms512M", "-Xmx512M"}) public class SumPerformance { - /** - * The seed to use to create the random benchmark input. - * Using a fixed seed ensures the same values are created across benchmarks. - */ - private static final long SEED = System.currentTimeMillis(); /** Class providing double arrays for benchmarks. + * + * <p>Samples are drawn using a fixed seed once per trial. This allows comparison + * of the same data between JVM runs, e.g. with different JVM options. */ @State(Scope.Benchmark) public static class ArrayInput { + /** The seed for the random samples. */ + @Param({"12345"}) + private long seed; + /** Number of array samples. */ @Param("100000") private int samples; @@ -100,7 +102,7 @@ public class SumPerformance { /** Create the input arrays for the instance. */ @Setup public void createArrays() { - final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_1024_PP.create(SEED); + final UniformRandomProvider rng = RandomSource.XO_RO_SHI_RO_1024_PP.create(seed); a = new double[samples][]; b = new double[samples][];
