-
Notifications
You must be signed in to change notification settings - Fork 2
/
hmsConfToDot.py
328 lines (276 loc) · 10.5 KB
/
hmsConfToDot.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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#
# generate graphviz dot file containing schematic of HMS setup across
# nodes for the given configuration file
#
# generated dot file is optimized for osage layout:
# python hmsConfToDot.py hmsConf.ini machinefile_algorithm | osage -Tpng -o foo.png
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import SafeConfigParser as ConfigParser
import math
import sys
#
# convert dictionary to sorted list of pairs
#
def dictToList(dictionary):
return sorted([(key, value) for key, value in dictionary.items()])
#
# parse machinefile and return list containing (hostname, number
# processor) pairs
#
#
# parse machinefile and return list containing (hostname, number
# processor) pairs
#
def parseMachinefile(fileName):
hosts = {}
machinefile = open(fileName)
for line in machinefile.readlines():
line = line.strip()
line = line.split()
host = line[0]
numHosts = 1
for token in line[1:]:
if token.startswith("slots="):
numHosts = int(token[6:])
if host in hosts:
hosts[host] += numHosts
else:
hosts[host] = numHosts
hosts = dictToList(hosts)
return hosts
#
# print header of dot file
#
def printHeader():
print("digraph hms {")
print("\tsize = \"100,100\";")
print("\tnode [shape=box, fontsize=12, style=filled];")
print("\tpackmode = array_u\n")
#
# style of various node types box
#
algorithmStyle = "[fillcolor = red]"
adaptiveSamplingStyle = "[fillcolor = white]"
brokerStyle = "[fillcolor = yellow]"
modelStyle = "[fillcolor = blue]"
#
# print edge between nodes
#
def printEdge(source, dest):
print("\t\"" + source + "\" -> \"" + dest + "\"")
#
# print footer of dot file
#
def printFooter():
print("}")
#
# print usage information
#
def printUsage(argv):
sys.stderr.write(argv[0] + " [hmsConf.ini] [algorithm machinefile]\n")
if __name__ == "__main__":
if len(sys.argv) != 3:
printUsage(sys.argv)
sys.exit(-1)
configFileName = sys.argv[1]
machineFileName = sys.argv[2]
#
# parse config file and machinefile to obtain list of resources
# for algorithm, adaptive sampling, brokers, and models
#
configParser = ConfigParser()
configParser.read(configFileName)
algorithmResources = parseMachinefile(machineFileName)
adaptiveSamplingHosts = (
configParser.get("AdaptiveSampling", "Hosts").split())
brokerHosts = configParser.get("Broker", "Hosts").split()
brokerResourceList = configParser.get("Broker", "Resources").split()
brokerResourceTypes = configParser.get("Broker", "ResourceListTypes").split()
if (len(brokerHosts) != len(brokerResourceList) and
len(brokerResourceList) == 1) :
brokerResourceList = [brokerResourceList[0] for i in brokerHosts]
if (len(brokerHosts) != len(brokerResourceTypes) and
len(brokerResourceTypes) == 1) :
brokerResourceTypes = [brokerResourceTypes[0] for i in brokerHosts]
if (len(brokerHosts) != len(brokerResourceList) or
len(brokerHosts) != len(brokerResourceTypes)):
sys.stderr.write("Error in file - number of broker resource files does "
"not equal number of brokers\n")
sys.exit(-1)
brokerResources = []
for resource, resourceType in zip(brokerResourceList, brokerResourceTypes):
if resourceType == "MPI":
brokerResources.append(parseMachinefile(resource))
elif resourceType == "List":
resourceDict = {}
for splitResource in resource.split(","):
if splitResource in resourceDict:
resourceDict[splitResource] += 1
else:
resourceDict[splitResource] = 1
brokerResources.append(dictToList(resourceDict))
elif resourceType == "Localhost":
continue
else:
sys.stderr.write("Invalid resource type in file.\n")
sys.exit(-1)
#
# get all hosts for entire problem
#
allHosts = set()
for algorithmResource in algorithmResources:
allHosts.add(algorithmResource[0])
for adaptiveSamplingHost in adaptiveSamplingHosts:
allHosts.add(adaptiveSamplingHost)
for brokerHost in brokerHosts:
allHosts.add(brokerHost)
for brokerResource in brokerResources:
for resource in brokerResource:
allHosts.add(resource[0])
#
# print header of dot file
#
printHeader()
#
# print each host as subgraph
#
allHosts = sorted(allHosts)
algIndex = 0
asIndex = 0
brokerIndex = 0
lsIndex = 0
for index, host in enumerate(allHosts):
print("\tsubgraph cluster" + str(index) + " {")
print("\t\tsortv = " + str(index)+ ";")
print("\t\tpackmode = \"node\";")
print("\t\tlabel = \"" + host + "\";")
# handle algorithm hosts
print("\t\tsubgraph cluster" + str(index) + "0 {")
print("\t\t\tstyle = \"invis\";")
for algorithmResource in algorithmResources:
if algorithmResource[0] == host:
for i in range(0, algorithmResource[1]):
print ("\t\t\t\"Algorithm " + str(algIndex) + "\" " +
algorithmStyle + " [sortv = " + str(algIndex) + "]")
algIndex += 1
print("\t\t}")
# handle adaptive sampling hosts
print("\t\tsubgraph cluster" + str(index) + "1 {")
print("\t\t\tstyle = \"invis\";")
print("\t\t\tpackmode = array_u;")
for adaptiveSamplingHost in adaptiveSamplingHosts:
if adaptiveSamplingHost == host:
print ("\t\t\t\"Adaptive Sampling " + str(asIndex) + "\" " +
adaptiveSamplingStyle + " [sortv = " + str(asIndex)
+ "]")
asIndex += 1
print("\t\t}")
# handle broker hosts
print("\t\tsubgraph cluster" + str(index) + "2 {")
print("\t\t\tstyle = \"invis\";")
print("\t\t\tpackmode = array_u;")
for brokerHost in brokerHosts:
if brokerHost == host:
print ("\t\t\t\"Broker " + str(brokerIndex) + "\" " +
brokerStyle + " [sortv = " + str(brokerIndex) + "]")
brokerIndex += 1
print("\t\t}")
# handle lower scale models
print("\t\tsubgraph cluster" + str(index) + "3 {")
print("\t\t\tstyle = \"invis\";")
print("\t\t\tpackmode = array_u;")
for brokerResource in brokerResources:
for resource in brokerResource:
if resource[0] == host:
for i in range(0, resource[1]):
print ("\t\t\t\"Model " + str(lsIndex) + "\" " +
modelStyle + " [sortv = " + str(lsIndex) + "]")
lsIndex += 1
print("\t\t}")
print("\t}")
#
# print edges
#
numberAlgorithmProcessors = 0
for algorithmResource in algorithmResources:
numberAlgorithmProcessors += algorithmResource[1]
#
# adaptive sampling to brokers
#
if len(adaptiveSamplingHosts) > 0:
#
# algorithm to adaptive sampling
#
if numberAlgorithmProcessors % len(adaptiveSamplingHosts) != 0:
sys.stderr.write("Cannot equally divide " +
str(numberAlgorithmProcessors) +
" algorithm processors among " +
str(len(adaptiveSamplingHosts)) + " adaptive" +
" sampling modules - unequal partitioning is" +
" currently unsupported in the code\n")
sys.exit(-1)
numberAlgorithmProcessorsPerAdaptiveSampling = int(
numberAlgorithmProcessors / len(adaptiveSamplingHosts))
algIndex = 0
for algorithmResource in algorithmResources:
for i in range(0, algorithmResource[1]):
printEdge("Algorithm " + str(algIndex),
("Adaptive Sampling " +
str(int(algIndex /
numberAlgorithmProcessorsPerAdaptiveSampling))))
algIndex += 1
#
# adaptive sampling to brokers
#
if len(brokerHosts) % len(adaptiveSamplingHosts) != 0:
sys.stderr.write("Cannot equally divide " + str(len(brokerHosts)) +
" brokers among " +
str(len(adaptiveSamplingHosts)) + " adaptive" +
" sampling modules - unequal partitioning is" +
" currently unsupported in the code\n")
sys.exit(-1)
numberBrokersPerAdaptiveSampling = int(
len(brokerHosts) / len(adaptiveSamplingHosts))
brokerIndex = 0
for i in range(0, len(adaptiveSamplingHosts)):
for j in range(0, numberBrokersPerAdaptiveSampling):
printEdge("Adaptive Sampling " + str(i),
"Broker " + str(brokerIndex))
brokerIndex += 1
else :
#
# algorithm to brokers
#
if numberAlgorithmProcessors % len(brokerHosts) != 0:
sys.stderr.write("Cannot equally divide " +
str(numberAlgorithmProcessors) + " algorithm"
" processors among " + str(len(brokerHosts)) +
" brokers - unequal partitioning is currently" +
" unsupported in the code\n")
sys.exit(-1)
numberAlgorithmProcessorsPerBroker = int(
numberAlgorithmProcessors / len(brokerHosts))
algIndex = 0
for algorithmResource in algorithmResources:
for i in range(0, algorithmResource[1]):
printEdge("Algorithm " + str(algIndex),
("Broker " +
str(int(algIndex /
numberAlgorithmProcessorsPerBroker))))
algIndex += 1
#
# brokers to lower scale models
#
lsIndex = 0
for index, brokerResource in enumerate(brokerResources):
for resource in brokerResource:
for i in range(0, resource[1]):
printEdge("Broker " + str(index),
"Model " + str(lsIndex))
lsIndex += 1
#
# print footer
#
printFooter()