|
| 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 (SLURMLikeBatchManagerBase, logger, iflogger, logging) |
| 15 | + |
| 16 | +from nipype.interfaces.base import CommandLine |
| 17 | + |
| 18 | + |
| 19 | + |
| 20 | + |
| 21 | +class SLURMPlugin(SLURMLikeBatchManagerBase): |
| 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 | +
|
| 31 | + ''' |
| 32 | + |
| 33 | + |
| 34 | + def __init__(self, **kwargs): |
| 35 | + ''' |
| 36 | + Constructor |
| 37 | + ''' |
| 38 | + template=""" |
| 39 | +#!/bin/sh |
| 40 | + """ |
| 41 | + self._retry_timeout = 2 |
| 42 | + self._max_tries = 2 |
| 43 | + if 'plugin_args' in kwargs and kwargs['plugin_args']: |
| 44 | + if 'retry_timeout' in kwargs['plugin_args']: |
| 45 | + self._retry_timeout = kwargs['plugin_args']['retry_timeout'] |
| 46 | + if 'max_tries' in kwargs['plugin_args']: |
| 47 | + self._max_tries = kwargs['plugin_args']['max_tries'] |
| 48 | + super(SLURMPlugin, self).__init__(template, **kwargs) |
| 49 | + |
| 50 | + def _is_pending(self, taskid): |
| 51 | + # subprocess.Popen requires taskid to be a string |
| 52 | + proc = subprocess.Popen(["showq", '-u'], |
| 53 | + stdout=subprocess.PIPE, |
| 54 | + stderr=subprocess.PIPE) |
| 55 | + o, _ = proc.communicate() |
| 56 | + return o.find(taskid) > -1 |
| 57 | + |
| 58 | + def _submit_batchtask(self, scriptfile, node): |
| 59 | + cmd = CommandLine('sbatch', environ=os.environ.data, |
| 60 | + terminal_output='allatonce') |
| 61 | + path = os.path.dirname(scriptfile) |
| 62 | + |
| 63 | + slurmargs = '' |
| 64 | + if self._slurmargs: |
| 65 | + slurmargs = self._slurm_args |
| 66 | + if 'slurm_args' in node.plugin_args: |
| 67 | + if 'overwrite' in node.plugin_args and\ |
| 68 | + node.plugin_args['overwrite']: |
| 69 | + slurmargs = node.plugin_args['slurm_args'] |
| 70 | + else: |
| 71 | + slurmargs += (" " + node.plugin_args['slurm_args']) |
| 72 | + if '-o' not in slurmargs: |
| 73 | + slurmargs = '%s -o %s' % (slurmargs, path) |
| 74 | + if '-e' not in slurmargs: |
| 75 | + slurmargs = '%s -e %s' % (slurmargs, path) |
| 76 | + if node._hierarchy: |
| 77 | + jobname = '.'.join((os.environ.data['LOGNAME'], |
| 78 | + node._hierarchy, |
| 79 | + node._id)) |
| 80 | + else: |
| 81 | + jobname = '.'.join((os.environ.data['LOGNAME'], |
| 82 | + node._id)) |
| 83 | + jobnameitems = jobname.split('.') |
| 84 | + jobnameitems.reverse() |
| 85 | + jobname = '.'.join(jobnameitems) |
| 86 | + cmd.inputs.args = '%s -J %s %s' % (slurmargs, |
| 87 | + jobname, |
| 88 | + scriptfile) |
| 89 | + oldlevel = iflogger.level |
| 90 | + iflogger.setLevel(logging.getLevelName('CRITICAL')) |
| 91 | + tries = 0 |
| 92 | + while True: |
| 93 | + try: |
| 94 | + result = cmd.run() |
| 95 | + except Exception, e: |
| 96 | + if tries < self._max_tries: |
| 97 | + tries += 1 |
| 98 | + sleep(self._retry_timeout) # sleep 2 seconds and try again. |
| 99 | + else: |
| 100 | + iflogger.setLevel(oldlevel) |
| 101 | + raise RuntimeError('\n'.join((('Could not submit slurm task' |
| 102 | + ' for node %s') % node._id, |
| 103 | + str(e)))) |
| 104 | + else: |
| 105 | + break |
| 106 | + iflogger.setLevel(oldlevel) |
| 107 | + # retrieve sge taskid |
| 108 | + lines = [line for line in result.runtime.stdout.split('\n') if line] |
| 109 | + taskid = int(re.match("Your job ([0-9]*) .* has been submitted", |
| 110 | + lines[-1]).groups()[0]) |
| 111 | + self._pending[taskid] = node.output_dir() |
| 112 | + logger.debug('submitted slurm task: %d for node %s' % (taskid, node._id)) |
| 113 | + return taskid |
0 commit comments