Skip to content
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

Java APIs to fetch CUDA runtime info [skip ci] #8465

Merged
merged 5 commits into from
Jun 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 37 additions & 1 deletion java/src/main/java/ai/rapids/cudf/Cuda.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019-2020, NVIDIA CORPORATION.
* Copyright (c) 2019-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.
Expand Down Expand Up @@ -244,6 +244,15 @@ public synchronized void close() {
}
}

/**
sperlingxx marked this conversation as resolved.
Show resolved Hide resolved
* Gets the CUDA compute mode of the current device.
*
* @return the enum value of CudaComputeMode
*/
public static CudaComputeMode getComputeMode() {
return CudaComputeMode.fromNative(Cuda.getNativeComputeMode());
}

/**
* Mapping: cudaMemGetInfo(size_t *free, size_t *total)
*/
Expand Down Expand Up @@ -347,6 +356,33 @@ static void asyncMemcpy(long dst, long src, long count, CudaMemcpyKind kind) {
*/
public static native void autoSetDevice() throws CudaException;

/**
* Get the CUDA Driver version, which is the latest version of CUDA supported by the driver.
* The version is returned as (1000 major + 10 minor). For example, CUDA 9.2 would be
* represented by 9020. If no driver is installed,then 0 is returned as the driver version.
*
* @return the CUDA driver version
* @throws CudaException on any error
*/
public static native int getDriverVersion() throws CudaException;

/**
* Get the CUDA Runtime version of the current CUDA Runtime instance. The version is returned
* as (1000 major + 10 minor). For example, CUDA 9.2 would be represented by 9020.
*
* @return the CUDA Runtime version
* @throws CudaException on any error
*/
public static native int getRuntimeVersion() throws CudaException;

/**
* Gets the CUDA device compute mode of the current device.
*
* @return the value of cudaComputeMode
* @throws CudaException on any error
*/
static native int getNativeComputeMode() throws CudaException;

/**
* Calls cudaFree(0). This can be used to initialize the GPU after a setDevice()
* @throws CudaException on any error
Expand Down
61 changes: 61 additions & 0 deletions java/src/main/java/ai/rapids/cudf/CudaComputeMode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* 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;

/**
* This is the Java mapping of CUDA device compute modes.
*/
public enum CudaComputeMode {
/**
* Default compute mode
* Multiple threads can use cudaSetDevice() with this device.
*/
DEFAULT(0),
/**
* Compute-exclusive-thread mode
* Only one thread in one process will be able to use cudaSetDevice() with this device.
*
* WARNING: This mode was deprecated! Using EXCLUSIVE_PROCESS instead.
*/
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This mode is deprecated. Better to add the same comment here.

(base) liangcail@liangcail-ubuntu18:~/work/projects/on_github/spark-rapids$ nvidia-smi -c 1
Warning: Exclusive_Thread was deprecated! Setting Exclusive_Process instead.
Unable to set the compute mode for GPU 00000000:01:00.0: Insufficient Permissions
Terminating early due to previous errors.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added warning message for the deprecation.

EXCLUSIVE(1),
/**
* Compute-prohibited mode
* No threads can use cudaSetDevice() with this device.
*/
PROHIBITED(2),
/**
* Compute-exclusive-process mode
* Many threads in one process will be able to use cudaSetDevice() with this device.
*/
EXCLUSIVE_PROCESS(3);

private CudaComputeMode(int nativeId) {
this.nativeId = nativeId;
}

static CudaComputeMode fromNative(int nativeId) {
for (CudaComputeMode mode : COMPUTE_MODES) {
if (mode.nativeId == nativeId) return mode;
}
throw new IllegalArgumentException("Could not translate " + nativeId + " into a CudaComputeMode");
}

// mapping to the value of native mode
final int nativeId;

private static final CudaComputeMode[] COMPUTE_MODES = CudaComputeMode.values();
}
32 changes: 32 additions & 0 deletions java/src/main/native/src/CudaJni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,38 @@ JNIEXPORT void JNICALL Java_ai_rapids_cudf_Cuda_autoSetDevice(JNIEnv *env, jclas
CATCH_STD(env, );
}

JNIEXPORT jint JNICALL Java_ai_rapids_cudf_Cuda_getDriverVersion(JNIEnv *env, jclass) {
try {
cudf::jni::auto_set_device(env);
jint driver_version;
JNI_CUDA_TRY(env, -2, cudaDriverGetVersion(&driver_version));
return driver_version;
}
CATCH_STD(env, -2);
}

JNIEXPORT jint JNICALL Java_ai_rapids_cudf_Cuda_getRuntimeVersion(JNIEnv *env, jclass) {
try {
cudf::jni::auto_set_device(env);
jint runtime_version;
JNI_CUDA_TRY(env, -2, cudaRuntimeGetVersion(&runtime_version));
return runtime_version;
}
CATCH_STD(env, -2);
}

JNIEXPORT jint JNICALL Java_ai_rapids_cudf_Cuda_getNativeComputeMode(JNIEnv *env, jclass) {
try {
cudf::jni::auto_set_device(env);
int device;
JNI_CUDA_TRY(env, -2, cudaGetDevice(&device));
cudaDeviceProp device_prop;
JNI_CUDA_TRY(env, -2, cudaGetDeviceProperties(&device_prop, device));
return device_prop.computeMode;
}
CATCH_STD(env, -2);
}

JNIEXPORT void JNICALL Java_ai_rapids_cudf_Cuda_freeZero(JNIEnv *env, jclass) {
try {
cudf::jni::auto_set_device(env);
Expand Down
32 changes: 32 additions & 0 deletions java/src/test/java/ai/rapids/cudf/CudaTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class CudaTest {

@Test
public void testGetCudaRuntimeInfo() {
assert Cuda.getDriverVersion() >= Cuda.getRuntimeVersion();
assert Cuda.getRuntimeVersion() > 1000;
assertEquals(Cuda.getNativeComputeMode(), Cuda.getComputeMode().nativeId);
}

}