-
Notifications
You must be signed in to change notification settings - Fork 3.8k
feat: SIMD sum vector aggregators (Long/Double/Float) #19561
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
clintropolis
wants to merge
5
commits into
apache:master
Choose a base branch
from
clintropolis:simd-vector-aggs
base: master
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
5 commits
Select commit
Hold shift + click to select a range
90c8117
feat: SIMD sum vector aggregators (Long/Double/Float)
clintropolis cebc8a9
review stuff
clintropolis a34a84b
Merge remote-tracking branch 'upstream/master' into simd-vector-aggs
clintropolis caa3cce
fix spelling
clintropolis 5b2e2ee
swap benchmark queries
clintropolis 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
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
46 changes: 46 additions & 0 deletions
46
processing/src/main/java/org/apache/druid/query/aggregation/NullAwareVectorAggregator.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,46 @@ | ||
| /* | ||
| * 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.druid.query.aggregation; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * Capability marker for {@link VectorAggregator}s that can efficiently aggregate a contiguous row range while | ||
| * skipping null inputs themselves (such as via SIMD masking) rather than relying on the | ||
| * {@link NullableNumericVectorAggregator} wrapper to filter rows into the per-row scatter-gather | ||
| * {@link VectorAggregator#aggregate(ByteBuffer, int, int[], int[], int) aggregate} variant. | ||
| * | ||
| * When the wrapper sees the input batch has nulls (i.e. {@code selector.getNullVector() != null}) and the | ||
| * delegate is an instance of this interface, it routes the call here instead of falling back to the | ||
| * scatter-gather path. | ||
| */ | ||
| public interface NullAwareVectorAggregator extends VectorAggregator | ||
| { | ||
| /** | ||
| * Aggregate rows {@code [startRow, endRow)} into the slot at {@code position}, skipping rows where | ||
| * {@code nullVector[i] == true}. Implementations should use {@link jdk.incubator.vector.VectorMask} (or | ||
| * equivalent) to avoid per-row branching. | ||
| * | ||
| * @return {@code true} if at least one non-null row was aggregated; {@code false} if every row in the range | ||
| * was null. {@link NullableNumericVectorAggregator} uses this to set or leave the null-marker byte | ||
| * that precedes the delegate's state. | ||
| */ | ||
| boolean aggregate(ByteBuffer buf, int position, int startRow, int endRow, boolean[] nullVector); | ||
| } |
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
98 changes: 98 additions & 0 deletions
98
.../src/main/java/org/apache/druid/query/aggregation/simd/SimdDoubleSumVectorAggregator.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,98 @@ | ||
| /* | ||
| * 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.druid.query.aggregation.simd; | ||
|
|
||
| import jdk.incubator.vector.DoubleVector; | ||
| import jdk.incubator.vector.VectorMask; | ||
| import jdk.incubator.vector.VectorOperators; | ||
| import jdk.incubator.vector.VectorSpecies; | ||
| import org.apache.druid.query.aggregation.DoubleSumVectorAggregator; | ||
| import org.apache.druid.query.aggregation.NullAwareVectorAggregator; | ||
| import org.apache.druid.segment.vector.VectorValueSelector; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * SIMD specialization of {@link DoubleSumVectorAggregator}'s ungrouped contiguous-range aggregation. The hot loop | ||
| * issues a hardcoded {@link DoubleVector#add} and a {@code reduceLanes(VectorOperators.ADD)} so the JIT emits the | ||
| * platform's double-add and double-add-reduce intrinsics. Null lanes are skipped via {@link VectorMask}. | ||
| * | ||
| * The grouped scatter-gather variant is inherited from the parent (scalar) class. | ||
| */ | ||
| public final class SimdDoubleSumVectorAggregator extends DoubleSumVectorAggregator implements NullAwareVectorAggregator | ||
| { | ||
| private static final VectorSpecies<Double> SPECIES = DoubleVector.SPECIES_PREFERRED; | ||
|
|
||
| private final VectorValueSelector selector; | ||
|
|
||
| public SimdDoubleSumVectorAggregator(VectorValueSelector selector) | ||
| { | ||
| super(selector); | ||
| this.selector = selector; | ||
| } | ||
|
|
||
| @Override | ||
| public void aggregate(ByteBuffer buf, int position, int startRow, int endRow) | ||
| { | ||
| final double[] vector = selector.getDoubleVector(); | ||
|
|
||
| final int laneCount = SPECIES.length(); | ||
| final int upperBound = startRow + SPECIES.loopBound(endRow - startRow); | ||
| int i = startRow; | ||
| DoubleVector vacc = DoubleVector.zero(SPECIES); | ||
| for (; i < upperBound; i += laneCount) { | ||
| vacc = vacc.add(DoubleVector.fromArray(SPECIES, vector, i)); | ||
| } | ||
| double sum = vacc.reduceLanes(VectorOperators.ADD); | ||
| for (; i < endRow; i++) { | ||
| sum += vector[i]; | ||
| } | ||
| buf.putDouble(position, buf.getDouble(position) + sum); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean aggregate(ByteBuffer buf, int position, int startRow, int endRow, boolean[] nullVector) | ||
| { | ||
| final double[] vector = selector.getDoubleVector(); | ||
|
|
||
| final int laneCount = SPECIES.length(); | ||
| final int upperBound = startRow + SPECIES.loopBound(endRow - startRow); | ||
| int i = startRow; | ||
| DoubleVector vacc = DoubleVector.zero(SPECIES); | ||
| int nonNullCount = 0; | ||
| for (; i < upperBound; i += laneCount) { | ||
| final VectorMask<Double> notNull = VectorMask.fromArray(SPECIES, nullVector, i).not(); | ||
| vacc = vacc.add(DoubleVector.fromArray(SPECIES, vector, i), notNull); | ||
| nonNullCount += notNull.trueCount(); | ||
| } | ||
| double sum = vacc.reduceLanes(VectorOperators.ADD); | ||
| for (; i < endRow; i++) { | ||
| if (!nullVector[i]) { | ||
| sum += vector[i]; | ||
| nonNullCount++; | ||
| } | ||
| } | ||
| if (nonNullCount > 0) { | ||
| buf.putDouble(position, buf.getDouble(position) + sum); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
98 changes: 98 additions & 0 deletions
98
...g/src/main/java/org/apache/druid/query/aggregation/simd/SimdFloatSumVectorAggregator.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,98 @@ | ||
| /* | ||
| * 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.druid.query.aggregation.simd; | ||
|
|
||
| import jdk.incubator.vector.FloatVector; | ||
| import jdk.incubator.vector.VectorMask; | ||
| import jdk.incubator.vector.VectorOperators; | ||
| import jdk.incubator.vector.VectorSpecies; | ||
| import org.apache.druid.query.aggregation.FloatSumVectorAggregator; | ||
| import org.apache.druid.query.aggregation.NullAwareVectorAggregator; | ||
| import org.apache.druid.segment.vector.VectorValueSelector; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| /** | ||
| * SIMD specialization of {@link FloatSumVectorAggregator}'s ungrouped contiguous-range aggregation. The hot loop | ||
| * issues a hardcoded {@link FloatVector#add} and a {@code reduceLanes(VectorOperators.ADD)} so the JIT emits the | ||
| * platform's float-add and float-add-reduce intrinsics. Null lanes are skipped via {@link VectorMask}. | ||
| * | ||
| * The grouped scatter-gather variant is inherited from the parent (scalar) class. | ||
| */ | ||
| public final class SimdFloatSumVectorAggregator extends FloatSumVectorAggregator implements NullAwareVectorAggregator | ||
| { | ||
| private static final VectorSpecies<Float> SPECIES = FloatVector.SPECIES_PREFERRED; | ||
|
|
||
| private final VectorValueSelector selector; | ||
|
|
||
| public SimdFloatSumVectorAggregator(VectorValueSelector selector) | ||
| { | ||
| super(selector); | ||
| this.selector = selector; | ||
| } | ||
|
|
||
| @Override | ||
| public void aggregate(ByteBuffer buf, int position, int startRow, int endRow) | ||
| { | ||
| final float[] vector = selector.getFloatVector(); | ||
|
|
||
| final int laneCount = SPECIES.length(); | ||
| final int upperBound = startRow + SPECIES.loopBound(endRow - startRow); | ||
| int i = startRow; | ||
| FloatVector vacc = FloatVector.zero(SPECIES); | ||
| for (; i < upperBound; i += laneCount) { | ||
| vacc = vacc.add(FloatVector.fromArray(SPECIES, vector, i)); | ||
| } | ||
| float sum = vacc.reduceLanes(VectorOperators.ADD); | ||
| for (; i < endRow; i++) { | ||
| sum += vector[i]; | ||
| } | ||
| buf.putFloat(position, buf.getFloat(position) + sum); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean aggregate(ByteBuffer buf, int position, int startRow, int endRow, boolean[] nullVector) | ||
| { | ||
| final float[] vector = selector.getFloatVector(); | ||
|
|
||
| final int laneCount = SPECIES.length(); | ||
| final int upperBound = startRow + SPECIES.loopBound(endRow - startRow); | ||
| int i = startRow; | ||
| FloatVector vacc = FloatVector.zero(SPECIES); | ||
| int nonNullCount = 0; | ||
| for (; i < upperBound; i += laneCount) { | ||
| final VectorMask<Float> notNull = VectorMask.fromArray(SPECIES, nullVector, i).not(); | ||
| vacc = vacc.add(FloatVector.fromArray(SPECIES, vector, i), notNull); | ||
| nonNullCount += notNull.trueCount(); | ||
| } | ||
| float sum = vacc.reduceLanes(VectorOperators.ADD); | ||
| for (; i < endRow; i++) { | ||
| if (!nullVector[i]) { | ||
| sum += vector[i]; | ||
| nonNullCount++; | ||
| } | ||
| } | ||
| if (nonNullCount > 0) { | ||
| buf.putFloat(position, buf.getFloat(position) + sum); | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why this change?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i can revert it, its mentioned in the PR description that I changed it because long1 has no nulls and long5 has a bunch of them, so they exercise different paths