Example #1
0
 def __init__(self, partition_type, other_splitter):
     super().__init__(other_splitter, "")
     self._partition_type = partition_type
     self._pre_vertices = OrderedSet()
     self._post_vertex = None
     self._pre_slices = OrderedSet()
     self._post_slice = None
     self._app_edge = None
Example #2
0
def test_obscure_stuff():
    o = OrderedSet()
    o.add(1)
    o.add(2)
    o.add(3)
    assert [x for x in reversed(o)] == [3, 2, 1]
    o2 = OrderedSet(o)
    assert [x for x in o2] == [1, 2, 3]
    assert o == o2
    o2 |= [4]
    assert o != o2
    assert repr(OrderedSet()) == "OrderedSet()"
Example #3
0
 def __init__(self, label, application_graph=None):
     """
     :param label: The label for the graph.
     :type label: str or None
     :param application_graph:
         The application graph that this machine graph is derived from, if
         it is derived from one at all.
     :type application_graph: ApplicationGraph or None
     """
     super(MachineGraph, self).__init__(MachineVertex, MachineEdge, label)
     if application_graph:
         application_graph.forget_machine_graph()
         # Check the first vertex added
         self._application_level_used = True
     else:
         # Must be false as there is no App_graph
         self._application_level_used = False
     self._multicast_partitions = DefaultOrderedDict(
         lambda: DefaultOrderedDict(set))
     self._edge_partitions = OrderedSet()
     self._fixed_route_edge_partitions_by_pre_vertex = (
         DefaultOrderedDict(OrderedSet))
     self._multicast_edge_partitions_by_pre_vertex = (
         DefaultOrderedDict(OrderedSet))
     self._sdram_edge_partitions_by_pre_vertex = (
         DefaultOrderedDict(OrderedSet))
     self._fixed_route_edge_partitions_by_post_vertex = (
         DefaultOrderedDict(OrderedSet))
     self._multicast_edge_partitions_by_post_vertex = (
         DefaultOrderedDict(OrderedSet))
     self._sdram_edge_partitions_by_post_vertex = (
         DefaultOrderedDict(OrderedSet))
Example #4
0
 def forget_machine_vertices(self):
     """ Arrange to forget all machine vertices that this application
         vertex maps to.
     """
     self._machine_vertices = OrderedSet()
     if self._splitter is not None:
         self._splitter.reset_called()
Example #5
0
    def __init__(self,
                 label=None,
                 constraints=None,
                 max_atoms_per_core=sys.maxsize,
                 splitter=None):
        """
        :param str label: The optional name of the vertex.
        :param iterable(AbstractConstraint) constraints:
            The optional initial constraints of the vertex.
        :param int max_atoms_per_core: The max number of atoms that can be
            placed on a core, used in partitioning.
        :param splitter: The splitter object needed for this vertex.
            Leave as None to delegate the choice of splitter to the selector.
        :type splitter: None or
            ~pacman.model.partitioner_interfaces.AbstractSplitterPartitioner
        :raise PacmanInvalidParameterException:
            If one of the constraints is not valid
        """
        # Need to set to None temporarily as add_constraint checks splitter
        self._splitter = None
        super(ApplicationVertex, self).__init__(label, constraints)
        self._machine_vertices = OrderedSet()

        # Use setter as there is extra work to do
        self.splitter = splitter

        # add a constraint for max partitioning
        self.add_constraint(MaxVertexAtomsConstraint(max_atoms_per_core))
def add_set(all_sets, new_set):
    """
    Adds a new set into the list of sets, concatenating sets if required.

    If the new set does not overlap any existing sets it is added.

    However if the new sets overlaps one or more existing sets, a superset is
    created combining all the overlapping sets.
    Existing overlapping sets are removed and only the new superset is added.

    :param list(set) all_sets: List of non-overlapping sets
    :param set new_set:
        A new set which may or may not overlap the previous sets.
    """

    union = OrderedSet()
    removes = []
    for a_set in all_sets:
        if not new_set.isdisjoint(a_set):
            removes.append(a_set)
            union |= a_set
    union |= new_set
    if removes:
        for a_set in removes:
            all_sets.remove(a_set)
    all_sets.append(union)
