Path: blob/master/test/jdk/java/util/DoubleStreamSums/CompensatedSums.java
66644 views
/*1* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 821476126* @key randomness27* @library /test/lib28* @build jdk.test.lib.RandomFactory29* @run testng CompensatedSums30* @summary31*/3233import java.util.Random;34import java.util.function.BiConsumer;35import java.util.function.ObjDoubleConsumer;36import java.util.function.Supplier;37import java.util.stream.DoubleStream;3839import jdk.test.lib.RandomFactory;40import org.testng.Assert;41import org.testng.annotations.Test;4243public class CompensatedSums {4445@Test46public void testCompensatedSums() {47Random r = RandomFactory.getRandom();4849double naive = 0;50double jdkSequentialStreamError = 0;51double goodSequentialStreamError = 0;52double jdkParallelStreamError = 0;53double goodParallelStreamError = 0;54double badParallelStreamError = 0;5556for (int loop = 0; loop < 100; loop++) {57// sequence of random numbers of varying magnitudes, both positive and negative58double[] rand = r.doubles(1_000_000)59.map(Math::log)60.map(x -> (Double.doubleToLongBits(x) % 2 == 0) ? x : -x)61.toArray();6263// base case: standard Kahan summation64double[] sum = new double[2];65for (int i=0; i < rand.length; i++) {66sumWithCompensation(sum, rand[i]);67}6869// All error is the squared difference of the standard Kahan Sum vs JDK Stream sum implementation70// Older less accurate implementations included here as the baseline.7172// squared error of naive sum by reduction - should be large73naive += square(DoubleStream.of(rand).reduce((x, y) -> x+y).getAsDouble() - sum[0]);7475// squared error of sequential sum - should be 076jdkSequentialStreamError += square(DoubleStream.of(rand).sum() - sum[0]);7778goodSequentialStreamError += square(computeFinalSum(DoubleStream.of(rand).collect(doubleSupplier,objDoubleConsumer,goodCollectorConsumer)) - sum[0]);7980// squared error of parallel sum from the JDK81jdkParallelStreamError += square(DoubleStream.of(rand).parallel().sum() - sum[0]);8283// squared error of parallel sum84goodParallelStreamError += square(computeFinalSum(DoubleStream.of(rand).parallel().collect(doubleSupplier,objDoubleConsumer,goodCollectorConsumer)) - sum[0]);8586// the bad parallel stream87badParallelStreamError += square(computeFinalSum(DoubleStream.of(rand).parallel().collect(doubleSupplier,objDoubleConsumer,badCollectorConsumer)) - sum[0]);888990}9192Assert.assertTrue(jdkParallelStreamError <= goodParallelStreamError);93Assert.assertTrue(badParallelStreamError >= jdkParallelStreamError);9495Assert.assertTrue(goodSequentialStreamError >= jdkSequentialStreamError);96Assert.assertTrue(naive > jdkSequentialStreamError);97Assert.assertTrue(naive > jdkParallelStreamError);9899}100101private static double square(double arg) {102return arg * arg;103}104105// from OpenJDK 18 Collectors, unmodified106static double[] sumWithCompensation(double[] intermediateSum, double value) {107double tmp = value - intermediateSum[1];108double sum = intermediateSum[0];109double velvel = sum + tmp; // Little wolf of rounding error110intermediateSum[1] = (velvel - sum) - tmp;111intermediateSum[0] = velvel;112return intermediateSum;113}114115// from OpenJDK 18 Collectors, unmodified116static double computeFinalSum(double[] summands) {117// Final sum with better error bounds subtract second summand as it is negated118double tmp = summands[0] - summands[1];119double simpleSum = summands[summands.length - 1];120if (Double.isNaN(tmp) && Double.isInfinite(simpleSum))121return simpleSum;122else123return tmp;124}125126//Suppliers and consumers for Double Stream summation collection.127static Supplier<double[]> doubleSupplier = () -> new double[3];128static ObjDoubleConsumer<double[]> objDoubleConsumer = (double[] ll, double d) -> {129sumWithCompensation(ll, d);130ll[2] += d;131};132static BiConsumer<double[], double[]> badCollectorConsumer =133(ll, rr) -> {134sumWithCompensation(ll, rr[0]);135sumWithCompensation(ll, rr[1]);136ll[2] += rr[2];137};138139static BiConsumer<double[], double[]> goodCollectorConsumer =140(ll, rr) -> {141sumWithCompensation(ll, rr[0]);142sumWithCompensation(ll, -rr[1]);143ll[2] += rr[2];144};145146}147148