|
| 1 | +"""Parallel workflow execution via SLURM |
| 2 | +""" |
| 3 | + |
| 4 | +import os |
| 5 | +import sys |
| 6 | + |
| 7 | +from .base import (GraphPluginBase, logger) |
| 8 | + |
| 9 | +from ...interfaces.base import CommandLine |
| 10 | + |
| 11 | + |
| 12 | +def node_completed_status( checknode): |
| 13 | + """ |
| 14 | + A function to determine if a node has previously completed it's work |
| 15 | + :param checknode: The node to check the run status |
| 16 | + :return: boolean value True indicates that the node does not need to be run. |
| 17 | + """ |
| 18 | + """ TODO: place this in the base.py file and refactor """ |
| 19 | + node_state_does_not_require_overwrite = ( checknode.overwrite == False or |
| 20 | + (checknode.overwrite == None and |
| 21 | + not checknode._interface.always_run ) |
| 22 | + ) |
| 23 | + hash_exists = False |
| 24 | + try: |
| 25 | + hash_exists, _, _, _ = checknode.hash_exists() |
| 26 | + except Exception: |
| 27 | + hash_exists = False |
| 28 | + return (hash_exists and node_state_does_not_require_overwrite ) |
| 29 | + |
| 30 | + |
| 31 | +class SLURMGraphPlugin(GraphPluginBase): |
| 32 | + """Execute using SLURM |
| 33 | +
|
| 34 | + The plugin_args input to run can be used to control the SGE execution. |
| 35 | + Currently supported options are: |
| 36 | +
|
| 37 | + - template : template to use for batch job submission |
| 38 | + - qsub_args : arguments to be prepended to the job execution script in the |
| 39 | + qsub call |
| 40 | +
|
| 41 | + """ |
| 42 | + _template="#!/bin/bash" |
| 43 | + |
| 44 | + def __init__(self, **kwargs): |
| 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 | + if 'dont_resubmit_completed_jobs' in kwargs['plugin_args']: |
| 57 | + self._dont_resubmit_completed_jobs = kwargs['plugin_args']['dont_resubmit_completed_jobs'] |
| 58 | + else: |
| 59 | + self._dont_resubmit_completed_jobs = False |
| 60 | + super(SLURMGraphPlugin, self).__init__(**kwargs) |
| 61 | + |
| 62 | + def _submit_graph(self, pyfiles, dependencies, nodes): |
| 63 | + def make_job_name(jobnumber, nodeslist): |
| 64 | + """ |
| 65 | + - jobnumber: The index number of the job to create |
| 66 | + - nodeslist: The name of the node being processed |
| 67 | + - return: A string representing this job to be displayed by SLURM |
| 68 | + """ |
| 69 | + job_name='j{0}_{1}'.format(jobnumber, nodeslist[jobnumber]._id) |
| 70 | + # Condition job_name to be a valid bash identifier (i.e. - is invalid) |
| 71 | + job_name=job_name.replace('-','_').replace('.','_').replace(':','_') |
| 72 | + return job_name |
| 73 | + batch_dir, _ = os.path.split(pyfiles[0]) |
| 74 | + submitjobsfile = os.path.join(batch_dir, 'submit_jobs.sh') |
| 75 | + |
| 76 | + cache_doneness_per_node = dict() |
| 77 | + if self._dont_resubmit_completed_jobs: ## A future parameter for controlling this behavior could be added here |
| 78 | + for idx, pyscript in enumerate(pyfiles): |
| 79 | + node = nodes[idx] |
| 80 | + node_status_done = node_completed_status(node) |
| 81 | + |
| 82 | + #if the node itself claims done, then check to ensure all |
| 83 | + #dependancies are also done |
| 84 | + if node_status_done and idx in dependencies: |
| 85 | + for child_idx in dependencies[idx]: |
| 86 | + if child_idx in cache_doneness_per_node: |
| 87 | + child_status_done = cache_doneness_per_node[child_idx] |
| 88 | + else: |
| 89 | + child_status_done = node_completed_status(nodes[child_idx]) |
| 90 | + node_status_done = node_status_done and child_status_done |
| 91 | + |
| 92 | + cache_doneness_per_node[idx] = node_status_done |
| 93 | + |
| 94 | + with open(submitjobsfile, 'wt') as fp: |
| 95 | + fp.writelines('#!/usr/bin/env bash\n') |
| 96 | + fp.writelines('# Condense format attempted\n') |
| 97 | + for idx, pyscript in enumerate(pyfiles): |
| 98 | + node = nodes[idx] |
| 99 | + if cache_doneness_per_node.get(idx,False): |
| 100 | + continue |
| 101 | + else: |
| 102 | + template, sbatch_args = self._get_args( |
| 103 | + node, ["template", "sbatch_args"]) |
| 104 | + |
| 105 | + batch_dir, name = os.path.split(pyscript) |
| 106 | + name = '.'.join(name.split('.')[:-1]) |
| 107 | + batchscript = '\n'.join((template, |
| 108 | + '%s %s' % (sys.executable, pyscript))) |
| 109 | + batchscriptfile = os.path.join(batch_dir, |
| 110 | + 'batchscript_%s.sh' % name) |
| 111 | + |
| 112 | + batchscriptoutfile = batchscriptfile + '.o' |
| 113 | + batchscripterrfile = batchscriptfile + '.e' |
| 114 | + |
| 115 | + with open(batchscriptfile, 'wt') as batchfp: |
| 116 | + batchfp.writelines(batchscript) |
| 117 | + batchfp.close() |
| 118 | + deps = '' |
| 119 | + if idx in dependencies: |
| 120 | + values = '' |
| 121 | + for jobid in dependencies[idx]: |
| 122 | + ## Avoid dependancies of done jobs |
| 123 | + if not self._dont_resubmit_completed_jobs or cache_doneness_per_node[jobid] == False: |
| 124 | + values += "${{{0}}}:".format(make_job_name(jobid, nodes)) |
| 125 | + if values != '': # i.e. if some jobs were added to dependency list |
| 126 | + values = values.rstrip(':') |
| 127 | + deps = '--dependency=afterok:%s' % values |
| 128 | + jobname = make_job_name(idx, nodes) |
| 129 | + # Do not use default output locations if they are set in self._sbatch_args |
| 130 | + stderrFile = '' |
| 131 | + if self._sbatch_args.count('-e ') == 0: |
| 132 | + stderrFile = '-e {errFile}'.format( |
| 133 | + errFile=batchscripterrfile) |
| 134 | + stdoutFile = '' |
| 135 | + if self._sbatch_args.count('-o ') == 0: |
| 136 | + stdoutFile = '-o {outFile}'.format( |
| 137 | + outFile=batchscriptoutfile) |
| 138 | + full_line = '{jobNm}=$(sbatch {outFileOption} {errFileOption} {extraSBatchArgs} {dependantIndex} -J {jobNm} {batchscript} | awk \'{{print $4}}\')\n'.format( |
| 139 | + jobNm=jobname, |
| 140 | + outFileOption=stdoutFile, |
| 141 | + errFileOption=stderrFile, |
| 142 | + extraSBatchArgs=sbatch_args, |
| 143 | + dependantIndex=deps, |
| 144 | + batchscript=batchscriptfile) |
| 145 | + fp.writelines(full_line) |
| 146 | + cmd = CommandLine('bash', environ=os.environ.data, |
| 147 | + terminal_output='allatonce') |
| 148 | + cmd.inputs.args = '%s' % submitjobsfile |
| 149 | + cmd.run() |
| 150 | + logger.info('submitted all jobs to queue') |
0 commit comments