-
Notifications
You must be signed in to change notification settings - Fork 0
/
generateHMSConf.py
209 lines (178 loc) · 5.46 KB
/
generateHMSConf.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
#
# generate hmsConf.ini given machinefile containing total resources and
# number of algorithm processors, adaptive sampling modules, and
# brokers - partitioning resources appropriately
#
try:
from configparser import ConfigParser
except ImportError:
from ConfigParser import SafeConfigParser as ConfigParser
import sys
#
# whether to write machinefiles for broker resources
#
isWriteMachinefile = False
#
# conver list to string without braces
#
def listToString(listToOutput):
string = ""
for item in listToOutput:
string += str(item) + " "
return string[:-1]
#
# 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
#
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
#
# round robin scheduling
#
def roundRobin(hosts, numberProcessors):
assignedHosts = {}
for i in range(0, numberProcessors):
hostIndex = i % len(hosts)
host = hosts[hostIndex]
hostName = host[0]
if hostName in assignedHosts:
assignedHosts[hostName] += 1
else:
assignedHosts[hostName] = 1
hosts[hostIndex] = (hostName, host[1] - 1)
assignedHosts = dictToList(assignedHosts)
return assignedHosts
#
# output machinefile for each host
#
def writeMachinefile(fileName, hosts):
f = open(fileName, "w")
for index, host in enumerate(hosts):
for i in range(0, host[1]):
f.write(host[0] + "\n")
#
# print usage information
#
def printUsage(argv):
sys.stderr.write(argv[0] + " [hmsConf.ini] " +
"[machinefile of all resources] " +
"[number algorithm processors] " +
"[number adaptive sampling modules] [overload-factor]\n")
machinefilePrefix = ""
if __name__ == "__main__":
if len(sys.argv) != 6:
printUsage(sys.argv)
sys.exit(-1)
configFileName = sys.argv[1]
machineFileName = sys.argv[2]
numberAlgorithmProcessors = int(sys.argv[3])
numberAdaptiveSamplingModules = int(sys.argv[4])
overloadFactor = int(sys.argv[5])
#
# parse machinefile containing all resources
#
hosts = parseMachinefile(machineFileName)
#
# remove hosts for adaptive sampling
#
assert(numberAdaptiveSamplingModules < len(hosts))
adaptiveSamplingHosts = []
for i in range(0, numberAdaptiveSamplingModules):
#
# remove node from hosts
#
adaptiveSamplingHost = hosts.pop()[0]
#
# append to adaptiveSamplingHosts
#
adaptiveSamplingHosts.append(adaptiveSamplingHost)
#
# compute total number of resources left
#
totalNumberResources = 0
for host, resourceAmount in hosts:
totalNumberResources += resourceAmount
#
# generate algorithm hosts
#
assert(numberAlgorithmProcessors < totalNumberResources)
algorithmHosts = roundRobin(hosts, numberAlgorithmProcessors)
#
# generate broker hosts
#
brokerHosts = []
for i, host in enumerate(hosts):
host = hosts[i]
hostName = host[0]
brokerHosts.append(hostName)
hosts[i] = (hostName, host[1] - 1)
#
# modify hosts for overload factor
#
for i, host in enumerate(hosts):
host = hosts[i]
hosts[i] = (host[0], host[1] * overloadFactor)
#
# output lower scale model machinefiles (for each broker)
#
modelResources = []
for i, host in enumerate(hosts):
if isWriteMachinefile:
fileName = "machinefile_broker" + "_" + str(i)
writeMachinefile(fileName, [host])
modelResources.append(fileName)
else:
hostName = host[0]
resources = (hostName + ",") * (host[1] - 1) + hostName
modelResources.append(resources)
#
# output upper scale algorithm machinefile
#
writeMachinefile("machinefile_algorithm", algorithmHosts)
#
# read configuration file
#
configParser = ConfigParser()
configParser.optionxform = str # maintain case
configParser.read(configFileName)
configParser.set("AdaptiveSampling",
"Hosts",
listToString(adaptiveSamplingHosts))
configParser.set("Broker",
"Hosts",
listToString(brokerHosts))
configParser.set("Broker",
"Resources",
listToString(modelResources))
configParser.set("Broker",
"ResourceTypes",
"CPU " * len(brokerHosts))
if isWriteMachinefile:
configParser.set("Broker",
"ResourceListTypes",
"MPI " * len(brokerHosts))
else:
configParser.set("Broker",
"ResourceListTypes",
"List " * len(brokerHosts))
configParser.write(sys.stdout)