Skip to content

Commit

Permalink
Update Java nvcomp JNI bindings to nvcomp 2.x API (#9384)
Browse files Browse the repository at this point in the history
This PR Closes #8336 

The Java nvcomp JNI bindings are updated to the nvcomp 2.x API and the nvcomp library is no longer built as part of the Java build.  It is reused from the dependency that libcudf is using.

Note that the nvcomp 1.x API is no longer supported by the CUDF Java JNI, so this is a breaking change.  Spark-Rapids currently depends on the nvcomp 1.x JNI.  I will be putting up a PR for spark-rapids that changes it to use this 2.x JNI, which will need to be merged soon after this PR to minimize incompatibilities.

Authors:
  - Jim Brennan (https://github.com/jbrennan333)

Approvers:
  - Alessandro Bellina (https://github.com/abellina)
  - Jason Lowe (https://github.com/jlowe)

URL: #9384
  • Loading branch information
jbrennan333 authored Oct 26, 2021
1 parent 5cd3003 commit 063c982
Show file tree
Hide file tree
Showing 18 changed files with 1,142 additions and 1,641 deletions.
106 changes: 106 additions & 0 deletions java/src/main/java/ai/rapids/cudf/CloseableArray.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* Copyright (c) 2021, 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;

/** Utility class that wraps an array of closeable instances and can be closed */
public class CloseableArray<T extends AutoCloseable> implements AutoCloseable {
private T[] array;

public static <T extends AutoCloseable> CloseableArray<T> wrap(T[] array) {
return new CloseableArray<T>(array);
}

CloseableArray(T[] array) {
this.array = array;
}

public int size() {
return array.length;
}

public T get(int i) {
return array[i];
}

public T set(int i, T obj) {
array[i] = obj;
return obj;
}

public T[] getArray() {
return array;
}

public T[] release() {
T[] result = array;
array = null;
return result;
}

public void closeAt(int i) {
try {
T toClose = array[i];
array[i] = null;
toClose.close();
} catch (RuntimeException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Override
public void close() {
close(null);
}

public void close(Exception pendingError) {
if (array == null) {
return;
}
T[] toClose = array;
array = null;
RuntimeException error = null;
if (pendingError instanceof RuntimeException) {
error = (RuntimeException) pendingError;
} else if (pendingError != null) {
error = new RuntimeException(pendingError);
}
for (T obj: toClose) {
if (obj != null) {
try {
obj.close();
} catch (RuntimeException e) {
if (error != null) {
error.addSuppressed(e);
} else {
error = e;
}
} catch (Exception e) {
if (error != null) {
error.addSuppressed(e);
} else {
error = new RuntimeException(e);
}
}
}
}
if (error != null) {
throw error;
}
}
}
26 changes: 26 additions & 0 deletions java/src/main/java/ai/rapids/cudf/Cuda.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
*/
package ai.rapids.cudf;

import ai.rapids.cudf.NvtxColor;
import ai.rapids.cudf.NvtxRange;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -521,4 +524,27 @@ public static synchronized boolean isEnvCompatibleForTesting() {
* Whether per-thread default stream is enabled.
*/
public static native boolean isPtdsEnabled();

/**
* Copy data from multiple device buffer sources to multiple device buffer destinations.
* For each buffer to copy there is a corresponding entry in the destination address, source
* address, and copy size vectors.
* @param destAddrs vector of device destination addresses
* @param srcAddrs vector of device source addresses
* @param copySizes vector of copy sizes
* @param stream CUDA stream to use for the copy
*/
public static void multiBufferCopyAsync(long [] destAddrs,
long [] srcAddrs,
long [] copySizes,
Stream stream) {
// Temporary sub-par stand-in for a multi-buffer copy CUDA kernel
assert(destAddrs.length == srcAddrs.length);
assert(copySizes.length == destAddrs.length);
try (NvtxRange copyRange = new NvtxRange("multiBufferCopyAsync", NvtxColor.CYAN)){
for (int i = 0; i < destAddrs.length; i++) {
asyncMemcpy(destAddrs[i], srcAddrs[i], copySizes[i], CudaMemcpyKind.DEVICE_TO_DEVICE, stream);
}
}
}
}
14 changes: 1 addition & 13 deletions java/src/main/java/ai/rapids/cudf/MemoryCleaner.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
package ai.rapids.cudf;

import ai.rapids.cudf.ast.CompiledExpression;
import ai.rapids.cudf.nvcomp.BatchedLZ4Decompressor;
import ai.rapids.cudf.nvcomp.Decompressor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -248,16 +246,6 @@ static void register(Cuda.Event event, Cleaner cleaner) {
all.add(new CleanerWeakReference(event, cleaner, collected, false));
}

public static void register(Decompressor.Metadata metadata, Cleaner cleaner) {
// It is now registered...
all.add(new CleanerWeakReference(metadata, cleaner, collected, false));
}

public static void register(BatchedLZ4Decompressor.BatchedMetadata metadata, Cleaner cleaner) {
// It is now registered...
all.add(new CleanerWeakReference(metadata, cleaner, collected, false));
}

static void register(CuFileDriver driver, Cleaner cleaner) {
// It is now registered...
all.add(new CleanerWeakReference(driver, cleaner, collected, false));
Expand Down Expand Up @@ -324,4 +312,4 @@ public String toString() {
+ "\n";
}
}
}
}
Loading

0 comments on commit 063c982

Please sign in to comment.