File size: 705 Bytes
8d4d3e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import pynvml

try:
    pynvml.nvmlInit()
    device_count = pynvml.nvmlDeviceGetCount()
    print(f"Number of GPUs: {device_count}")
    
    for i in range(device_count):
        handle = pynvml.nvmlDeviceGetHandleByIndex(i)
        name = pynvml.nvmlDeviceGetName(handle)
        memory_info = pynvml.nvmlDeviceGetMemoryInfo(handle)
        print(f"GPU {i}: {name.decode('utf-8')}")
        print(f"  Memory Total: {memory_info.total / (1024 ** 2)} MB")
        print(f"  Memory Free: {memory_info.free / (1024 ** 2)} MB")
        print(f"  Memory Used: {memory_info.used / (1024 ** 2)} MB")
        
    pynvml.nvmlShutdown()
except pynvml.NVMLError as e:
    print(f"Failed to initialize NVML: {e}")