run.py 4.08 KB
Newer Older
1
2
3
4
import argparse
import sys
import os
import importlib
5
import pickle
6
import modes.experiments as exp
7
import modes.runtime as runtime
8
9
10
11
12

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

13

14
15
16
parser = argparse.ArgumentParser()
parser.add_argument('experiments', metavar='EXP', type=str, nargs='+',
        help='An experiment file to run')
17
18
19
parser.add_argument('--pickled', action='store_const', const=True,
        default=False,
        help='Read exp files as pickled runs instead of exp.py files')
20
parser.add_argument('--runs', metavar='N', type=int, default=1,
21
        help='Number of repetition for each experiment')
22
23
parser.add_argument('--firstrun', metavar='N', type=int, default=1,
        help='ID for first run')
24
25
parser.add_argument('--force', action='store_const', const=True, default=False,
        help='Run experiments even if output already exists')
26
27
28
parser.add_argument('--verbose', action='store_const', const=True,
        default=False,
        help='Verbose output')
29
30
31

g_env = parser.add_argument_group('Environment')
g_env.add_argument('--repo', metavar='DIR', type=str,
32
        default='..', help='Repo directory')
33
g_env.add_argument('--workdir', metavar='DIR', type=str,
34
        default='./out/', help='Work directory base')
35
g_env.add_argument('--outdir', metavar='DIR',  type=str,
36
        default='./out/', help='Output directory base')
37
38
g_env.add_argument('--cpdir', metavar='DIR',  type=str,
        default='./out/', help='Checkpoint directory base')
39

40
41
42
43
44
45
46
47
48
49
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)')

50
51
52
53
54
55
56
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')

57

58
args = parser.parse_args()
59

60
# initialize runtime
61
if args.runtime == 'parallel':
62
63
    rt = runtime.LocalParallelRuntime(cores=args.cores, mem=args.mem,
            verbose=args.verbose)
64
65
elif args.runtime == 'slurm':
    rt = runtime.SlurmRuntime(args.slurmdir, args, verbose=args.verbose)
66
else:
67
    rt = runtime.LocalSimpleRuntime(verbose=args.verbose)
68

69
70
def add_exp(e, run, prereq, create_cp, restore_cp):
    outpath = '%s/%s-%d.json' % (args.outdir, e.name, run)
71
    if os.path.exists(outpath) and not args.force:
72
73
74
75
        print('skip %s run %d' % (e.name, run))
        return None

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

78
    env = exp.ExpEnv(args.repo, workdir, cpdir)
79
80
81
82
83
84
85
    env.create_cp = create_cp
    env.restore_cp = restore_cp

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

86
87
88
89
90
91
92
93
94
95
96
# 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
97

98
    for e in experiments:
99
100
101
102
103
        # if this is an experiment with a checkpoint we might have to create it
        if e.checkpoint:
            prereq = add_exp(e, 0, None, True, False)
        else:
            prereq = None
104

105
106
        for run in range(args.firstrun, args.firstrun + args.runs):
            add_exp(e, run, prereq, False, e.checkpoint)
107
108
109
110
111
else:
    # otherwise load pickled run object
    for path in args.experiments:
        with open(path, 'rb') as f:
            rt.add_run(pickle.load(f))
112

113
rt.start()