Skip to content

Commit f1649fa

Browse files
Move SLURM to separate module (#528)
* Move SLURM to separate module * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * clean up --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 44214f7 commit f1649fa

File tree

5 files changed

+99
-97
lines changed

5 files changed

+99
-97
lines changed

executorlib/interactive/executor.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
InteractiveStepExecutor,
88
execute_tasks_with_dependencies,
99
)
10+
from executorlib.interactive.slurm import SrunSpawner
1011
from executorlib.standalone.inputcheck import (
1112
check_command_line_argument_lst,
1213
check_executor,
@@ -17,10 +18,7 @@
1718
check_pmi,
1819
validate_number_of_cores,
1920
)
20-
from executorlib.standalone.interactive.spawner import (
21-
MpiExecSpawner,
22-
SrunSpawner,
23-
)
21+
from executorlib.standalone.interactive.spawner import MpiExecSpawner
2422
from executorlib.standalone.plot import (
2523
draw,
2624
generate_nodes_and_edges,

executorlib/interactive/slurm.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
from typing import Optional
2+
3+
from executorlib.standalone.interactive.spawner import SubprocessSpawner
4+
5+
SLURM_COMMAND = "srun"
6+
7+
8+
class SrunSpawner(SubprocessSpawner):
9+
def __init__(
10+
self,
11+
cwd: Optional[str] = None,
12+
cores: int = 1,
13+
threads_per_core: int = 1,
14+
gpus_per_core: int = 0,
15+
openmpi_oversubscribe: bool = False,
16+
slurm_cmd_args: list[str] = [],
17+
):
18+
"""
19+
Srun interface implementation.
20+
21+
Args:
22+
cwd (str, optional): The current working directory. Defaults to None.
23+
cores (int, optional): The number of cores to use. Defaults to 1.
24+
threads_per_core (int, optional): The number of threads per core. Defaults to 1.
25+
gpus_per_core (int, optional): The number of GPUs per core. Defaults to 0.
26+
openmpi_oversubscribe (bool, optional): Whether to oversubscribe the cores. Defaults to False.
27+
slurm_cmd_args (list[str], optional): Additional command line arguments. Defaults to [].
28+
"""
29+
super().__init__(
30+
cwd=cwd,
31+
cores=cores,
32+
openmpi_oversubscribe=openmpi_oversubscribe,
33+
threads_per_core=threads_per_core,
34+
)
35+
self._gpus_per_core = gpus_per_core
36+
self._slurm_cmd_args = slurm_cmd_args
37+
38+
def generate_command(self, command_lst: list[str]) -> list[str]:
39+
"""
40+
Generate the command list for the Srun interface.
41+
42+
Args:
43+
command_lst (list[str]): The command list.
44+
45+
Returns:
46+
list[str]: The generated command list.
47+
"""
48+
command_prepend_lst = generate_slurm_command(
49+
cores=self._cores,
50+
cwd=self._cwd,
51+
threads_per_core=self._threads_per_core,
52+
gpus_per_core=self._gpus_per_core,
53+
openmpi_oversubscribe=self._openmpi_oversubscribe,
54+
slurm_cmd_args=self._slurm_cmd_args,
55+
)
56+
return super().generate_command(
57+
command_lst=command_prepend_lst + command_lst,
58+
)
59+
60+
61+
def generate_slurm_command(
62+
cores: int,
63+
cwd: str,
64+
threads_per_core: int = 1,
65+
gpus_per_core: int = 0,
66+
openmpi_oversubscribe: bool = False,
67+
slurm_cmd_args: list[str] = [],
68+
) -> list[str]:
69+
"""
70+
Generate the command list for the SLURM interface.
71+
72+
Args:
73+
cores (int): The number of cores.
74+
cwd (str): The current working directory.
75+
threads_per_core (int, optional): The number of threads per core. Defaults to 1.
76+
gpus_per_core (int, optional): The number of GPUs per core. Defaults to 0.
77+
openmpi_oversubscribe (bool, optional): Whether to oversubscribe the cores. Defaults to False.
78+
slurm_cmd_args (list[str], optional): Additional command line arguments. Defaults to [].
79+
80+
Returns:
81+
list[str]: The generated command list.
82+
"""
83+
command_prepend_lst = [SLURM_COMMAND, "-n", str(cores)]
84+
if cwd is not None:
85+
command_prepend_lst += ["-D", cwd]
86+
if threads_per_core > 1:
87+
command_prepend_lst += ["--cpus-per-task" + str(threads_per_core)]
88+
if gpus_per_core > 0:
89+
command_prepend_lst += ["--gpus-per-task=" + str(gpus_per_core)]
90+
if openmpi_oversubscribe:
91+
command_prepend_lst += ["--oversubscribe"]
92+
if len(slurm_cmd_args) > 0:
93+
command_prepend_lst += slurm_cmd_args
94+
return command_prepend_lst

executorlib/standalone/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
interface_send,
77
interface_shutdown,
88
)
9-
from executorlib.standalone.interactive.spawner import MpiExecSpawner, SrunSpawner
9+
from executorlib.standalone.interactive.spawner import MpiExecSpawner
1010
from executorlib.standalone.thread import RaisingThread
1111

