-
Notifications
You must be signed in to change notification settings - Fork 915
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
JNI support for segmented reduce (#10413)
This adds in JNI support for #9621. It also adds in a helper API to allow us to do the processing on a list easily. Authors: - Robert (Bobby) Evans (https://github.com/revans2) Approvers: - Liangcai Li (https://github.com/firestarman) - Jason Lowe (https://github.com/jlowe) URL: #10413
- Loading branch information
Showing
4 changed files
with
352 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
104 changes: 104 additions & 0 deletions
104
java/src/main/java/ai/rapids/cudf/SegmentedReductionAggregation.java
This file contains 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,104 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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 ai.rapids.cudf; | ||
|
||
/** | ||
* An aggregation that can be used for a reduce. | ||
*/ | ||
public final class SegmentedReductionAggregation { | ||
private final Aggregation wrapped; | ||
|
||
private SegmentedReductionAggregation(Aggregation wrapped) { | ||
this.wrapped = wrapped; | ||
} | ||
|
||
long createNativeInstance() { | ||
return wrapped.createNativeInstance(); | ||
} | ||
|
||
long getDefaultOutput() { | ||
return wrapped.getDefaultOutput(); | ||
} | ||
|
||
Aggregation getWrapped() { | ||
return wrapped; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return wrapped.hashCode(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object other) { | ||
if (other == this) { | ||
return true; | ||
} else if (other instanceof SegmentedReductionAggregation) { | ||
SegmentedReductionAggregation o = (SegmentedReductionAggregation) other; | ||
return wrapped.equals(o.wrapped); | ||
} | ||
return false; | ||
} | ||
|
||
/** | ||
* Sum Aggregation | ||
*/ | ||
public static SegmentedReductionAggregation sum() { | ||
return new SegmentedReductionAggregation(Aggregation.sum()); | ||
} | ||
|
||
/** | ||
* Product Aggregation. | ||
*/ | ||
public static SegmentedReductionAggregation product() { | ||
return new SegmentedReductionAggregation(Aggregation.product()); | ||
} | ||
|
||
/** | ||
* Min Aggregation | ||
*/ | ||
public static SegmentedReductionAggregation min() { | ||
return new SegmentedReductionAggregation(Aggregation.min()); | ||
} | ||
|
||
/** | ||
* Max Aggregation | ||
*/ | ||
public static SegmentedReductionAggregation max() { | ||
return new SegmentedReductionAggregation(Aggregation.max()); | ||
} | ||
|
||
/** | ||
* Any reduction. Produces a true or 1, depending on the output type, | ||
* if any of the elements in the range are true or non-zero, otherwise produces a false or 0. | ||
* Null values are skipped. | ||
*/ | ||
public static SegmentedReductionAggregation any() { | ||
return new SegmentedReductionAggregation(Aggregation.any()); | ||
} | ||
|
||
/** | ||
* All reduction. Produces true or 1, depending on the output type, if all of the elements in | ||
* the range are true or non-zero, otherwise produces a false or 0. | ||
* Null values are skipped. | ||
*/ | ||
public static SegmentedReductionAggregation all() { | ||
return new SegmentedReductionAggregation(Aggregation.all()); | ||
} | ||
} |
This file contains 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
115 changes: 115 additions & 0 deletions
115
java/src/test/java/ai/rapids/cudf/SegmentedReductionTest.java
This file contains 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,115 @@ | ||
/* | ||
* | ||
* Copyright (c) 2022, NVIDIA CORPORATION. | ||
* | ||
* Licensed 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 ai.rapids.cudf; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Arrays; | ||
|
||
class SegmentedReductionTest extends CudfTestBase { | ||
|
||
@Test | ||
public void testListSum() { | ||
HostColumnVector.DataType dt = new HostColumnVector.ListType(true, | ||
new HostColumnVector.BasicType(true, DType.INT32)); | ||
try (ColumnVector listCv = ColumnVector.fromLists(dt, | ||
Arrays.asList(1, 2, 3), | ||
Arrays.asList(2, 3, 4), | ||
null, | ||
Arrays.asList(null, 1, 2)); | ||
ColumnVector excludeExpected = ColumnVector.fromBoxedInts(6, 9, null, 3); | ||
ColumnVector nullExcluded = listCv.listReduce(SegmentedReductionAggregation.sum(), NullPolicy.EXCLUDE, DType.INT32); | ||
ColumnVector includeExpected = ColumnVector.fromBoxedInts(6, 9, null, null); | ||
ColumnVector nullIncluded = listCv.listReduce(SegmentedReductionAggregation.sum(), NullPolicy.INCLUDE, DType.INT32)) { | ||
AssertUtils.assertColumnsAreEqual(excludeExpected, nullExcluded); | ||
AssertUtils.assertColumnsAreEqual(includeExpected, nullIncluded); | ||
} | ||
} | ||
|
||
@Test | ||
public void testListMin() { | ||
HostColumnVector.DataType dt = new HostColumnVector.ListType(true, | ||
new HostColumnVector.BasicType(true, DType.INT32)); | ||
try (ColumnVector listCv = ColumnVector.fromLists(dt, | ||
Arrays.asList(1, 2, 3), | ||
Arrays.asList(2, 3, 4), | ||
null, | ||
Arrays.asList(null, 1, 2)); | ||
ColumnVector excludeExpected = ColumnVector.fromBoxedInts(1, 2, null, 1); | ||
ColumnVector nullExcluded = listCv.listReduce(SegmentedReductionAggregation.min(), NullPolicy.EXCLUDE, DType.INT32); | ||
ColumnVector includeExpected = ColumnVector.fromBoxedInts(1, 2, null, null); | ||
ColumnVector nullIncluded = listCv.listReduce(SegmentedReductionAggregation.min(), NullPolicy.INCLUDE, DType.INT32)) { | ||
AssertUtils.assertColumnsAreEqual(excludeExpected, nullExcluded); | ||
AssertUtils.assertColumnsAreEqual(includeExpected, nullIncluded); | ||
} | ||
} | ||
|
||
@Test | ||
public void testListMax() { | ||
HostColumnVector.DataType dt = new HostColumnVector.ListType(true, | ||
new HostColumnVector.BasicType(true, DType.INT32)); | ||
try (ColumnVector listCv = ColumnVector.fromLists(dt, | ||
Arrays.asList(1, 2, 3), | ||
Arrays.asList(2, 3, 4), | ||
null, | ||
Arrays.asList(null, 1, 2)); | ||
ColumnVector excludeExpected = ColumnVector.fromBoxedInts(3, 4, null, 2); | ||
ColumnVector nullExcluded = listCv.listReduce(SegmentedReductionAggregation.max(), NullPolicy.EXCLUDE, DType.INT32); | ||
ColumnVector includeExpected = ColumnVector.fromBoxedInts(3, 4, null, null); | ||
ColumnVector nullIncluded = listCv.listReduce(SegmentedReductionAggregation.max(), NullPolicy.INCLUDE, DType.INT32)) { | ||
AssertUtils.assertColumnsAreEqual(excludeExpected, nullExcluded); | ||
AssertUtils.assertColumnsAreEqual(includeExpected, nullIncluded); | ||
} | ||
} | ||
|
||
@Test | ||
public void testListAny() { | ||
HostColumnVector.DataType dt = new HostColumnVector.ListType(true, | ||
new HostColumnVector.BasicType(true, DType.BOOL8)); | ||
try (ColumnVector listCv = ColumnVector.fromLists(dt, | ||
Arrays.asList(true, false, false), | ||
Arrays.asList(false, false, false), | ||
null, | ||
Arrays.asList(null, true, false)); | ||
ColumnVector excludeExpected = ColumnVector.fromBoxedBooleans(true, false, null, true); | ||
ColumnVector nullExcluded = listCv.listReduce(SegmentedReductionAggregation.any(), NullPolicy.EXCLUDE, DType.BOOL8); | ||
ColumnVector includeExpected = ColumnVector.fromBoxedBooleans(true, false, null, null); | ||
ColumnVector nullIncluded = listCv.listReduce(SegmentedReductionAggregation.any(), NullPolicy.INCLUDE, DType.BOOL8)) { | ||
AssertUtils.assertColumnsAreEqual(excludeExpected, nullExcluded); | ||
AssertUtils.assertColumnsAreEqual(includeExpected, nullIncluded); | ||
} | ||
} | ||
|
||
@Test | ||
public void testListAll() { | ||
HostColumnVector.DataType dt = new HostColumnVector.ListType(true, | ||
new HostColumnVector.BasicType(true, DType.BOOL8)); | ||
try (ColumnVector listCv = ColumnVector.fromLists(dt, | ||
Arrays.asList(true, true, true), | ||
Arrays.asList(false, true, false), | ||
null, | ||
Arrays.asList(null, true, true)); | ||
ColumnVector excludeExpected = ColumnVector.fromBoxedBooleans(true, false, null, true); | ||
ColumnVector nullExcluded = listCv.listReduce(SegmentedReductionAggregation.all(), NullPolicy.EXCLUDE, DType.BOOL8); | ||
ColumnVector includeExpected = ColumnVector.fromBoxedBooleans(true, false, null, null); | ||
ColumnVector nullIncluded = listCv.listReduce(SegmentedReductionAggregation.all(), NullPolicy.INCLUDE, DType.BOOL8)) { | ||
AssertUtils.assertColumnsAreEqual(excludeExpected, nullExcluded); | ||
AssertUtils.assertColumnsAreEqual(includeExpected, nullIncluded); | ||
} | ||
} | ||
} |