Skip to content

Commit

Permalink
Merge pull request #472 from SpiNNakerManchester/one_key_and_mask2
Browse files Browse the repository at this point in the history
Single key_and_mask
  • Loading branch information
rowleya committed Sep 30, 2022
2 parents b294fdb + 2f2e413 commit ad054d4
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 56 deletions.
4 changes: 2 additions & 2 deletions pacman/model/routing_info/app_vertex_routing_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def merge_machine_entries(self, entries):
if next_entries <= (n_entries - i) or is_last:
mask = self.__group_mask(next_entries)
yield MulticastRoutingEntry(
r_info.first_key, mask, defaultable=entry.defaultable,
r_info.key, mask, defaultable=entry.defaultable,
spinnaker_route=entry.spinnaker_route)
i += next_entries

Expand All @@ -66,7 +66,7 @@ def merge_machine_entries(self, entries):
mask = self.__group_mask(next_entries)
(_, _, entry, r_info) = entries[i]
yield MulticastRoutingEntry(
r_info.first_key, mask,
r_info.key, mask,
defaultable=entry.defaultable,
spinnaker_route=entry.spinnaker_route)
entries_to_go -= next_entries
Expand Down
5 changes: 2 additions & 3 deletions pacman/model/routing_info/routing_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,8 @@ def get_first_key_from_pre_vertex(self, vertex, partition_id):
:return: The routing key of the partition
:rtype: int
"""
key = (vertex, partition_id)
if key in self._info:
return self._info[key].keys_and_masks[0].key
if (vertex, partition_id) in self._info:
return self._info[(vertex, partition_id)].key
return None

def __iter__(self):
Expand Down
36 changes: 15 additions & 21 deletions pacman/model/routing_info/vertex_routing_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import numpy
from pacman.exceptions import PacmanConfigurationException
from spinn_utilities.abstract_base import abstractproperty, AbstractBase
from .base_key_and_mask import BaseKeyAndMask


class VertexRoutingInfo(object, metaclass=AbstractBase):
Expand All @@ -25,21 +26,22 @@ class VertexRoutingInfo(object, metaclass=AbstractBase):

__slots__ = [
# The keys allocated to the machine partition
"__keys_and_masks",
"__key_and_mask",

# The partition identifier of the allocation
"__partition_id"
]

def __init__(self, keys_and_masks, partition_id):
def __init__(self, key_and_mask, partition_id):
"""
:param iterable(BaseKeyAndMask) keys_and_masks:\
The keys allocated to the machine partition
:param str partition_id: The partition to set the keys for
:param MachineVertex machine_vertex: The vertex to set the keys for
:param int index: The index of the machine vertex
"""
self.__keys_and_masks = keys_and_masks
assert isinstance(key_and_mask, BaseKeyAndMask)
self.__key_and_mask = key_and_mask
self.__partition_id = partition_id

def get_keys(self, n_keys=None):
Expand All @@ -50,7 +52,7 @@ def get_keys(self, n_keys=None):
:rtype: ~numpy.ndarray
"""

max_n_keys = sum(km.n_keys for km in self.__keys_and_masks)
max_n_keys = self.__key_and_mask.n_keys

if n_keys is None:
n_keys = max_n_keys
Expand All @@ -61,41 +63,33 @@ def get_keys(self, n_keys=None):

key_array = numpy.zeros(n_keys, dtype=">u4")
offset = 0
for key_and_mask in self.__keys_and_masks:
_, offset = key_and_mask.get_keys(
key_array=key_array, offset=offset, n_keys=(n_keys - offset))
_, offset = self.__key_and_mask.get_keys(
key_array=key_array, offset=offset, n_keys=(n_keys - offset))
return key_array

@property
def keys_and_masks(self):
"""
:rtype: iterable(BaseKeyAndMask)
"""
return self.__keys_and_masks

