-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslurm_utils.py
147 lines (119 loc) · 4.22 KB
/
slurm_utils.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
"""
"""
import subprocess
import re
cluster_name = 'coma'
def get_nodes_dict():
"""
return dictionary whose keys are the node names and whose entries are dictionaroes of properties
"""
result = subprocess.check_output(['scontrol', 'show', 'node']).strip()
node_entries = re.split(r'\n\n', result)
node_dict = {}
for node_entry in node_entries:
# split by new lines or spaces
node_entry = re.split(r'\n|\s+', node_entry)
# remove empty lines
node_entry = [x for x in node_entry if x != '']
try:
node_name = node_entry[0].split('=')[1]
# skip head node
if node_name == cluster_name:
pass
else:
# check if node is in dict
if node_name in node_dict.keys():
pass
else:
node_dict[node_name] = {}
# add node properties to node entry in dict
for prop in node_entry[1:]:
try:
key, value = prop.split('=', 1)
try:
node_dict[node_name][key] = value
except KeyError:
pass
#print(node_name, key, value)
except ValueError:
pass
#print(node_name, prop)
except IndexError:
print(node_entry)
# further process some property dictionaries
nodes = node_dict.keys()
for node in nodes:
# partitions
try:
partition_list = node_dict[node]['Partitions']
partition_list = partition_list.split(',')
node_dict[node]['Partitions'] = partition_list
except KeyError:
pass
try:
# active features
features_list = node_dict[node]['ActiveFeatures']
features_list = features_list.split(',')
node_dict[node]['ActiveFeatures'] = features_list
# available features
features_list = node_dict[node]['AvailableFeatures']
features_list = features_list.split(',')
node_dict[node]['AvailableFeatures'] = features_list
except KeyError:
pass
# CfgTRES
try:
l = node_dict[node]['CfgTRES']
l = l.split(',')
d = {}
for ll in l:
key, value = ll.split('=', 1)
d[key]=value
node_dict[node]['CfgTRES'] = d
except KeyError:
pass
# AllocTRES
try:
l = node_dict[node]['AllocTRES']
l = l.split(',')
d = {}
for ll in l:
try:
key, value = ll.split('=', 1)
d[key]=value
except ValueError:
pass
node_dict[node]['AllocTRES'] = d
except KeyError:
pass
return node_dict
def get_partitions_dict():
"""
return dictionary whose keys are the partitions with entries which are dictionaries of properties
"""
# read in 'sinfo output'
result = subprocess.check_output(['sinfo'])
lines = result.split('\n')
colnames = lines[0].split()
partition_dict = {}
for line in lines[1:-1]:
# remove * from partition name
partition = line.split()[0].strip('*')
avail = line.split()[1]
time_limit = line.split()[2]
num_nodes = int(line.split()[3])
state = line.split()[4].strip('*')
partition_dict[partition] = {'AVAIL': avail,
'TIMELIMIT': time_limit,
'NODES': num_nodes,
'STATE': state,
'NODELIST': []}
# populate node list for each partition entry
result = subprocess.check_output(['sinfo', '-N']).strip()
lines = result.split('\n')
for line in lines[1:-1]:
node_name = line.split()[0]
partition = line.split()[2].strip('*')
state = line.split()[3].strip('*')
partition_dict[partition]['NODELIST'].append(node_name)
return partition_dict