Example #1
0
def communicator2() -> Communicator:
    with patch('libmuscle.communicator.server_types', [DirectServer]):
        instance_id = Reference('other')
        communicator = Communicator(instance_id, [], None, MagicMock())
        communicator._peer_manager = MagicMock()
        pm = communicator._peer_manager
        pm.is_connected.return_value = True

        def gpp(x: Reference) -> Reference:
            if 'out' in str(x):
                return Reference('in')
            return Reference('out')

        pm.get_peer_port = gpp

        pm.get_peer_dims.return_value = []
        pm.get_peer_locations.return_value = ['direct:test']

        def gpe(p, s) -> Reference:
            endpoint = MagicMock()
            endpoint.instance.return_value = Reference('kernel[13]')
            if 'out' in str(p):
                endpoint.ref.return_value = Reference('kernel[13].in')
            else:
                endpoint.ref.return_value = Reference('kernel[13].out')
            return endpoint

        pm.get_peer_endpoint = gpe

        communicator._ports = {
            'out': Port('out', Operator.O_I, True, True, 0, [20]),
            'in': Port('in', Operator.S, True, True, 0, [20])
        }
        yield communicator
        communicator.shutdown()
Example #2
0
    def __ports_from_conduits(self,
                              conduits: List[Conduit]) -> Dict[str, Port]:
        """Derives port definitions from conduits.

        Args:
            conduits: The list of conduits.
        """
        ports = dict()
        for conduit in conduits:
            if conduit.sending_compute_element() == self._kernel:
                port_id = conduit.sending_port()
                operator = Operator.O_F
                port_peer_dims = self._peer_manager.get_peer_dims(
                    conduit.receiving_compute_element())
            elif conduit.receiving_compute_element() == self._kernel:
                port_id = conduit.receiving_port()
                operator = Operator.F_INIT
                port_peer_dims = self._peer_manager.get_peer_dims(
                    conduit.sending_compute_element())
            else:
                continue

            ndims = max(0, len(port_peer_dims) - len(self._index))
            is_vector = (ndims == 1)
            is_connected = self._peer_manager.is_connected(port_id)
            if not str(port_id).startswith('muscle_'):
                ports[str(port_id)] = Port(str(port_id), operator,
                                           is_vector, is_connected,
                                           len(self._index), port_peer_dims)
        return ports
Example #3
0
    def __settings_in_port(self, conduits: List[Conduit]) -> Port:
        """Creates a Port representing muscle_settings_in.

        Args:
            conduits: The list of conduits.
        """
        for conduit in conduits:
            if conduit.receiving_compute_element() == self._kernel:
                port_id = conduit.receiving_port()
                if str(port_id) == 'muscle_settings_in':
                    return Port(
                        str(port_id), Operator.F_INIT, False,
                        self._peer_manager.is_connected(port_id),
                        len(self._index),
                        self._peer_manager.get_peer_dims(
                            conduit.sending_compute_element()))
        return Port('muscle_settings_in', Operator.F_INIT, False, False,
                    len(self._index), [])
Example #4
0
def communicator() -> Communicator:
    # Creating a Communicator will start servers, and some servers are not
    # fork-compatible. This then crashes the later integration tests, which
    # fork. So we disable everything except DirectServer.
    with patch('libmuscle.communicator.server_types', [DirectServer]):
        instance_id = Reference('kernel')
        communicator = Communicator(instance_id, [13], None, MagicMock())
        communicator._peer_manager = MagicMock()
        pm = communicator._peer_manager
        pm.is_connected.return_value = True

        def gpp(x) -> Reference:
            if 'out' in str(x):
                return Reference('in')
            return Reference('out')

        pm.get_peer_port = gpp

        pm.get_peer_dims.return_value = []
        pm.get_peer_locations.return_value = ['direct:test']

        def gpe(p, s) -> Reference:
            endpoint = MagicMock()
            endpoint.instance.return_value = Reference('other')
            if 'out' in str(p):
                endpoint.ref.return_value = Reference('other.in[13]')
            else:
                endpoint.ref.return_value = Reference('other.out')
            return endpoint

        pm.get_peer_endpoint = gpe

        communicator._ports = {
            'out': Port('out', Operator.O_I, False, True, 1, []),
            'in': Port('in', Operator.S, False, True, 1, [])
        }
        yield communicator
        communicator.shutdown()
Example #5
0
 def __ports_from_declared(self) -> Dict[str, Port]:
     """Derives port definitions from supplied declaration.
     """
     ports = dict()
     declared_ports = cast(Dict[Operator, List[str]], self._declared_ports)
     for operator, port_list in declared_ports.items():
         for port_desc in port_list:
             port_name, is_vector = self.__split_port_desc(port_desc)
             if port_name.startswith('muscle_'):
                 raise RuntimeError(('Port names starting with "muscle_"'
                                     ' are reserved for MUSCLE, please'
                                     ' rename port "{}"'.format(port_name)))
             port_id = Identifier(port_name)
             is_connected = self._peer_manager.is_connected(port_id)
             if is_connected:
                 peer_port = self._peer_manager.get_peer_port(port_id)
                 peer_ce = cast(Reference, peer_port[:-1])
                 port_peer_dims = self._peer_manager.get_peer_dims(peer_ce)
             else:
                 port_peer_dims = []
             ports[port_name] = Port(port_name, operator,
                                     is_vector, is_connected,
                                     len(self._index), port_peer_dims)
     return ports