-
Notifications
You must be signed in to change notification settings - Fork 12
/
select_gpu.py
executable file
·47 lines (45 loc) · 1.78 KB
/
select_gpu.py
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
39
40
41
42
43
44
45
46
47
import subprocess
import logging
def select_gpu():
nvidia_info = subprocess.run('nvidia-smi', stdout=subprocess.PIPE)
gpu_info = False
gpu_info_line = 0
proc_info = False
gpu_mem = []
gpu_occupied = set()
i = 0
for line in nvidia_info.stdout.split(b'\n'):
line = line.decode().strip()
if gpu_info:
gpu_info_line += 1
if line == '':
gpu_info = False
continue
if gpu_info_line % 3 == 2:
mem_info = line.split('|')[2]
used_mem_mb = int(mem_info.strip().split()[0][:-3])
gpu_mem.append(used_mem_mb)
if proc_info:
if line == '| No running processes found |':
continue
if line == '+-----------------------------------------------------------------------------+':
proc_info = False
continue
proc_gpu = int(line.split()[1])
#proc_type = line.split()[3]
gpu_occupied.add(proc_gpu)
if line == '|===============================+======================+======================|':
gpu_info = True
if line == '|=============================================================================|':
proc_info = True
i += 1
for i in range(0,len(gpu_mem)):
if i not in gpu_occupied:
logging.info('Automatically selected GPU %d because it is vacant.', i)
return i
for i in range(0,len(gpu_mem)):
if gpu_mem[i] == min(gpu_mem):
logging.info('All GPUs are occupied. Automatically selected GPU %d because it has the most free memory.', i)
return i
if __name__ == '__main__':
print(select_gpu())