Example #7
0
def test_set_ness():
    o = OrderedSet()
    assert len(o) == 0
    o.add(123)
    assert len(o) == 1
    o.add(123)
    assert len(o) == 1
    o.add(456)
    assert len(o) == 2
    o.add(456)
    assert len(o) == 2
    o.add(123)
    assert len(o) == 2
    assert o == set([123, 456])
    assert o == set([456, 123])
    assert o == [123, 456]
    assert o == [456, 123]
    o.remove(123)
    assert len(o) == 1
    o.remove(456)
    assert len(o) == 0
    with pytest.raises(KeyError):  # @UndefinedVariable
        o.remove(789)
    o.discard(789)
    assert len(o) == 0
    o.add(789)
    assert len(o) == 1
    assert 789 in o
    o.discard(789)
    assert 789 not in o
    assert len(o) == 0
Example #8
0
def test_reverse():
    o = OrderedSet()
    o.add(1)
    o.add(2)
    o.add(3)
    a = list(reversed(o))
    assert a == [3, 2, 1]
Example #9
0
 def __init__(self,
              pre_vertex,
              post_vertex,
              label=None,
              machine_edge_type=MachineEdge):
     """
     :param ApplicationVertex pre_vertex:
         The application vertex at the start of the edge.
     :param ApplicationVertex post_vertex:
         The application vertex at the end of the edge.
     :param label: The name of the edge.
     :type label: str or None
     :param machine_edge_type:
         The type of machine edges made from this app edge. If ``None``,
         standard machine edges will be made.
     :type machine_edge_type: type(MachineEdge)
     """
     self._label = label
     self._pre_vertex = pre_vertex
     self._post_vertex = post_vertex
     if not issubclass(machine_edge_type, MachineEdge):
         raise ValueError(
             "machine_edge_type must be a kind of machine edge")
     self._machine_edge_type = machine_edge_type
     self.__machine_edges = OrderedSet()
Example #10
0
 def _get_all_possible_recordable_variables(self):
     variables = OrderedSet()
     if isinstance(self._population._vertex, AbstractSpikeRecordable):
         variables.add(SPIKES)
     if isinstance(self._population._vertex, AbstractNeuronRecordable):
         variables.update(
             self._population._vertex.get_recordable_variables())
     return variables
def __allocate_tags_for_placement(placement, resource_tracker, tag_collector,
                                  ports_collector, tag_port_tasks):
    """
    :param Placement placement:
    :param ResourceTracker resource_tracker:
    :param Tags tag_collector:
    :param dict(str,set(int)) ports_collector:
    :param list(_Task) tag_port_tasks:
    """
    vertex = placement.vertex
    resources = vertex.resources_required

    # Get the constraint details for the tags
    (board_address, ip_tags, reverse_ip_tags) = \
        ResourceTracker.get_ip_tag_info(resources, vertex.constraints)

    # Allocate the tags, first-come, first-served, using the fixed
    # placement of the vertex, and the required resources
    chips = [(placement.x, placement.y)]
    (_, _, _, returned_ip_tags, returned_reverse_ip_tags) = \
        resource_tracker.allocate_resources(
            resources, chips, placement.p, board_address, ip_tags,
            reverse_ip_tags)

    # Put the allocated IP tag information into the tag object
    if returned_ip_tags is not None:
        for (tag_constraint, (board_address, tag, dest_x, dest_y)) in \
                zip(ip_tags, returned_ip_tags):
            ip_tag = IPTag(
                board_address=board_address,
                destination_x=dest_x,
                destination_y=dest_y,
                tag=tag,
                ip_address=tag_constraint.ip_address,
                port=tag_constraint.port,
                strip_sdp=tag_constraint.strip_sdp,
                traffic_identifier=tag_constraint.traffic_identifier)
            tag_collector.add_ip_tag(ip_tag, vertex)

    if returned_reverse_ip_tags is None:
        return

    # Put the allocated reverse IP tag information into the tag object
    for tag_constraint, (board_address, tag) in zip(reverse_ip_tags,
                                                    returned_reverse_ip_tags):
        if board_address not in ports_collector:
            ports_collector[board_address] = OrderedSet(_BOARD_PORTS)
        if tag_constraint.port is not None:
            reverse_ip_tag = ReverseIPTag(board_address, tag,
                                          tag_constraint.port, placement.x,
                                          placement.y, placement.p,
                                          tag_constraint.sdp_port)
            tag_collector.add_reverse_ip_tag(reverse_ip_tag, vertex)

            ports_collector[board_address].discard(tag_constraint.port)
        else:
            tag_port_tasks.append(
                _Task(tag_constraint, board_address, tag, vertex, placement))
