run.py 9.53 KB
Newer Older
Antoine Kaufmann's avatar
Antoine Kaufmann committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright 2021 Max Planck Institute for Software Systems, and
# National University of Singapore
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

23
import argparse
Jonas Kaufmann's avatar
Jonas Kaufmann committed
24
import fnmatch
25
import importlib
Hejing Li's avatar
Hejing Li committed
26
import importlib.util
27
import json
Jonas Kaufmann's avatar
Jonas Kaufmann committed
28
import os
29
import pickle
Jonas Kaufmann's avatar
Jonas Kaufmann committed
30
import sys
31
32
import typing as tp

33
34
35
from simbricks.exectools import LocalExecutor, RemoteExecutor
from simbricks.experiment.experiment_environment import ExpEnv
from simbricks.experiments import DistributedExperiment, Experiment
36
37
38
39
from simbricks.runtime.common import Run
from simbricks.runtime.distributed import DistributedSimpleRuntime, auto_dist
from simbricks.runtime.local import LocalParallelRuntime, LocalSimpleRuntime
from simbricks.runtime.slurm import SlurmRuntime
Jonas Kaufmann's avatar
Jonas Kaufmann committed
40
41


42
# pylint: disable=redefined-outer-name
43
44
45
46
def mkdir_if_not_exists(path):
    if not os.path.exists(path):
        os.mkdir(path)

47

48
parser = argparse.ArgumentParser()
Jonas Kaufmann's avatar
Jonas Kaufmann committed
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
parser.add_argument(
    'experiments',
    metavar='EXP',
    type=str,
    nargs='+',
    help='An experiment file to run'
)
parser.add_argument(
    '--list',
    action='store_const',
    const=True,
    default=False,
    help='Only list available experiment names'
)
parser.add_argument(
    '--filter',
    metavar='PATTERN',
    type=str,
    nargs='+',
    help='Pattern to match experiment names against'
)
parser.add_argument(
    '--pickled',
    action='store_const',
    const=True,
    default=False,
    help='Read exp files as pickled runs instead of exp.py files'
)
parser.add_argument(
    '--runs',
    metavar='N',
    type=int,
    default=1,
    help='Number of repetition for each experiment'
)
parser.add_argument(
    '--firstrun', metavar='N', type=int, default=1, help='ID for first run'
)
parser.add_argument(
    '--force',
    action='store_const',
    const=True,
    default=False,
    help='Run experiments even if output already exists'
)
parser.add_argument(
    '--verbose',
    action='store_const',
    const=True,
    default=False,
    help='Verbose output'
)
parser.add_argument(
    '--pcap',
    action='store_const',
    const=True,
    default=False,
    help='Dump pcap file (if supported by simulator)'
)
108
109

g_env = parser.add_argument_group('Environment')
Jonas Kaufmann's avatar
Jonas Kaufmann committed
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
g_env.add_argument(
    '--repo', metavar='DIR', type=str, default='..', help='Repo directory'
)
g_env.add_argument(
    '--workdir',
    metavar='DIR',
    type=str,
    default='./out/',
    help='Work directory base'
)
g_env.add_argument(
    '--outdir',
    metavar='DIR',
    type=str,
    default='./out/',
    help='Output directory base'
)
g_env.add_argument(
    '--cpdir',
    metavar='DIR',
    type=str,
    default='./out/',
    help='Checkpoint directory base'
)
g_env.add_argument(
    '--hosts',
    metavar='JSON_FILE',
    type=str,
    default=None,
    help='List of hosts to use (json)'
)
g_env.add_argument(
    '--shmdir',
    metavar='DIR',
    type=str,
    default=None,
    help='Shared memory directory base (workdir if not set)'
)
148

149
g_par = parser.add_argument_group('Parallel Runtime')
Jonas Kaufmann's avatar
Jonas Kaufmann committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
g_par.add_argument(
    '--parallel',
    dest='runtime',
    action='store_const',
    const='parallel',
    default='sequential',
    help='Use parallel instead of sequential runtime'
)
g_par.add_argument(
    '--cores',
    metavar='N',
    type=int,
    default=len(os.sched_getaffinity(0)),
    help='Number of cores to use for parallel runs'
)
g_par.add_argument(
    '--mem',
    metavar='N',
    type=int,
    default=None,
    help='Memory limit for parallel runs (in MB)'
)
172

173
g_slurm = parser.add_argument_group('Slurm Runtime')
Jonas Kaufmann's avatar
Jonas Kaufmann committed
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
g_slurm.add_argument(
    '--slurm',
    dest='runtime',
    action='store_const',
    const='slurm',
    default='sequential',
    help='Use slurm instead of sequential runtime'
)
g_slurm.add_argument(
    '--slurmdir',
    metavar='DIR',
    type=str,
    default='./slurm/',
    help='Slurm communication directory'
)
189

190
g_dist = parser.add_argument_group('Distributed Runtime')
Jonas Kaufmann's avatar
Jonas Kaufmann committed
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
g_dist.add_argument(
    '--dist',
    dest='runtime',
    action='store_const',
    const='dist',
    default='sequential',
    help='Use sequential distributed runtime instead of local'
)
g_dist.add_argument(
    '--auto-dist',
    action='store_const',
    const=True,
    default=False,
    help='Automatically distribute non-distributed experiments'
)
g_dist.add_argument(
    '--proxy-type',
    metavar='TYPE',
    type=str,
    default='sockets',
    help='Proxy type to use (sockets,rdma) for auto distribution'
)
213
args = parser.parse_args()
214

215

216
# pylint: disable=redefined-outer-name
217
def load_executors(path):
Jonas Kaufmann's avatar
Jonas Kaufmann committed
218
    """Load hosts list from json file and return list of executors."""
