param_runner 3.29 KB
Newer Older
David Trudgian's avatar
David Trudgian committed
1
2
3
4
5
6
7
8
#!/usr/bin/env python2


"""
param_runner - Run an application multiple times on the BioHPC cluster,
  exploring a parameter space and summarizing results.

Usage:
9
10
11
12
13
14
  param_runner check <paramfile> [--verbose]
  param_runner submit <paramfile> [--verbose]
  param_runner srun <paramfile> [--verbose]
  param_runner run <paramfile> [--verbose]

param_runner -h | --help | --version
David Trudgian's avatar
David Trudgian committed
15
16
17
18
19
20
21

Options:
  --verbose     Show debug messages

"""

import logging
David Trudgian's avatar
David Trudgian committed
22
import os
23
24
25
26
27
import sys
import subprocess

import colorlog
from docopt import docopt
David Trudgian's avatar
David Trudgian committed
28
29

from runner import __version__
David Trudgian's avatar
David Trudgian committed
30
from runner import executors
31
from runner import param
32

David Trudgian's avatar
David Trudgian committed
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

def main():
    arguments = docopt(__doc__, version='param_runner %s' % __version__)

    handler = colorlog.StreamHandler()
    handler.setFormatter(colorlog.ColoredFormatter(
        '%(log_color)s%(levelname)-8s %(message)s'))

    logger = colorlog.getLogger()
    logger.addHandler(handler)

    if arguments['--verbose']:
        logger.setLevel(logging.DEBUG)
    else:
        logger.setLevel(logging.INFO)

    if arguments['<paramfile>']:

51
        param_file = arguments['<paramfile>']
David Trudgian's avatar
David Trudgian committed
52
53
54
55
56
57
58
59
60
61

        print "param_runner - version %s" % __version__
        print "-------------------------------"
        print "Parameter exploration runner for the BioHPC Cluster"
        print "D. C. Trudgian, UT Southwestern BioHPC"
        print "biohpc-help@utsouthwestern.edu"
        print

        try:

62
63
64
65
66
67
68
69
70
71
72
73
74
75
            p = param.ParamFile(param_file)
            p.load()

            if arguments['run']:
                runner = executors.LocalExecutor(p.commands, os.path.dirname(
                    (os.path.abspath(param_file))), p)
                runner.run()

            if arguments['srun']:
                runner = executors.SrunExecutor(p.commands, os.path.dirname(
                    (os.path.abspath(param_file))), p)
                runner.run()

            if arguments['submit']:
David Trudgian's avatar
David Trudgian committed
76

77
                # We submit using sbatch
David Trudgian's avatar
David Trudgian committed
78

79
80
81
82
83
84
85
86
87
88
                python_exe = sys.executable
                this_script = os.path.abspath(__file__)

                logger.debug("Python is %s" % python_exe)
                logger.debug("Script location is %s" % this_script)

                nodes = str(p.vals['nodes'])
                partition = str(p.vals['partition'])
                time_limit = str(p.vals['time_limit'])

89
                sbatch_cmd = [ 'sbatch', '-N', nodes, '-p', partition, '-t', time_limit, '-oparam_runner_%j.out' ]
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111

                batch_script = "#!/bin/bash\n"
                batch_script += python_exe
                batch_script += ' -u '
                batch_script += this_script
                batch_script += ' srun '
                batch_script += os.path.abspath(param_file)
                batch_script += ' 2>&1\n'

                logger.debug(sbatch_cmd)

                sbatch_proc = subprocess.Popen( sbatch_cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
                          stderr=subprocess.PIPE)

                stdout, stderr = sbatch_proc.communicate(batch_script)

                if stdout:
                    logger.info(stdout)
                if stderr:
                    logger.error(stderr)

            logger.info("Done.")
David Trudgian's avatar
David Trudgian committed
112
113

        except Exception as e:
114
            logger.error("[%s] %s" % (e.__class__.__name__, e))
David Trudgian's avatar
David Trudgian committed
115
116
117
            print "\n"


118
119


David Trudgian's avatar
David Trudgian committed
120
if __name__ == '__main__':
121
    main()