|
| 1 | +# BSD 2-Clause License |
| 2 | +# |
| 3 | +# Copyright (c) 2021-2024, Hewlett Packard Enterprise |
| 4 | +# All rights reserved. |
| 5 | +# |
| 6 | +# Redistribution and use in source and binary forms, with or without |
| 7 | +# modification, are permitted provided that the following conditions are met: |
| 8 | +# |
| 9 | +# 1. Redistributions of source code must retain the above copyright notice, this |
| 10 | +# list of conditions and the following disclaimer. |
| 11 | +# |
| 12 | +# 2. Redistributions in binary form must reproduce the above copyright notice, |
| 13 | +# this list of conditions and the following disclaimer in the documentation |
| 14 | +# and/or other materials provided with the distribution. |
| 15 | +# |
| 16 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 17 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 18 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | +# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 20 | +# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 21 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 22 | +# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 23 | +# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 | +# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 25 | +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 26 | + |
| 27 | +import typing as t |
| 28 | +from copy import deepcopy |
| 29 | + |
| 30 | +from smartsim.entity.entity import SmartSimEntity |
| 31 | +from smartsim.error.errors import SSUnsupportedError |
| 32 | +from smartsim.launchable.basejob import BaseJob |
| 33 | +from smartsim.launchable.mpmdpair import MPMDPair |
| 34 | +from smartsim.settings.base import RunSettings |
| 35 | + |
| 36 | + |
| 37 | +def _check_launcher(mpmd_pairs: t.List[MPMDPair]) -> None: |
| 38 | + """Enforce all pairs have the same launcher""" |
| 39 | + flag = 0 |
| 40 | + ret = None |
| 41 | + for mpmd_pair in mpmd_pairs: |
| 42 | + if flag == 1: |
| 43 | + if ret == mpmd_pair.launch_settings.run_command: |
| 44 | + flag = 0 |
| 45 | + else: |
| 46 | + raise SSUnsupportedError("MPMD pairs must all share the same launcher.") |
| 47 | + ret = mpmd_pair.launch_settings.run_command |
| 48 | + flag = 1 |
| 49 | + |
| 50 | + |
| 51 | +def _check_entity(mpmd_pairs: t.List[MPMDPair]) -> None: |
| 52 | + """Enforce all pairs have the same entity types""" |
| 53 | + flag = 0 |
| 54 | + ret = None |
| 55 | + for mpmd_pair in mpmd_pairs: |
| 56 | + if flag == 1: |
| 57 | + if type(ret) == type(mpmd_pair.entity): |
| 58 | + flag = 0 |
| 59 | + else: |
| 60 | + raise SSUnsupportedError( |
| 61 | + "MPMD pairs must all share the same entity type." |
| 62 | + ) |
| 63 | + ret = mpmd_pair.entity |
| 64 | + flag = 1 |
| 65 | + |
| 66 | + |
| 67 | +class MPMDJob(BaseJob): |
| 68 | + """An MPMDJob holds references to SmartSimEntity and |
| 69 | + LaunchSettings pairs. It is responsible for turning |
| 70 | + The stored pairs into an MPMD command(s) |
| 71 | + """ |
| 72 | + |
| 73 | + def __init__(self, mpmd_pairs: t.List[MPMDPair] = None) -> None: |
| 74 | + super().__init__() |
| 75 | + self._mpmd_pairs = deepcopy(mpmd_pairs) if mpmd_pairs else [] |
| 76 | + _check_launcher(self._mpmd_pairs) |
| 77 | + _check_entity(self._mpmd_pairs) |
| 78 | + # TODO: self.warehouse_runner = MPMDJobWarehouseRunner |
| 79 | + |
| 80 | + @property |
| 81 | + def mpmd_pairs(self) -> t.List[MPMDPair]: |
| 82 | + return deepcopy(self._mpmd_pairs) |
| 83 | + |
| 84 | + @mpmd_pairs.setter |
| 85 | + def mpmd_pair(self, value): |
| 86 | + self._mpmd_pair = deepcopy(value) |
| 87 | + |
| 88 | + def add_mpmd_pair( |
| 89 | + self, entity: SmartSimEntity, launch_settings: RunSettings |
| 90 | + ) -> None: |
| 91 | + """ |
| 92 | + Add a mpmd pair to the mpmd job |
| 93 | + """ |
| 94 | + self._mpmd_pairs.append(MPMDPair(entity, launch_settings)) |
| 95 | + _check_launcher(self.mpmd_pairs) |
| 96 | + _check_entity(self.mpmd_pairs) |
| 97 | + |
| 98 | + def get_launch_steps(self) -> None: # TODO: -> LaunchSteps: |
| 99 | + """Return the launch steps corresponding to the |
| 100 | + internal data. |
| 101 | + """ |
| 102 | + pass |
| 103 | + # TODO: return MPMDJobWarehouseRunner.run(self) |
| 104 | + |
| 105 | + def __str__(self) -> str: # pragma: no cover |
| 106 | + """returns A user-readable string of a MPMD Job""" |
| 107 | + for mpmd_pair in self.mpmd_pairs: |
| 108 | + string = "\n== MPMD Pair == \n{}\n{}\n" |
| 109 | + return string.format(mpmd_pair.entity, mpmd_pair.launch_settings) |
| 110 | + return string |
0 commit comments