Example #12
0
    def constraints(self):
        """ An iterable of constraints

        :rtype: iterable(AbstractConstraint)
        """
        try:
            return self._constraints
        except Exception:  # pylint: disable=broad-except
            return OrderedSet()
Example #13
0
def test_repr():
    o = OrderedSet()
    o.add(12)
    o.add(78)
    o.add(56)
    o.add(34)
    o.add(90)
    s = "{}".format(o)
    assert s == "OrderedSet([12, 78, 56, 34, 90])"
Example #14
0
 def _allocate_ports_for_reverse_ip_tags(self, tasks, ports, tags):
     for tag_constraint, board_address, tag, vertex, placement in tasks:
         if board_address not in ports:
             ports[board_address] = OrderedSet(_BOARD_PORTS)
         port = ports[board_address].pop(last=False)
         reverse_ip_tag = ReverseIPTag(board_address, tag, port,
                                       placement.x, placement.y,
                                       placement.p, tag_constraint.sdp_port)
         tags.add_reverse_ip_tag(reverse_ip_tag, vertex)
Example #15
0
 def __active_chips(machine, placements):
     """
     :param ~.Machine machine:
     :param ~.Placements placements
     :rtype: set(~.Chip)
     """
     return OrderedSet(
         machine.get_chip_at(placement.x, placement.y)
         for placement in placements
         if isinstance(placement.vertex, ChipPowerMonitorMachineVertex))
Example #16
0
def test_update():
    o = OrderedSet()
    o.add(1)
    o.add(2)
    o.add(3)
    o.update([3, 4, 5])
    for item in (5, 4, 3, 2, 1):
        assert o.pop() == item
    with pytest.raises(KeyError):
        o.pop()
    def __unique_names(items, index):
        """ Produces an iterable of 1-tuples of the *unique* names in at \
            particular index into the provenance items' names.

        :param iterable(ProvenanceDataItem) items: The prov items
        :param int index: The index into the names
        :rtype: iterable(tuple(str))
        """
        return ((name,) for name in OrderedSet(
            item.names[index] for item in items))
Example #18
0
def test_containment():
    o = OrderedSet()
    o.add(12)
    o.add(78)
    o.add(56)
    o.add(34)
    o.add(90)
    for item in [12, 78, 56, 34, 90]:
        assert item in o
    for item in [123, 456, 789]:
        assert item not in o
Example #19
0
def test_pop():
    o = OrderedSet()
    o.add(12)
    o.add(78)
    o.add(56)
    o.add(34)
    o.add(90)
    for item in [90, 34, 56, 78, 12]:
        assert o.pop() == item
    with pytest.raises(KeyError):  # @UndefinedVariable
        o.pop()
