|
| 1 | +# Copyright 2016-2021 Swiss National Supercomputing Centre (CSCS/ETH Zurich) |
| 2 | +# ReFrame Project Developers. See the top-level LICENSE file for details. |
| 3 | +# |
| 4 | +# SPDX-License-Identifier: BSD-3-Clause |
| 5 | + |
| 6 | +# |
| 7 | +# LSF backend |
| 8 | +# |
| 9 | +# - Initial version submitted by Ryan Goodner, UNM (based on PBS backend) |
| 10 | +# |
| 11 | + |
| 12 | +import functools |
| 13 | +import re |
| 14 | +import time |
| 15 | + |
| 16 | +import reframe.core.runtime as rt |
| 17 | +import reframe.utility.osext as osext |
| 18 | +from reframe.core.backends import register_scheduler |
| 19 | +from reframe.core.exceptions import JobSchedulerError |
| 20 | +from reframe.core.schedulers.pbs import PbsJobScheduler |
| 21 | + |
| 22 | +_run_strict = functools.partial(osext.run_command, check=True) |
| 23 | + |
| 24 | + |
| 25 | +@register_scheduler('lsf') |
| 26 | +class LsfJobScheduler(PbsJobScheduler): |
| 27 | + def __init__(self): |
| 28 | + self._prefix = '#BSUB' |
| 29 | + self._submit_timeout = rt.runtime().get_option( |
| 30 | + f'schedulers/@{self.registered_name}/job_submit_timeout' |
| 31 | + ) |
| 32 | + |
| 33 | + def emit_preamble(self, job): |
| 34 | + num_tasks_per_node = job.num_tasks_per_node or 1 |
| 35 | + num_nodes = job.num_tasks // num_tasks_per_node |
| 36 | + |
| 37 | + preamble = [ |
| 38 | + self._format_option(f'-J {job.name}'), |
| 39 | + self._format_option(f'-o {job.stdout}'), |
| 40 | + self._format_option(f'-e {job.stderr}'), |
| 41 | + self._format_option(f'-nnodes {num_nodes}') |
| 42 | + ] |
| 43 | + |
| 44 | + # add job time limit in minutes |
| 45 | + if job.time_limit is not None: |
| 46 | + preamble.append( |
| 47 | + self._format_option(f'-W {int(job.time_limit // 60)}') |
| 48 | + ) |
| 49 | + |
| 50 | + # emit the rest of the options |
| 51 | + options = job.options + job.cli_options |
| 52 | + for opt in options: |
| 53 | + if opt.startswith('#'): |
| 54 | + preamble.append(opt) |
| 55 | + else: |
| 56 | + preamble.append(self._format_option(opt)) |
| 57 | + |
| 58 | + # change to working dir with cd |
| 59 | + preamble.append(f'cd {job.workdir}') |
| 60 | + |
| 61 | + return preamble |
| 62 | + |
| 63 | + def submit(self, job): |
| 64 | + cmd = f'bsub {job.script_filename}' |
| 65 | + completed = _run_strict(cmd, timeout=self._submit_timeout) |
| 66 | + jobid_match = re.search(r'^Job <(?P<jobid>\S+)> is submitted', |
| 67 | + completed.stdout) |
| 68 | + if not jobid_match: |
| 69 | + raise JobSchedulerError('could not retrieve the job id ' |
| 70 | + 'of the submitted job') |
| 71 | + |
| 72 | + job._jobid = jobid_match.group('jobid') |
| 73 | + job._submit_time = time.time() |
| 74 | + |
| 75 | + def poll(self, *jobs): |
| 76 | + if jobs: |
| 77 | + # filter out non-jobs |
| 78 | + jobs = [job for job in jobs if job is not None] |
| 79 | + |
| 80 | + if not jobs: |
| 81 | + return |
| 82 | + |
| 83 | + completed = _run_strict( |
| 84 | + f'bjobs -noheader {" ".join(job.jobid for job in jobs)}' |
| 85 | + ) |
| 86 | + job_status = {} |
| 87 | + job_status_lines = completed.stdout.split('\n') |
| 88 | + |
| 89 | + for line in job_status_lines: |
| 90 | + job_regex = (r'(?P<jobid>\d+)\s+' |
| 91 | + r'(?P<user>\S+)\s+' |
| 92 | + r'(?P<status>\S+)\s+' |
| 93 | + r'(?P<queue>\S+)') |
| 94 | + job_match = re.search(job_regex, line) |
| 95 | + if job_match: |
| 96 | + job_status[job_match['jobid']] = job_match['status'] |
| 97 | + |
| 98 | + for job in jobs: |
| 99 | + if job.jobid not in job_status: |
| 100 | + # job id not found |
| 101 | + self.log(f'Job {job.jobid} not known to scheduler, ' |
| 102 | + f'assuming job completed') |
| 103 | + job._state = 'COMPLETED' |
| 104 | + job._completed = True |
| 105 | + elif job_status[job.jobid] in ('DONE', 'EXIT'): |
| 106 | + # job done |
| 107 | + job._state = 'COMPLETED' |
| 108 | + job._completed = True |
| 109 | + elif job_status[job.jobid] == 'RUN': |
| 110 | + # job running |
| 111 | + job._state = 'RUNNING' |
| 112 | + elif job_status[job.jobid] == 'PEND': |
| 113 | + # job pending |
| 114 | + job._state = 'PENDING' |
| 115 | + elif job_status[job.jobid] in ['PSUSP', 'SSUSP', 'USUSP']: |
| 116 | + # job suspended |
| 117 | + job._state = 'SUSPENDED' |
| 118 | + else: |
| 119 | + # job status unknown |
| 120 | + self.log(f'Job {job_status[job.jobid]} not known, ' |
| 121 | + f'assuming job completed') |
| 122 | + job._state = 'COMPLETED' |
| 123 | + job._completed = True |
| 124 | + |
| 125 | + def finished(self, job): |
| 126 | + if job.exception: |
| 127 | + raise job.exception |
| 128 | + |
| 129 | + return job.state == 'COMPLETED' |
0 commit comments