|
35 | 35 |
|
36 | 36 | public class Util { |
37 | 37 |
|
38 | | - /** |
39 | | - * Returns the number of GPUs connected to the system using CuVSResources. |
40 | | - * |
41 | | - * @param resources The CuVSResources object managing native resources. |
42 | | - * @return Number of GPUs connected, or -1 if an error occurred. |
43 | | - */ |
44 | | - public static int getNumberOfGPUs(CuVSResources resources) { |
45 | | - try { |
46 | | - MethodHandle getNumberOfGPUsHandle = resources.linker.downcallHandle( |
47 | | - resources.libcuvsNativeLibrary.find("get_number_of_gpus") |
48 | | - .orElseThrow(() -> new IllegalStateException("get_number_of_gpus not found in library")), |
49 | | - FunctionDescriptor.of(ValueLayout.JAVA_INT)); |
50 | | - |
51 | | - return (int) getNumberOfGPUsHandle.invokeExact(); |
| 38 | + public static String getGpuDetails(CuVSResources resources, int maxGpus, int maxDetailLength) { |
| 39 | + try (Arena arena = Arena.ofConfined()) { |
| 40 | + MemorySegment detailSegment = arena.allocate(maxGpus * maxDetailLength); |
| 41 | + MethodHandle getGpuDetailsHandle = resources.linker.downcallHandle( |
| 42 | + resources.libcuvsNativeLibrary.find("get_gpu_details") |
| 43 | + .orElseThrow(() -> new IllegalStateException("get_gpu_details not found in library")), |
| 44 | + FunctionDescriptor.of(ValueLayout.JAVA_INT, ValueLayout.ADDRESS, ValueLayout.JAVA_INT, ValueLayout.JAVA_INT)); |
| 45 | + |
| 46 | + int gpuCount = (int) getGpuDetailsHandle.invoke(detailSegment, maxGpus, maxDetailLength); |
| 47 | + if (gpuCount < 0) { |
| 48 | + throw new RuntimeException("Failed to retrieve GPU details"); |
| 49 | + } |
| 50 | + |
| 51 | + // Convert MemorySegment to String |
| 52 | + String details = new String(detailSegment.toArray(ValueLayout.JAVA_BYTE), 0, gpuCount * maxDetailLength); |
| 53 | + return details.trim(); |
52 | 54 | } catch (Throwable e) { |
53 | | - System.err.println("Failed to invoke get_number_of_gpus: " + e.getMessage()); |
54 | | - return -1; // Return -1 to indicate an error |
| 55 | + System.err.println("Error invoking get_gpu_details: " + e.getMessage()); |
| 56 | + throw new RuntimeException("Failed to invoke get_gpu_details", e); |
55 | 57 | } |
56 | 58 | } |
57 | 59 |
|
|
0 commit comments