diff --git a/java/src/main/java/ai/rapids/cudf/Cuda.java b/java/src/main/java/ai/rapids/cudf/Cuda.java index 65ff148adb9..02e4d32617d 100755 --- a/java/src/main/java/ai/rapids/cudf/Cuda.java +++ b/java/src/main/java/ai/rapids/cudf/Cuda.java @@ -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. @@ -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) */ @@ -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 diff --git a/java/src/main/java/ai/rapids/cudf/CudaComputeMode.java b/java/src/main/java/ai/rapids/cudf/CudaComputeMode.java new file mode 100644 index 00000000000..2da246da62b --- /dev/null +++ b/java/src/main/java/ai/rapids/cudf/CudaComputeMode.java @@ -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(); +} diff --git a/java/src/main/native/src/CudaJni.cpp b/java/src/main/native/src/CudaJni.cpp index f5eb09fa2d4..987ff87f8ac 100644 --- a/java/src/main/native/src/CudaJni.cpp +++ b/java/src/main/native/src/CudaJni.cpp @@ -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); diff --git a/java/src/test/java/ai/rapids/cudf/CudaTest.java b/java/src/test/java/ai/rapids/cudf/CudaTest.java new file mode 100644 index 00000000000..d33785e0577 --- /dev/null +++ b/java/src/test/java/ai/rapids/cudf/CudaTest.java @@ -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); + } + +}