Skip to content

Commit

Permalink
Merge 4970fe9 into 5ea34de
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian-B committed Aug 29, 2019
2 parents 5ea34de + 4970fe9 commit 0bb2890
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 56 deletions.
4 changes: 4 additions & 0 deletions pacman/model/graphs/abstract_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ def add_vertex(self, vertex):
:type vertex: :py:class:`pacman.model.graphs.AbstractVertex`
:raises PacmanInvalidParameterException:\
If the vertex is not of a valid type
:raises PacmanConfigurationException:
If there is an attempt to add the same vertex more than once
"""

def add_vertices(self, vertices):
Expand All @@ -76,6 +78,8 @@ def add_vertices(self, vertices):
iterable(:py:class:`pacman.model.graphs.AbstractVertex`)
:raises PacmanInvalidParameterException:\
If any vertex is not of a valid type
:raises PacmanConfigurationException:
If there is an attempt to add the same vertex more than once
"""
for v in vertices:
self.add_vertex(v)
Expand Down
72 changes: 46 additions & 26 deletions pacman/model/graphs/abstract_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,45 +13,65 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from six import add_metaclass
from spinn_utilities.abstract_base import (
AbstractBase, abstractmethod, abstractproperty)
from pacman.exceptions import PacmanConfigurationException
from pacman.model.graphs.common import ConstrainedObject


@add_metaclass(AbstractBase)
class AbstractVertex(object):
class AbstractVertex(ConstrainedObject):
""" A vertex in a graph.
"""

__slots__ = ()
# Because of diamond inheritance slots must be empty
__slots__ = [
# Indicates if the Vertex has been added to a graph
"_added_to_graph",
# Label for the vertex. Changable until added to graph
"_label"]

@abstractproperty
def label(self):
""" The label of the vertex.
:rtype: str
def __init__(self, label=None, constraints=None):
"""
:param label: The optional name of the vertex
:type label: str
:param constraints: The optional initial constraints of the vertex
:type constraints: \
iterable(:py:class:`pacman.model.constraints.AbstractConstraint`)
:raise pacman.exceptions.PacmanInvalidParameterException:\
* If one of the constraints is not valid
"""

@abstractproperty
def constraints(self):
""" The constraints of the vertex.
super(AbstractVertex, self).__init__(constraints)
self._label = label
self._added_to_graph = False

:rtype: iterable(:py:class:`AbstractConstraint`)
@property
def label(self):
"""
Returns the current label to the vertex.
@abstractmethod
def add_constraint(self, constraint):
""" Add a constraint to the vertex.
This label could change when the vertex is added to the graph.
:return: The label
:rtype: str
"""
return self._label

:param constraint: The constraint to add
:type constraint: :py:class:`AbstractConstraint`
def set_label(self, label):
"""
Changes the label for a vertex NOT yet ADDED to a graph
def add_constraints(self, constraints):
""" Add a list of constraints to the vertex.
:param label: new value for the label
:raises PacmanConfigurationException:
If there is an attempt to change the label once the vertex has
been added to a graph
"""
if self._added_to_graph:
raise PacmanConfigurationException(
"As Labels are also IDs they can not be changed.")
self._label = label

:param constraints: The list of constraints to add
:type constraints: list(:py:class:`AbstractConstraint`)
def addedToGraph(self):
"""
Records that the vertex has been added to a graph
:raises PacmanConfigurationException:
If there is an attempt to add the same vertex more than once
"""
for constraint in constraints:
self.add_constraint(constraint)
self._added_to_graph = True
14 changes: 3 additions & 11 deletions pacman/model/graphs/application/application_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,20 @@

import sys
from six import add_metaclass
from spinn_utilities.overrides import overrides
from spinn_utilities.abstract_base import (
abstractmethod, abstractproperty, AbstractBase)
from pacman.model.constraints.partitioner_constraints import (
MaxVertexAtomsConstraint)
from pacman.model.graphs import AbstractVertex
from pacman.model.graphs.common import ConstrainedObject


@add_metaclass(AbstractBase)
class ApplicationVertex(ConstrainedObject, AbstractVertex):
class ApplicationVertex(AbstractVertex):
""" A vertex that can be broken down into a number of smaller vertices
based on the resources that the vertex requires.
"""

__slots__ = ["_label"]
__slots__ = []

