Skip to content

Commit

Permalink
Path injection and Generator class refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
amandarichardsonn committed Jul 17, 2024
1 parent 9797442 commit 2a667f2
Show file tree
Hide file tree
Showing 26 changed files with 449 additions and 456 deletions.
447 changes: 215 additions & 232 deletions smartsim/_core/generation/generator.py

Large diffs are not rendered by default.

158 changes: 0 additions & 158 deletions smartsim/_core/generation/modelwriter.py

This file was deleted.

2 changes: 1 addition & 1 deletion smartsim/_core/utils/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def unpack_colo_fs_identifier(fs_id: str) -> str:
return "_" + fs_id if fs_id else ""


def create_short_id_str() -> str:
def create_short_id_str() -> str: # here
return str(uuid.uuid4())[:7]


Expand Down
3 changes: 1 addition & 2 deletions smartsim/entity/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ def _create_applications(self) -> tuple[Application, ...]:
# ^^^^^^^^^^^^^^^^^^^^^^^
# FIXME: remove this constructor arg! It should not exist!!
exe_args=self.exe_args,
path=os.path.join(self.path, self.name),
files=self.files,
params=permutation.params,
params_as_args=permutation.exe_args, # type: ignore[arg-type]
Expand All @@ -111,4 +110,4 @@ def as_jobs(self, settings: LaunchSettings) -> tuple[Job, ...]:
apps = self._create_applications()
if not apps:
raise ValueError("There are no members as part of this ensemble")
return tuple(Job(app, settings) for app in apps)
return tuple(Job(app, settings, f"job_{i}", ensemble_name=self.name) for i, app in enumerate(apps, 1))
3 changes: 1 addition & 2 deletions smartsim/entity/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def _on_disable(self) -> None:


class SmartSimEntity:
def __init__(self, name: str, path: str, run_settings: "RunSettings") -> None:
def __init__(self, name: str, run_settings: "RunSettings") -> None:
"""Initialize a SmartSim entity.
Each entity must have a name, path, and
Expand All @@ -110,7 +110,6 @@ def __init__(self, name: str, path: str, run_settings: "RunSettings") -> None:
"""
self.name = name
self.run_settings = run_settings
self.path = path

