-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathscheduler.py
101 lines (75 loc) · 2.38 KB
/
scheduler.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
from subprocess import call, Popen, run
import time
import threading
import queue
import os
from pynvml.pynvml import *
import time
# start_time = time.time()
# call(["python", "main.py", "--train=./train.py" , "--batch_size=10", "--spatial_epochs=100", "--temporal_epochs=100", "--train_id=default", "--dB=SAMM_CASME_Optical", "--spatial_size=224", "--flag=st"])
# elapsed_time = time.time() - start_time
# print(str(elapsed_time) + " seconds\n")
def console(q, lock):
while 1:
input()
print("%i process on queue." % (q.qsize()))
input()
with lock:
cmd = input('> ')
# q.put(cmd)
if cmd == 'quit':
break
def invalid_input(lock):
with lock:
print("Invalid Command")
def action_help(lock):
with lock:
print("Run python scripts, for eg: python main.py --dB 'CASME2_Optical'")
def run_process(q, lock, threshold):
while 1:
with lock:
free_mem = check_gpu_resources()
print("Available VRAM: %0.2f %%" % (free_mem))
input()
print("%i process on queue." % (q.qsize()))
cmd = input('command> ')
filename = input('filename> ')
cmd = "nohup " + cmd + " > " + filename + "&"
q.put(cmd)
free_mem = check_gpu_resources()
print("Available VRAM: %0.2f %%" % (free_mem))
# if free_mem >= float(threshold):
# cmd = q.get()
# run(cmd, shell=True, check=True)
def check_gpu_resources():
nvmlInit()
for i in range(nvmlDeviceGetCount()):
handle = nvmlDeviceGetHandleByIndex(i)
meminfo = nvmlDeviceGetMemoryInfo(handle)
# print("%s: %0.1f MB free, %0.1f MB used, %0.1f MB total" % (
# nvmlDeviceGetName(handle),
# meminfo.free/1024.**2, meminfo.used/1024.**2, meminfo.total/1024.**2))
free_memory = meminfo.free/1024.**2
used_memory = meminfo.used/1024.**2
total_memory = meminfo.total/1024.**2
free_percentage = ( free_memory / total_memory ) * 100
return free_percentage
def main():
cmd_queue = queue.Queue()
stdout_lock = threading.Lock()
cmd_actions = {'help': action_help, 'run_process': run_process}
threshold = input('Threshold > ')
dj = threading.Thread(target=run_process, args=(cmd_queue, stdout_lock, threshold))
dj.start()
flag = 0
while 1:
free_mem = check_gpu_resources()
if free_mem > float(threshold) and flag == 0:
cmd = cmd_queue.get()
flag = 1
run(cmd, shell=True, check=True)
start = time.time()
elif flag == 1 and (time.time()-start) > 120:
flag = 0
cmd = cmd_queue.qsize()
main()