Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
amandarichardsonn committed Aug 20, 2024
1 parent f370b83 commit 18b1455
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 22 deletions.
7 changes: 5 additions & 2 deletions smartsim/_core/launcher/dragon/dragonLauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

import os
import typing as t
import pathlib

from smartsim._core.schemas.dragonRequests import DragonRunPolicy
from smartsim.error import errors
Expand Down Expand Up @@ -368,6 +369,8 @@ def _as_run_request_args_and_policy(
exe: ExecutableProtocol,
path: str | os.PathLike[str],
env: t.Mapping[str, str | None],
stdout_path: pathlib.Path,
stderr_path: pathlib.Path,
) -> tuple[DragonRunRequestView, DragonRunPolicy]:
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# FIXME: This type is 100% unacceptable, but I don't want to spend too much
Expand All @@ -389,8 +392,8 @@ def _as_run_request_args_and_policy(
env=env,
# TODO: Not sure how this info is injected
name=None,
output_file=None,
error_file=None,
output_file=stdout_path,
error_file=stderr_path,
**run_args,
),
policy,
Expand Down
23 changes: 21 additions & 2 deletions smartsim/settings/arguments/launch/lsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,35 @@
from __future__ import annotations

import typing as t
import pathlib
import subprocess

from smartsim.log import get_logger
from smartsim.settings.dispatch import ShellLauncher, dispatch, make_shell_format_fn
from smartsim.settings.dispatch import ShellLauncher, dispatch, ExecutableProtocol, _EnvironMappingType, ShellLauncherCommand

from ...common import set_check_input
from ...launchCommand import LauncherType
from ..launchArguments import LaunchArguments

logger = get_logger(__name__)
_as_jsrun_command = make_shell_format_fn(run_command="jsrun")

def _as_jsrun_command(args: LaunchArguments,
exe: ExecutableProtocol,
path: pathlib.Path,
env: _EnvironMappingType,
stdout_path: pathlib.Path,
stderr_path: pathlib.Path) -> ShellLauncherCommand:
command_tuple = (
"jsrun",
*(args.format_launch_args() or ()),
"--",
*exe.as_program_arguments(),
f"--stdio_stdout={stdout_path}",
f"--stdio_stderr={stderr_path}",

)
# add output and err to CMD tuple -> add dev Null for stdout and stderr
return ShellLauncherCommand(env, path, subprocess.DEVNULL, subprocess.DEVNULL, command_tuple)


@dispatch(with_format=_as_jsrun_command, to_launcher=ShellLauncher)
Expand Down
6 changes: 3 additions & 3 deletions smartsim/settings/dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@
class ShellLauncherCommand(t.NamedTuple):
env: _EnvironMappingType
path: pathlib.Path
stdout: pathlib.Path
stderr: pathlib.Path
stdout: TextIOWrapper
stderr: TextIOWrapper
command_tuple: tuple[str, tuple[str, ...]] | t.Sequence[str]


