jpountz commented on code in PR #14896:
URL: https://github.com/apache/lucene/pull/14896#discussion_r2191970807


##########
lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java:
##########
@@ -390,6 +391,45 @@ private static int slowFindNextGEQ(int[] buffer, int 
length, int target, int fro
     return length;
   }
 
+  public void testFilterByScore() {
+    for (int iter = 0; iter < 1_000; ++iter) {
+      int padding = TestUtil.nextInt(random(), 0, 5);
+      DocAndScoreAccBuffer b1 = new DocAndScoreAccBuffer();
+      DocAndScoreAccBuffer b2 = new DocAndScoreAccBuffer();
+      b1.growNoCopy(128 + padding);
+      b2.growNoCopy(128 + padding);
+
+      int doc = 0;
+      for (int i = 0; i < 128; ++i) {
+        doc += TestUtil.nextInt(random(), 1, 1000);
+        b1.docs[i] = b2.docs[i] = doc;
+        b1.scores[i] = b2.scores[i] = random().nextDouble();
+      }
+
+      double minScoreInclusive = random().nextDouble();
+      int upTo = TestUtil.nextInt(random(), 0, 127);
+      b1.size = slowFilterByScore(b1.docs, b1.scores, minScoreInclusive, upTo);
+      b2.size = VectorUtil.filterByScore(b2.docs, b2.scores, 
minScoreInclusive, upTo);
+      assertEquals(b1.size, b2.size);
+      assertArrayEquals(b1.docs, b2.docs);
+      // two double array should be exactly the same, so the delta should be 0
+      assertArrayEquals(b1.scores, b2.scores, 0);

Review Comment:
   I suspect that these checks will fail if values after `size` are not zeros, 
we should only compare arrays up to index `size`?



##########
lucene/core/src/test/org/apache/lucene/util/TestVectorUtil.java:
##########
@@ -390,6 +391,45 @@ private static int slowFindNextGEQ(int[] buffer, int 
length, int target, int fro
     return length;
   }
 
+  public void testFilterByScore() {
+    for (int iter = 0; iter < 1_000; ++iter) {
+      int padding = TestUtil.nextInt(random(), 0, 5);
+      DocAndScoreAccBuffer b1 = new DocAndScoreAccBuffer();
+      DocAndScoreAccBuffer b2 = new DocAndScoreAccBuffer();
+      b1.growNoCopy(128 + padding);
+      b2.growNoCopy(128 + padding);
+
+      int doc = 0;
+      for (int i = 0; i < 128; ++i) {

Review Comment:
   Maybe add data up to 128+padding to make sure that having non-zero values 
after `size` doesn't affect correctness?



##########
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/CompetitiveBenchmark.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.lucene.benchmark.jmh;
+
+import java.util.Arrays;
+import java.util.SplittableRandom;
+import java.util.concurrent.TimeUnit;
+import java.util.function.IntSupplier;
+import org.apache.lucene.util.ArrayUtil;
+import org.apache.lucene.util.VectorUtil;
+import org.openjdk.jmh.annotations.Benchmark;
+import org.openjdk.jmh.annotations.BenchmarkMode;
+import org.openjdk.jmh.annotations.Fork;
+import org.openjdk.jmh.annotations.Level;
+import org.openjdk.jmh.annotations.Measurement;
+import org.openjdk.jmh.annotations.Mode;
+import org.openjdk.jmh.annotations.OutputTimeUnit;
+import org.openjdk.jmh.annotations.Param;
+import org.openjdk.jmh.annotations.Scope;
+import org.openjdk.jmh.annotations.Setup;
+import org.openjdk.jmh.annotations.State;
+import org.openjdk.jmh.annotations.Warmup;
+
+@BenchmarkMode(Mode.Throughput)
+@OutputTimeUnit(TimeUnit.MILLISECONDS)
+@State(Scope.Benchmark)
+@Warmup(iterations = 3, time = 1)
+@Measurement(iterations = 5, time = 1)
+@Fork(
+    value = 1,
+    jvmArgsAppend = {
+      "-Xmx1g",
+      "-Xms1g",
+      "-XX:+AlwaysPreTouch",
+      "--add-modules",
+      "jdk.incubator.vector"
+    })
+public class CompetitiveBenchmark {
+
+  private final SplittableRandom R = new SplittableRandom(0);
+
+  @Param("128")
+  int size;
+
+  double[] scores;
+  int[] docs;
+
+  // scores generated by nextDouble() locate in range [0, 1), so we can tune 
this parameter and
+  // see how the performance changes depends on how selective the filter is.
+  @Param({"0", "0.2", "0.4", "0.5", "0.8"})
+  double minScoreInclusive;
+
+  @Setup(Level.Trial)
+  public void setUpTrial() {
+    scores = new double[size];
+    docs = new int[size];
+  }
+
+  @Setup(Level.Invocation)
+  public void setUpInvocation() {
+    for (int i = 0; i < size; i++) {
+      docs[i] = R.nextInt(Integer.MAX_VALUE);
+      scores[i] = R.nextDouble();
+    }
+  }
+
+  @Benchmark
+  public int baseline() {
+    int newSize = 0;
+    for (int i = 0; i < size; ++i) {
+      if (scores[i] >= minScoreInclusive) {
+        docs[newSize] = docs[i];
+        scores[newSize] = scores[i];
+        newSize++;
+      }
+    }
+    return newSize;
+  }
+
+  @Benchmark
+  public int branchlessCandidate() {
+    int newSize = 0;
+    for (int i = 0; i < size; ++i) {
+      int inc = scores[i] >= minScoreInclusive ? 1 : 0;
+      docs[newSize] = docs[i];
+      scores[newSize] = scores[i];
+      newSize += inc;
+    }
+    return newSize;
+  }
+
+  // This is an effort try to make the modification of newSize using cmov
+  // see https://github.com/apache/lucene/pull/14906
+  @Benchmark
+  public int branchlessCandidateCmov() {
+    int newSize = 0;
+    for (int i = 0; i < size; ++i) {
+      int doc = docs[i];
+      double score = scores[i];
+      docs[newSize] = doc;
+      scores[newSize] = score;
+      if (score >= minScoreInclusive) {
+        newSize += 1;
+      }
+    }
+    return newSize;
+  }
+
+  @Benchmark
+  public int vectorizedCandidate() {
+    return VectorUtil.filterByScore(docs, scores, minScoreInclusive, size);
+  }
+
+  public static void main(String[] args) {
+    CompetitiveBenchmark baseline = new CompetitiveBenchmark();
+    baseline.size = 128;
+    baseline.setUpTrial();
+    baseline.setUpInvocation();
+    int baselineSize = baseline.baseline();
+
+    CompetitiveBenchmark candidate = new CompetitiveBenchmark();
+    candidate.size = 128;
+    candidate.setUpTrial();
+    candidate.setUpInvocation();
+
+    for (IntSupplier s :
+        new IntSupplier[] {candidate::branchlessCandidate, 
candidate::vectorizedCandidate}) {

Review Comment:
   Let's includebranchlessCandidateCmov here as well?



##########
lucene/core/src/java24/org/apache/lucene/internal/vectorization/PanamaVectorUtilSupport.java:
##########
@@ -1001,4 +1007,34 @@ public float recalculateScalarQuantizationOffset(
 
     return correction;
   }
+
+  // Experiments suggest that we need at least 4 lanes (double)
+  // so that the overhead of going with the vector approach, compress values
+  // and counting trues on vector masks pays off.
+  private static final boolean ENABLE_FILTER_BY_SCORE_VECTOR_OPTO = 
DOUBLE_SPECIES.length() >= 4;
+
+  @Override
+  public int filterByScore(
+      int[] docBuffer, double[] scoreBuffer, double minScoreInclusive, int 
upTo) {
+    int newUpto = 0;
+    int i = 0;
+    if (ENABLE_FILTER_BY_SCORE_VECTOR_OPTO) {
+      for (int bound = DOUBLE_SPECIES.loopBound(upTo); i < bound; i += 
DOUBLE_SPECIES.length()) {
+        DoubleVector scoreVector = DoubleVector.fromArray(DOUBLE_SPECIES, 
scoreBuffer, i);
+        IntVector docVector = IntVector.fromArray(INT_FOR_DOUBLE_SPECIES, 
docBuffer, i);
+        VectorMask<Double> mask = scoreVector.compare(VectorOperators.GE, 
minScoreInclusive);
+        scoreVector.compress(mask).intoArray(scoreBuffer, newUpto);
+        
docVector.compress(mask.cast(INT_FOR_DOUBLE_SPECIES)).intoArray(docBuffer, 
newUpto);
+        newUpto += mask.trueCount();
+      }
+    }
+
+    for (; i < upTo; ++i) {
+      int inc = scoreBuffer[i] >= minScoreInclusive ? 1 : 0;
+      docBuffer[newUpto] = docBuffer[i];
+      scoreBuffer[newUpto] = scoreBuffer[i];
+      newUpto += inc;

Review Comment:
   Let's use the approach that compiles to a cmov instruction here?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to