-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Vectorize filterCompetitiveHits
#14896
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
HUSTERGS
wants to merge
22
commits into
apache:main
Choose a base branch
from
HUSTERGS:vectorize_filter_competitive_score
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
1a50e9d
init
4ae9348
use VectorUtil instead & add javadoc & tidy
5cc84ec
fix java doc
9f20b04
add test
d1019d5
fix comment & add jmh
0d2a3d5
add license for jmh & add changelog
6299bed
fix comment
b57f459
Add non-vectorized branchless candidate.
jpountz 442fbf0
Random -> SplittableRandom.
jpountz e20bf7a
Generate data in setupTrial instead of setupInvocation.
jpountz fb3021b
adjust comment
8487bcc
Revert "Generate data in setupTrial instead of setupInvocation."
jpountz 9860ee4
add flag to enable vectorized filterByScore according to lanes
b2bfa5c
merge main
9d87b53
add cmov benchmark candidate
1e1a0b8
let default vector util support use cmov
39532ed
resolve comment
f7bbc86
add check for HAS_FAST_COMPRESS
f258908
resolve comment
9e3372f
merge main
cbc47f1
fix forbidden-api check
16af5d7
resolve comment
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
169 changes: 169 additions & 0 deletions
169
lucene/benchmark-jmh/src/java/org/apache/lucene/benchmark/jmh/CompetitiveBenchmark.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
/* | ||
* 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(); | ||
} | ||
HUSTERGS marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
@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++; | ||
} | ||
} | ||
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, | ||
candidate::branchlessCandidateCmov | ||
}) { | ||
|
||
int candidateSize = s.getAsInt(); | ||
|
||
if (baselineSize != candidateSize) { | ||
throw new IllegalArgumentException("incorrect size"); | ||
} | ||
|
||
if (Arrays.equals(baseline.docs, 0, baselineSize, candidate.docs, 0, candidateSize) | ||
== false) { | ||
throw new IllegalArgumentException( | ||
"incorrect docs," | ||
+ "\nbaseline: " | ||
+ Arrays.toString(ArrayUtil.copyOfSubArray(baseline.docs, 0, baselineSize)) | ||
+ "\ncandidate: " | ||
+ Arrays.toString(ArrayUtil.copyOfSubArray(candidate.docs, 0, candidateSize))); | ||
} | ||
|
||
if (Arrays.equals(baseline.scores, 0, baselineSize, candidate.scores, 0, candidateSize) | ||
== false) { | ||
throw new IllegalArgumentException("incorrect scores"); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.