run.py 8.34 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
24
25
26
import argparse
import sys
import os
import importlib
Hejing Li's avatar
Hejing Li committed
27
import importlib.util
28
import json
29
import pickle
30
import fnmatch
31
32
import typing as tp

33
import simbricks.exectools as exectools
34
35
import simbricks.experiments as exp
import simbricks.runtime as runtime
36
37
38
39
40

def mkdir_if_not_exists(path):
    if not os.path.exists(path):
        os.mkdir(path)

41

42
43
44
parser = argparse.ArgumentParser()
parser.add_argument('experiments', metavar='EXP', type=str, nargs='+',
        help='An experiment file to run')
45
46
parser.add_argument('--filter', metavar='PATTERN', type=str, nargs='+',
        help='Pattern to match experiment names against')
47
48
49
parser.add_argument('--pickled', action='store_const', const=True,
        default=False,
        help='Read exp files as pickled runs instead of exp.py files')
50
parser.add_argument('--runs', metavar='N', type=int, default=1,
51
        help='Number of repetition for each experiment')
52
53
parser.add_argument('--firstrun', metavar='N', type=int, default=1,
        help='ID for first run')
54
55
parser.add_argument('--force', action='store_const', const=True, default=False,
        help='Run experiments even if output already exists')
56
57
58
parser.add_argument('--verbose', action='store_const', const=True,
        default=False,
        help='Verbose output')
59
60
parser.add_argument('--pcap', action='store_const', const=True, default=False,
        help='Dump pcap file (if supported by simulator)')
61
62
63

g_env = parser.add_argument_group('Environment')
g_env.add_argument('--repo', metavar='DIR', type=str,
64
        default='..', help='Repo directory')
65
g_env.add_argument('--workdir', metavar='DIR', type=str,
66
        default='./out/', help='Work directory base')
67
g_env.add_argument('--outdir', metavar='DIR',  type=str,
68
        default='./out/', help='Output directory base')
69
70
g_env.add_argument('--cpdir', metavar='DIR',  type=str,
        default='./out/', help='Checkpoint directory base')
71
72
g_env.add_argument('--hosts', metavar='JSON_FILE', type=str,
        default=None, help='List of hosts to use (json)')
73
74
g_env.add_argument('--shmdir', metavar='DIR',  type=str,
        default=None, help='Shared memory directory base (workdir if not set)')
75

76
77
78
79
80
81
82
83
84
85
g_par = parser.add_argument_group('Parallel Runtime')
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)')

86
87
88
89
90
91
92
g_slurm = parser.add_argument_group('Slurm Runtime')
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')

93
g_dist = parser.add_argument_group('Distributed Runtime')
94
g_dist.add_argument('--dist', dest='runtime', action='store_const',
95
96
        const='dist', default='sequential',
        help='Use sequential distributed runtime instead of local')
97
g_dist.add_argument('--auto-dist', action='store_const', const=True,
98
99
        default=False,
        help='Automatically distribute non-distributed experiments')
100
101
102
g_dist.add_argument('--proxy-type', metavar='TYPE', type=str,
        default='sockets',
        help='Proxy type to use (sockets,rdma) for auto distribution')
103
args = parser.parse_args()
104

105
106
107
108
109
110
111
112
113

def load_executors(path):
    """ Load hosts list from json file and return list of executors. """
    with open(path, 'r') as f:
        hosts = json.load(f)

        exs = []
        for h in hosts:
            if h['type'] == 'local':
114
                ex = exectools.LocalExecutor()
115
            elif h['type'] == 'remote':
116
                ex = exectools.RemoteExecutor(h['host'], h['workdir'])
117
118
            else:
                raise RuntimeError('invalid host type "' + h['type'] + '"')
119
120
            ex.ip = h['ip']
            exs.append(ex)
121
122
123
124
125
126
127
128
129
130
131
132
    return exs

if args.hosts is None:
    executors = [exectools.LocalExecutor()]
else:
    executors = load_executors(args.hosts)

def warn_multi_exec():
    if len(executors) > 1:
        print('Warning: multiple hosts specified, only using first one for now',
                file=sys.stderr)

133
# initialize runtime
134
if args.runtime == 'parallel':
135
    warn_multi_exec()
136
    rt = runtime.LocalParallelRuntime(cores=args.cores, mem=args.mem,
137
            verbose=args.verbose, exec=executors[0])
138
139
elif args.runtime == 'slurm':
    rt = runtime.SlurmRuntime(args.slurmdir, args, verbose=args.verbose)
140
141
elif args.runtime == 'dist':
    rt = runtime.DistributedSimpleRuntime(executors, verbose=args.verbose)
142
else:
143
144
    warn_multi_exec()
    rt = runtime.LocalSimpleRuntime(verbose=args.verbose, exec=executors[0])
145

146
147
148
149
150

def add_exp(
    e: exp.Experiment, run: int, prereq: tp.Optional[runtime.Run],
    create_cp: bool, restore_cp: bool, no_simbricks: bool
):
151
    outpath = '%s/%s-%d.json' % (args.outdir, e.name, run)
152
    if os.path.exists(outpath) and not args.force:
153
154
155
156
        print('skip %s run %d' % (e.name, run))
        return None

    workdir = '%s/%s/%d' % (args.workdir, e.name, run)
157
    cpdir = '%s/%s/%d' % (args.cpdir, e.name, 0)
158
159
    if args.shmdir is not None:
        shmdir = '%s/%s/%d' % (args.shmdir, e.name, run)
160

161
    env = exp.ExpEnv(args.repo, workdir, cpdir)
162
163
    env.create_cp = create_cp
    env.restore_cp = restore_cp
164
    env.no_simbricks=no_simbricks
165
166
167
    env.pcap_file = ''
    if args.pcap:
        env.pcap_file = workdir+'/pcap'
168
169
    if args.shmdir is not None:
        env.shm_base = os.path.abspath(shmdir)
170
171
172
173
174

    run = runtime.Run(e, run, env, outpath, prereq)
    rt.add_run(run)
    return run

175
176
177
178
179
180
181
182
183
184
185
# 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))

        spec = importlib.util.spec_from_file_location(modname, path)
        mod = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(mod)
        experiments += mod.experiments
186

187
    for e in experiments:
188
        if args.auto_dist and not isinstance(e, exp.DistributedExperiment):
189
            e = runtime.auto_dist(e, executors, args.proxy_type)
190
        # apply filter if any specified
191
        if (args.filter) and (len(args.filter) > 0):
192
193
194
195
196
197
198
199
            match = False
            for f in args.filter:
                if fnmatch.fnmatch(e.name, f):
                    match = True
                    break
            if not match:
                continue

200
        # if this is an experiment with a checkpoint we might have to create it
201
202
203
204
        if e.no_simbricks:
                no_simbricks = True
        else:
                no_simbricks = False
205
        if e.checkpoint:
206
            prereq = add_exp(e, 0, None, True, False, no_simbricks)
207
208
        else:
            prereq = None
209

210
        for run in range(args.firstrun, args.firstrun + args.runs):
211
            add_exp(e, run, prereq, False, e.checkpoint, no_simbricks)
212
213
214
215
216
else:
    # otherwise load pickled run object
    for path in args.experiments:
        with open(path, 'rb') as f:
            rt.add_run(pickle.load(f))
217

218
rt.start()