Skip to content

Commit 246e5de

Browse files
committed
use AbstractVertex rather than Union
1 parent 1e750d5 commit 246e5de

File tree

3 files changed

+29
-22
lines changed

3 files changed

+29
-22
lines changed

pacman/model/partitioner_splitters/abstract_splitter_common.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
Iterable, Generic, Optional, Sequence, Tuple, TypeVar, Union)
1616
from spinn_utilities.abstract_base import AbstractBase, abstractmethod
1717
from pacman.exceptions import PacmanConfigurationException
18+
from pacman.model.graphs import AbstractVertex
1819
from pacman.model.graphs.application import ApplicationVertex
1920
from pacman.utilities.utility_objs import ChipCounter
2021
from pacman.model.graphs.common import Slice
@@ -150,8 +151,7 @@ def get_in_coming_vertices(
150151
def get_source_specific_in_coming_vertices(
151152
self, source_vertex: ApplicationVertex,
152153
partition_id: str) -> Sequence[Tuple[
153-
MachineVertex, Sequence[
154-
Union[MachineVertex, ApplicationVertex]]]]:
154+
MachineVertex, Sequence[AbstractVertex]]]:
155155
"""
156156
Get machine post-vertices for a given source.
157157
@@ -171,9 +171,6 @@ def get_source_specific_in_coming_vertices(
171171
:param str partition_id: The identifier of the incoming partition
172172
:return: A list of tuples of (target machine vertex, list of source
173173
machine or application vertices that should hit the target)
174-
:rtype: list(tuple(~pacman.model.graphs.machine.MachineVertex,
175-
list(~pacman.model.graphs.machine.MachineVertex or
176-
~pacman.model.graphs.application.ApplicationVertex)))
177174
"""
178175
return [(m_vertex, [source_vertex])
179176
for m_vertex in self.get_in_coming_vertices(partition_id)]

pacman/model/routing_info/vertex_routing_info.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
from spinn_utilities.abstract_base import abstractmethod, AbstractBase
1818

1919
from pacman.exceptions import PacmanConfigurationException
20+
from pacman.model.graphs import AbstractVertex
2021
from pacman.model.graphs.application import ApplicationVertex
2122
from pacman.model.graphs.machine import MachineVertex
2223

@@ -106,7 +107,7 @@ def partition_id(self) -> str:
106107

107108
@property
108109
@abstractmethod
109-
def vertex(self) -> Union[ApplicationVertex, MachineVertex]:
110+
def vertex(self) -> AbstractVertex:
110111
"""
111112
The vertex of the information.
112113

pacman/operations/router_algorithms/application_router.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
from spinn_machine import Machine, RoutingEntry
2222
from pacman.data import PacmanDataView
2323
from pacman.exceptions import PacmanRoutingException
24+
from pacman.model.graphs import AbstractVertex
2425
from pacman.model.routing_table_by_partition import (
2526
MulticastRoutingTableByPartition)
2627
from pacman.utilities.algorithm_utilities.routing_algorithm_utilities import (
@@ -31,10 +32,9 @@
3132
from pacman.model.graphs.machine import MachineVertex, MulticastEdgePartition
3233
from pacman.model.graphs import AbstractEdgePartition
3334

34-
_AnyVertex: TypeAlias = Union[ApplicationVertex, MachineVertex]
3535
_Node: TypeAlias = Tuple[int, XY]
3636
_OptInt: TypeAlias = Optional[int]
37-
_MappedSrc: TypeAlias = Tuple[_AnyVertex, _OptInt, _OptInt]
37+
_MappedSrc: TypeAlias = Tuple[AbstractVertex, _OptInt, _OptInt]
3838

3939

4040
class _Targets(object):
@@ -45,10 +45,10 @@ class _Targets(object):
4545

4646
def __init__(self) -> None:
4747
self.__targets_by_source: Dict[
48-
_AnyVertex, Tuple[List[int], List[int]]] = defaultdict(
48+
AbstractVertex, Tuple[List[int], List[int]]] = defaultdict(
4949
lambda: (list(), list()))
5050

51-
def ensure_source(self, source_vertex: _AnyVertex) -> None:
51+
def ensure_source(self, source_vertex: AbstractVertex) -> None:
5252
"""
5353
Ensure that a source exists, even if it targets nothing.
5454
@@ -60,7 +60,8 @@ def ensure_source(self, source_vertex: _AnyVertex) -> None:
6060

