File size: 919 Bytes
8ab1018
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"""
Module for continuously printing the GPU usage after a set interval, until
it is terminated.
"""

import time
import GPUtil

# Set the interval for printing GPU memory (in seconds)
INTERVAL = 2

def print_gpu_memory():
    try:
        # Get the list of available GPUs
        gpus = GPUtil.getGPUs()

        for i, gpu in enumerate(gpus):
            print(f"GPU {i + 1}: {gpu.name}")
            print(f"    Memory Free: {gpu.memoryFree:.2f} MB | Memory Used: {gpu.memoryUsed:.2f} MB | Memory Total: {gpu.memoryTotal:.2f} MB")
        
        print("\n-----------------------------------------\n")

    except Exception as e:
        print(f"Error while getting GPU information: {e}")


def main():
    try:
        while True:
            print_gpu_memory()
            time.sleep(INTERVAL)

    except KeyboardInterrupt:
        print("Monitoring stopped by the user.")

if __name__ == "__main__":
    main()