@property
def type(self) -> str:
Expand Down
3 changes: 1 addition & 2 deletions smartsim/entity/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ def __init__(
run_settings: "RunSettings",
params: t.Optional[t.Dict[str, str]] = None,
exe_args: t.Optional[t.List[str]] = None,
path: t.Optional[str] = getcwd(),
params_as_args: t.Optional[t.List[str]] = None,
batch_settings: t.Optional["BatchSettings"] = None,
files: t.Optional[EntityFiles] = None,
Expand All @@ -85,7 +84,7 @@ def __init__(
application as a batch job
:param files: Files to have available to the application
"""
super().__init__(name, str(path), run_settings)
super().__init__(name, run_settings)
self.exe = [expand_exe_path(exe)]
# self.exe = [exe] if run_settings.container else [expand_exe_path(exe)]
self.exe_args = exe_args or []
Expand Down
28 changes: 17 additions & 11 deletions smartsim/experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

from __future__ import annotations

import datetime
import itertools
import os
import os.path as osp
Expand Down Expand Up @@ -173,6 +174,12 @@ def __init__(
exp_path = osp.join(getcwd(), name)

self.exp_path = exp_path
self.run_ID = (
"run-"
+ datetime.datetime.now().strftime("%H:%M:%S")
+ "-"
+ datetime.datetime.now().strftime("%Y-%m-%d")
)

# TODO: Remove this! The contoller is becoming obsolete
self._control = Controller(launcher="local")
Expand Down Expand Up @@ -203,6 +210,7 @@ def _start(job: Job) -> LaunchedJobID:
if launcher is None:
launcher = launcher_type.create(self)
self._active_launchers.add(launcher)
job_execution_path = self._generate(job)
# >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
# FIXME: Opting out of type check here. Fix this later!!
# TODO: Very much dislike that we have to pass in attrs off of `job`
Expand All @@ -212,7 +220,9 @@ def _start(job: Job) -> LaunchedJobID:
# to protocol
# ---------------------------------------------------------------------
exe_like = t.cast("ExecutableLike", job.entity)
finalized = builder.finalize(exe_like, job.launch_settings.env_vars)
finalized = builder.finalize(
exe_like, job.launch_settings.env_vars, job_execution_path
)
# <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
return launcher.start(finalized)

Expand Down Expand Up @@ -326,13 +336,10 @@ def stop(
raise

@_contextualize
def generate(
def _generate(
self,
*args: t.Union[SmartSimEntity, EntitySequence[SmartSimEntity]],
tag: t.Optional[str] = None,
overwrite: bool = False,
verbose: bool = False,
) -> None:
job: Job,
) -> str:
"""Generate the file structure for an ``Experiment``
``Experiment.generate`` creates directories for each entity
Expand All @@ -351,10 +358,9 @@ def generate(
:param verbose: log parameter settings to std out
"""
try:
generator = Generator(self.exp_path, overwrite=overwrite, verbose=verbose)
if tag:
generator.set_tag(tag)
generator.generate_experiment(*args)
generator = Generator(self.exp_path, self.run_ID, job)
job_path = generator.generate_experiment()
return job_path
except SmartSimError as e:
logger.error(e)
raise
Expand Down
16 changes: 16 additions & 0 deletions smartsim/launchable/job.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
from smartsim._core.commands.launchCommands import LaunchCommands
from smartsim.launchable.basejob import BaseJob
from smartsim.settings import LaunchSettings
from smartsim._core.utils.helpers import create_short_id_str

if t.TYPE_CHECKING:
from smartsim.entity.entity import SmartSimEntity

if t.TYPE_CHECKING:
from smartsim.entity.entity import SmartSimEntity
Expand All @@ -50,12 +54,24 @@ def __init__(
self,
entity: SmartSimEntity,
launch_settings: LaunchSettings,
name: str = "job",
**kwargs: t.Any,
):
super().__init__()
self._entity = deepcopy(entity)
self._launch_settings = deepcopy(launch_settings)
self._name = deepcopy(name)
self._ensemble_name = kwargs.get('ensemble_name', None)
if self._ensemble_name is not None:
self._ensemble_name += f"-{create_short_id_str()}"
# TODO: self.warehouse_runner = JobWarehouseRunner

# TODO do we want the user to be allowed to reset the Job name? Therefore, add setter
@property
def name(self) -> str:
"""Retrieves the name of the Job."""
return deepcopy(self._name)

@property
def entity(self) -> SmartSimEntity:
return deepcopy(self._entity)
Expand Down
7 changes: 7 additions & 0 deletions smartsim/launchable/jobGroup.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,16 @@ class JobGroup(BaseJobGroup):
def __init__(
self,
jobs: t.List[BaseJob],
name: str = "jobGroup",
) -> None:
super().__init__()
self._jobs = deepcopy(jobs)
self._name = deepcopy(name)

@property
def name(self) -> str:
"""Retrieves the name of the JobGroup."""
return deepcopy(self._name)

@property
def jobs(self) -> t.List[BaseJob]:
Expand Down
9 changes: 6 additions & 3 deletions smartsim/settings/builders/launch/alps.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,11 +220,14 @@ def set(self, key: str, value: str | None) -> None:
self._launch_args[key] = value

def finalize(
self, exe: ExecutableLike, env: t.Mapping[str, str | None]
) -> t.Sequence[str]:
self,
exe: ExecutableLike,
env: t.Mapping[str, str | None],
job_execution_path: str,
) -> t.Tuple[t.Sequence[str], str]:
return (
"aprun",
*(self.format_launch_args() or ()),
"--",
*exe.as_program_arguments(),
)
), job_execution_path
7 changes: 5 additions & 2 deletions smartsim/settings/builders/launch/dragon.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ def set(self, key: str, value: str | None) -> None:
self._launch_args[key] = value

def finalize(
self, exe: ExecutableLike, env: t.Mapping[str, str | None]
self,
exe: ExecutableLike,
env: t.Mapping[str, str | None],
job_execution_path: str,
) -> DragonRunRequestView:
exe_, *args = exe.as_program_arguments()
return DragonRunRequestView(
Expand All @@ -81,7 +84,7 @@ def finalize(
# the command execute next to any generated files. A similar
# problem exists for the other settings.
# TODO: Find a way to inject this path
path=os.getcwd(),
path=job_execution_path,
env=env,
# TODO: Not sure how this info is injected
name=None,
Expand Down
9 changes: 6 additions & 3 deletions smartsim/settings/builders/launch/lsf.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,14 @@ def set(self, key: str, value: str | None) -> None:
self._launch_args[key] = value

def finalize(
self, exe: ExecutableLike, env: t.Mapping[str, str | None]
) -> t.Sequence[str]:
self,
exe: ExecutableLike,
env: t.Mapping[str, str | None],
job_execution_path: str,
) -> t.Tuple[t.Sequence[str], str]:
return (
"jsrun",
*(self.format_launch_args() or ()),
"--",
*exe.as_program_arguments(),
)
), job_execution_path
Loading

0 comments on commit 2a667f2

Please sign in to comment.