Apache Lucene open-source search software
fix Panama provider to only have static final constants, but allow sysprops for testing.
Robert Muir94619e00e060
diff --git a/gradle/testing/randomization.gradle b/gradle/testing/randomization.gradleindex 4ad2dcc8727..30aad60e9e0 100644--- a/gradle/testing/randomization.gradle+++ b/gradle/testing/randomization.gradle@@ -104,6 +104,13 @@ allprojects {// miscellaneous; some of them very weird.[propName: 'tests.LUCENE_VERSION', value: baseVersion, description: "Base Lucene version."],[propName: 'tests.bwcdir', value: null, description: "Data for backward-compatibility indexes."],+ // vectorization related+ [propName: 'tests.vectorsize',+ value: { ->+ RandomPicks.randomFrom(new Random(projectSeedLong), ["128", "256", "512"])+ },+ description: "Sets preferred vector size in bits."],+ [propName: 'tests.forceintegervectors', value: "true", description: "Forces use of integer vectors even when slow."],]}}diff --git a/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.java b/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.javaindex ec7941077c9..3192af72b2c 100644--- a/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.java+++ b/lucene/core/src/java/org/apache/lucene/internal/vectorization/VectorizationProvider.java@@ -98,10 +98,9 @@ public abstract class VectorizationProvider {final var cls =lookup.findClass("org.apache.lucene.internal.vectorization.PanamaVectorizationProvider");- final var constr =- lookup.findConstructor(cls, MethodType.methodType(void.class, boolean.class));+ final var constr = lookup.findConstructor(cls, MethodType.methodType(void.class));try {- return (VectorizationProvider) constr.invoke(testMode);+ return (VectorizationProvider) constr.invoke();} catch (UnsupportedOperationException uoe) {// not supported because preferred vector size too small or similarLOG.warning("Java vector incubator API was not enabled. " + uoe.getMessage());diff --git a/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java b/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.javaindex 9129259c9f9..75c775d7286 100644--- a/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java+++ b/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java@@ -28,35 +28,65 @@ import jdk.incubator.vector.ShortVector;import jdk.incubator.vector.Vector;import jdk.incubator.vector.VectorShape;import jdk.incubator.vector.VectorSpecies;+import org.apache.lucene.util.Constants;+/**+ * VectorUtil methods implemented with Panama incubating vector API.+ *+ * <p>Supports two system properties for correctness testing purposes only:+ * <li>+ *+ * <ul>+ * tests.vectorsize (int)+ * </ul>+ *+ * <ul>+ * tests.forceintegervectors (boolean)+ * </ul>+ *+ * Setting these properties will make this code run EXTREMELY slow!+ */final class PanamaVectorUtilSupport implements VectorUtilSupport {- // we always use the platform's maximum floating point/int vector size- private static final VectorSpecies<Float> FLOAT_SPECIES = FloatVector.SPECIES_PREFERRED;- private static final VectorSpecies<Integer> INT_SPECIES = IntVector.SPECIES_PREFERRED;-- // for integer methods, it is more complicated due to conversions- private static final int INT_SPECIES_VSIZE = INT_SPECIES.vectorBitSize();+ // preferred vector sizes, which can be altered for testing+ private static final VectorSpecies<Float> FLOAT_SPECIES;+ private static final VectorSpecies<Integer> INT_SPECIES;private static final VectorSpecies<Byte> BYTE_SPECIES;private static final VectorSpecies<Short> SHORT_SPECIES;- // compute BYTE/SHORT sizes relative to preferred integer vector size+ static final int VECTOR_BITSIZE;+ static final boolean HAS_FAST_INTEGER_VECTORS;+static {- if (INT_SPECIES_VSIZE >= 256) {- BYTE_SPECIES =- ByteVector.SPECIES_MAX.withShape(VectorShape.forBitSize(INT_SPECIES_VSIZE >> 2));+ // default to platform supported bitsize+ int vectorBitSize = VectorShape.preferredShape().vectorBitSize();+ // but allow easy overriding for testing+ try {+ vectorBitSize = Integer.getInteger("tests.vectorsize", vectorBitSize);+ } catch (SecurityException ignored) {+ }+ INT_SPECIES = VectorSpecies.of(int.class, VectorShape.forBitSize(vectorBitSize));+ VECTOR_BITSIZE = INT_SPECIES.vectorBitSize();+ FLOAT_SPECIES = INT_SPECIES.withLanes(float.class);+ // compute BYTE/SHORT sizes relative to preferred integer vector size+ if (VECTOR_BITSIZE >= 256) {+ BYTE_SPECIES = ByteVector.SPECIES_MAX.withShape(VectorShape.forBitSize(VECTOR_BITSIZE >> 2));SHORT_SPECIES =- ShortVector.SPECIES_MAX.withShape(VectorShape.forBitSize(INT_SPECIES_VSIZE >> 1));+ ShortVector.SPECIES_MAX.withShape(VectorShape.forBitSize(VECTOR_BITSIZE >> 1));} else {BYTE_SPECIES = null;SHORT_SPECIES = null;}- }-- private final boolean useIntegerVectors;-- PanamaVectorUtilSupport(boolean useIntegerVectors) {- this.useIntegerVectors = useIntegerVectors;+ // hotspot misses some SSE intrinsics, workaround it+ // to be fair, they do document this thing only works well with AVX2/AVX3 and Neon+ boolean isAMD64withoutAVX2 = Constants.OS_ARCH.equals("amd64") && VECTOR_BITSIZE < 256;+ boolean hasFastIntegerVectors = isAMD64withoutAVX2 == false;+ try {+ hasFastIntegerVectors =+ Boolean.parseBoolean(System.getProperty("tests.forceintegervectors", Boolean.toString(hasFastIntegerVectors)));+ } catch (SecurityException ignored) {+ }+ HAS_FAST_INTEGER_VECTORS = hasFastIntegerVectors;}@Override@@ -302,12 +332,12 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {// only vectorize if we'll at least enter the loop a single time, and we have at least 128-bit// vectors (256-bit on intel to dodge performance landmines)- if (a.length >= 16 && useIntegerVectors) {+ if (a.length >= 16 && HAS_FAST_INTEGER_VECTORS) {// compute vectorized dot product consistent with VPDPBUSD instruction- if (INT_SPECIES_VSIZE >= 512) {+ if (VECTOR_BITSIZE >= 512) {i += BYTE_SPECIES.loopBound(a.length);res += dotProductBody512(a, b, i);- } else if (INT_SPECIES_VSIZE == 256) {+ } else if (VECTOR_BITSIZE == 256) {i += BYTE_SPECIES.loopBound(a.length);res += dotProductBody256(a, b, i);} else {@@ -390,12 +420,12 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {// only vectorize if we'll at least enter the loop a single time, and we have at least 128-bit// vectors (256-bit on intel to dodge performance landmines)- if (a.length >= 16 && useIntegerVectors) {+ if (a.length >= 16 && HAS_FAST_INTEGER_VECTORS) {final float[] ret;- if (INT_SPECIES_VSIZE >= 512) {+ if (VECTOR_BITSIZE >= 512) {i += BYTE_SPECIES.loopBound(a.length);ret = cosineBody512(a, b, i);- } else if (INT_SPECIES_VSIZE == 256) {+ } else if (VECTOR_BITSIZE == 256) {i += BYTE_SPECIES.loopBound(a.length);ret = cosineBody256(a, b, i);} else {@@ -508,8 +538,8 @@ final class PanamaVectorUtilSupport implements VectorUtilSupport {// only vectorize if we'll at least enter the loop a single time, and we have at least 128-bit// vectors (256-bit on intel to dodge performance landmines)- if (a.length >= 16 && useIntegerVectors) {- if (INT_SPECIES_VSIZE >= 256) {+ if (a.length >= 16 && HAS_FAST_INTEGER_VECTORS) {+ if (VECTOR_BITSIZE >= 256) {i += BYTE_SPECIES.loopBound(a.length);res += squareDistanceBody256(a, b, i);} else {diff --git a/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java b/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.javaindex 4292d62b7fb..b47bf802097 100644--- a/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java+++ b/lucene/core/src/java20/org/apache/lucene/internal/vectorization/PanamaVectorizationProvider.java@@ -21,8 +21,6 @@ import java.security.PrivilegedAction;import java.util.Locale;import java.util.logging.Logger;import jdk.incubator.vector.FloatVector;-import jdk.incubator.vector.IntVector;-import org.apache.lucene.util.Constants;import org.apache.lucene.util.SuppressForbidden;/** A vectorization provider that leverages the Panama Vector API. */@@ -30,14 +28,6 @@ final class PanamaVectorizationProvider extends VectorizationProvider {private final VectorUtilSupport vectorUtilSupport;- /**- * x86 and less than 256-bit vectors.- *- * <p>it could be that it has only AVX1 and integer vectors are fast. it could also be that it has- * no AVX and integer vectors are extremely slow. don't use integer vectors to avoid landmines.- */- private final boolean hasFastIntegerVectors;-// Extracted to a method to be able to apply the SuppressForbidden annotation@SuppressWarnings("removal")@SuppressForbidden(reason = "security manager")@@ -45,13 +35,7 @@ final class PanamaVectorizationProvider extends VectorizationProvider {return AccessController.doPrivileged(action);}- PanamaVectorizationProvider(boolean testMode) {- final int intPreferredBitSize = IntVector.SPECIES_PREFERRED.vectorBitSize();- if (!testMode && intPreferredBitSize < 128) {- throw new UnsupportedOperationException(- "Vector bit size is less than 128: " + intPreferredBitSize);- }-+ PanamaVectorizationProvider() {// hack to work around for JDK-8309727:try {doPrivileged(@@ -65,20 +49,22 @@ final class PanamaVectorizationProvider extends VectorizationProvider {"We hit initialization failure described in JDK-8309727: " + se);}- // check if the system is x86 and less than 256-bit vectors:- var isAMD64withoutAVX2 = Constants.OS_ARCH.equals("amd64") && intPreferredBitSize < 256;- this.hasFastIntegerVectors = testMode || false == isAMD64withoutAVX2;+ if (PanamaVectorUtilSupport.VECTOR_BITSIZE < 128) {+ throw new UnsupportedOperationException(+ "Vector bit size is less than 128: " + PanamaVectorUtilSupport.VECTOR_BITSIZE);+ }- this.vectorUtilSupport = new PanamaVectorUtilSupport(hasFastIntegerVectors);+ this.vectorUtilSupport = new PanamaVectorUtilSupport();var log = Logger.getLogger(getClass().getName());log.info(String.format(Locale.ENGLISH,- "Java vector incubator API enabled%s; uses preferredBitSize=%d%s",- testMode ? " (test mode)" : "",- intPreferredBitSize,- hasFastIntegerVectors ? "" : "; floating-point vectors only"));+ "Java vector incubator API enabled; uses preferredBitSize=%d%s",+ PanamaVectorUtilSupport.VECTOR_BITSIZE,+ PanamaVectorUtilSupport.HAS_FAST_INTEGER_VECTORS+ ? ""+ : "; floating-point vectors only"));}@Override