Example #20
0
    def get_all_possible_recordable_variables(self):
        """ All variables that could be recorded.

        :rtype: set(str)
        """
        variables = OrderedSet()
        if isinstance(self.__vertex, AbstractSpikeRecordable):
            variables.add(SPIKES)
        if isinstance(self.__vertex, AbstractNeuronRecordable):
            variables.update(self.__vertex.get_recordable_variables())
        return variables
    def _find_one_to_one_vertices(vertex, graph):
        """ Find vertices which have one to one connections with the given\
            vertex, and where their constraints don't force them onto\
            different chips.

        :param MachineGraph graph:
            the graph to look for other one to one vertices
        :param MachineVertex vertex:
            the vertex to use as a basis for one to one connections
        :return: set of one to one vertices
        :rtype: set(MachineVertex)
        """
        # Virtual vertices can't be forced on other chips
        if isinstance(vertex, AbstractVirtual):
            return []
        found_vertices = OrderedSet()
        vertices_seen = {vertex}

        # look for one to ones leaving this vertex
        outgoing = graph.get_edges_starting_at_vertex(vertex)
        vertices_to_try = deque(
            edge.post_vertex for edge in outgoing
            if edge.post_vertex not in vertices_seen)
        while vertices_to_try:
            next_vertex = vertices_to_try.pop()
            if next_vertex not in vertices_seen and \
                    not isinstance(next_vertex, AbstractVirtual):
                vertices_seen.add(next_vertex)
                if is_single(graph.get_edges_ending_at_vertex(next_vertex)):
                    found_vertices.add(next_vertex)
                    outgoing = graph.get_edges_starting_at_vertex(next_vertex)
                    vertices_to_try.extend(
                        edge.post_vertex for edge in outgoing
                        if edge.post_vertex not in vertices_seen)

        # look for one to ones entering this vertex
        incoming = graph.get_edges_ending_at_vertex(vertex)
        vertices_to_try = deque(
            edge.pre_vertex for edge in incoming
            if edge.pre_vertex not in vertices_seen)
        while vertices_to_try:
            next_vertex = vertices_to_try.pop()
            if next_vertex not in vertices_seen:
                vertices_seen.add(next_vertex)
                if is_single(graph.get_edges_starting_at_vertex(next_vertex)):
                    found_vertices.add(next_vertex)
                    incoming = graph.get_edges_ending_at_vertex(next_vertex)
                    vertices_to_try.extend(
                        edge.pre_vertex for edge in incoming
                        if edge.pre_vertex not in vertices_seen)

        found_vertices.update(get_vertices_on_same_chip(vertex, graph))
        return found_vertices
Example #22
0
    def __init__(self, constraints=None):
        """
        :param iterable(AbstractConstraint) constraints:
            Any initial constraints
        """

        # safety point for diamond inheritance
        if not hasattr(self, '_constraints') or self._constraints is None:
            self._constraints = OrderedSet()

        # add new constraints to the set
        self.add_constraints(constraints)
Example #23
0
def test_ordered_ness():
    o = OrderedSet()
    o.add(12)
    o.add(78)
    o.add(56)
    o.add(34)
    o.add(90)
    assert len(o) == 5
    assert list(o) == [12, 78, 56, 34, 90]
    result = []
    for item in o:
        result.append(item)
    assert result == [12, 78, 56, 34, 90]
Example #24
0
def test_peek():
    o = OrderedSet()
    o.add(1)
    o.add(2)
    o.add(3)
    p1 = o.peek()
    p2 = o.pop()
    assert p1 == 3
    assert p1 == p2
    p3 = o.peek(last=False)
    assert p3 == 1
    p4 = o.pop(last=False)
    assert p4 == p3