@property
def first_key_and_mask(self):
""" The first key and mask (or only one if there is only one)
def key_and_mask(self):
""" The only key and mask
:rtype: BaseKeyAndMask
"""
return self.__keys_and_masks[0]
return self.__key_and_mask

@property
def first_key(self):
def key(self):
""" The first key (or only one if there is only one)
:rtype: int
"""
return self.__keys_and_masks[0].key
return self.__key_and_mask.key

@property
def first_mask(self):
def mask(self):
""" The first mask (or only one if there is only one)
:rtype: int
"""
return self.__keys_and_masks[0].mask
return self.__key_and_mask.mask

@property
def partition_id(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,10 +99,9 @@ def validate_routes(placements, routing_tables):
m_vertex, partition.identifier)

# search for these destinations
for key_and_mask in r_info.keys_and_masks:
_search_route(
placement, destinations[m_vertex], key_and_mask,
routing_tables, m_vertex.vertex_slice.n_atoms)
_search_route(
placement, destinations[m_vertex], r_info.key_and_mask,
routing_tables, m_vertex.vertex_slice.n_atoms)


def _search_route(source_placement, dest_placements, key_and_mask,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,13 @@ def __allocate_all_fixed(self):
n_bits_atoms = self.__atom_bits_per_app_part[vertex, part_id]
routing_infos.add_routing_info(
AppVertexRoutingInfo(
[key_and_mask], part_id, vertex,
key_and_mask, part_id, vertex,
self.__mask(n_bits_atoms), n_bits_atoms,
len(vertex.machine_vertices)-1))
elif isinstance(vertex, MachineVertex):
routing_infos.add_routing_info(
MachineVertexRoutingInfo(
[key_and_mask], part_id, vertex, vertex.index))
key_and_mask, part_id, vertex, vertex.index))
return routing_infos

def __allocate(self):
Expand Down Expand Up @@ -365,7 +365,7 @@ def __allocate(self):
key = key << n_bits_atoms
key_and_mask = BaseKeyAndMask(base_key=key, mask=mask)
routing_infos.add_routing_info(MachineVertexRoutingInfo(
[key_and_mask], identifier, machine_vertex,
key_and_mask, identifier, machine_vertex,
machine_index))

# Add application-level routing information
Expand All @@ -377,7 +377,7 @@ def __allocate(self):
mask = self.__mask(n_bits_atoms + n_bits_machine)
key_and_mask = BaseKeyAndMask(key, mask)
routing_infos.add_routing_info(AppVertexRoutingInfo(
[key_and_mask], identifier, pre,
key_and_mask, identifier, pre,
self.__mask(n_bits_atoms), n_bits_atoms,
len(machine_vertices) - 1))
app_part_index += 1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ def __create_routing_table(x, y, partitions_in_table, routing_infos):
r_info = routing_infos.get_routing_info_from_pre_vertex(
source_vertex, partition_id)
entry = partitions_in_table[source_vertex, partition_id]
for key_and_mask in r_info.keys_and_masks:
table.add_multicast_routing_entry(
__create_entry(key_and_mask, entry))
table.add_multicast_routing_entry(
__create_entry(r_info.key_and_mask, entry))

return table


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ def __match(iterator, vertex, part_id, r_info, entry, routing_info):
return False
if part_id != next_part_id:
return False
if __mask_has_holes(r_info.first_mask):
if __mask_has_holes(r_info.mask):
return False
next_r_info = routing_info.get_routing_info_from_pre_vertex(
next_vertex, next_part_id)
Expand Down Expand Up @@ -120,7 +120,7 @@ def __merged_keys_and_masks(entries, routing_info):
(vertex, part_id, entry, r_info) = entries[0]
if isinstance(vertex, ApplicationVertex) or len(entries) == 1:
yield MulticastRoutingEntry(
r_info.first_key, r_info.first_mask, defaultable=entry.defaultable,
r_info.key, r_info.mask, defaultable=entry.defaultable,
spinnaker_route=entry.spinnaker_route)
else:
app_r_info = routing_info.get_routing_info_from_pre_vertex(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,7 @@ def _add_partition_based(
for partition in partitions:
r_info = routing_infos.get_routing_info_from_partition(partition)
entry = partitions_in_table[partition]
for key_and_mask in r_info.keys_and_masks:
self.__add_key_and_mask(key_and_mask, entry, table)
self.__add_key_and_mask(r_info.key_and_mask, entry, table)

@staticmethod
def __add_key_and_mask(key_and_mask, entry, table):
Expand Down
14 changes: 3 additions & 11 deletions unittests/model_tests/routing_info_tests/test_routing_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_routing_info(self):
pre_vertex = SimpleMachineVertex(ConstantSDRAM(0))
key = 12345
info = MachineVertexRoutingInfo(
[BaseKeyAndMask(key, FULL_MASK)], "Test", pre_vertex, 0)
BaseKeyAndMask(key, FULL_MASK), "Test", pre_vertex, 0)
routing_info = RoutingInfo()
routing_info.add_routing_info(info)

Expand All @@ -57,14 +57,14 @@ def test_routing_info(self):
assert next(iter(routing_info)) == info

info2 = MachineVertexRoutingInfo(
[BaseKeyAndMask(key, FULL_MASK)], "Test", pre_vertex, 0)
BaseKeyAndMask(key, FULL_MASK), "Test", pre_vertex, 0)

with self.assertRaises(PacmanAlreadyExistsException):
routing_info.add_routing_info(info2)
assert info != info2

info3 = MachineVertexRoutingInfo(
[BaseKeyAndMask(key, FULL_MASK)], "Test2", pre_vertex, 0)
BaseKeyAndMask(key, FULL_MASK), "Test2", pre_vertex, 0)
routing_info.add_routing_info(info3)
assert info != info3
assert routing_info.get_routing_info_from_pre_vertex(
Expand All @@ -74,14 +74,6 @@ def test_routing_info(self):
assert routing_info.get_routing_info_from_pre_vertex(
pre_vertex, "Test2").get_keys().tolist() == [key]

info4 = MachineVertexRoutingInfo(
[BaseKeyAndMask(key, FULL_MASK),
BaseKeyAndMask(key * 2, FULL_MASK)], "Test4", pre_vertex, 0)
routing_info.add_routing_info(info4)

assert routing_info.get_routing_info_from_pre_vertex(
pre_vertex, "Test4").get_keys().tolist() == [key, key * 2]

def test_base_key_and_mask(self):
with self.assertRaises(PacmanConfigurationException):
BaseKeyAndMask(0xF0, 0x40)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,10 @@ def check_masks_all_the_same(routing_info, mask):
seen_keys = set()
for r_info in routing_info:
if isinstance(r_info.vertex, MachineVertex):
assert len(r_info.keys_and_masks) == 1
assert (r_info.first_mask == mask or
assert (r_info.mask == mask or
r_info.machine_vertex.label == "RETINA")
assert r_info.first_key not in seen_keys
seen_keys.add(r_info.first_key)
assert r_info.key not in seen_keys
seen_keys.add(r_info.key)


def check_fixed(m_vertex, part_id, key):
Expand Down

0 comments on commit ad054d4

Please sign in to comment.