Skip to content

Commit

Permalink
Java APIs to fetch CUDA runtime info (#8465)
Browse files Browse the repository at this point in the history
Closes #8084 #6363

Signed-off-by: sperlingxx <[email protected]>

Authors:
  - Alfred Xu (https://github.com/sperlingxx)

Approvers:
  - Robert (Bobby) Evans (https://github.com/revans2)
  - Alessandro Bellina (https://github.com/abellina)

URL: #8465
  • Loading branch information
sperlingxx authored Jun 11, 2021
1 parent 72d8de5 commit d3b440e
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 1 deletion.
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() {
}
}

/**
* 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.
*/
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);
}

}

0 comments on commit d3b440e

Please sign in to comment.