-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
158 lines (112 loc) · 3.96 KB
/
client.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/usr/bin/env python3
import argparse
import subprocess
import time
import db
import numpy as np
data = db.DB()
SLEEP_TIME = 10 # seconds
PARAM_PREFIX = 'flags:'
SCORE_PREFIX = 'final_score'
RESULT_INFO_PREFIX = '@@'
name = subprocess.getoutput('hostname')
def register_client():
# 1) check if we should do sth
gpu_info = subprocess.getoutput('nvidia-smi')
action = data.check_action(name)
print(action)
if action is not None:
if action[0] == 'restart':
print('restarting')
# todo: restart here
elif action[0] == 'shutdown':
data.set_client_status(name, db.SHUTDOWN, gpu_info)
import sys
sys.exit()
# 2) register (or update) client info on db
data.set_client_status(name, db.ONLINE, gpu_info)
def get_gpu_usage(gpu):
cur_usage = []
for i in range(3):
query = 'nvidia-smi --query-gpu=utilization.gpu --format=csv,nounits,noheader | sed -n "{}p"'.format(gpu+1)
cur_usage.append(int(subprocess.getoutput(query)))
time.sleep(1)
return np.mean(np.asarray(cur_usage))
def get_free_memory(gpu):
query = 'nvidia-smi --query-gpu=memory.free --format=csv,nounits,noheader | sed -n "{}p"'.format(gpu+1)
return int(subprocess.getoutput(query))
def process_task(gpu, skip_gpu_check):
# check GPU usage. If used, wait
if not skip_gpu_check: # magic number to just run it
usage = get_gpu_usage(gpu)
print('usage', usage)
if usage > 20:
time.sleep(SLEEP_TIME)
return
free_mem = get_free_memory(gpu)
print('free gpu memory', free_mem)
else:
free_mem = 99999
task = data.get_task(free_mem)
if task is None:
time.sleep(SLEEP_TIME)
return
print('processing task', task)
cuda_string = 'CUDA_VISIBLE_DEVICES=' + str(gpu)+' '
#(1, 3, None, u'cmd', u'params', u'queued', None, u'2017-10-03 11:10:35', None
(id_task, id_run, id_client, cmd, params, status, log, changed, score) = task
data.update_task(id_task, name, db.PROCESSING)
cmd += ' --taskid '+str(id_task)
proc = subprocess.Popen(cuda_string + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
log = []
while True:
output = proc.stdout.readline()
output = output.decode('utf-8')
if output == '' and proc.poll() is not None:
break
if output:
print('>> ' + output.strip())
log.append(output.strip())
rc = proc.poll()
if rc == 0:
param_lines = [line for line in log if line.startswith(PARAM_PREFIX)]
if len(param_lines):
params = param_lines[-1][len(PARAM_PREFIX)+1:]
else:
params = ''
score_lines = [line for line in log if line.startswith(SCORE_PREFIX)]
if len(score_lines):
score = float(score_lines[-1][len(SCORE_PREFIX)+1:])
else:
score = 0
data.update_task(id_task, name, db.DONE, '\n'.join(log), score, params=params)
# find additional info
info_lines = [line for line in log if line.startswith(RESULT_INFO_PREFIX)]
info = dict()
for line in info_lines:
try:
c_ind = line.index(':')
except ValueError:
continue
param_name = line[len(RESULT_INFO_PREFIX):c_ind]
value = line[c_ind+1:]
print(param_name, value)
info[param_name] = value
data.add_task_result_info(id_task, info)
else:
data.update_task(id_task, name, db.FAILED, '\n'.join(log))
print('exiting client, as task failed', name)
import sys
sys.exit()
time.sleep(1)
if __name__ == '__main__':
# really not much to do here
parser = argparse.ArgumentParser()
parser.add_argument("--gpu", help="GPU ID", default=0, type=int)
parser.add_argument("--skip_gpu_check", help="Skip check if GPU is available", default=False, type=bool)
args = parser.parse_args()
gpu = args.gpu
skip_gpu_check = args.skip_gpu_check
while True:
register_client()
process_task(gpu, skip_gpu_check)