-
Notifications
You must be signed in to change notification settings - Fork 217
[Java][C] Expose GPUInfo #1267
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
Merged
rapids-bot
merged 13 commits into
NVIDIA:branch-25.10
from
ldematte:java/expose-gpu-info
Aug 27, 2025
Merged
[Java][C] Expose GPUInfo #1267
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
5769464
C API changes: adding method to retrieve the ID of the device associa…
ldematte fea987e
Introducing GPUInfoProvider with and a first (incomplete) implementat…
ldematte 2bd9217
GPUInfoProviderImpl implementation + IT tests
ldematte 1c784a1
Renaming
ldematte e9f32b8
More renaming, exposing totalDeviceMemoryInBytes to getCurrentInfo fo…
ldematte bcfb9d7
Merge remote-tracking branch 'upstream/branch-25.10' into java/expose…
ldematte 02e5788
Separate major/minor, add more GPUInfo stats, adjust IT test logging
ldematte d6ac665
Moved deviceId to CuVSResources
ldematte 6913f1b
Merge remote-tracking branch 'upstream/branch-25.10' into java/expose…
ldematte ad2dc03
Merge branch 'branch-25.10' into java/expose-gpu-info
mythrocks da0b79b
Review: cache GPUInfo
ldematte 49ab6d2
Merge branch 'java/expose-gpu-info' of github.com:ldematte/cuvs into …
ldematte 10d38a1
Merge branch 'branch-25.10' into java/expose-gpu-info
mythrocks File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
java/cuvs-java/src/main/java/com/nvidia/cuvs/CuVSResourcesInfo.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package com.nvidia.cuvs; | ||
|
|
||
| /** | ||
| * Contains performance-related information associated to a {@link CuVSResources} and its GPU. | ||
| * Can be extended to report different types of GPU memory linked to the resources, | ||
| * e.g. the type and capacity of the underlying RMM {@code device_memory_resource} | ||
| * | ||
| * @param freeDeviceMemoryInBytes free memory in bytes, as reported by the device driver | ||
| * @param totalDeviceMemoryInBytes total device memory in bytes | ||
| */ | ||
| public record CuVSResourcesInfo(long freeDeviceMemoryInBytes, long totalDeviceMemoryInBytes) {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
java/cuvs-java/src/main/java/com/nvidia/cuvs/GPUInfoProvider.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package com.nvidia.cuvs; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface GPUInfoProvider { | ||
|
|
||
| int MIN_COMPUTE_CAPABILITY_MAJOR = 7; | ||
| int MIN_COMPUTE_CAPABILITY_MINOR = 0; | ||
|
|
||
| int MIN_DEVICE_MEMORY_IN_MB = 8192; | ||
|
|
||
| /** | ||
| * Gets all the available GPUs | ||
| * | ||
| * @return a list of {@link GPUInfo} objects with GPU details | ||
| */ | ||
| List<GPUInfo> availableGPUs(); | ||
|
|
||
| /** | ||
| * Get the list of compatible GPUs based on compute capability >= 7.0 and total | ||
| * memory >= 8GB | ||
| * | ||
| * @return a list of compatible GPUs. See {@link GPUInfo} | ||
| */ | ||
| List<GPUInfo> compatibleGPUs(); | ||
|
|
||
| /** | ||
| * Gets memory information relative to a {@link CuVSResources} | ||
| * @param resources from which to obtain memory information | ||
| * @return a {@link CuVSResourcesInfo} record containing the memory information | ||
| */ | ||
| CuVSResourcesInfo getCurrentInfo(CuVSResources resources); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
java/cuvs-java/src/main/java22/com/nvidia/cuvs/internal/GPUInfoProviderImpl.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| /* | ||
| * 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. | ||
| */ | ||
| package com.nvidia.cuvs.internal; | ||
|
|
||
| import static com.nvidia.cuvs.internal.common.LinkerHelper.C_INT; | ||
| import static com.nvidia.cuvs.internal.common.Util.checkCudaError; | ||
| import static com.nvidia.cuvs.internal.panama.headers_h.cudaMemGetInfo; | ||
| import static com.nvidia.cuvs.internal.panama.headers_h_1.*; | ||
|
|
||
| import com.nvidia.cuvs.CuVSResources; | ||
| import com.nvidia.cuvs.CuVSResourcesInfo; | ||
| import com.nvidia.cuvs.GPUInfo; | ||
| import com.nvidia.cuvs.GPUInfoProvider; | ||
| import com.nvidia.cuvs.internal.panama.cudaDeviceProp; | ||
| import java.lang.foreign.Arena; | ||
| import java.lang.foreign.MemorySegment; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class GPUInfoProviderImpl implements GPUInfoProvider { | ||
|
|
||
| // Lazy initialization for list of available GPUs. | ||
| private static class AvailableGpuInitializer { | ||
|
|
||
| // Available GPUs are initialized only once when first accessed. | ||
| // This is assumed to be invariant for the lifetime of the program. | ||
| static final List<GPUInfo> AVAILABLE_GPUS = getAvailableGpusInfo(); | ||
|
|
||
| private static List<GPUInfo> getAvailableGpusInfo() { | ||
| try (var localArena = Arena.ofConfined()) { | ||
|
|
||
| MemorySegment numGpus = localArena.allocate(C_INT); | ||
| int returnValue = cudaGetDeviceCount(numGpus); | ||
| checkCudaError(returnValue, "cudaGetDeviceCount"); | ||
|
|
||
| int numGpuCount = numGpus.get(C_INT, 0); | ||
| List<GPUInfo> gpuInfoArr = new ArrayList<GPUInfo>(); | ||
|
|
||
| MemorySegment deviceProp = cudaDeviceProp.allocate(localArena); | ||
|
|
||
| for (int i = 0; i < numGpuCount; i++) { | ||
| returnValue = cudaGetDeviceProperties_v2(deviceProp, i); | ||
| checkCudaError(returnValue, "cudaGetDeviceProperties_v2"); | ||
|
|
||
| GPUInfo gpuInfo = | ||
| new GPUInfo( | ||
| i, | ||
| cudaDeviceProp.name(deviceProp).getString(0), | ||
| cudaDeviceProp.totalGlobalMem(deviceProp), | ||
| cudaDeviceProp.major(deviceProp), | ||
| cudaDeviceProp.minor(deviceProp), | ||
| cudaDeviceProp.asyncEngineCount(deviceProp) > 0, | ||
| cudaDeviceProp.concurrentKernels(deviceProp) > 0); | ||
|
|
||
| gpuInfoArr.add(gpuInfo); | ||
| } | ||
| return gpuInfoArr; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static boolean hasMinimumCapability(GPUInfo gpuInfo) { | ||
| return gpuInfo.computeCapabilityMajor() > GPUInfoProvider.MIN_COMPUTE_CAPABILITY_MAJOR | ||
| || (gpuInfo.computeCapabilityMajor() == GPUInfoProvider.MIN_COMPUTE_CAPABILITY_MAJOR | ||
| && gpuInfo.computeCapabilityMinor() >= GPUInfoProvider.MIN_COMPUTE_CAPABILITY_MINOR); | ||
| } | ||
|
|
||
| @Override | ||
| public List<GPUInfo> availableGPUs() { | ||
| return AvailableGpuInitializer.AVAILABLE_GPUS; | ||
| } | ||
|
|
||
| @Override | ||
| public List<GPUInfo> compatibleGPUs() { | ||
| List<GPUInfo> compatibleGPUs = new ArrayList<>(); | ||
| long minDeviceMemoryInBytes = 1024L * 1024L * GPUInfoProvider.MIN_DEVICE_MEMORY_IN_MB; | ||
| for (GPUInfo gpuInfo : AvailableGpuInitializer.AVAILABLE_GPUS) { | ||
| if (hasMinimumCapability(gpuInfo) | ||
| && gpuInfo.totalDeviceMemoryInBytes() >= minDeviceMemoryInBytes) { | ||
| compatibleGPUs.add(gpuInfo); | ||
| } | ||
| } | ||
| return compatibleGPUs; | ||
| } | ||
|
|
||
| @Override | ||
| public CuVSResourcesInfo getCurrentInfo(CuVSResources resources) { | ||
| try (var localArena = Arena.ofConfined()) { | ||
| var deviceIdPtr = localArena.allocate(C_INT); | ||
| checkCudaError(cudaGetDevice(deviceIdPtr), "cudaGetDevice"); | ||
| var currentDeviceId = deviceIdPtr.get(C_INT, 0); | ||
|
|
||
| if (resources.deviceId() != currentDeviceId) { | ||
| checkCudaError(cudaSetDevice(resources.deviceId()), "cudaSetDevice"); | ||
| } | ||
|
|
||
| MemorySegment freeMemoryPtr = localArena.allocate(size_t); | ||
| MemorySegment totalMemoryPtr = localArena.allocate(size_t); | ||
| checkCudaError(cudaMemGetInfo(freeMemoryPtr, totalMemoryPtr), "cudaMemGetInfo"); | ||
|
|
||
| if (resources.deviceId() != currentDeviceId) { | ||
| checkCudaError(cudaSetDevice(currentDeviceId), "cudaSetDevice"); | ||
| } | ||
|
|
||
| return new CuVSResourcesInfo(freeMemoryPtr.get(size_t, 0), totalMemoryPtr.get(size_t, 0)); | ||
| } | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.