1212
__all__ = [
@@ -18,5 +18,4 @@
1818
interface_receive,
1919
RaisingThread,
2020
MpiExecSpawner,
21-
SrunSpawner,
2221
]

executorlib/standalone/interactive/spawner.py

Lines changed: 0 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from typing import Optional
44

55
MPI_COMMAND = "mpiexec"
6-
SLURM_COMMAND = "srun"
76

87

98
class BaseSpawner(ABC):
@@ -147,59 +146,6 @@ def generate_command(self, command_lst: list[str]) -> list[str]:
147146
)
148147

149148

150-
class SrunSpawner(SubprocessSpawner):
151-
def __init__(
152-
self,
153-
cwd: Optional[str] = None,
154-
cores: int = 1,
155-
threads_per_core: int = 1,
156-
gpus_per_core: int = 0,
157-
openmpi_oversubscribe: bool = False,
158-
slurm_cmd_args: list[str] = [],
159-
):
160-
"""
161-
Srun interface implementation.
162-
163-
Args:
164-
cwd (str, optional): The current working directory. Defaults to None.
165-
cores (int, optional): The number of cores to use. Defaults to 1.
166-
threads_per_core (int, optional): The number of threads per core. Defaults to 1.
167-
gpus_per_core (int, optional): The number of GPUs per core. Defaults to 0.
168-
openmpi_oversubscribe (bool, optional): Whether to oversubscribe the cores. Defaults to False.
169-
slurm_cmd_args (list[str], optional): Additional command line arguments. Defaults to [].
170-
"""
171-
super().__init__(
172-
cwd=cwd,
173-
cores=cores,
174-
openmpi_oversubscribe=openmpi_oversubscribe,
175-
threads_per_core=threads_per_core,
176-
)
177-
self._gpus_per_core = gpus_per_core
178-
self._slurm_cmd_args = slurm_cmd_args
179-
180-
def generate_command(self, command_lst: list[str]) -> list[str]:
181-
"""
182-
Generate the command list for the Srun interface.
183-
184-
Args:
185-
command_lst (list[str]): The command list.
186-
187-
Returns:
188-
list[str]: The generated command list.
189-
"""
190-
command_prepend_lst = generate_slurm_command(
191-
cores=self._cores,
192-
cwd=self._cwd,
193-
threads_per_core=self._threads_per_core,
194-
gpus_per_core=self._gpus_per_core,
195-
openmpi_oversubscribe=self._openmpi_oversubscribe,
196-
slurm_cmd_args=self._slurm_cmd_args,
197-
)
198-
return super().generate_command(
199-
command_lst=command_prepend_lst + command_lst,
200-
)
201-
202-
203149
def generate_mpiexec_command(
204150
cores: int, openmpi_oversubscribe: bool = False
205151
) -> list[str]:
@@ -220,39 +166,3 @@ def generate_mpiexec_command(
220166
if openmpi_oversubscribe:
221167
command_prepend_lst += ["--oversubscribe"]
222168
return command_prepend_lst
223-
224-
225-
def generate_slurm_command(
226-
cores: int,
227-
cwd: str,
228-
threads_per_core: int = 1,
229-
gpus_per_core: int = 0,
230-
openmpi_oversubscribe: bool = False,
231-
slurm_cmd_args: list[str] = [],
232-
) -> list[str]:
233-
"""
234-
Generate the command list for the SLURM interface.
235-
236-
Args:
237-
cores (int): The number of cores.
238-
cwd (str): The current working directory.
239-
threads_per_core (int, optional): The number of threads per core. Defaults to 1.
240-
gpus_per_core (int, optional): The number of GPUs per core. Defaults to 0.
241-
openmpi_oversubscribe (bool, optional): Whether to oversubscribe the cores. Defaults to False.
242-
slurm_cmd_args (list[str], optional): Additional command line arguments. Defaults to [].
243-
244-
Returns:
245-
list[str]: The generated command list.
246-
"""
247-
command_prepend_lst = [SLURM_COMMAND, "-n", str(cores)]
248-
if cwd is not None:
249-
command_prepend_lst += ["-D", cwd]
250-
if threads_per_core > 1:
251-
command_prepend_lst += ["--cpus-per-task" + str(threads_per_core)]
252-
if gpus_per_core > 0:
253-
command_prepend_lst += ["--gpus-per-task=" + str(gpus_per_core)]
254-
if openmpi_oversubscribe:
255-
command_prepend_lst += ["--oversubscribe"]
256-
if len(slurm_cmd_args) > 0:
257-
command_prepend_lst += slurm_cmd_args
258-
return command_prepend_lst

tests/test_shared_backend.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
import unittest
44

55
from executorlib.standalone.interactive.backend import parse_arguments
6-
from executorlib.standalone.interactive.spawner import SrunSpawner, MpiExecSpawner
6+
from executorlib.standalone.interactive.spawner import MpiExecSpawner
7+
from executorlib.interactive.slurm import SrunSpawner
78

89

910
class TestParser(unittest.TestCase):

0 commit comments

Comments
 (0)