6161
def add_sources_for_target(
6262
self, core: _OptInt, link: _OptInt,
63-
source_vertices: Iterable[_AnyVertex], partition_id: str) -> None:
63+
source_vertices: Iterable[AbstractVertex],
64+
partition_id: str) -> None:
6465
"""
6566
Add a set of vertices that target a given core or link.
6667
@@ -78,14 +79,17 @@ def add_sources_for_target(
7879
self.__add_m_vertices(vertex, partition_id, core, link)
7980
else:
8081
self.__add_source(vertex, core, link)
81-
else:
82+
elif isinstance(vertex, MachineVertex):
8283
if vertex.app_vertex in self.__targets_by_source:
8384
self.__replace_app_vertex(vertex.app_vertex, partition_id)
8485
self.__add_source(vertex, core, link)
86+
else:
87+
raise TypeError(f"Unexpected {vertex=}")
8588

8689
def add_machine_sources_for_target(
8790
self, core: _OptInt, link: _OptInt,
88-
source_vertices: Iterable[_AnyVertex], partition_id: str) -> None:
91+
source_vertices: Iterable[AbstractVertex],
92+
partition_id: str) -> None:
8993
"""
9094
Add a set of machine vertices that target a given core or link.
9195
@@ -102,10 +106,12 @@ def add_machine_sources_for_target(
102106
if vertex in self.__targets_by_source:
103107
self.__replace_app_vertex(vertex, partition_id)
104108
self.__add_m_vertices(vertex, partition_id, core, link)
105-
else:
109+
elif isinstance(vertex, MachineVertex):
106110
if vertex.app_vertex in self.__targets_by_source:
107111
self.__replace_app_vertex(vertex.app_vertex, partition_id)
108112
self.__add_source(vertex, core, link)
113+
else:
114+
raise TypeError(f"Unexpected {vertex=}")
109115

110116
def __is_m_vertex(
111117
self, vertex: ApplicationVertex, partition_id: str) -> bool:
@@ -145,7 +151,7 @@ def __add_m_vertices(
145151
self.__add_source(vtx, core, link)
146152

147153
def __add_source(
148-
self, source: _AnyVertex, core: _OptInt, link: _OptInt) -> None:
154+
self, source: AbstractVertex, core: _OptInt, link: _OptInt) -> None:
149155
"""
150156
:param source:
151157
:param core:
@@ -159,7 +165,7 @@ def __add_source(
159165

160166
@property
161167
def targets_by_source(self) -> Iterable[
162-
Tuple[_AnyVertex, Tuple[List[int], List[int]]]]:
168+
Tuple[AbstractVertex, Tuple[List[int], List[int]]]]:
163169
"""
164170
List of (source, (list of cores, list of links)) to target.
165171
@@ -168,8 +174,8 @@ def targets_by_source(self) -> Iterable[
168174
"""
169175
return self.__targets_by_source.items()
170176

171-
def get_targets_for_source(self, vertex: _AnyVertex) -> Tuple[
172-
_AnyVertex, Tuple[List[int], List[int]]]:
177+
def get_targets_for_source(self, vertex: AbstractVertex) -> Tuple[
178+
AbstractVertex, Tuple[List[int], List[int]]]:
173179
"""
174180
Get the cores and links for a specific source.
175181
@@ -916,7 +922,7 @@ def _find_path(
916922

917923
def _convert_a_route(
918924
routing_tables: MulticastRoutingTableByPartition,
919-
source_vertex: _AnyVertex, partition_id: str,
925+
source_vertex: AbstractVertex, partition_id: str,
920926
first_incoming_processor: _OptInt, first_incoming_link: _OptInt,
921927
first_route: RoutingTree, targets: Dict[XY, _Targets],
922928
use_source_for_targets: bool = False,
@@ -964,7 +970,7 @@ def _convert_a_route(
964970
if (x, y) in targets:
965971
chip_targets = targets[x, y]
966972
targets_by_source: Iterable[
967-
Tuple[_AnyVertex, Tuple[List[int], List[int]]]]
973+
Tuple[AbstractVertex, Tuple[List[int], List[int]]]]
968974
if use_source_for_targets:
969975
targets_by_source = [
970976
chip_targets.get_targets_for_source(source_vertex)]
@@ -978,8 +984,11 @@ def _convert_a_route(
978984
for (source, (add_cores, add_links)) in targets_by_source:
979985
if isinstance(source, ApplicationVertex):
980986
app_vertex_source = True
981-
else:
987+
elif isinstance(source, MachineVertex):
982988
machine_vertex_sources.add(source)
989+
else:
990+
raise TypeError(f"Unexpected vertex {source}")
991+
983992
entry = RoutingEntry(
984993
link_ids=link_ids + add_links,
985994
processor_ids=processor_ids + add_cores,
@@ -1016,7 +1025,7 @@ def _add_routing_entry(
10161025
first_route: RoutingTree,
10171026
routing_tables: MulticastRoutingTableByPartition,
10181027
entry: RoutingEntry,
1019-
x: int, y: int, source: _AnyVertex, partition_id: str) -> None:
1028+
x: int, y: int, source: AbstractVertex, partition_id: str) -> None:
10201029
try:
10211030
routing_tables.add_path_entry(entry, x, y, source, partition_id)
10221031
except Exception as e:

0 commit comments

Comments
 (0)