Vector API reproducibility bug (?)

Udo Höfel udo at hoefel.eu
Tue Aug 9 08:56:35 UTC 2022


Dear all,

I am playing around with the (beautiful!) Vector API on Java 18, and I 
get, sometimes, a deviation in behavior from the non-SIMD version. 
Consider the following code:

     public static final double[] acos(double... values) {
         Objects.requireNonNull(values);

         double[] acos = new double[values.length];
         for (int i = 0; i < values.length; i += SPECIES.length()) {
             var mask = SPECIES.indexInRange(i, values.length);
             var va = DoubleVector.fromArray(SPECIES, values, i, mask);
             va = va.lanewise(VectorOperators.ACOS);
             va.intoArray(acos, i, mask);
         }
         return acos;
     }

with SPECIES being

     private static final VectorSpecies<Double> SPECIES = 
DoubleVector.SPECIES_PREFERRED;

and the corresponding junit5 test:

     @Test
     void testAcos() {
         double[] vals = { -1, -0.5, -0.1, 0, 0.6, 0.7, 0.85, 0.99, 1 };
         double[] expected = { Math.PI, 2.0943951023931957, 
1.6709637479564565, 1.5707963267948966, 0.9272952180016123,
                 0.7953988301841436, 0.5548110329800715, 
0.1415394733244273, 0 };
         assertArrayEquals(expected, acos(vals));
     }

The expected values come from calling Math::acos explicitly on each 
element of vals. This test runs just fine if I run it alone, or together 
with the other tests in its class, but if I run the tests on the whole 
project (which unfortunately I cannot share), I get a reproducible error:

     org.opentest4j.AssertionFailedError: array contents differ at index 
[4], expected: <0.9272952180016123> but was: <0.9272952180016122>

Debugging doesn't really seem to work, if I set a breakpoint and 
evaluate acos(vals) during the debugging session both the manually 
evaluated and the test version succeed without issues. I am aware that 
most likely I am missing something, but I thought I ask if this may be a 
bug, or what my mistakes are. I was running the tests with

     openjdk version "18.0.2" 2022-07-19
     OpenJDK Runtime Environment (build 18.0.2+9-61)
     OpenJDK 64-Bit Server VM (build 18.0.2+9-61, mixed mode, sharing)

in Eclipse with

     Version: 2022-06 (4.24.0)
     Build id: 20220609-1112

Best regards,

Udo



More information about the panama-dev mailing list