def __init__(self, label=None, constraints=None,
max_atoms_per_core=sys.maxsize):
Expand All @@ -47,18 +45,12 @@ def __init__(self, label=None, constraints=None,
* If one of the constraints is not valid
"""

super(ApplicationVertex, self).__init__(constraints)
self._label = label
super(ApplicationVertex, self).__init__(label, constraints)

# add a constraint for max partitioning
self.add_constraint(
MaxVertexAtomsConstraint(max_atoms_per_core))

@property
@overrides(AbstractVertex.label)
def label(self):
return self._label

def __str__(self):
return self.label

Expand Down
29 changes: 26 additions & 3 deletions pacman/model/graphs/impl/graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ class Graph(ConstrainedObject, AbstractGraph):
# the outgoing partitions by edge
"_outgoing_edge_partition_by_edge",
# The label of the graph
"_label"]
"_label",
# map between labels and vertex
"_vertex_by_label",
# count of vertex which had a None or already used label
"_none_labelled_vertex_count",
]

def __init__(self, allowed_vertex_types, allowed_edge_types,
allowed_partition_types, label):
Expand All @@ -73,7 +78,9 @@ def __init__(self, allowed_vertex_types, allowed_edge_types,
self._allowed_edge_types = allowed_edge_types
self._allowed_partition_types = allowed_partition_types

self._vertices = OrderedSet()
self._vertices = []
self._vertex_by_label = dict()
self._none_labelled_vertex_count = 0
self._outgoing_edge_partitions_by_name = OrderedDict()
self._outgoing_edges = DefaultOrderedDict(OrderedSet)
self._incoming_edges = DefaultOrderedDict(OrderedSet)
Expand All @@ -88,14 +95,27 @@ def __init__(self, allowed_vertex_types, allowed_edge_types,
def label(self):
return self._label

def _label_postfix(self):
self._none_labelled_vertex_count += 1
return str(self._none_labelled_vertex_count)

@overrides(AbstractGraph.add_vertex)
def add_vertex(self, vertex):
if not isinstance(vertex, self._allowed_vertex_types):
raise PacmanInvalidParameterException(
"vertex", vertex.__class__,
"Vertices of this graph must be one of the following types:"
" {}".format(self._allowed_vertex_types))
self._vertices.add(vertex)
if not vertex.label:
vertex.set_label(
vertex.__class__.__name__ + "_" + self._label_postfix())
elif vertex.label in self._vertex_by_label:
if self._vertex_by_label[vertex.label] == vertex:
raise PacmanAlreadyExistsException("vertex", vertex.label)
vertex.set_label(vertex.label + self._label_postfix())
vertex.addedToGraph()
self._vertices.append(vertex)
self._vertex_by_label[vertex.label] = vertex

@overrides(AbstractGraph.add_edge)
def add_edge(self, edge, outgoing_edge_partition_name):
Expand Down Expand Up @@ -166,6 +186,9 @@ def add_outgoing_edge_partition(self, outgoing_edge_partition):
def vertices(self):
return self._vertices

def vertex_by_label(self, label):
return self._vertex_by_label[label]

@property
@overrides(AbstractGraph.n_vertices)
def n_vertices(self):
Expand Down
20 changes: 6 additions & 14 deletions pacman/model/graphs/machine/machine_vertex.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,15 @@

from six import add_metaclass
from spinn_utilities.abstract_base import AbstractBase, abstractproperty
from spinn_utilities.overrides import overrides
from pacman.model.graphs import AbstractVertex
from pacman.model.graphs.common import ConstrainedObject


@add_metaclass(AbstractBase)
class MachineVertex(ConstrainedObject, AbstractVertex):
class MachineVertex(AbstractVertex):
""" A machine graph vertex.
"""

__slots__ = ["_label"]
__slots__ = []

def __init__(self, label=None, constraints=None):
"""
Expand All @@ -37,16 +35,10 @@ def __init__(self, label=None, constraints=None):
:raise pacman.exceptions.PacmanInvalidParameterException:
* If one of the constraints is not valid
"""
super(MachineVertex, self).__init__(constraints)
if label:
self._label = label
else:
self._label = str(type(self))

@property
@overrides(AbstractVertex.label)
def label(self):
return self._label
if label is None:
label = str(type(self))
super(MachineVertex, self).__init__(label, constraints)
self._added_to_graph = False

def __str__(self):
_l = self.label
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
import unittest
from pacman.model.graphs.machine import (
MachineEdge, MachineGraph, SimpleMachineVertex)
from pacman.exceptions import PacmanInvalidParameterException
from pacman.exceptions import (
PacmanAlreadyExistsException, PacmanInvalidParameterException)


class TestMachineGraphModel(unittest.TestCase):
Expand Down Expand Up @@ -72,6 +73,8 @@ def test_new_graph(self):
vertices_from_graph = list(graph.vertices)
for vert in vertices_from_graph:
self.assertIn(vert, vertices)
for vert in vertices:
self.assertEqual(vert, graph.vertex_by_label(vert.label))
edges_from_graph = list(graph.edges)
for edge in edges_from_graph:
self.assertIn(edge, edges)
Expand All @@ -90,7 +93,8 @@ def test_add_duplicate_vertex(self):
edges.append(MachineEdge(vertices[0], vertices[1]))
edges.append(MachineEdge(vertices[1], vertices[0]))
graph = MachineGraph("foo")
graph.add_vertices(vertices)
with self.assertRaises(PacmanAlreadyExistsException):
graph.add_vertices(vertices)
graph.add_edges(edges, "bar")

def test_add_duplicate_edge(self):
Expand Down

0 comments on commit 0bb2890

Please sign in to comment.