-
Notifications
You must be signed in to change notification settings - Fork 932
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
Update how to manage host UDF instance #17770
Changes from all commits
8dea797
9c13484
7e59794
dc5b518
17211d1
65206cb
ae9fa51
7020037
5444332
2f5a0dc
062a493
48abceb
362c964
8431d2b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
/* | ||
* Copyright (c) 2024, NVIDIA CORPORATION. | ||
* Copyright (c) 2024-2025, NVIDIA CORPORATION. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
|
@@ -19,18 +19,56 @@ | |
/** | ||
* A wrapper around native host UDF aggregations. | ||
* <p> | ||
* This class is used to store the native handle of a host UDF aggregation and is used as | ||
* This class is used to create the native handle of a host UDF aggregation and is used as | ||
* a proxy object to compute hash code and compare two host UDF aggregations for equality. | ||
* <p> | ||
* A new host UDF aggregation implementation must extend this class and override the | ||
* {@code hashCode} and {@code equals} methods for such purposes. | ||
* In addition, since this class implements {@code AutoCloseable}, the {@code close} method must | ||
* also be overridden to automatically delete the native UDF instance upon class destruction. | ||
* {@code computeHashCode} and {@code isEqual} methods for such purposes. | ||
* | ||
*/ | ||
public abstract class HostUDFWrapper implements AutoCloseable { | ||
public final long udfNativeHandle; | ||
public abstract class HostUDFWrapper { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pardon my ignorance, but why is this not an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previsously |
||
|
||
/** | ||
* Create a derived host UDF native instance. | ||
* The instance created by this function MUST be closed by `closeUDFInstance` | ||
* <p>Typical usage, refer to Aggregation.java:</p> | ||
* <pre> | ||
* long udf = 0; | ||
* try { | ||
* udf = wrapper.createUDFInstance(); | ||
* return Aggregation.createHostUDFAgg(udf); | ||
* } finally { | ||
* // a new UDF is cloned in `createHostUDFAgg`, here should close the UDF instance. | ||
* if (udf != 0) { | ||
* HostUDFWrapper.closeUDFInstance(udf); | ||
* } | ||
* } | ||
* </pre> | ||
* | ||
*/ | ||
public abstract long createUDFInstance(); | ||
|
||
/** | ||
* Close the derived UDF instance created by `createUDFInstance`. | ||
* @param hostUDFInstance the UDF instance | ||
*/ | ||
public static void closeUDFInstance(long hostUDFInstance) { | ||
close(hostUDFInstance); | ||
} | ||
|
||
public abstract int computeHashCode(); | ||
|
||
public HostUDFWrapper(long udfNativeHandle) { | ||
this.udfNativeHandle = udfNativeHandle; | ||
@Override | ||
public int hashCode() { | ||
return computeHashCode(); | ||
} | ||
|
||
public abstract boolean isEqual(Object obj); | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
return isEqual(obj); | ||
} | ||
|
||
static native void close(long hostUDFInstance); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* | ||
* Copyright (c) 2025, 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. | ||
*/ | ||
|
||
#include "cudf_jni_apis.hpp" | ||
|
||
#include <cudf/aggregation/host_udf.hpp> | ||
|
||
extern "C" { | ||
|
||
JNIEXPORT void JNICALL Java_ai_rapids_cudf_HostUDFWrapper_close(JNIEnv* env, | ||
jclass class_object, | ||
jlong ptr) | ||
{ | ||
try { | ||
auto to_del = reinterpret_cast<cudf::host_udf_base*>(ptr); | ||
delete to_del; | ||
Comment on lines
+28
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here we can colse all kind of derived UDF instances. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Previously, it's in the derived class, link. |
||
} | ||
CATCH_STD(env, ); | ||
} | ||
|
||
} // extern "C" |
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.
UDF instance is hold by Aggregation instance via a
unique_ptr
, the Aggregation instance is responsible for the life cycle of UDF instance.Here pass in a UDF instance and
createHostUDFAgg
clone it and construct a Aggregation instance, in the finally block, we close the original UDF instance.