-
Notifications
You must be signed in to change notification settings - Fork 1
/
simple_scheduler.py
executable file
·95 lines (74 loc) · 2.32 KB
/
simple_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
from multiprocessing import Pool
import os
import os.path as osp
import subprocess
import time
import random
from copy import deepcopy
import random
def cfg2cmd(ccfg, gpuID=0):
cfg = deepcopy(ccfg)
cmd = "CUDA_VISIBLE_DEVICES=%d " % gpuID
cmd += " python %s " % cfg["main"]
del cfg["main"]
for key, value in cfg.items():
cmd += " --%s %s" % (key, value)
return cmd
def run(cfg, gpuID=None):
# time.sleep(random.randint(1, 10) * 3)
cmd = "CUDA_VISIBLE_DEVICES=%d " % gpuID
cmd += " python %s " % cfg["main"]
del cfg["main"]
for key, value in cfg.items():
cmd += " --%s %s" % (key, value)
os.makedirs("logs", exist_ok=True)
with open("logs/gpu-%d.txt" % gpuID, "w+") as fp:
subprocess.call(cmd, shell=True, stdout=fp, stderr=fp)
if __name__ == "__main__":
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('config', metavar='CFG',
help='path to configuration')
parser.add_argument('-t', '--threads', type=int, default=4,
help="how many GPUs are avaliable on this machine")
args = parser.parse_args()
with open(args.config, "r") as fp:
configs = json.load(fp)
threads = args.threads
# if threads <= 1:
# run(configs[0])
# else:
# p = Pool(4)
# p.map(run, configs)
it = iter(configs)
from threading import Thread
import time
import random
from queue import Queue
from threading import Thread, Condition, Lock
my_queue = Queue(threads)
def Producer():
global my_queue
for each in configs:
my_queue.put(each)
class Consumer():
def __init__(self, id):
self.id = id
def __call__(self):
global my_queue
while not my_queue.empty():
cfg = my_queue.get()
cmd = cfg2cmd(cfg, self.id)
print("Consumer %d:" % self.id, cmd)
run(cfg, self.id)
time.sleep(1)
my_queue.task_done()
P = Thread(name="Producer", target=Producer)
C_list = []
for i in range(threads):
c = Thread(name="Consumer%d" % i, target=Consumer(i))
C_list.append(c)
P.start()
for consumer in C_list:
consumer.start()