|
| 1 | +# Copyright 2022-2025 The Ramble Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 4 | +# https://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 5 | +# <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your |
| 6 | +# option. This file may not be copied, modified, or distributed |
| 7 | +# except according to those terms. |
| 8 | + |
| 9 | +import os |
| 10 | +import shutil |
| 11 | +import textwrap |
| 12 | + |
| 13 | +from ramble.wmkit import * |
| 14 | + |
| 15 | +from spack.util.executable import Executable, ProcessError |
| 16 | + |
| 17 | + |
| 18 | +class GkeMpi(WorkflowManagerBase): |
| 19 | + """GKE workflow manager that uses the MPI operator""" |
| 20 | + |
| 21 | + name = "gke-mpi" |
| 22 | + |
| 23 | + maintainers("linsword13") |
| 24 | + |
| 25 | + tags("workflow", "gke", "mpi") |
| 26 | + |
| 27 | + workflow_manager_variable( |
| 28 | + name="job_name", |
| 29 | + default="{application_name}-{workload_name}-{experiment_name}", |
| 30 | + description="GKE job name", |
| 31 | + ) |
| 32 | + |
| 33 | + workflow_manager_variable( |
| 34 | + name="extra_metadata", |
| 35 | + default="", |
| 36 | + description="Extra line-separated key:val pairs for the metadata section", |
| 37 | + ) |
| 38 | + |
| 39 | + workflow_manager_variable( |
| 40 | + name="cores_per_node", |
| 41 | + default="{processes_per_node}", |
| 42 | + description="Cores per node", |
| 43 | + ) |
| 44 | + |
| 45 | + workflow_manager_variable( |
| 46 | + name="container_image", |
| 47 | + default="", |
| 48 | + description="url to the container image", |
| 49 | + ) |
| 50 | + |
| 51 | + workflow_manager_variable( |
| 52 | + name="extra_container_config_files", |
| 53 | + default="", |
| 54 | + description="extra line-separated list of config files to be mapped to containers", |
| 55 | + ) |
| 56 | + |
| 57 | + workflow_manager_variable( |
| 58 | + name="container_work_dir", |
| 59 | + default="/config", |
| 60 | + description="working directory inside the container", |
| 61 | + ) |
| 62 | + |
| 63 | + workflow_manager_variable( |
| 64 | + name="launcher_execute_script_template", |
| 65 | + default="launcher_execute_script.tpl", |
| 66 | + description="execute script template for the launcher", |
| 67 | + ) |
| 68 | + |
| 69 | + register_template( |
| 70 | + name="launcher_execute_script", |
| 71 | + src_path="{launcher_execute_script_template}", |
| 72 | + ) |
| 73 | + |
| 74 | + workflow_manager_variable( |
| 75 | + name="worker_execute_script_template", |
| 76 | + default="worker_execute_script.tpl", |
| 77 | + description="execute script template for the workers", |
| 78 | + ) |
| 79 | + |
| 80 | + register_template( |
| 81 | + name="worker_execute_script", |
| 82 | + src_path="{worker_execute_script_template}", |
| 83 | + ) |
| 84 | + |
| 85 | + register_template( |
| 86 | + name="gke_mpi_yaml", |
| 87 | + src_path="gke_mpi.yaml.tpl", |
| 88 | + dest_path="gke_mpi.yaml", |
| 89 | + extra_vars_func="gke_mpi_yaml_vars", |
| 90 | + ) |
| 91 | + |
| 92 | + def _gke_mpi_yaml_vars(self): |
| 93 | + expander = self.app_inst.expander |
| 94 | + extra_metadata_str = expander.expand_var_name("extra_metadata") |
| 95 | + launcher_script = expander.expand_var_name("launcher_execute_script") |
| 96 | + worker_script = expander.expand_var_name("worker_execute_script") |
| 97 | + if extra_metadata_str: |
| 98 | + extra_metadata_section = textwrap.indent( |
| 99 | + extra_metadata_str, " " * 2 |
| 100 | + ) |
| 101 | + else: |
| 102 | + extra_metadata_section = "" |
| 103 | + |
| 104 | + return { |
| 105 | + "extra_metadata_section": extra_metadata_section, |
| 106 | + "launcher_script_path": os.path.join( |
| 107 | + "/config", os.path.basename(launcher_script) |
| 108 | + ), |
| 109 | + "worker_script_path": os.path.join( |
| 110 | + "/config", os.path.basename(worker_script) |
| 111 | + ), |
| 112 | + } |
| 113 | + |
| 114 | + register_template( |
| 115 | + name="kustomization.yaml", |
| 116 | + src_path="kustomization.yaml.tpl", |
| 117 | + dest_path="kustomization.yaml", |
| 118 | + extra_vars_func="kustomization_yaml_vars", |
| 119 | + ) |
| 120 | + |
| 121 | + def _kustomization_yaml_vars(self): |
| 122 | + files = ["{launcher_execute_script}", "{worker_execute_script}"] |
| 123 | + expander = self.app_inst.expander |
| 124 | + extra_files_str = expander.expand_var_name( |
| 125 | + "extra_container_config_files" |
| 126 | + ) |
| 127 | + if extra_files_str: |
| 128 | + files.extend(extra_files_str.split("\n")) |
| 129 | + file_lines = "\n".join( |
| 130 | + [expander.expand_var(f.lstrip("- ")) for f in files] |
| 131 | + ) |
| 132 | + lines = [ |
| 133 | + "configMapGenerator:", |
| 134 | + "- name: gke-mpi-config", |
| 135 | + " files:", |
| 136 | + textwrap.indent(file_lines, " - "), |
| 137 | + "generatorOptions:", |
| 138 | + # For some reason kustomization does not apply the generated name properly. |
| 139 | + # So disable the suffix as a workaround. |
| 140 | + " disableNameSuffixHash: true", |
| 141 | + ] |
| 142 | + config_map_gen_section = "\n".join(lines) |
| 143 | + return { |
| 144 | + "config_map_gen_section": config_map_gen_section, |
| 145 | + } |
| 146 | + |
| 147 | + register_template( |
| 148 | + name="batch_submit", |
| 149 | + src_path="batch_submit.tpl", |
| 150 | + dest_path="batch_submit", |
| 151 | + ) |
| 152 | + |
| 153 | + register_template( |
| 154 | + name="batch_query", |
| 155 | + src_path="batch_query.tpl", |
| 156 | + dest_path="batch_query", |
| 157 | + ) |
| 158 | + |
| 159 | + register_template( |
| 160 | + name="batch_cancel", |
| 161 | + src_path="batch_cancel.tpl", |
| 162 | + dest_path="batch_cancel", |
| 163 | + ) |
| 164 | + |
| 165 | + # A convenience for printing the deployment config |
| 166 | + register_template( |
| 167 | + name="batch_print_deployment", |
| 168 | + src_path="batch_print_deployment.tpl", |
| 169 | + dest_path="batch_print_deployment", |
| 170 | + ) |
| 171 | + |
| 172 | + def _prepare_analysis(self, workspace): |
| 173 | + if workspace.dry_run: |
| 174 | + return |
| 175 | + expander = self.app_inst.expander |
| 176 | + query_script = expander.expand_var_name("batch_query") |
| 177 | + query_cmd = Executable(query_script) |
| 178 | + try: |
| 179 | + query_cmd(output=os.devnull) |
| 180 | + except ProcessError as e: |
| 181 | + logger.warn(f"batch_query returns error {e}") |
| 182 | + run_dir = expander.expand_var_name("experiment_run_dir") |
| 183 | + launcher_log = os.path.join(run_dir, "launcher.log") |
| 184 | + if os.path.exists(launcher_log): |
| 185 | + log_file = expander.expand_var_name("log_file") |
| 186 | + shutil.copy2(launcher_log, log_file) |
0 commit comments