-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.lua
438 lines (358 loc) · 11.9 KB
/
main.lua
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
local OptionParser = require"std.optparse"
local lpeg = require"lpeg"
local posix = require"posix"
function verifyUndeclared(_, n)
if (n ~= "_PROMPT") then
error("attempt to read undeclared variable " .. n)
end
end
setmetatable(_G, {
__index = verifyUndeclared,
})
command = {
short = "Run MPI in docker containers",
long = [[
IBM Research 2017
Usage: dockerw -f hosts -n procs img cmd
Run a command with mpi exec in a cluster of ephemerous containers
dockerw will start one worker container for every host in the host file
if the number of processes is equal or greater than the number of hosts.
Otherwise the number of containers started will be the same as the number of
processes
Options:
-n, --procs=N The total number of processes
-f, --hosts=hostfile A file containing all available hosts as well as the number of processors
--version display version information, then exit
--help display this help, then exit
Please report bugs to [email protected]
]],
validate = function(argErr, args, opts)
if #args < 2 then
argErr("You must specify the image name followed by a command for mpiexec to run")
end
local config = {}
if opts.procs then
config.procs = tonumber(opts.procs)
end
if opts.hosts then
config.hostfile = opts.hosts
config.hostspec = parseHostFile(config.hostfile)
end
config.image = args[1]
config.command = deepCopy(args)
table.remove(config.command, 1)
return config
end,
execute = function(config)
local configFile, err = loadfile("config.lua")
if not configFile then
error("Error loading file config.lua: "..err)
end
configFile()
run(config)
end
}
function deepCopy(el)
if type(el) ~= "table" then
return el
else
local copy = {}
for k,v in pairs(el) do copy[deepCopy(k)] = deepCopy(v) end
return copy
end
end
-- Begin lpeg error handling wrapper
lpeg.locale(lpeg)
local P, S, V = lpeg.P, lpeg.S, lpeg.V
local C, Carg, Cb, Cc = lpeg.C, lpeg.Carg, lpeg.Cb, lpeg.Cc
local Cf, Cg, Cmt, Cp, Ct = lpeg.Cf, lpeg.Cg, lpeg.Cmt, lpeg.Cp, lpeg.Ct
local alpha, digit, alnum = lpeg.alpha, lpeg.digit, lpeg.alnum
local xdigit = lpeg.xdigit
local space = lpeg.space
function escape(str)
local s = str
s = string.gsub(s, "\n", "\\n")
s = string.gsub(s, "\r", "\\r")
return s
end
local function loc (str, where)
local line, pos, linepos = 1, 1, 1
local lbegin, lend = 1
while true do
pos = string.find (str, "\n", pos, true)
if pos and pos < where then
line = line + 1
linepos = pos
pos = pos + 1
else
lbegin = linepos
lend = pos
break
end
end
return line, (where - linepos), lbegin, lend
end
function newLabeledFailure(str, pos, label, msg)
return {str = str, pos = pos, label = label, msg = msg}
end
function newState()
return {
currentLabel = nil,
}
end
function fail(msg, l)
return Cmt(Carg(1), function(s, i, state)
state.currentLabel = newLabeledFailure(s, i, l, msg)
return false
end) * P(false)
end
function Pf(pat, l)
l = l or pat
return P(pat) + fail(fmt("expected %s", escape(tostring(pat))), l)
end
function clear()
return Cmt(Carg(1), function(s, i, state)
state.currentLabel = nil
return i
end)
end
function getLabel(state)
if state.currentLabel then
return state.currentLabel.label
end
end
function find(t,e)
if not t then
return not e
end
for _,v in ipairs(t) do if v == e then return true end end
return false
end
function Y(p1, p2, ls)
return p1*clear() + (
Cmt(Carg(1), function(s, i, state)
local l = getLabel(state)
if not find(ls, l) then
return nil
else
return i
end
end)
* p2
) + P(false)
end
function trace(num)
return Cmt(P(true), function(s, i)
print("checkpoint "..tostring(num))
return i
end)
end
newline = P("\r\n") + S("\r\n"); -- cr + lf
wspace = S(" \t")
xwspace = S(" \t\r\n")
Hex = ((P("0x") + P("0X")) * xdigit^1) + fail("Expected a hexadecimal integer", "HEX")
Expo = S("eE") * S("+-")^-1 * digit^1;
Float = ((((digit^1 * P(".") * digit^0) + (P(".") * digit^1)) * Expo^-1) + (digit^1 * Expo)) + fail("Expected a floating point number", "FLOAT")
Int = digit^1 + fail("Expected an integer", "INT")
Number = ((P"-"^-1 * (Hex + Float + Int)) / function (n) return tonumber(n) end) + fail("Expected a number", "Number")
Comment = C(P("--") * (1 - P("\n"))^0)
function lmatch(pat, str, pos)
local state = newState()
local ret = { lpeg.match(pat, str, pos or 1, state) }
if not ret[1] then
if state.currentLabel then
local c = state.currentLabel
local line, col, lbegin, lend = loc(c.str, c.pos)
if lend then lend = lend - 1 end
local msg = string.format("Error at line %d, column %d: %s\nContext:\n%s\n", line, col, c.msg, string.sub(c.str, lbegin, lend))
local s = '^'
for k=2,col do s = '~'..s end
msg = msg..s
return nil, msg
else
return nil, "Unknown error"
end
end
return unpack(ret)
end
function multiply_pattern(prefix, item)
return lpeg.Cmt(Carg(1)*prefix*Ct(item^0), function(s,i,state,count,t)
if #t ~= count then
state.currentLabel = newLabeledFailure(s, i, "MULT", fmt("A set of exactly %d elements was expected, %d were found", count, #t))
return nil
else
return i, t
end
end)
end
function multiply_pattern_numeric(prefix, item)
local t = {}
local count = nil
local pos = 0
local function peek_count(s, i, c)
count = tonumber(c)
if ffi then
t = new_float_array(count)
end
return i
end
local function insert_number(s,i,n)
pos = pos + 1
if pos <= count then
t[pos] = tonumber(n)
end
return i
end
return lpeg.Cmt(Carg(1)*lpeg.Cmt(prefix,peek_count)*lpeg.Cmt(C(item), insert_number)^0, function(s,i,state, ...)
local rest = {...}
print("size of captures = ", #rest)
if pos ~= count then
state.currentLabel = newLabeledFailure(s, i, "MULT", fmt("A set of exactly %d elements was expected, %d were found", count, pos))
return nil
else
return i, t
end
end)
end
function anywhere (p, ...)
local labels = {...}
return lpeg.P{ Y(p, 1 * P(V(1)), labels) }
end
-- End lpeg error handling wrapper
function parseHostString(hosts)
local hostString_G = {
"hosts",
hostname = C((alnum + S("-_"))^1);
procs = C(Int^1);
host = Ct((Cg(V"hostname", "hostname") * ":" * Cg(V"procs", "procs")) + Cg(V"hostname", "hostname"));
hosts = Ct((wspace^0 * V"host" * wspace^0 * newline^1)^1);
}
return assert(lmatch(hostString_G, hosts))
end
function parseHostFile(filePath)
local hostFile = assert(io.open(filePath))
local input = hostFile:read"*all"
hostFile:close()
return parseHostString(input)
end
function planExecution(config)
local exec_hosts = {}
local nprocs = config.procs or 1
if config.hostspec then
local alloc_guard = nprocs
for _,v in ipairs(config.hostspec) do
slots = v.procs or 1
table.insert(exec_hosts, {v.hostname, slots})
alloc_guard = alloc_guard - slots
if alloc_guard <= 0 then break end
end
else
for i=1,nprocs do
--we repeat localhost to simplify
table.insert(exec_hosts, {"localhost", 1})
end
end
return exec_hosts
end
function runDockerCommand(contname, img, cmd, host)
local cmd_t = {"/usr/bin/docker", "-H", options.docker_host, "run", "-i", "--rm", "--net=host", "--name", contname }
for _,opt in ipairs(options.extra_docker_options or {}) do table.insert(cmd_t, opt) end
table.insert(cmd_t, img)
if type(cmd) == "table" then
for k,v in ipairs(cmd) do table.insert(cmd_t, v) end
elseif type(cmd) == "string" then
table.insert(cmd_t, cmd)
else
print("wrong cmd type")
return nil
end
if host then
table.insert(cmd_t, 1, host)
table.insert(cmd_t, 1, "/usr/bin/ssh")
end
local cmd = table.concat(cmd_t, " ")
print("going to run", cmd)
return io.popen(cmd)
end
function runDockerCommand_swarm(contname, img, cmd, host_constraint)
local cmd_t = {"/usr/bin/docker", "-H", options.docker_host, "run", "-i", "--rm", "--net=mpi_exp2", "--name", contname, "--hostname", contname, host_constraint }
for _,opt in ipairs(options.extra_docker_options or {}) do table.insert(cmd_t, opt) end
table.insert(cmd_t, img)
if type(cmd) == "table" then
for k,v in ipairs(cmd) do table.insert(cmd_t, v) end
elseif type(cmd) == "string" then
table.insert(cmd_t, cmd)
else
print("wrong cmd type")
return nil
end
local cmd = table.concat(cmd_t, " ")
print("going to run", cmd)
return io.popen(cmd)
end
function run(config)
local hosts = planExecution(config)
local hoststring = {}
if options.mode == 'single' then
for k,v in ipairs(hosts) do
local contname = "worker_"..tostring(k)
if #hoststring > 0 then table.insert(hoststring,",") end
table.insert(hoststring, contname)
table.insert(hoststring, ":")
table.insert(hoststring, tostring(v[2]))
end
else
for k,v in ipairs(hosts) do
if #hoststring > 0 then table.insert(hoststring,",") end
table.insert(hoststring, v[1])
table.insert(hoststring, ":")
table.insert(hoststring, tostring(v[2]))
end
end
hoststring = table.concat(hoststring)
local full_command = { "/usr/lib64/mvapich2/bin/mpiexec", "--launcher", "manual", "-n", tostring(config.procs or 1), "-hosts", hoststring }
for _,v in ipairs(config.command) do
table.insert(full_command, v)
end
local mpiexec_proc = nil
if options.mode == 'single' then
mpiexec_proc = runDockerCommand_swarm("master", config.image, full_command, "")
else
mpiexec_proc = runDockerCommand("master", config.image, full_command)
end
local hydra_commands = {}
while true do
local line = mpiexec_proc:read("*line")
if not line then break end
if line == "HYDRA_LAUNCH_END" then break end
local hydra_cmd, subs = string.gsub(line, "HYDRA_LAUNCH: ", "")
if not subs then
print("unrecognized line from mpiexec:")
print("line")
os.exit(1)
end
if options.mode == 'single' then
hydra_cmd = string.gsub(hydra_cmd, "masternode", "master")
end
table.insert(hydra_commands, hydra_cmd)
end
local worker_procs = {}
for i=1,#hydra_commands do
local contname = "worker_"..tostring(i)
if options.mode == 'single' then
table.insert(worker_procs, runDockerCommand_swarm(contname, config.image, hydra_commands[i], "-e constraint:node==nx36000"..tostring(i)..".compute"))
else
table.insert(worker_procs, runDockerCommand(contname, config.image, hydra_commands[i], "nx36000"..tostring(i)..".compute"))
end
end
while true do
local line = mpiexec_proc:read("*line")
if not line then break end
print(line)
end
mpiexec_proc:close()
end
local parser = OptionParser(command.long)
args, opts = parser:parse (arg)
local config = command.validate(function(...) parser:opterr(...) end, args, opts)
command.execute(config)