|
3 | 3 | import sys |
4 | 4 | from typing import Optional |
5 | 5 |
|
| 6 | +SLURM_COMMAND = "srun" |
| 7 | + |
6 | 8 |
|
7 | 9 | def get_command_path(executable: str) -> str: |
8 | 10 | """ |
@@ -112,3 +114,51 @@ def get_interactive_execute_command( |
112 | 114 | else: |
113 | 115 | command_lst += [get_command_path(executable="interactive_serial.py")] |
114 | 116 | return command_lst |
| 117 | + |
| 118 | + |
| 119 | +def generate_slurm_command( |
| 120 | + cores: int, |
| 121 | + cwd: Optional[str], |
| 122 | + threads_per_core: int = 1, |
| 123 | + gpus_per_core: int = 0, |
| 124 | + num_nodes: Optional[int] = None, |
| 125 | + exclusive: bool = False, |
| 126 | + openmpi_oversubscribe: bool = False, |
| 127 | + slurm_cmd_args: Optional[list[str]] = None, |
| 128 | + pmi_mode: Optional[str] = None, |
| 129 | +) -> list[str]: |
| 130 | + """ |
| 131 | + Generate the command list for the SLURM interface. |
| 132 | +
|
| 133 | + Args: |
| 134 | + cores (int): The number of cores. |
| 135 | + cwd (str): The current working directory. |
| 136 | + threads_per_core (int, optional): The number of threads per core. Defaults to 1. |
| 137 | + gpus_per_core (int, optional): The number of GPUs per core. Defaults to 0. |
| 138 | + num_nodes (int, optional): The number of compute nodes to use for executing the task. Defaults to None. |
| 139 | + exclusive (bool): Whether to exclusively reserve the compute nodes, or allow sharing compute notes. Defaults to False. |
| 140 | + openmpi_oversubscribe (bool, optional): Whether to oversubscribe the cores. Defaults to False. |
| 141 | + slurm_cmd_args (list[str], optional): Additional command line arguments. Defaults to []. |
| 142 | + pmi_mode (str): PMI interface to use (OpenMPI v5 requires pmix) default is None |
| 143 | +
|
| 144 | + Returns: |
| 145 | + list[str]: The generated command list. |
| 146 | + """ |
| 147 | + command_prepend_lst = [SLURM_COMMAND, "-n", str(cores)] |
| 148 | + if cwd is not None: |
| 149 | + command_prepend_lst += ["-D", cwd] |
| 150 | + if pmi_mode is not None: |
| 151 | + command_prepend_lst += ["--mpi=" + pmi_mode] |
| 152 | + if num_nodes is not None: |
| 153 | + command_prepend_lst += ["-N", str(num_nodes)] |
| 154 | + if threads_per_core > 1: |
| 155 | + command_prepend_lst += ["--cpus-per-task=" + str(threads_per_core)] |
| 156 | + if gpus_per_core > 0: |
| 157 | + command_prepend_lst += ["--gpus-per-task=" + str(gpus_per_core)] |
| 158 | + if exclusive: |
| 159 | + command_prepend_lst += ["--exact"] |
| 160 | + if openmpi_oversubscribe: |
| 161 | + command_prepend_lst += ["--oversubscribe"] |
| 162 | + if slurm_cmd_args is not None and len(slurm_cmd_args) > 0: |
| 163 | + command_prepend_lst += slurm_cmd_args |
| 164 | + return command_prepend_lst |
0 commit comments