base.py 3.87 KB
Newer Older
Hejing Li's avatar
Hejing Li committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# Copyright 2024 Max Planck Institute for Software Systems, and
# National University of Singapore
#
# Permission is hereby granted, free of charge, to any person obtaining
# a copy of this software and associated documentation files (the
# "Software"), to deal in the Software without restriction, including
# without limitation the rights to use, copy, modify, merge, publish,
# distribute, sublicense, and/or sell copies of the Software, and to
# permit persons to whom the Software is furnished to do so, subject to
# the following conditions:
#
# The above copyright notice and this permission notice shall be
# included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
# IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
# CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
# TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
# SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

23
import abc
Hejing Li's avatar
Hejing Li committed
24
import simbricks.orchestration.experiments as exp
25
26
27
28
from simbricks.orchestration.system import base as sys_base
from simbricks.orchestration.simulation import channel as sim_chan
from simbricks.orchestration.experiment import experiment_environment_new as exp_env
from simbricks.orchestration.instantiation import base as inst_base
Hejing Li's avatar
Hejing Li committed
29

30
31

class Simulator(abc.ABC):
Hejing Li's avatar
Hejing Li committed
32
33
34
    """Base class for all simulators."""

    def __init__(self, e: exp.Experiment) -> None:
35
36
        self.extra_deps: list[Simulator] = []
        self.name = ""
Hejing Li's avatar
Hejing Li committed
37
        self.experiment = e
38
        self._components: set[sys_base.Component] = []
Hejing Li's avatar
Hejing Li committed
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57

    def resreq_cores(self) -> int:
        """
        Number of cores this simulator requires during execution.

        This is used for scheduling multiple runs and experiments.
        """
        return 1

    def resreq_mem(self) -> int:
        """
        Number of memory in MB this simulator requires during execution.

        This is used for scheduling multiple runs and experiments.
        """
        return 64

    def full_name(self) -> str:
        """Full name of the simulator."""
58
        return ""
Hejing Li's avatar
Hejing Li committed
59
60

    # pylint: disable=unused-argument
61
    def prep_cmds(self, env: exp_env.ExpEnv) -> list[str]:
Hejing Li's avatar
Hejing Li committed
62
63
64
        """Commands to prepare execution of this simulator."""
        return []

65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
    # TODO: call this in subclasses
    def _add_component(self, comp: sys_base.Channel) -> None:
        self._components.add(comp)

    def _chan_needs_instance(self, chan: sys_base.Channel) -> bool:
        if (
            chan.a.component in self._components
            and chan.b.component in self._components
        ):
            return False
        return True

    def _get_sock_path(
        self, inst: inst_base.Instantiation, chan: sys_base.Channel
    ) -> tuple[sim_chan.Channel, inst_base.Socket] | tuple[None, None]:
        if not self._chan_needs_instance(chan):
            return None, None
        channel = self.experiment.retrieve_or_create_channel(chan)
        return channel, inst.get_socket_path(channel)

Hejing Li's avatar
Hejing Li committed
85
    # pylint: disable=unused-argument
86
87
    @abc.abstractmethod
    def run_cmd(self, env: exp_env.ExpEnv) -> str:
Hejing Li's avatar
Hejing Li committed
88
        """Command to execute this simulator."""
89
        return ""
Hejing Li's avatar
Hejing Li committed
90

91
    def dependencies(self) -> list[Simulator]:
Hejing Li's avatar
Hejing Li committed
92
93
94
95
96
        """Other simulators to execute before this one."""
        return []

    # Sockets to be cleaned up
    # pylint: disable=unused-argument
97
    def sockets_cleanup(self, env: exp_env.ExpEnv) -> list[str]:
Hejing Li's avatar
Hejing Li committed
98
99
100
101
        return []

    # sockets to wait for indicating the simulator is ready
    # pylint: disable=unused-argument
102
    def sockets_wait(self, env: exp_env.ExpEnv) -> list[str]:
Hejing Li's avatar
Hejing Li committed
103
104
105
106
107
108
        return []

    def start_delay(self) -> int:
        return 5

    def wait_terminate(self) -> bool:
109
        return False