run.py 6.09 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 pickle
29
import fnmatch
30
31
import simbricks.experiments as exp
import simbricks.runtime as runtime
32
33
34
35
36

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

37

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

g_env = parser.add_argument_group('Environment')
g_env.add_argument('--repo', metavar='DIR', type=str,
60
        default='..', help='Repo directory')
61
g_env.add_argument('--workdir', metavar='DIR', type=str,
62
        default='./out/', help='Work directory base')
63
g_env.add_argument('--outdir', metavar='DIR',  type=str,
64
        default='./out/', help='Output directory base')
65
66
g_env.add_argument('--cpdir', metavar='DIR',  type=str,
        default='./out/', help='Checkpoint directory base')
67

68
69
70
71
72
73
74
75
76
77
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)')

78
79
80
81
82
83
84
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')

85

86
args = parser.parse_args()
87

88
# initialize runtime
89
if args.runtime == 'parallel':
90
91
    rt = runtime.LocalParallelRuntime(cores=args.cores, mem=args.mem,
            verbose=args.verbose)
92
93
elif args.runtime == 'slurm':
    rt = runtime.SlurmRuntime(args.slurmdir, args, verbose=args.verbose)
94
else:
95
    rt = runtime.LocalSimpleRuntime(verbose=args.verbose)
96

97
def add_exp(e, run, prereq, create_cp, restore_cp, no_simbricks):
98
    outpath = '%s/%s-%d.json' % (args.outdir, e.name, run)
99
    if os.path.exists(outpath) and not args.force:
100
101
102
103
        print('skip %s run %d' % (e.name, run))
        return None

    workdir = '%s/%s/%d' % (args.workdir, e.name, run)
104
    cpdir = '%s/%s/%d' % (args.cpdir, e.name, 0)
105

106
    env = exp.ExpEnv(args.repo, workdir, cpdir)
107
108
    env.create_cp = create_cp
    env.restore_cp = restore_cp
109
    env.no_simbricks=no_simbricks
110
111
112
    env.pcap_file = ''
    if args.pcap:
        env.pcap_file = workdir+'/pcap'
113
114
115
116
117

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

118
119
120
121
122
123
124
125
126
127
128
# 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
129

130
    for e in experiments:
131
        # apply filter if any specified
132
        if (args.filter) and (len(args.filter) > 0):
133
134
135
136
137
138
139
140
            match = False
            for f in args.filter:
                if fnmatch.fnmatch(e.name, f):
                    match = True
                    break
            if not match:
                continue

141
        # if this is an experiment with a checkpoint we might have to create it
142
143
144
145
        if e.no_simbricks:
                no_simbricks = True
        else:
                no_simbricks = False
146
        if e.checkpoint:
147
            prereq = add_exp(e, 0, None, True, False, no_simbricks)
148
149
        else:
            prereq = None
150

151
        for run in range(args.firstrun, args.firstrun + args.runs):
152
            add_exp(e, run, prereq, False, e.checkpoint, no_simbricks)
153
154
155
156
157
else:
    # otherwise load pickled run object
    for path in args.experiments:
        with open(path, 'rb') as f:
            rt.add_run(pickle.load(f))
158

159
rt.start()