-
Notifications
You must be signed in to change notification settings - Fork 0
/
gpu.py
78 lines (60 loc) · 2.1 KB
/
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
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
import sys
import os
import subprocess
import json
import argparse
empty_node_usage_cmd = 'empty-node-usage'
get_node_info_cmd = 'get-node-info'
empty_node_capacity_cmd = 'empty-node-capacity'
def pretty_print_json(d):
print(json.dumps(d, indent=2))
def empty_gpu_usage(cwd, nodename):
cmd = [os.path.join(cwd, "gpu"), "fix-node-resource"]
d = {
"nodename": nodename,
}
in_str = json.dumps(d)
out = subprocess.run(cmd, cwd=cwd, input=in_str, capture_output=True, text=True).stdout
return json.loads(out)
def empty_gpu_capacity(cwd, nodename):
cmd = [os.path.join(cwd, "gpu"), "set-node-resource-capacity"]
d = {
"nodename": nodename,
"delta": False,
"incr": False,
}
in_str = json.dumps(d)
out = subprocess.run(cmd, cwd=cwd, input=in_str, capture_output=True, text=True).stdout
return json.loads(out)
def get_node_info(cwd, nodename):
cmd = [os.path.join(cwd, "gpu"), "get-node-resource-info"]
d = {
"nodename": nodename,
}
in_str = json.dumps(d)
out = subprocess.run(cmd, cwd=cwd, input=in_str, capture_output=True, text=True).stdout
return json.loads(out)
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument("-cwd", "--cwd", help="dir of binary", default=os.getcwd())
parser.add_argument("-nodename", "--nodename", help="nodename", required=True)
subparsers = parser.add_subparsers(dest="cmd")
subparsers.add_parser(empty_node_usage_cmd)
subparsers.add_parser(empty_node_capacity_cmd)
subparsers.add_parser(get_node_info_cmd)
args = parser.parse_args()
return args
if __name__ == "__main__":
args = parse_args()
if args.cmd == empty_node_usage_cmd:
ans = empty_gpu_usage(args.cwd, args.nodename)
pretty_print_json(ans)
elif args.cmd == empty_node_capacity_cmd:
ans = empty_gpu_capacity(args.cwd, args.nodename)
pretty_print_json(ans)
elif args.cmd == get_node_info_cmd:
ans = get_node_info(args.cwd, args.nodename)
pretty_print_json(ans)
else:
print("unknown cmd")
sys.exit(-1)