|
| 1 | +''' |
| 2 | +Created on Aug 2, 2013 |
| 3 | +
|
| 4 | +@author: chadcumba |
| 5 | +
|
| 6 | +Parallel workflow execution with SLURM |
| 7 | +''' |
| 8 | + |
| 9 | +import os |
| 10 | +import re |
| 11 | +import subprocess |
| 12 | +from time import sleep |
| 13 | + |
| 14 | +from .base import (SGELikeBatchManagerBase, logger, iflogger, logging) |
| 15 | + |
| 16 | +from nipype.interfaces.base import CommandLine |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +class SLURMPlugin(SGELikeBatchManagerBase): |
| 22 | + ''' |
| 23 | + Execute using SLURM |
| 24 | +
|
| 25 | + The plugin_args input to run can be used to control the SLURM execution. |
| 26 | + Currently supported options are: |
| 27 | +
|
| 28 | + - template : template to use for batch job submission |
| 29 | + |
| 30 | + - sbatch_args: arguments to pass prepend to the sbatch call |
| 31 | + |
| 32 | +
|
| 33 | + ''' |
| 34 | + |
| 35 | + |
| 36 | + def __init__(self, **kwargs): |
| 37 | + |
| 38 | + template="#!/bin/bash" |
| 39 | + |
| 40 | + self._retry_timeout = 2 |
| 41 | + self._max_tries = 2 |
| 42 | + self._template = template |
| 43 | + self._sbatch_args = None |
| 44 | + |
| 45 | + if 'plugin_args' in kwargs and kwargs['plugin_args']: |
| 46 | + if 'retry_timeout' in kwargs['plugin_args']: |
| 47 | + self._retry_timeout = kwargs['plugin_args']['retry_timeout'] |
| 48 | + if 'max_tries' in kwargs['plugin_args']: |
| 49 | + self._max_tries = kwargs['plugin_args']['max_tries'] |
| 50 | + if 'template' in kwargs['plugin_args']: |
| 51 | + self._template = kwargs['plugin_args']['template'] |
| 52 | + if os.path.isfile(self._template): |
| 53 | + self._template = open(self._template).read() |
| 54 | + if 'sbatch_args' in kwargs['plugin_args']: |
| 55 | + self._sbatch_args = kwargs['plugin_args']['sbatch_args'] |
| 56 | + self._pending = {} |
| 57 | + super(SLURMPlugin, self).__init__(template, **kwargs) |
| 58 | + |
| 59 | + def _is_pending(self, taskid): |
| 60 | + # subprocess.Popen requires taskid to be a string |
| 61 | + proc = subprocess.Popen(["showq", '-u'], |
| 62 | + stdout=subprocess.PIPE, |
| 63 | + stderr=subprocess.PIPE) |
| 64 | + o, _ = proc.communicate() |
| 65 | + |
| 66 | + return o.find(str(taskid)) > -1 |
| 67 | + |
| 68 | + def _submit_batchtask(self, scriptfile, node): |
| 69 | + """ |
| 70 | + This is more or less the _submit_batchtask from sge.py with flipped variable |
| 71 | + names, different command line switches, and different output formatting/processing |
| 72 | + """ |
| 73 | + cmd = CommandLine('sbatch', environ=os.environ.data, |
| 74 | + terminal_output='allatonce') |
| 75 | + path = os.path.dirname(scriptfile) |
| 76 | + |
| 77 | + sbatch_args = '' |
| 78 | + if self._sbatch_args: |
| 79 | + sbatch_args = self._sbatch_args |
| 80 | + if 'sbatch_args' in node.plugin_args: |
| 81 | + if 'overwrite' in node.plugin_args and\ |
| 82 | + node.plugin_args['overwrite']: |
| 83 | + sbatch_args = node.plugin_args['sbatch_args'] |
| 84 | + else: |
| 85 | + sbatch_args += (" " + node.plugin_args['sbatch_args']) |
| 86 | + if '-o' not in sbatch_args: |
| 87 | + sbatch_args = '%s -o %s' % (sbatch_args, os.path.join(path, 'slurm-%j.out')) |
| 88 | + if '-e' not in sbatch_args: |
| 89 | + sbatch_args = '%s -e %s' % (sbatch_args, os.path.join(path, 'slurm-%j.out')) |
| 90 | + if '-p' not in sbatch_args: |
| 91 | + sbatch_args = '%s -p normal' % (sbatch_args) |
| 92 | + if '-n' not in sbatch_args: |
| 93 | + sbatch_args = '%s -n 16' % (sbatch_args) |
| 94 | + if '-t' not in sbatch_args: |
| 95 | + sbatch_args = '%s -t 1:00:00' % (sbatch_args) |
| 96 | + if node._hierarchy: |
| 97 | + jobname = '.'.join((os.environ.data['LOGNAME'], |
| 98 | + node._hierarchy, |
| 99 | + node._id)) |
| 100 | + else: |
| 101 | + jobname = '.'.join((os.environ.data['LOGNAME'], |
| 102 | + node._id)) |
| 103 | + jobnameitems = jobname.split('.') |
| 104 | + jobnameitems.reverse() |
| 105 | + jobname = '.'.join(jobnameitems) |
| 106 | + cmd.inputs.args = '%s -J %s %s' % (sbatch_args, |
| 107 | + jobname, |
| 108 | + scriptfile) |
| 109 | + oldlevel = iflogger.level |
| 110 | + iflogger.setLevel(logging.getLevelName('CRITICAL')) |
| 111 | + tries = 0 |
| 112 | + while True: |
| 113 | + try: |
| 114 | + result = cmd.run() |
| 115 | + except Exception, e: |
| 116 | + if tries < self._max_tries: |
| 117 | + tries += 1 |
| 118 | + sleep(self._retry_timeout) # sleep 2 seconds and try again. |
| 119 | + else: |
| 120 | + iflogger.setLevel(oldlevel) |
| 121 | + raise RuntimeError('\n'.join((('Could not submit sbatch task' |
| 122 | + ' for node %s') % node._id, |
| 123 | + str(e)))) |
| 124 | + else: |
| 125 | + break |
| 126 | + logger.debug('Ran command ({0})'.format(cmd.cmdline)) |
| 127 | + iflogger.setLevel(oldlevel) |
| 128 | + # retrieve taskid |
| 129 | + lines = [line for line in result.runtime.stdout.split('\n') if line] |
| 130 | + taskid = int(re.match("Submitted batch job ([0-9]*)", |
| 131 | + lines[-1]).groups()[0]) |
| 132 | + self._pending[taskid] = node.output_dir() |
| 133 | + logger.debug('submitted sbatch task: %d for node %s' % (taskid, node._id)) |
| 134 | + return taskid |
0 commit comments