Expand Down Expand Up @@ -516,7 +516,7 @@ def start(
exe, *rest = shell_command.command_tuple
expanded_exe = helpers.expand_exe_path(exe)
# pylint: disable-next=consider-using-with
self._launched[id_] = sp.Popen((expanded_exe, *rest), cwd=shell_command.path, env={k:v for k,v in shell_command.env.items() if v is not None}, stdout=open(shell_command.stdout), stderr=open(shell_command.stderr))
self._launched[id_] = sp.Popen((expanded_exe, *rest), cwd=shell_command.path, env={k:v for k,v in shell_command.env.items() if v is not None}, stdout=shell_command.stdout, stderr=shell_command.stderr)
# Popen starts a new process and gives you back a handle to process, getting back the pid - process id
return id_

Expand Down
6 changes: 4 additions & 2 deletions tests/temp_tests/test_settings/test_dragonLauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_formatting_launch_args_into_request(
if gpu_affinity is not NOT_SET:
launch_args.set_gpu_affinity(gpu_affinity)
req, policy = _as_run_request_args_and_policy(
launch_args, mock_echo_executable, test_dir, {}
launch_args, mock_echo_executable, test_dir, {}, "output.txt", "error.txt"
)

expected_args = {
Expand All @@ -90,7 +90,7 @@ def test_formatting_launch_args_into_request(
if v is not NOT_SET
}
expected_run_req = DragonRunRequestView(
exe="echo", exe_args=["hello", "world"], path=test_dir, env={}, **expected_args
exe="echo", exe_args=["hello", "world"], path=test_dir, env={}, output_file="output.txt", error_file="error.txt", **expected_args
)
assert req.exe == expected_run_req.exe
assert req.exe_args == expected_run_req.exe_args
Expand All @@ -99,6 +99,8 @@ def test_formatting_launch_args_into_request(
assert req.hostlist == expected_run_req.hostlist
assert req.pmi_enabled == expected_run_req.pmi_enabled
assert req.path == expected_run_req.path
assert req.output_file == expected_run_req.output_file
assert req.error_file == expected_run_req.error_file

expected_run_policy_args = {
k: v
Expand Down
24 changes: 12 additions & 12 deletions tests/temp_tests/test_settings/test_lsfLauncher.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import pytest
import pathlib
import subprocess

from smartsim.settings import LaunchSettings
from smartsim.settings.arguments.launch.lsf import (
Expand Down Expand Up @@ -92,42 +92,42 @@ def test_launch_args():
@pytest.mark.parametrize(
"args, expected",
(
pytest.param({}, ("jsrun", "--", "echo", "hello", "world"), id="Empty Args"),
pytest.param({}, ("jsrun", "--", "echo", "hello", "world", '--stdio_stdout=output.txt', '--stdio_stderr=error.txt'), id="Empty Args"),
pytest.param(
{"n": "1"},
("jsrun", "-n", "1", "--", "echo", "hello", "world"),
("jsrun", "-n", "1", "--", "echo", "hello", "world", '--stdio_stdout=output.txt', '--stdio_stderr=error.txt'),
id="Short Arg",
),
pytest.param(
{"nrs": "1"},
("jsrun", "--nrs=1", "--", "echo", "hello", "world"),
("jsrun", "--nrs=1", "--", "echo", "hello", "world", '--stdio_stdout=output.txt', '--stdio_stderr=error.txt'),
id="Long Arg",
),
pytest.param(
{"v": None},
("jsrun", "-v", "--", "echo", "hello", "world"),
("jsrun", "-v", "--", "echo", "hello", "world", '--stdio_stdout=output.txt', '--stdio_stderr=error.txt'),
id="Short Arg (No Value)",
),
pytest.param(
{"verbose": None},
("jsrun", "--verbose", "--", "echo", "hello", "world"),
("jsrun", "--verbose", "--", "echo", "hello", "world", '--stdio_stdout=output.txt', '--stdio_stderr=error.txt'),
id="Long Arg (No Value)",
),
pytest.param(
{"tasks_per_rs": "1", "n": "123"},
("jsrun", "--tasks_per_rs=1", "-n", "123", "--", "echo", "hello", "world"),
("jsrun", "--tasks_per_rs=1", "-n", "123", "--", "echo", "hello", "world", '--stdio_stdout=output.txt', '--stdio_stderr=error.txt'),
id="Short and Long Args",
),
),
)
def test_formatting_launch_args(mock_echo_executable, args, expected, test_dir):
outfile = "out.txt"
errfile = "err.txt"
outfile = "output.txt"
errfile = "error.txt"
env, path, stdin, stdout, args = _as_jsrun_command(
JsrunLaunchArguments(args), mock_echo_executable, test_dir, {}, outfile, errfile
)
assert tuple(args) == expected
assert path == pathlib.Path(test_dir)
assert path == test_dir
assert env == {}
assert stdin == f"--stdio_stdout={outfile}"
assert stdout == f"--stdio_stderr={errfile}"
assert stdin == subprocess.DEVNULL
assert stdout == subprocess.DEVNULL
3 changes: 2 additions & 1 deletion tests/test_shell_launcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import tempfile
import unittest.mock
import pytest
import subprocess
import pathlib
import psutil
import difflib
Expand Down Expand Up @@ -135,7 +136,7 @@ def test_popen_returns_popen_object(test_dir: str):
shell_launcher = ShellLauncher()
run_dir, out_file, err_file = generate_directory(test_dir)
with open(out_file, "w", encoding="utf-8") as out, open(err_file, "w", encoding="utf-8") as err:
cmd = ShellLauncherCommand({}, run_dir, out, err, EchoHelloWorldEntity().as_program_arguments())
cmd = ShellLauncherCommand({}, run_dir, subprocess.DEVNULL, subprocess.DEVNULL, EchoHelloWorldEntity().as_program_arguments())
id = shell_launcher.start(cmd)
proc = shell_launcher._launched[id]
assert isinstance(proc, sp.Popen)
Expand Down

0 comments on commit 18b1455

Please sign in to comment.