219
    with open(path, 'r', encoding='utf-8') as f:
220
221
222
223
224
        hosts = json.load(f)

        exs = []
        for h in hosts:
            if h['type'] == 'local':
225
                ex = LocalExecutor()
226
            elif h['type'] == 'remote':
227
                ex = RemoteExecutor(h['host'], h['workdir'])
228
229
230
231
                if 'ssh_args' in h:
                    ex.ssh_extra_args += h['ssh_args']
                if 'scp_args' in h:
                    ex.scp_extra_args += h['scp_args']
232
233
            else:
                raise RuntimeError('invalid host type "' + h['type'] + '"')
234
235
            ex.ip = h['ip']
            exs.append(ex)
236
237
    return exs

Jonas Kaufmann's avatar
Jonas Kaufmann committed
238

239
if args.hosts is None:
240
    executors = [LocalExecutor()]
241
242
243
else:
    executors = load_executors(args.hosts)

Jonas Kaufmann's avatar
Jonas Kaufmann committed
244

245
246
def warn_multi_exec():
    if len(executors) > 1:
Jonas Kaufmann's avatar
Jonas Kaufmann committed
247
248
249
250
251
        print(
            'Warning: multiple hosts specified, only using first one for now',
            file=sys.stderr
        )

252

253
# initialize runtime
254
if args.runtime == 'parallel':
255
    warn_multi_exec()
Jonas Kaufmann's avatar
Jonas Kaufmann committed
256
    rt = LocalParallelRuntime(
257
258
259
260
        cores=args.cores,
        mem=args.mem,
        verbose=args.verbose,
        executor=executors[0]
Jonas Kaufmann's avatar
Jonas Kaufmann committed
261
    )
262
elif args.runtime == 'slurm':
Jonas Kaufmann's avatar
Jonas Kaufmann committed
263
    rt = SlurmRuntime(args.slurmdir, args, verbose=args.verbose)
264
elif args.runtime == 'dist':
Jonas Kaufmann's avatar
Jonas Kaufmann committed
265
    rt = DistributedSimpleRuntime(executors, verbose=args.verbose)
266
else:
267
    warn_multi_exec()
268
    rt = LocalSimpleRuntime(verbose=args.verbose, executor=executors[0])
269

270

271
# pylint: disable=redefined-outer-name
272
def add_exp(
273
    e: Experiment,
Jonas Kaufmann's avatar
Jonas Kaufmann committed
274
275
276
277
278
    run: int,
    prereq: tp.Optional[Run],
    create_cp: bool,
    restore_cp: bool,
    no_simbricks: bool
279
):
280
    outpath = f'{args.outdir}/{e.name}-{run}.json'
281
    if os.path.exists(outpath) and not args.force:
282
        print(f'skip {e.name} run {run}')
283
284
        return None

285
286
    workdir = f'{args.workdir}/{e.name}/{run}'
    cpdir = f'{args.cpdir}/{e.name}/0'
287
    if args.shmdir is not None:
288
        shmdir = f'{args.shmdir}/{e.name}/{run}'
289

290
    env = ExpEnv(args.repo, workdir, cpdir)
291
292
    env.create_cp = create_cp
    env.restore_cp = restore_cp
Jonas Kaufmann's avatar
Jonas Kaufmann committed
293
    env.no_simbricks = no_simbricks
294
295
    env.pcap_file = ''
    if args.pcap:
Jonas Kaufmann's avatar
Jonas Kaufmann committed
296
        env.pcap_file = workdir + '/pcap'
297
298
    if args.shmdir is not None:
        env.shm_base = os.path.abspath(shmdir)
299

Jonas Kaufmann's avatar
Jonas Kaufmann committed
300
    run = Run(e, run, env, outpath, prereq)
301
302
303
    rt.add_run(run)
    return run

Jonas Kaufmann's avatar
Jonas Kaufmann committed
304

305
306
307
308
309
310
311
# load experiments
if not args.pickled:
    # default: load python modules with experiments
    experiments = []
    for path in args.experiments:
        modname, _ = os.path.splitext(os.path.basename(path))

312
313
314
        class ExperimentModuleLoadError(Exception):
            pass

315
        spec = importlib.util.spec_from_file_location(modname, path)
316
317
        if spec is None:
            raise ExperimentModuleLoadError('spec is None')
318
        mod = importlib.util.module_from_spec(spec)
319
320
        if spec.loader is None:
            raise ExperimentModuleLoadError('spec.loader is None')
321
322
        spec.loader.exec_module(mod)
        experiments += mod.experiments
323

324
325
326
327
328
    if args.list:
        for e in experiments:
            print(e.name)
        sys.exit(0)

329
    for e in experiments:
330
        if args.auto_dist and not isinstance(e, DistributedExperiment):
Jonas Kaufmann's avatar
Jonas Kaufmann committed
331
            e = auto_dist(e, executors, args.proxy_type)
332
        # apply filter if any specified
333
        if (args.filter) and (len(args.filter) > 0):
334
335
            match = False
            for f in args.filter:
336
337
                match = fnmatch.fnmatch(e.name, f)
                if match:
338
                    break
339

340
341
342
            if not match:
                continue

343
        # if this is an experiment with a checkpoint we might have to create it
344
        no_simbricks = e.no_simbricks
345
        if e.checkpoint:
346
            prereq = add_exp(e, 0, None, True, False, no_simbricks)
347
348
        else:
            prereq = None
349

350
        for run in range(args.firstrun, args.firstrun + args.runs):
351
            add_exp(e, run, prereq, False, e.checkpoint, no_simbricks)
352
353
354
355
356
else:
    # otherwise load pickled run object
    for path in args.experiments:
        with open(path, 'rb') as f:
            rt.add_run(pickle.load(f))
357

358
rt.start()