|
| 1 | +import shlex |
| 2 | +from typing import List |
| 3 | + |
| 4 | +from dargs import Argument |
| 5 | + |
| 6 | +from dpdispatcher.dlog import dlog |
| 7 | +from dpdispatcher.machine import Machine |
| 8 | +from dpdispatcher.utils.job_status import JobStatus |
| 9 | +from dpdispatcher.utils.utils import ( |
| 10 | + RetrySignal, |
| 11 | + customized_script_header_template, |
| 12 | + retry, |
| 13 | +) |
| 14 | + |
| 15 | +JH_UniScheduler_script_header_template = """\ |
| 16 | +#!/bin/bash -l |
| 17 | +#JSUB -e %J.err |
| 18 | +#JSUB -o %J.out |
| 19 | +{JH_UniScheduler_nodes_line} |
| 20 | +{JH_UniScheduler_ptile_line} |
| 21 | +{JH_UniScheduler_partition_line} |
| 22 | +{JH_UniScheduler_number_gpu_line}""" |
| 23 | + |
| 24 | + |
| 25 | +class JH_UniScheduler(Machine): |
| 26 | + """JH_UniScheduler batch.""" |
| 27 | + |
| 28 | + def gen_script(self, job): |
| 29 | + JH_UniScheduler_script = super().gen_script(job) |
| 30 | + return JH_UniScheduler_script |
| 31 | + |
| 32 | + def gen_script_header(self, job): |
| 33 | + resources = job.resources |
| 34 | + script_header_dict = { |
| 35 | + "JH_UniScheduler_nodes_line": f"#JSUB -n {resources.number_node * resources.cpu_per_node}", |
| 36 | + "JH_UniScheduler_ptile_line": f"#JSUB -R 'span[ptile={resources.cpu_per_node}]'", |
| 37 | + "JH_UniScheduler_partition_line": f"#JSUB -q {resources.queue_name}", |
| 38 | + } |
| 39 | + custom_gpu_line = resources.kwargs.get("custom_gpu_line", None) |
| 40 | + if not custom_gpu_line: |
| 41 | + script_header_dict["JH_UniScheduler_number_gpu_line"] = ( |
| 42 | + "" f"#JSUB -gpgpu {resources.gpu_per_node}" |
| 43 | + ) |
| 44 | + else: |
| 45 | + script_header_dict["JH_UniScheduler_number_gpu_line"] = custom_gpu_line |
| 46 | + if ( |
| 47 | + resources["strategy"].get("customized_script_header_template_file") |
| 48 | + is not None |
| 49 | + ): |
| 50 | + JH_UniScheduler_script_header = customized_script_header_template( |
| 51 | + resources["strategy"]["customized_script_header_template_file"], |
| 52 | + resources, |
| 53 | + ) |
| 54 | + else: |
| 55 | + JH_UniScheduler_script_header = ( |
| 56 | + JH_UniScheduler_script_header_template.format(**script_header_dict) |
| 57 | + ) |
| 58 | + |
| 59 | + return JH_UniScheduler_script_header |
| 60 | + |
| 61 | + @retry() |
| 62 | + def do_submit(self, job): |
| 63 | + script_file_name = job.script_file_name |
| 64 | + script_str = self.gen_script(job) |
| 65 | + job_id_name = job.job_hash + "_job_id" |
| 66 | + self.context.write_file(fname=script_file_name, write_str=script_str) |
| 67 | + script_run_str = self.gen_script_command(job) |
| 68 | + script_run_file_name = f"{job.script_file_name}.run" |
| 69 | + self.context.write_file(fname=script_run_file_name, write_str=script_run_str) |
| 70 | + |
| 71 | + try: |
| 72 | + stdin, stdout, stderr = self.context.block_checkcall( |
| 73 | + "cd {} && {} {}".format( |
| 74 | + shlex.quote(self.context.remote_root), |
| 75 | + "jsub < ", |
| 76 | + shlex.quote(script_file_name), |
| 77 | + ) |
| 78 | + ) |
| 79 | + except RuntimeError as err: |
| 80 | + raise RetrySignal(err) from err |
| 81 | + |
| 82 | + subret = stdout.readlines() |
| 83 | + job_id = subret[0].split()[1][1:-1] |
| 84 | + self.context.write_file(job_id_name, job_id) |
| 85 | + return job_id |
| 86 | + |
| 87 | + def default_resources(self, resources): |
| 88 | + pass |
| 89 | + |
| 90 | + @retry() |
| 91 | + def check_status(self, job): |
| 92 | + try: |
| 93 | + job_id = job.job_id |
| 94 | + except AttributeError: |
| 95 | + return JobStatus.terminated |
| 96 | + if job_id == "": |
| 97 | + return JobStatus.unsubmitted |
| 98 | + ret, stdin, stdout, stderr = self.context.block_call("jjobs " + job_id) |
| 99 | + err_str = stderr.read().decode("utf-8") |
| 100 | + if (f"Job <{job_id}> is not found") in err_str: |
| 101 | + if self.check_finish_tag(job): |
| 102 | + return JobStatus.finished |
| 103 | + else: |
| 104 | + return JobStatus.terminated |
| 105 | + elif ret != 0: |
| 106 | + # just retry when any unknown error raised. |
| 107 | + raise RetrySignal( |
| 108 | + "Get error code %d in checking status through ssh with job: %s . message: %s" |
| 109 | + % (ret, job.job_hash, err_str) |
| 110 | + ) |
| 111 | + status_out = stdout.read().decode("utf-8").split("\n") |
| 112 | + if len(status_out) < 2: |
| 113 | + return JobStatus.unknown |
| 114 | + else: |
| 115 | + status_line = status_out[1] |
| 116 | + status_word = status_line.split()[2] |
| 117 | + |
| 118 | + if status_word in ["PEND"]: |
| 119 | + return JobStatus.waiting |
| 120 | + elif status_word in ["RUN", "PSUSP", "SSUSP", "USUSP"]: |
| 121 | + return JobStatus.running |
| 122 | + elif status_word in ["DONE", "EXIT"]: |
| 123 | + if self.check_finish_tag(job): |
| 124 | + dlog.info(f"job: {job.job_hash} {job.job_id} finished") |
| 125 | + return JobStatus.finished |
| 126 | + else: |
| 127 | + return JobStatus.terminated |
| 128 | + else: |
| 129 | + return JobStatus.unknown |
| 130 | + |
| 131 | + def check_finish_tag(self, job): |
| 132 | + job_tag_finished = job.job_hash + "_job_tag_finished" |
| 133 | + return self.context.check_file_exists(job_tag_finished) |
| 134 | + |
| 135 | + @classmethod |
| 136 | + def resources_subfields(cls) -> List[Argument]: |
| 137 | + """Generate the resources subfields. |
| 138 | +
|
| 139 | + Returns |
| 140 | + ------- |
| 141 | + list[Argument] |
| 142 | + resources subfields |
| 143 | + """ |
| 144 | + doc_custom_gpu_line = "Custom GPU configuration, starting with #JSUB" |
| 145 | + |
| 146 | + return [ |
| 147 | + Argument( |
| 148 | + "kwargs", |
| 149 | + dict, |
| 150 | + [ |
| 151 | + Argument( |
| 152 | + "custom_gpu_line", |
| 153 | + str, |
| 154 | + optional=True, |
| 155 | + default=None, |
| 156 | + doc=doc_custom_gpu_line, |
| 157 | + ), |
| 158 | + ], |
| 159 | + optional=False, |
| 160 | + doc="Extra arguments.", |
| 161 | + ) |
| 162 | + ] |
| 163 | + |
| 164 | + def kill(self, job): |
| 165 | + """Kill the job. |
| 166 | +
|
| 167 | + Parameters |
| 168 | + ---------- |
| 169 | + job : Job |
| 170 | + job |
| 171 | + """ |
| 172 | + job_id = job.job_id |
| 173 | + ret, stdin, stdout, stderr = self.context.block_call( |
| 174 | + "jctrl kill " + str(job_id) |
| 175 | + ) |
0 commit comments