Example #25
0
 def _get_all_recording_variables(self):
     possibles = self._get_all_possible_recordable_variables()
     variables = OrderedSet()
     for possible in possibles:
         if possible == SPIKES:
             if isinstance(self._population._vertex,
                           AbstractSpikeRecordable) \
                     and self._population._vertex.is_recording_spikes():
                 variables.add(possible)
         elif isinstance(self._population._vertex,
                         AbstractNeuronRecordable) and \
                 self._population._vertex.is_recording(possible):
             variables.add(possible)
     return variables
    def allocate(self, p):
        if p is None:
            p = self._cores.pop()
        else:
            self._cores.remove(p)
        if self._cores_counter:
            self._cores_counter[self._n_cores] -= 1
        self._n_cores -= 1
        if self._cores_counter:
            self._cores_counter[self._n_cores] += 1

        if self._n_cores <= 0:
            self._cores = OrderedSet()
        return p
Example #27
0
    def _sort_vertices_for_one_to_one_connection(self, machine_graph,
                                                 same_chip_vertex_groups):
        """

        :param machine_graph: the graph to place
        :return: list of sorted vertices
        """
        sorted_vertices = list()
        found_list = set()

        # order vertices based on constraint priority
        vertices = sort_vertices_by_known_constraints(machine_graph.vertices)

        for vertex in vertices:
            if vertex not in found_list:

                # vertices that are one to one connected with vertex and are
                # not forced off chip
                connected_vertices = self._find_one_to_one_vertices(
                    vertex, machine_graph)

                # create list for each vertex thats connected haven't already
                #  been seen before
                new_list = OrderedSet()
                for found_vertex in connected_vertices:
                    if found_vertex not in found_list:
                        new_list.add(found_vertex)

                # looks for vertices that have same chip constraints but not
                # found by the one to one connection search.
                same_chip_vertices = list()
                for found_vertex in new_list:
                    for same_chip_constrained_vertex in \
                            same_chip_vertex_groups[found_vertex]:
                        if same_chip_constrained_vertex not in new_list:
                            same_chip_vertices.append(
                                same_chip_constrained_vertex)

                # add these newly found vertices to the list
                new_list.update(same_chip_vertices)

                sorted_vertices.append(new_list)
                found_list.update(new_list)

        # locate vertices which have no output or input, and add them for
        # placement
        for vertex in vertices:
            if vertex not in found_list:
                sorted_vertices.append([vertex])
        return sorted_vertices
def __allocate_ports_for_reverse_ip_tags(tasks, ports, tags):
    """
    :param list(_Task) tag_port_tasks:
    :param dict(str,set(int)) ports:
    :param Tags tags:
    """
    for task in tasks:
        if task.board not in ports:
            ports[task.board] = OrderedSet(_BOARD_PORTS)
        port = ports[task.board].pop(last=False)
        reverse_ip_tag = ReverseIPTag(task.board, task.tag, port,
                                      task.placement.x, task.placement.y,
                                      task.placement.p,
                                      task.constraint.sdp_port)
        tags.add_reverse_ip_tag(reverse_ip_tag, task.vertex)
Example #29
0
    def get_all_recording_variables(self):
        """ All variables that have been set to record.

        :rtype: set(str)
        """
        possibles = self.get_all_possible_recordable_variables()
        variables = OrderedSet()
        for possible in possibles:
            if possible == SPIKES:
                if isinstance(self.__vertex, AbstractSpikeRecordable) \
                        and self.__vertex.is_recording_spikes():
                    variables.add(possible)
            elif isinstance(self.__vertex, AbstractNeuronRecordable) \
                    and self.__vertex.is_recording(possible):
                variables.add(possible)
        return variables
    def __old_get_data_for_placements_with_monitors(self, placements,
                                                    progress):
        # locate receivers
        receivers = list(
            OrderedSet(
                locate_extra_monitor_mc_receiver(
                    self._machine, placement.x, placement.y,
                    self._packet_gather_cores_to_ethernet_connection_map)
                for placement in placements))

        # Ugly, to avoid an import loop...
        with receivers[0].streaming(receivers, self._transceiver,
                                    self._extra_monitor_cores,
                                    self._placements):
            # get data
            self.__old_get_data_for_placements(placements, progress)