Exemplo n.º 1
0
    def setUp(self):

        self.machine = virtual_machine(8, 8)
        self.mach_graph = MachineGraph("machine")
        self.vertices = list()
        self.vertex1 = T_MachineVertex(
            0, 1, get_resources_used_by_atoms(0, 1, []), "First vertex")
        self.vertex2 = T_MachineVertex(
            1, 5, get_resources_used_by_atoms(1, 5, []), "Second vertex")
        self.vertex3 = T_MachineVertex(
            5, 10, get_resources_used_by_atoms(5, 10, []), "Third vertex")
        self.vertex4 = T_MachineVertex(
            10, 100, get_resources_used_by_atoms(10, 100, []),
            "Fourth vertex")
        self.vertices.append(self.vertex1)
        self.mach_graph.add_vertex(self.vertex1)
        self.vertices.append(self.vertex2)
        self.mach_graph.add_vertex(self.vertex2)
        self.vertices.append(self.vertex3)
        self.mach_graph.add_vertex(self.vertex3)
        self.vertices.append(self.vertex4)
        self.mach_graph.add_vertex(self.vertex4)
        self.edges = list()

        self.plan_n_timesteps = 100
Exemplo n.º 2
0
 def test_new_graph(self):
     """
     tests that after building a machine graph, all partitined vertices
     and partitioned edges are in existence
     """
     vertices = list()
     edges = list()
     for i in range(10):
         vertices.append(
             SimpleMachineVertex(ResourceContainer(), "V{}".format(i)))
     with self.assertRaises(NotImplementedError):
         vertices[1].add_constraint(SameAtomsAsVertexConstraint(
             vertices[4]))
         vertices[4].add_constraint(SameAtomsAsVertexConstraint(
             vertices[1]))
     for i in range(5):
         edges.append(MachineEdge(vertices[0], vertices[(i + 1)]))
     for i in range(5, 10):
         edges.append(MachineEdge(vertices[5], vertices[(i + 1) % 10]))
     graph = MachineGraph("foo")
     graph.add_vertices(vertices)
     graph.add_outgoing_edge_partition(
         MulticastEdgePartition(identifier="bar", pre_vertex=vertices[0]))
     graph.add_outgoing_edge_partition(
         MulticastEdgePartition(identifier="bar", pre_vertex=vertices[5]))
     graph.add_edges(edges, "bar")
     self.graph_there_and_back(graph)
    def test_call(self):
        """ Test calling the binary gatherer normally
        """

        vertex_1 = _TestVertexWithBinary("test.aplx", ExecutableType.RUNNING)
        vertex_2 = _TestVertexWithBinary("test2.aplx", ExecutableType.RUNNING)
        vertex_3 = _TestVertexWithBinary("test2.aplx", ExecutableType.RUNNING)
        vertex_4 = _TestVertexWithoutBinary()

        graph = MachineGraph("Test")
        graph.add_vertices([vertex_1, vertex_2, vertex_3])

        placements = Placements(placements=[
            Placement(vertex_1, 0, 0, 0),
            Placement(vertex_2, 0, 0, 1),
            Placement(vertex_3, 0, 0, 2),
            Placement(vertex_4, 0, 0, 3)
        ])

        gatherer = GraphBinaryGatherer()
        targets = gatherer.__call__(placements, graph, _TestExecutableFinder())
        gatherer = LocateExecutableStartType()
        start_type = gatherer.__call__(graph, placements)
        self.assertEqual(next(iter(start_type)), ExecutableType.RUNNING)
        self.assertEqual(targets.total_processors, 3)

        test_cores = targets.get_cores_for_binary("test.aplx")
        test_2_cores = targets.get_cores_for_binary("test2.aplx")
        self.assertEqual(len(test_cores), 1)
        self.assertEqual(len(test_2_cores), 2)
        self.assertIn((0, 0, 0), test_cores)
        self.assertIn((0, 0, 1), test_2_cores)
        self.assertIn((0, 0, 2), test_2_cores)
def test_sdram_links():
    """ Test sdram edges which should explode
        """

    # Create a graph
    machine_graph = MachineGraph("Test")

    # Connect a set of vertices in a chain of length 3
    last_vertex = None
    for x in range(20):
        vertex = SimpleMachineVertex(
            resources=ResourceContainer(),
            label="Vertex_{}".format(x))
        machine_graph.add_vertex(vertex)
        last_vertex = vertex

    for vertex in machine_graph.vertices:
        edge = MachineEdge(vertex, last_vertex,
                           traffic_type=EdgeTrafficType.SDRAM)
        machine_graph.add_edge(edge, "SDRAM")

    # Do placements
    machine = VirtualMachine(version=5)
    try:
        OneToOnePlacer()(machine_graph, machine, plan_n_timesteps=1000)
        raise Exception("should blow up here")
    except PacmanException:
        pass
Exemplo n.º 5
0
    def test_many_vertices(self):
        vertices = list()
        for i in range(20 * 17):  # 51 atoms per each processor on 20 chips
            vertices.append(
                SimpleMachineVertex(0, 50,
                                    get_resources_used_by_atoms(0, 50, []),
                                    "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        placements = self.bp.place(self.graph, self.graph_mapper)

        unorderdered_info = list()
        for placement in placements.placements:
            unorderdered_info.append(
                (placement.vertex.label.split(" ")[0],
                 "{:<4}".format(placement.vertex.label.split(" ")[1]),
                 placement.vertex.n_atoms, 'x: ', placement.x, 'y: ',
                 placement.y, 'p: ', placement.p))

        sorted_info = sorted(unorderdered_info, key=itemgetter(4, 6, 8))
        pp(sorted_info)

        pp("{}".format("=" * 50))
        sorted_info = sorted(unorderdered_info, key=lambda x: int(x[1]))
        pp(sorted_info)
    def test_many_vertices(self):
        vertices = list()
        for i in range(20 * 17):  # 51 atoms per each processor on 20 chips
            vertices.append(SimpleMachineVertex(
                0, 50, get_resources_used_by_atoms(0, 50, []),
                "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        placements = self.bp.place(self.graph, self.graph_mapper)

        unorderdered_info = list()
        for placement in placements.placements:
            unorderdered_info.append(
                (placement.vertex.label.split(" ")[0],
                 "{:<4}".format(placement.vertex.label.split(" ")[1]),
                 placement.vertex.n_atoms, 'x: ',
                 placement.x, 'y: ', placement.y, 'p: ', placement.p))

        sorted_info = sorted(unorderdered_info, key=itemgetter(4, 6, 8))
        pp(sorted_info)

        pp("{}".format("=" * 50))
        sorted_info = sorted(unorderdered_info, key=lambda x: int(x[1]))
        pp(sorted_info)
Exemplo n.º 7
0
    def test_get_vertices_from_vertex(self):
        """
        test getting the vertex from a graph mapper via the vertex
        """
        vertices = list()
        app_graph = ApplicationGraph("bacon")
        vert = SimpleTestVertex(10, "Some testing vertex")
        app_graph.add_vertex(vert)
        vertices.append(SimpleMachineVertex(None, ""))
        vertices.append(SimpleMachineVertex(None, ""))
        mac_graph = MachineGraph("cooked bacon", application_graph=app_graph)
        vertex1 = SimpleMachineVertex(None,
                                      "",
                                      vertex_slice=Slice(0, 1),
                                      app_vertex=vert)
        vertex2 = SimpleMachineVertex(None,
                                      "",
                                      vertex_slice=Slice(2, 3),
                                      app_vertex=vert)
        mac_graph.add_vertex(vertex1)
        mac_graph.add_vertex(vertex2)

        returned_vertices = vert.machine_vertices

        self.assertIn(vertex1, returned_vertices)
        self.assertIn(vertex2, returned_vertices)
        for v in vertices:
            self.assertNotIn(v, returned_vertices)
Exemplo n.º 8
0
def test_virtual_placement(placer):
    machine = virtual_machine(width=8, height=8)
    graph = MachineGraph("Test")
    virtual_vertex = MachineSpiNNakerLinkVertex(spinnaker_link_id=0)
    graph.add_vertex(virtual_vertex)
    extended_machine = MallocBasedChipIdAllocator()(machine, graph)
    n_keys_map = DictBasedMachinePartitionNKeysMap()

    inputs = {
        "MemoryExtendedMachine": machine,
        "MemoryMachine": machine,
        "MemoryMachineGraph": graph,
        "PlanNTimeSteps": 1000,
        "MemoryMachinePartitionNKeysMap": n_keys_map
    }
    algorithms = [placer]
    xml_paths = []
    executor = PACMANAlgorithmExecutor(algorithms, [], inputs, [], [], [],
                                       xml_paths)
    executor.execute_mapping()
    placements = executor.get_item("MemoryPlacements")

    placement = placements.get_placement_of_vertex(virtual_vertex)
    chip = extended_machine.get_chip_at(placement.x, placement.y)
    assert chip.virtual
Exemplo n.º 9
0
    def setUp(self):
        unittest_setup()
        self.machine = virtual_machine(8, 8)
        self.mach_graph = MachineGraph("machine")
        self.vertices = list()
        self.vertex1 = get_resourced_machine_vertex(0, 1, "First vertex")
        self.vertex2 = get_resourced_machine_vertex(1, 5, "Second vertex")
        self.vertex3 = get_resourced_machine_vertex(5, 10, "Third vertex")
        self.vertex4 = get_resourced_machine_vertex(10, 100, "Fourth vertex")
        self.vertices.append(self.vertex1)
        self.mach_graph.add_vertex(self.vertex1)
        self.vertices.append(self.vertex2)
        self.mach_graph.add_vertex(self.vertex2)
        self.vertices.append(self.vertex3)
        self.mach_graph.add_vertex(self.vertex3)
        self.vertices.append(self.vertex4)
        self.mach_graph.add_vertex(self.vertex4)
        self.edges = list()
        edge1 = MachineEdge(self.vertex2, self.vertex3)
        self.edges.append(edge1)
        self.mach_graph.add_edge(edge1, "packet")
        edge2 = MachineEdge(self.vertex2, self.vertex4)
        self.edges.append(edge2)
        self.mach_graph.add_edge(edge2, "packet")
        edge3 = MachineEdge(self.vertex3, self.vertex4)
        self.edges.append(edge3)
        self.mach_graph.add_edge(edge3, "packet")
        edge4 = MachineEdge(self.vertex3, self.vertex1)
        self.edges.append(edge4)

        self.plan_n_timesteps = 100
Exemplo n.º 10
0
def test_one_to_one():
    """ Test normal 1-1 placement
    """

    # Create a graph
    machine_graph = MachineGraph("Test")

    # Connect a set of vertices in a chain of length 3
    one_to_one_chains = list()
    for i in range(10):
        last_vertex = None
        chain = list()
        for j in range(3):
            vertex = SimpleMachineVertex(resources=ResourceContainer(),
                                         label="Vertex_{}_{}".format(i, j))
            machine_graph.add_vertex(vertex)
            if last_vertex is not None:
                edge = MachineEdge(last_vertex, vertex)
                machine_graph.add_edge(edge, "SPIKES")
            last_vertex = vertex
            chain.append(vertex)
        one_to_one_chains.append(chain)

    # Connect a set of 20 vertices in a chain
    too_many_vertices = list()
    last_vertex = None
    for i in range(20):
        vertex = SimpleMachineVertex(resources=ResourceContainer(),
                                     label="Vertex_{}".format(i))
        machine_graph.add_vertex(vertex)
        if last_vertex is not None:
            edge = MachineEdge(last_vertex, vertex)
            machine_graph.add_edge(edge, "SPIKES")
        too_many_vertices.append(vertex)
        last_vertex = vertex

    # Do placements
    machine = virtual_machine(width=8, height=8)
    placements = OneToOnePlacer()(machine_graph,
                                  machine,
                                  plan_n_timesteps=1000)

    # The 1-1 connected vertices should be on the same chip
    for chain in one_to_one_chains:
        first_placement = placements.get_placement_of_vertex(chain[0])
        for i in range(1, 3):
            placement = placements.get_placement_of_vertex(chain[i])
            assert placement.x == first_placement.x
            assert placement.y == first_placement.y

    # The other vertices should be on more than one chip
    too_many_chips = set()
    for vertex in too_many_vertices:
        placement = placements.get_placement_of_vertex(vertex)
        too_many_chips.add((placement.x, placement.y))
    assert len(too_many_chips) > 1
    def _integration_setup(self):
        machine_graph = MachineGraph(label="test me you git")
        n_keys_map = DictBasedMachinePartitionNKeysMap()
        v1 = SimpleMachineVertex(ResourceContainer())
        v2 = SimpleMachineVertex(ResourceContainer())
        v3 = SimpleMachineVertex(ResourceContainer())
        v4 = SimpleMachineVertex(ResourceContainer())
        machine_graph.add_vertex(v1)
        machine_graph.add_vertex(v2)
        machine_graph.add_vertex(v3)
        machine_graph.add_vertex(v4)

        e1 = MachineEdge(v1, v2, label="e1")
        e2 = MachineEdge(v1, v3, label="e2")
        e3 = MachineEdge(v2, v3, label="e3")
        e4 = MachineEdge(v1, v4, label="e4")

        machine_graph.add_edge(e1, "part1")
        machine_graph.add_edge(e2, "part1")
        machine_graph.add_edge(e3, "part2")
        machine_graph.add_edge(e4, "part2")

        for partition in machine_graph.outgoing_edge_partitions:
            n_keys_map.set_n_keys_for_partition(partition, 24)

        return machine_graph, n_keys_map, v1, v2, v3, v4, e1, e2, e3, e4
def test_ner_route_default():
    unittest_setup()
    graph = MachineGraph("Test")
    machine = virtual_machine(8, 8)
    placements = Placements()

    source_vertex = SimpleMachineVertex(None)
    graph.add_vertex(source_vertex)
    placements.add_placement(Placement(source_vertex, 0, 0, 1))
    target_vertex = SimpleMachineVertex(None)
    graph.add_vertex(target_vertex)
    placements.add_placement(Placement(target_vertex, 0, 2, 1))
    edge = MachineEdge(source_vertex, target_vertex)
    graph.add_edge(edge, "Test")
    partition = graph.get_outgoing_partition_for_edge(edge)

    routes = ner_route(graph, machine, placements)

    source_route = routes.get_entries_for_router(0, 0)[partition]
    assert (not source_route.defaultable)
    mid_route = routes.get_entries_for_router(0, 1)[partition]
    print(mid_route.incoming_link, mid_route.link_ids)
    assert (mid_route.defaultable)
    end_route = routes.get_entries_for_router(0, 2)[partition]
    assert (not end_route.defaultable)
Exemplo n.º 13
0
 def test_place_vertex_too_big_with_vertex(self):
     large_vertex = T_AppVertex(500, "Large vertex 500")
     large_machine_vertex = large_vertex.create_machine_vertex(
         0, 499, get_resources_used_by_atoms(0, 499, []))
     self.graph.add_vertex(large_vertex)
     self.graph = ApplicationGraph("Graph", [large_vertex])
     self.graph_mapper = GraphMapper()
     self.graph_mapper.add_vertices([large_machine_vertex], large_vertex)
     self.bp = RadialPlacer(self.machine, self.graph)
     self.graph = MachineGraph(vertices=[large_machine_vertex])
     with self.assertRaises(PacmanPlaceException):
         self.bp.place(self.graph, self.graph_mapper)
Exemplo n.º 14
0
    def _do_test(self, placer):
        machine = virtual_machine(width=8, height=8)
        graph = MachineGraph("Test")
        plan_n_timesteps = 100

        vertices = [
            SimpleMachineVertex(ResourceContainer(),
                                label="v{}".format(i),
                                sdram_cost=20) for i in range(100)
        ]
        for vertex in vertices:
            graph.add_vertex(vertex)

        same_vertices = [
            SimpleMachineVertex(ResourceContainer(),
                                label="same{}".format(i),
                                sdram_cost=20) for i in range(10)
        ]
        random.seed(12345)
        sdram_edges = list()
        for vertex in same_vertices:
            graph.add_vertex(vertex)
            graph.add_outgoing_edge_partition(
                ConstantSDRAMMachinePartition(identifier="Test",
                                              pre_vertex=vertex,
                                              label="bacon"))
            for _i in range(0, random.randint(1, 5)):
                sdram_edge = SDRAMMachineEdge(vertex,
                                              vertices[random.randint(0, 99)],
                                              label="bacon",
                                              app_edge=None)
                sdram_edges.append(sdram_edge)
                graph.add_edge(sdram_edge, "Test")
        n_keys_map = DictBasedMachinePartitionNKeysMap()

        inputs = {
            "MemoryExtendedMachine": machine,
            "MemoryMachine": machine,
            "MemoryMachineGraph": graph,
            "PlanNTimeSteps": plan_n_timesteps,
            "MemoryMachinePartitionNKeysMap": n_keys_map
        }
        algorithms = [placer]
        xml_paths = []
        executor = PACMANAlgorithmExecutor(algorithms, [], inputs, [], [], [],
                                           xml_paths)
        executor.execute_mapping()
        placements = executor.get_item("MemoryPlacements")
        for edge in sdram_edges:
            pre_place = placements.get_placement_of_vertex(edge.pre_vertex)
            post_place = placements.get_placement_of_vertex(edge.post_vertex)
            assert pre_place.x == post_place.x
            assert pre_place.y == post_place.y
Exemplo n.º 15
0
    def test_routing(self):
        graph = MachineGraph("Test")
        machine = virtual_machine(2, 2)
        placements = Placements()
        vertices = list()

        for chip in machine.chips:
            for processor in chip.processors:
                if not processor.is_monitor:
                    vertex = SimpleMachineVertex(resources=ResourceContainer())
                    graph.add_vertex(vertex)
                    placements.add_placement(
                        Placement(vertex, chip.x, chip.y,
                                  processor.processor_id))
                    vertices.append(vertex)

        for vertex in vertices:
            graph.add_outgoing_edge_partition(
                MulticastEdgePartition(identifier="Test", pre_vertex=vertex))
            for vertex_to in vertices:
                if vertex != vertex_to:
                    graph.add_edge(MachineEdge(vertex, vertex_to), "Test")

        router = BasicDijkstraRouting()
        routing_paths = router.__call__(placements, machine, graph)

        for vertex in vertices:
            vertices_reached = set()
            queue = deque()
            seen_entries = set()
            placement = placements.get_placement_of_vertex(vertex)
            partition = graph.get_outgoing_edge_partition_starting_at_vertex(
                vertex, "Test")
            entry = routing_paths.get_entry_on_coords_for_edge(
                partition, placement.x, placement.y)
            self.assertEqual(entry.incoming_processor, placement.p)
            queue.append((placement.x, placement.y))
            while len(queue) > 0:
                x, y = queue.pop()
                entry = routing_paths.get_entry_on_coords_for_edge(
                    partition, x, y)
                self.assertIsNotNone(entry)
                chip = machine.get_chip_at(x, y)
                for p in entry.processor_ids:
                    self.assertIsNotNone(chip.get_processor_with_id(p))
                    vertex_found = placements.get_vertex_on_processor(x, y, p)
                    vertices_reached.add(vertex_found)
                seen_entries.add((x, y))
                for link_id in entry.link_ids:
                    link = chip.router.get_link(link_id)
                    self.assertIsNotNone(link)
                    dest_x, dest_y = link.destination_x, link.destination_y
                    if (dest_x, dest_y) not in seen_entries:
                        queue.append((dest_x, dest_y))

            for vertex_to in vertices:
                if vertex != vertex_to:
                    self.assertIn(vertex_to, vertices_reached)
 def test_place_vertex_too_big_with_vertex(self):
     large_vertex = T_AppVertex(500, "Large vertex 500")
     large_machine_vertex = large_vertex.create_machine_vertex(
         0, 499, get_resources_used_by_atoms(0, 499, []))
     self.graph.add_vertex(large_vertex)
     self.graph = ApplicationGraph("Graph", [large_vertex])
     self.graph_mapper = GraphMapper()
     self.graph_mapper.add_vertices([large_machine_vertex], large_vertex)
     self.bp = RadialPlacer(self.machine, self.graph)
     self.graph = MachineGraph(vertices=[large_machine_vertex])
     with self.assertRaises(PacmanPlaceException):
         self.bp.place(self.graph, self.graph_mapper)
Exemplo n.º 17
0
def test_virtual_vertices_one_to_one():
    """ Test that the placer works with a virtual vertex
    """

    # Create a graph with a virtual vertex
    machine_graph = MachineGraph("Test")
    virtual_vertex = MachineSpiNNakerLinkVertex(spinnaker_link_id=0,
                                                label="Virtual")
    machine_graph.add_vertex(virtual_vertex)

    # These vertices are fixed on 0, 0
    misc_vertices = list()
    for i in range(3):
        misc_vertex = SimpleMachineVertex(
            resources=ResourceContainer(),
            constraints=[ChipAndCoreConstraint(0, 0)],
            label="Fixed_0_0_{}".format(i))
        machine_graph.add_vertex(misc_vertex)
        misc_vertices.append(misc_vertex)

    # These vertices are 1-1 connected to the virtual vertex
    one_to_one_vertices = list()
    for i in range(16):
        one_to_one_vertex = SimpleMachineVertex(resources=ResourceContainer(),
                                                label="Vertex_{}".format(i))
        machine_graph.add_vertex(one_to_one_vertex)
        edge = MachineEdge(virtual_vertex, one_to_one_vertex)
        machine_graph.add_edge(edge, "SPIKES")
        one_to_one_vertices.append(one_to_one_vertex)

    # Get and extend the machine for the virtual chip
    machine = virtual_machine(width=8, height=8)
    extended_machine = MallocBasedChipIdAllocator()(machine, machine_graph)

    # Do placements
    placements = OneToOnePlacer()(machine_graph,
                                  extended_machine,
                                  plan_n_timesteps=1000)

    # The virtual vertex should be on a virtual chip
    placement = placements.get_placement_of_vertex(virtual_vertex)
    assert machine.get_chip_at(placement.x, placement.y).virtual

    # The 0, 0 vertices should be on 0, 0
    for vertex in misc_vertices:
        placement = placements.get_placement_of_vertex(vertex)
        assert placement.x == placement.y == 0

    # The other vertices should *not* be on a virtual chip
    for vertex in one_to_one_vertices:
        placement = placements.get_placement_of_vertex(vertex)
        assert not machine.get_chip_at(placement.x, placement.y).virtual
    def test_fill_machine(self):
        vertices = list()
        for i in range(99 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(SimpleMachineVertex(
                0, 50, get_resources_used_by_atoms(0, 50, []),
                "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        self.bp.place(self.graph, self.graph_mapper)
Exemplo n.º 19
0
    def test_fill_machine(self):
        vertices = list()
        for i in range(99 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(
                SimpleTestVertex(0, 50, get_resources_used_by_atoms(0, 50, []),
                                 "SimpleMachineVertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = BasicPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        self.bp.place(self.graph, self.graph_mapper)
def test_one_to_one():
    """ Test normal 1-1 placement
    """

    # Create a graph
    machine_graph = MachineGraph("Test")

    # Connect a set of vertices in a chain of length 3
    one_to_one_chains = list()
    for i in range(10):
        last_vertex = None
        chain = list()
        for j in range(3):
            vertex = SimpleMachineVertex(
                resources=ResourceContainer(),
                label="Vertex_{}_{}".format(i, j))
            machine_graph.add_vertex(vertex)
            if last_vertex is not None:
                edge = MachineEdge(last_vertex, vertex)
                machine_graph.add_edge(edge, "SPIKES")
            last_vertex = vertex
            chain.append(vertex)
        one_to_one_chains.append(chain)

    # Connect a set of 20 vertices in a chain
    too_many_vertices = list()
    last_vertex = None
    for i in range(20):
        vertex = SimpleMachineVertex(
            resources=ResourceContainer(), label="Vertex_{}".format(i))
        machine_graph.add_vertex(vertex)
        if last_vertex is not None:
            edge = MachineEdge(last_vertex, vertex)
            machine_graph.add_edge(edge, "SPIKES")
        too_many_vertices.append(vertex)
        last_vertex = vertex

    # Do placements
    machine = VirtualMachine(version=5)
    placements = OneToOnePlacer()(
        machine_graph, machine, plan_n_timesteps=1000)

    # The 1-1 connected vertices should be on the same chip
    for chain in one_to_one_chains:
        first_placement = placements.get_placement_of_vertex(chain[0])
        for i in range(1, 3):
            placement = placements.get_placement_of_vertex(chain[i])
            assert placement.x == first_placement.x
            assert placement.y == first_placement.y

    # The other vertices should be on more than one chip
    too_many_chips = set()
    for vertex in too_many_vertices:
        placement = placements.get_placement_of_vertex(vertex)
        too_many_chips.add((placement.x, placement.y))
    assert len(too_many_chips) > 1
 def test_add_duplicate_edge(self):
     """
     test that adding the same machine edge will cause an error
     """
     vertices = list()
     edges = list()
     vertices.append(SimpleMachineVertex(None, ""))
     vertices.append(SimpleMachineVertex(None, ""))
     edge = MachineEdge(vertices[0], vertices[1])
     edges.append(edge)
     edges.append(edge)
     graph = MachineGraph("foo")
     graph.add_vertices(vertices)
     graph.add_edges(edges, "bar")
Exemplo n.º 22
0
    def _do_test(self, placer):
        machine = VirtualMachine(width=8, height=8)
        graph = MachineGraph("Test")

        vertices = [
            SimpleMachineVertex(ResourceContainer(), label="v{}".format(i))
            for i in range(100)
        ]
        for vertex in vertices:
            graph.add_vertex(vertex)

        same_vertices = [
            SimpleMachineVertex(ResourceContainer(), label="same{}".format(i))
            for i in range(10)
        ]
        random.seed(12345)
        sdram_edges = list()
        for vertex in same_vertices:
            graph.add_vertex(vertex)
            for i in range(0, random.randint(1, 5)):
                sdram_edge = MachineEdge(
                    vertex, vertices[random.randint(0, 99)],
                    traffic_type=EdgeTrafficType.SDRAM)
                sdram_edges.append(sdram_edge)
                graph.add_edge(sdram_edge, "Test")

        placements = placer(graph, machine)
        for edge in sdram_edges:
            pre_place = placements.get_placement_of_vertex(edge.pre_vertex)
            post_place = placements.get_placement_of_vertex(edge.post_vertex)
            self.assert_(pre_place.x == post_place.x)
            self.assert_(pre_place.y == post_place.y)
Exemplo n.º 23
0
def test_sdram_links():
    """ Test sdram edges which should explode
        """

    # Create a graph
    machine_graph = MachineGraph("Test")

    # Connect a set of vertices in a chain of length 3
    last_vertex = None
    for x in range(20):
        vertex = SimpleMachineVertex(
            resources=ResourceContainer(),
            label="Vertex_{}".format(x), sdram_cost=20)
        machine_graph.add_vertex(vertex)
        last_vertex = vertex

    for vertex in machine_graph.vertices:
        machine_graph.add_outgoing_edge_partition(
            ConstantSDRAMMachinePartition(
                identifier="SDRAM", pre_vertex=vertex, label="bacon"))
        edge = SDRAMMachineEdge(vertex, last_vertex, "bacon", app_edge=None)
        machine_graph.add_edge(edge, "SDRAM")
    n_keys_map = DictBasedMachinePartitionNKeysMap()

    # Do placements
    machine = virtual_machine(width=8, height=8)
    try:
        SpreaderPlacer()(machine_graph, machine, n_keys_map,
                         plan_n_timesteps=1000)
        raise Exception("should blow up here")
    except PacmanException:
        pass
    def test_too_many_vertices(self):
        vertices = list()
        for i in range(100 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(SimpleMachineVertex(
                0, 50, get_resources_used_by_atoms(0, 50, []),
                "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        with self.assertRaises(PacmanPlaceException):
            self.bp.place(self.graph, self.graph_mapper)
Exemplo n.º 25
0
 def test_machine_vertices(self):
     app_graph = ApplicationGraph("super bacon")
     machine_graph = MachineGraph("bacon", application_graph=app_graph)
     vert = SimpleTestVertex(12, "New AbstractConstrainedVertex", 256)
     sub1 = vert.create_machine_vertex(
         Slice(0, 7), vert.get_resources_used_by_atoms(Slice(0, 7)), "M1")
     sub2 = vert.create_machine_vertex(
         Slice(7, 11), vert.get_resources_used_by_atoms(Slice(7, 11)), "M2")
     machine_graph.add_vertex(sub1)
     machine_graph.add_vertex(sub2)
     self.assertIn(sub1, vert.machine_vertices)
     self.assertIn(sub2, vert.machine_vertices)
     self.assertIn(Slice(0, 7), vert.vertex_slices)
     self.assertIn(Slice(7, 11), vert.vertex_slices)
Exemplo n.º 26
0
    def _do_test(self, placer):
        machine = virtual_machine(width=8, height=8)
        graph = MachineGraph("Test")

        vertices = [
            MockMachineVertex(ResourceContainer(),
                              label="v{}".format(i),
                              sdram_requirement=20) for i in range(100)
        ]
        for vertex in vertices:
            graph.add_vertex(vertex)

        same_vertices = [
            MockMachineVertex(ResourceContainer(),
                              label="same{}".format(i),
                              sdram_requirement=20) for i in range(10)
        ]
        random.seed(12345)
        sdram_edges = list()
        for vertex in same_vertices:
            graph.add_vertex(vertex)
            graph.add_outgoing_edge_partition(
                ConstantSDRAMMachinePartition(identifier="Test",
                                              pre_vertex=vertex,
                                              label="bacon"))
            for _i in range(0, random.randint(1, 5)):
                sdram_edge = SDRAMMachineEdge(vertex,
                                              vertices[random.randint(0, 99)],
                                              label="bacon",
                                              app_edge=None)
                sdram_edges.append(sdram_edge)
                graph.add_edge(sdram_edge, "Test")
        n_keys_map = DictBasedMachinePartitionNKeysMap()

        if placer == "ConnectiveBasedPlacer":
            placements = connective_based_placer(graph, machine, None)
        elif placer == "OneToOnePlacer":
            placements = one_to_one_placer(graph, machine, None)
        elif placer == "RadialPlacer":
            placements = radial_placer(graph, machine, None)
        elif placer == "SpreaderPlacer":
            placements = spreader_placer(graph, machine, n_keys_map, None)
        else:
            raise NotImplementedError(placer)
        for edge in sdram_edges:
            pre_place = placements.get_placement_of_vertex(edge.pre_vertex)
            post_place = placements.get_placement_of_vertex(edge.post_vertex)
            assert pre_place.x == post_place.x
            assert pre_place.y == post_place.y
def test_create_constraints_to_file(tmpdir):
    # Construct the sample machine and graph
    machine = VirtualMachine(version=3, with_wrap_arounds=None)
    # TODO: define some extra monitor cores (how?)
    graph = MachineGraph("foo")
    tag1 = IPtagResource("1.2.3.4", 5, False, tag="footag")
    tag2 = ReverseIPtagResource(tag="bartag")
    v0 = SimpleMachineVertex(ResourceContainer(
        iptags=[tag1], reverse_iptags=[tag2]), constraints=[
        ChipAndCoreConstraint(1, 1, 3)])
    graph.add_vertex(v0)
    v0_id = ident(v0)
    v1 = MachineSpiNNakerLinkVertex(0)
    v1.set_virtual_chip_coordinates(0, 2)
    graph.add_vertex(v1)
    v1_id = ident(v1)

    machine = MallocBasedChipIdAllocator()(machine, graph)
    algo = CreateConstraintsToFile()
    fn = tmpdir.join("foo.json")
    filename, mapping = algo(graph, machine, str(fn))
    assert filename == str(fn)
    for vid in mapping:
        assert vid in [v0_id, v1_id]
        assert vid == ident(mapping[vid])
    obj = json.loads(fn.read())
    baseline = [
        {
            "type": "reserve_resource",
            "location": None, "reservation": [0, 1], "resource": "cores"},
        {
            "type": "location",
            "location": [1, 1], "vertex": v0_id},
        {
            "type": "resource",
            "resource": "cores", "range": [3, 4], "vertex": v0_id},
        {
            "type": "resource",
            "resource": "iptag", "range": [0, 1], "vertex": v0_id},
        {
            "type": "resource",
            "resource": "reverse_iptag", "range": [0, 1], "vertex": v0_id},
        {
            "type": "route_endpoint",
            "direction": "west", "vertex": v1_id},
        {
            "type": "location",
            "location": [0, 0], "vertex": v1_id}]
    assert obj == baseline
Exemplo n.º 28
0
    def test_mixed_binaries(self):
        """ Test calling the binary gatherer with mixed executable types
        """

        vertex_1 = _TestVertexWithBinary("test.aplx",
                                         ExecutableStartType.RUNNING)
        vertex_2 = _TestVertexWithBinary("test2.aplx",
                                         ExecutableStartType.SYNC)

        graph = MachineGraph("Test")
        graph.add_vertices([vertex_1, vertex_2])

        gatherer = LocateExecutableStartType()
        with self.assertRaises(ConfigurationException):
            gatherer.__call__(graph)
Exemplo n.º 29
0
    def test_too_many_vertices(self):
        vertices = list()
        for i in range(100 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(
                SimpleMachineVertex(0, 50,
                                    get_resources_used_by_atoms(0, 50, []),
                                    "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        with self.assertRaises(PacmanPlaceException):
            self.bp.place(self.graph, self.graph_mapper)
    def __call__(self,
                 nengo_operator_graph,
                 machine,
                 nengo_random_number_generator,
                 pre_allocated_resources=None):
        machine_graph = MachineGraph(label=constants.MACHINE_GRAPH_LABEL)
        graph_mapper = GraphMapper()

        self._resource_tracker = ResourceTracker(
            machine, preallocated_resources=pre_allocated_resources)

        progress_bar = ProgressBar(
            total_number_of_things_to_do=(
                len(nengo_operator_graph.vertices) +
                len(nengo_operator_graph.outgoing_edge_partitions)),
            string_describing_what_being_progressed="partitioning")

        # convert application vertices into machine vertices
        for operator in progress_bar.over(nengo_operator_graph.vertices,
                                          False):

            # create the machine verts
            operator.create_machine_vertices(self._resource_tracker,
                                             machine_graph, graph_mapper)

        self._handle_edges(nengo_operator_graph, machine_graph, graph_mapper,
                           progress_bar, nengo_random_number_generator)

        return machine_graph, graph_mapper, self._resource_tracker.chips_used
 def test_add_duplicate_vertex(self):
     """
     testing that adding the same machine vertex twice will cause an
     error
     """
     vertices = list()
     edges = list()
     subv = SimpleMachineVertex(None, "")
     vertices.append(subv)
     vertices.append(SimpleMachineVertex(None, ""))
     vertices.append(subv)
     edges.append(MachineEdge(vertices[0], vertices[1]))
     edges.append(MachineEdge(vertices[1], vertices[0]))
     graph = MachineGraph("foo")
     graph.add_vertices(vertices)
     graph.add_edges(edges, "bar")
 def test_add_edge_with_no_existing_pre_vertex_in_graph(self):
     """
     test that adding a edge where the pre vertex has not been added
     to the machine graph causes an error
     """
     vertices = list()
     edges = list()
     vertices.append(SimpleMachineVertex(None, ""))
     vertices.append(SimpleMachineVertex(None, ""))
     edges.append(MachineEdge(vertices[0], vertices[1]))
     edges.append(MachineEdge(
         SimpleMachineVertex(None, ""), vertices[0]))
     with self.assertRaises(PacmanInvalidParameterException):
         graph = MachineGraph("foo")
         graph.add_vertices(vertices)
         graph.add_edges(edges, "bar")
Exemplo n.º 33
0
    def test_many_vertices(self):
        vertices = list()
        for i in range(20 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(SimpleTestVertex(
                0, 50, get_resources_used_by_atoms(0, 50, []),
                "SimpleMachineVertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = BasicPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        placements = self.bp.place(self.graph, self.graph_mapper)
        for placement in placements.placements:
            print(placement.vertex.label, placement.vertex.n_atoms,
                  'x:', placement.x, 'y:', placement.y, 'p:', placement.p)
Exemplo n.º 34
0
    def test_many_vertices(self):
        vertices = list()
        for i in range(20 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(
                SimpleTestVertex(0, 50, get_resources_used_by_atoms(0, 50, []),
                                 "SimpleMachineVertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = BasicPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        placements = self.bp.place(self.graph, self.graph_mapper)
        for placement in placements.placements:
            print(placement.vertex.label, placement.vertex.n_atoms, 'x:',
                  placement.x, 'y:', placement.y, 'p:', placement.p)
Exemplo n.º 35
0
    def __setup_objects(self, app_graph, machine, plan_n_time_steps,
                        pre_allocated_resources):
        """ sets up the machine_graph, resource_tracker, vertices, \
            progress bar.

        :param ApplicationGraph app_graph: app graph
        :param ~spinn_machine.Machine machine: machine
        :param int plan_n_time_steps: the number of time steps to run for.
        :param pre_allocated_resources: pre allocated res from other systems.
        :type PreAllocatedResourceContainer or None
        :return: (machine graph, res tracker, verts, progress bar)
        :rtype: tuple(MachineGraph, ResourceTracker, list(ApplicationVertex),
            ~.ProgressBar)
        """
        # Load the vertices and create the machine_graph to fill
        machine_graph = MachineGraph(label="partitioned graph for {}".format(
            app_graph.label),
                                     application_graph=app_graph)

        resource_tracker = ResourceTracker(
            machine,
            plan_n_time_steps,
            preallocated_resources=pre_allocated_resources)

        # sort out vertex's by placement constraints
        vertices = sort_vertices_by_known_constraints(app_graph.vertices)

        # Group vertices that are supposed to be the same size
        self.order_vertices_for_dependent_splitters(vertices)

        # Set up the progress
        progress = ProgressBar(len(app_graph.vertices),
                               self.__PROGRESS_BAR_VERTICES)

        return machine_graph, resource_tracker, vertices, progress
Exemplo n.º 36
0
    def __call__(self, machine_graph, graph_mapper):
        """
        :param machine_graph: the machine_graph whose edges are to be filtered
        :param graph_mapper: the graph mapper between graphs
        :return: a new graph mapper and machine graph
        """
        new_machine_graph = MachineGraph(label=machine_graph.label)
        new_graph_mapper = GraphMapper()

        # create progress bar
        progress = ProgressBar(
            machine_graph.n_vertices +
            machine_graph.n_outgoing_edge_partitions, "Filtering edges")

        # add the vertices directly, as they wont be pruned.
        for vertex in progress.over(machine_graph.vertices, False):
            self._add_vertex_to_new_graph(vertex, graph_mapper,
                                          new_machine_graph, new_graph_mapper)

        # start checking edges to decide which ones need pruning....
        for partition in progress.over(machine_graph.outgoing_edge_partitions):
            for edge in partition.edges:
                if self._is_filterable(edge, graph_mapper):
                    logger.debug("this edge was pruned %s", edge)
                    continue
                logger.debug("this edge was not pruned %s", edge)
                self._add_edge_to_new_graph(edge, partition, graph_mapper,
                                            new_machine_graph,
                                            new_graph_mapper)

        # returned the pruned graph and graph_mapper
        return new_machine_graph, new_graph_mapper
def test_virtual_vertices_one_to_one():
    """ Test that the placer works with a virtual vertex
    """

    # Create a graph with a virtual vertex
    machine_graph = MachineGraph("Test")
    virtual_vertex = MachineSpiNNakerLinkVertex(
        spinnaker_link_id=0, label="Virtual")
    machine_graph.add_vertex(virtual_vertex)

    # These vertices are fixed on 0, 0
    misc_vertices = list()
    for i in range(3):
        misc_vertex = SimpleMachineVertex(
            resources=ResourceContainer(), constraints=[
                ChipAndCoreConstraint(0, 0)],
            label="Fixed_0_0_{}".format(i))
        machine_graph.add_vertex(misc_vertex)
        misc_vertices.append(misc_vertex)

    # These vertices are 1-1 connected to the virtual vertex
    one_to_one_vertices = list()
    for i in range(16):
        one_to_one_vertex = SimpleMachineVertex(
            resources=ResourceContainer(),
            label="Vertex_{}".format(i))
        machine_graph.add_vertex(one_to_one_vertex)
        edge = MachineEdge(virtual_vertex, one_to_one_vertex)
        machine_graph.add_edge(edge, "SPIKES")
        one_to_one_vertices.append(one_to_one_vertex)

    # Get and extend the machine for the virtual chip
    machine = VirtualMachine(version=5)
    extended_machine = MallocBasedChipIdAllocator()(machine, machine_graph)

    # Do placements
    placements = OneToOnePlacer()(
        machine_graph, extended_machine, plan_n_timesteps=1000)

    # The virtual vertex should be on a virtual chip
    placement = placements.get_placement_of_vertex(virtual_vertex)
    assert machine.get_chip_at(placement.x, placement.y).virtual

    # The 0, 0 vertices should be on 0, 0
    for vertex in misc_vertices:
        placement = placements.get_placement_of_vertex(vertex)
        assert placement.x == placement.y == 0

    # The other vertices should *not* be on a virtual chip
    for vertex in one_to_one_vertices:
        placement = placements.get_placement_of_vertex(vertex)
        assert not machine.get_chip_at(placement.x, placement.y).virtual
    def _do_test(self, placer):
        machine = VirtualMachine(width=8, height=8)
        graph = MachineGraph("Test")
        plan_n_timesteps = 100

        vertices = [
            SimpleMachineVertex(ResourceContainer(), label="v{}".format(i))
            for i in range(100)
        ]
        for vertex in vertices:
            graph.add_vertex(vertex)

        same_vertices = [
            SimpleMachineVertex(ResourceContainer(), label="same{}".format(i))
            for i in range(10)
        ]
        random.seed(12345)
        sdram_edges = list()
        for vertex in same_vertices:
            graph.add_vertex(vertex)
            for i in range(0, random.randint(1, 5)):
                sdram_edge = MachineEdge(
                    vertex, vertices[random.randint(0, 99)],
                    traffic_type=EdgeTrafficType.SDRAM)
                sdram_edges.append(sdram_edge)
                graph.add_edge(sdram_edge, "Test")

        placements = placer(graph, machine, plan_n_timesteps)
        for edge in sdram_edges:
            pre_place = placements.get_placement_of_vertex(edge.pre_vertex)
            post_place = placements.get_placement_of_vertex(edge.post_vertex)
            assert pre_place.x == post_place.x
            assert pre_place.y == post_place.y
    def test_that_3_lpgs_are_generated_on_3_board_app_graph(self):
        machine = virtual_machine(width=12, height=12)
        app_graph = ApplicationGraph("Test")
        graph = MachineGraph("Test", app_graph)

        default_params = {
            'use_prefix': False,
            'key_prefix': None,
            'prefix_type': None,
            'message_type': EIEIOType.KEY_32_BIT,
            'right_shift': 0,
            'payload_as_time_stamps': True,
            'use_payload_prefix': True,
            'payload_prefix': None,
            'payload_right_shift': 0,
            'number_of_packets_sent_per_time_step': 0,
            'hostname': None,
            'port': None,
            'strip_sdp': None,
            'tag': None,
            'label': "Test"}

        # data stores needed by algorithm
        live_packet_gatherers = dict()
        default_params_holder = LivePacketGatherParameters(**default_params)
        live_packet_gatherers[default_params_holder] = list()

        # run edge inserter that should go boom
        edge_inserter = InsertLivePacketGatherersToGraphs()
        lpg_verts_mapping = edge_inserter(
            live_packet_gatherer_parameters=live_packet_gatherers,
            machine=machine, machine_graph=graph, application_graph=app_graph)

        self.assertEqual(len(lpg_verts_mapping[default_params_holder]), 3)
        locs = list()
        locs.append((0, 0))
        locs.append((4, 8))
        locs.append((8, 4))
        for vertex in itervalues(lpg_verts_mapping[default_params_holder]):
            x = list(vertex.constraints)[0].x
            y = list(vertex.constraints)[0].y
            key = (x, y)
            locs.remove(key)

        self.assertEqual(len(locs), 0)

        verts = lpg_verts_mapping[default_params_holder].values()
        for vertex in graph.vertices:
            self.assertIn(vertex, verts)

        app_verts = set()
        for vertex in itervalues(lpg_verts_mapping[default_params_holder]):
            app_vertex = vertex.app_vertex
            self.assertNotEqual(app_vertex, None)
            self.assertIsInstance(app_vertex, ApplicationVertex)
            app_verts.add(app_vertex)
        self.assertEqual(len(app_verts), 3)
Exemplo n.º 40
0
 def setUp(self):
     # sort out graph
     self.vert1 = Vertex(10, "New AbstractConstrainedVertex 1")
     self.vert2 = Vertex(5, "New AbstractConstrainedVertex 2")
     self.edge1 = ApplicationEdge(self.vert1, self.vert2, "First edge")
     self.verts = [self.vert1, self.vert2]
     self.edges = [self.edge1]
     self.graph = ApplicationGraph("Graph", self.verts, self.edges)
     # sort out graph
     self.graph = MachineGraph()
     self.vertex1 = SimpleMachineVertex(
         0, 10, self.vert1.get_resources_used_by_atoms(0, 10, []))
     self.vertex2 = SimpleMachineVertex(
         0, 5, self.vert2.get_resources_used_by_atoms(0, 10, []))
     self.edge = MachineEdge(self.vertex1, self.vertex2)
     self.graph.add_vertex(self.vertex1)
     self.graph.add_vertex(self.vertex2)
     self.graph.add_edge(self.edge, "TEST")
Exemplo n.º 41
0
    def test_that_6_lpgs_are_generated_2_on_each_eth_chip(self):
        machine = VirtualMachine(width=12, height=12, with_wrap_arounds=True)
        graph = MachineGraph("Test")

        default_params = {
            'use_prefix': False,
            'key_prefix': None,
            'prefix_type': None,
            'message_type': EIEIOType.KEY_32_BIT,
            'right_shift': 0,
            'payload_as_time_stamps': True,
            'use_payload_prefix': True,
            'payload_prefix': None,
            'payload_right_shift': 0,
            'number_of_packets_sent_per_time_step': 0,
            'hostname': None,
            'port': None,
            'strip_sdp': None,
            'board_address': None,
            'tag': None,
            'label': "test"
        }

        # data stores needed by algorithm
        live_packet_gatherers = dict()
        extended = dict(default_params)
        extended.update({'partition_id': "EVENTS"})
        default_params_holder = LivePacketGatherParameters(**extended)
        live_packet_gatherers[default_params_holder] = list()

        # and special LPG on Ethernet connected chips
        index = 1
        chip_special = dict()
        for chip in machine.ethernet_connected_chips:
            extended['label'] = "test{}".format(index)
            extended['board_address'] = chip.ip_address
            default_params_holder2 = LivePacketGatherParameters(**extended)
            live_packet_gatherers[default_params_holder2] = list()
            chip_special[(chip.x, chip.y)] = default_params_holder2

        # run edge inserter that should go boom
        edge_inserter = InsertLivePacketGatherersToGraphs()
        lpg_verts_mapping = edge_inserter(
            live_packet_gatherer_parameters=live_packet_gatherers,
            machine=machine,
            machine_graph=graph,
            application_graph=None,
            graph_mapper=None)

        self.assertEqual(len(lpg_verts_mapping[default_params_holder]), 3)

        for eth_chip in chip_special:
            params = chip_special[eth_chip]
            self.assertEqual(len(lpg_verts_mapping[params]), 1)
            vertex = lpg_verts_mapping[params][eth_chip]
            self.assertEqual(eth_chip[0], list(vertex.constraints)[0].x)
            self.assertEqual(eth_chip[1], list(vertex.constraints)[0].y)
Exemplo n.º 42
0
 def test_no_app_graph_no_app_vertex(self):
     graph = MachineGraph("foo")
     app1 = SimpleTestVertex(12, "app1")
     mach1 = SimpleMachineVertex("mach1", app_vertex=app1)
     mach2 = SimpleMachineVertex("mach2", app_vertex=None)
     mach3 = SimpleMachineVertex("mach3", app_vertex=app1)
     with self.assertRaises(PacmanInvalidParameterException):
         graph.add_vertex(mach1)
     graph.add_vertex(mach2)
     with self.assertRaises(PacmanInvalidParameterException):
         graph.add_vertex(mach3)
    def test_get_vertex_from_vertex(self):
        """
        test that the graph mapper can retrieve a vertex from a given vertex
        """
        app_graph = ApplicationGraph("bacon")
        vert = SimpleTestVertex(10, "Some testing vertex")
        app_graph.add_vertex(vert)
        vertex1 = SimpleMachineVertex(None, "", app_vertex=vert,
                                      vertex_slice=Slice(0, 1))
        vertex2 = SimpleMachineVertex(None, "", app_vertex=vert,
                                      vertex_slice=Slice(2, 3))
        machine_graph = MachineGraph(
            application_graph=app_graph, label="cooked_bacon")
        machine_graph.add_vertex(vertex1)
        machine_graph.add_vertex(vertex2)

        self.assertEqual(vert, vertex1.app_vertex)
        self.assertEqual(vert, vertex2.app_vertex)
        self.assertEqual([vertex1, vertex2], list(vert.machine_vertices))
Exemplo n.º 44
0
    def test_that_3_lpgs_are_generated_on_3_board(self):
        machine = VirtualMachine(width=12, height=12, with_wrap_arounds=True)
        graph = MachineGraph("Test")

        default_params = {
            'use_prefix': False,
            'key_prefix': None,
            'prefix_type': None,
            'message_type': EIEIOType.KEY_32_BIT,
            'right_shift': 0,
            'payload_as_time_stamps': True,
            'use_payload_prefix': True,
            'payload_prefix': None,
            'payload_right_shift': 0,
            'number_of_packets_sent_per_time_step': 0,
            'hostname': None,
            'port': None,
            'strip_sdp': None,
            'board_address': None,
            'tag': None,
            'label': "test"
        }

        # data stores needed by algorithm
        live_packet_gatherers = dict()
        extended = dict(default_params)
        extended.update({'partition_id': "EVENTS"})
        default_params_holder = LivePacketGatherParameters(**extended)
        live_packet_gatherers[default_params_holder] = list()

        # run edge inserter that should go boom
        edge_inserter = InsertLivePacketGatherersToGraphs()
        lpg_verts_mapping = edge_inserter(
            live_packet_gatherer_parameters=live_packet_gatherers,
            machine=machine,
            machine_graph=graph,
            application_graph=None,
            graph_mapper=None)

        self.assertEqual(len(lpg_verts_mapping[default_params_holder]), 3)
        locs = list()
        locs.append((0, 0))
        locs.append((4, 8))
        locs.append((8, 4))
        for vertex in lpg_verts_mapping[default_params_holder].itervalues():
            x = list(vertex.constraints)[0].x
            y = list(vertex.constraints)[0].y
            key = (x, y)
            locs.remove(key)

        self.assertEqual(len(locs), 0)

        verts = lpg_verts_mapping[default_params_holder].values()
        for vertex in graph.vertices:
            self.assertIn(vertex, verts)
    def _do_test(self, placer):
        machine = VirtualMachine(width=8, height=8)
        graph = MachineGraph("Test")

        vertices = [
            SimpleMachineVertex(ResourceContainer(), label="v{}".format(i))
            for i in range(100)
        ]
        for vertex in vertices:
            graph.add_vertex(vertex)

        same_vertices = [
            SimpleMachineVertex(ResourceContainer(), label="same{}".format(i))
            for i in range(10)
        ]
        random.seed(12345)
        for vertex in same_vertices:
            graph.add_vertex(vertex)
            for i in range(0, random.randint(1, 5)):
                vertex.add_constraint(
                    SameChipAsConstraint(
                        vertices[random.randint(0, 99)]))

        placements = placer(graph, machine, plan_n_timesteps=None)
        for same in same_vertices:
            print("{0.vertex.label}, {0.x}, {0.y}, {0.p}: {1}".format(
                placements.get_placement_of_vertex(same),
                ["{0.vertex.label}, {0.x}, {0.y}, {0.p}".format(
                    placements.get_placement_of_vertex(constraint.vertex))
                 for constraint in same.constraints]))
            placement = placements.get_placement_of_vertex(same)
            for constraint in same.constraints:
                if isinstance(constraint, SameChipAsConstraint):
                    other_placement = placements.get_placement_of_vertex(
                        constraint.vertex)
                    self.assert_(
                        other_placement.x == placement.x and
                        other_placement.y == placement.y,
                        "Vertex was not placed on the same chip as requested")
    def test_mixed_binaries(self):
        """ Test calling the binary gatherer with mixed executable types
        """

        vertex_1 = _TestVertexWithBinary(
            "test.aplx", ExecutableType.RUNNING)
        vertex_2 = _TestVertexWithBinary(
            "test2.aplx", ExecutableType.SYNC)

        placements = Placements(placements=[
            Placement(vertex_1, 0, 0, 0),
            Placement(vertex_2, 0, 0, 1)])

        graph = MachineGraph("Test")
        graph.add_vertices([vertex_1, vertex_2])

        gatherer = LocateExecutableStartType()
        results = gatherer.__call__(graph, placements=placements)
        self.assertIn(ExecutableType.RUNNING, results)
        self.assertIn(ExecutableType.SYNC, results)
        self.assertNotIn(ExecutableType.USES_SIMULATION_INTERFACE, results)
        self.assertNotIn(ExecutableType.NO_APPLICATION, results)
    def test_routing(self):
        graph = MachineGraph("Test")
        machine = VirtualMachine(2, 2)
        placements = Placements()
        vertices = list()

        for x in range(machine.max_chip_x + 1):
            for y in range(machine.max_chip_y + 1):
                chip = machine.get_chip_at(x, y)
                if chip is not None:
                    for processor in chip.processors:
                        if not processor.is_monitor:
                            vertex = SimpleMachineVertex(
                                resources=ResourceContainer())
                            graph.add_vertex(vertex)
                            placements.add_placement(Placement(
                                vertex, x, y, processor.processor_id))
                            vertices.append(vertex)

        for vertex in vertices:
            for vertex_to in vertices:
                if vertex != vertex_to:
                    graph.add_edge(MachineEdge(vertex, vertex_to), "Test")

        router = BasicDijkstraRouting()
        routing_paths = router.__call__(placements, machine, graph)

        for vertex in vertices:
            vertices_reached = set()
            queue = deque()
            seen_entries = set()
            placement = placements.get_placement_of_vertex(vertex)
            partition = graph.get_outgoing_edge_partition_starting_at_vertex(
                vertex, "Test")
            entry = routing_paths.get_entry_on_coords_for_edge(
                partition, placement.x, placement.y)
            self.assertEqual(entry.incoming_processor, placement.p)
            queue.append((placement.x, placement.y))
            while len(queue) > 0:
                x, y = queue.pop()
                entry = routing_paths.get_entry_on_coords_for_edge(
                    partition, x, y)
                self.assertIsNotNone(entry)
                chip = machine.get_chip_at(x, y)
                for p in entry.processor_ids:
                    self.assertIsNotNone(chip.get_processor_with_id(p))
                    vertex_found = placements.get_vertex_on_processor(x, y, p)
                    vertices_reached.add(vertex_found)
                seen_entries.add((x, y))
                for link_id in entry.link_ids:
                    link = chip.router.get_link(link_id)
                    self.assertIsNotNone(link)
                    dest_x, dest_y = link.destination_x, link.destination_y
                    if (dest_x, dest_y) not in seen_entries:
                        queue.append((dest_x, dest_y))

            for vertex_to in vertices:
                if vertex != vertex_to:
                    self.assertIn(vertex_to, vertices_reached)
def test_memory_io():
    vertex = MyVertex()
    graph = MachineGraph("Test")
    graph.add_vertex(vertex)
    placements = Placements()
    placements.add_placement(Placement(vertex, 0, 0, 1))
    transceiver = _MockTransceiver()
    temp = tempfile.mkdtemp()
    print("ApplicationDataFolder = {}".format(temp))
    inputs = {
        "MemoryTransceiver": transceiver,
        "MemoryMachineGraph": graph,
        "MemoryPlacements": placements,
        "IPAddress": "testing",
        "ApplicationDataFolder": temp,
        "APPID": 30
    }
    algorithms = ["WriteMemoryIOData"]
    executor = PACMANAlgorithmExecutor(
        algorithms, [], inputs, [], [], [],
        xml_paths=get_front_end_common_pacman_xml_paths())
    executor.execute_mapping()
    assert(vertex._test_tag == vertex._tag)
    def test_call(self):
        """ Test calling the binary gatherer normally
        """

        vertex_1 = _TestVertexWithBinary(
            "test.aplx", ExecutableType.RUNNING)
        vertex_2 = _TestVertexWithBinary(
            "test2.aplx", ExecutableType.RUNNING)
        vertex_3 = _TestVertexWithBinary(
            "test2.aplx", ExecutableType.RUNNING)
        vertex_4 = _TestVertexWithoutBinary()

        graph = MachineGraph("Test")
        graph.add_vertices([vertex_1, vertex_2, vertex_3])

        placements = Placements(placements=[
            Placement(vertex_1, 0, 0, 0),
            Placement(vertex_2, 0, 0, 1),
            Placement(vertex_3, 0, 0, 2),
            Placement(vertex_4, 0, 0, 3)])

        gatherer = GraphBinaryGatherer()
        targets = gatherer.__call__(
            placements, graph, _TestExecutableFinder())
        gatherer = LocateExecutableStartType()
        start_type = gatherer.__call__(graph, placements)
        self.assertEqual(next(iter(start_type)), ExecutableType.RUNNING)
        self.assertEqual(targets.total_processors, 3)

        test_cores = targets.get_cores_for_binary("test.aplx")
        test_2_cores = targets.get_cores_for_binary("test2.aplx")
        self.assertEqual(len(test_cores), 1)
        self.assertEqual(len(test_2_cores), 2)
        self.assertIn((0, 0, 0), test_cores)
        self.assertIn((0, 0, 1), test_2_cores)
        self.assertIn((0, 0, 2), test_2_cores)
 def test_deal_with_constraint_placement_vertices_have_vertices(self):
     self.bp = RadialPlacer(self.machine, self.graph)
     self.vertex1.add_constraint(ChipAndCoreConstraint(1, 5, 2))
     self.assertIsInstance(self.vertex1.constraints[0],
                           ChipAndCoreConstraint)
     self.vertex2.add_constraint(ChipAndCoreConstraint(3, 5, 7))
     self.vertex3.add_constraint(ChipAndCoreConstraint(2, 4, 6))
     self.vertex4.add_constraint(ChipAndCoreConstraint(6, 7, 16))
     self.vertices = list()
     self.vertices.append(self.vertex1)
     self.vertices.append(self.vertex2)
     self.vertices.append(self.vertex3)
     self.vertices.append(self.vertex4)
     self.edges = list()
     self.graph = MachineGraph(self.vertices, self.edges)
     self.graph_mapper = GraphMapper()
     self.graph_mapper.add_vertices(self.vertices, self.vert1)
     placements = self.bp.place(self.graph, self.graph_mapper)
     for placement in placements.placements:
         print(placement.vertex.label, placement.vertex.n_atoms,
               'x:', placement.x, 'y:', placement.y, 'p:', placement.p)
    def test_new_graph(self):
        """
        tests that after building a machine graph, all partitined vertices
        and partitioned edges are in existence
        """
        vertices = list()
        edges = list()
        for i in range(10):
            vertices.append(SimpleMachineVertex(None, ""))
        for i in range(5):
            edges.append(MachineEdge(vertices[0], vertices[(i + 1)]))
        for i in range(5, 10):
            edges.append(MachineEdge(
                vertices[5], vertices[(i + 1) % 10]))
        graph = MachineGraph("foo")
        graph.add_vertices(vertices)
        graph.add_edges(edges, "bar")
        outgoing = set(graph.get_edges_starting_at_vertex(vertices[0]))
        for i in range(5):
            assert edges[i] in outgoing, \
                "edges[" + str(i) + "] is not in outgoing and should be"
        for i in range(5, 10):
            assert edges[i] not in outgoing, \
                "edges[" + str(i) + "] is in outgoing and shouldn't be"

        incoming = set(graph.get_edges_ending_at_vertex(vertices[0]))

        assert edges[9] in incoming, \
            "edges[9] is not in incoming and should be"
        for i in range(9):
            assert edges[i] not in incoming, \
                "edges[" + str(i) + "] is in incoming and shouldn't be"

        vertices_from_graph = list(graph.vertices)
        for vert in vertices_from_graph:
            self.assertIn(vert, vertices)
        edges_from_graph = list(graph.edges)
        for edge in edges_from_graph:
            self.assertIn(edge, edges)
    def setUp(self):
        #######################################################################
        # Setting up vertices, edges and graph                                #
        #######################################################################
        self.vert1 = T_AppVertex(100, "New AbstractConstrainedVertex 1")
        self.vert2 = T_AppVertex(5, "New AbstractConstrainedVertex 2")
        self.vert3 = T_AppVertex(3, "New AbstractConstrainedVertex 3")
        self.edge1 = ApplicationEdge(self.vert1, self.vert2, "First edge")
        self.edge2 = ApplicationEdge(self.vert2, self.vert1, "Second edge")
        self.edge3 = ApplicationEdge(self.vert1, self.vert3, "Third edge")
        self.verts = [self.vert1, self.vert2, self.vert3]
        self.edges = [self.edge1, self.edge2, self.edge3]
        self.graph = ApplicationGraph("Graph", self.verts, self.edges)

        #######################################################################
        # Setting up machine                                                  #
        #######################################################################
        flops = 1000
        (_, _, n, _, _, s) = range(6)

        processors = list()
        for i in range(18):
            processors.append(Processor(i, flops))

        _sdram = SDRAM(128*(2**20))

        ip = "192.168.240.253"
        chips = list()
        for x in range(10):
            for y in range(10):
                links = list()

                links.append(Link(x, y, 0, (x + 1) % 10, y, n, n))
                links.append(Link(x, y, 1, (x + 1) % 10, (y + 1) % 10, s, s))
                links.append(Link(x, y, 2, x, (y + 1) % 10, n, n))
                links.append(Link(x, y, 3, (x - 1) % 10, y, s, s))
                links.append(Link(x, y, 4, (x - 1) % 10, (y - 1) % 10, n, n))
                links.append(Link(x, y, 5, x, (y - 1) % 10, s, s))

                r = Router(links, False, 100, 1024)
                chips.append(Chip(x, y, processors, r, _sdram, ip))

        self.machine = Machine(chips)
        #######################################################################
        # Setting up graph and graph_mapper                                   #
        #######################################################################
        self.vertices = list()
        self.vertex1 = T_MachineVertex(
            0, 1, get_resources_used_by_atoms(0, 1, []), "First vertex")
        self.vertex2 = T_MachineVertex(
            1, 5, get_resources_used_by_atoms(1, 5, []), "Second vertex")
        self.vertex3 = T_MachineVertex(
            5, 10, get_resources_used_by_atoms(5, 10, []), "Third vertex")
        self.vertex4 = T_MachineVertex(
            10, 100, get_resources_used_by_atoms(10, 100, []),
            "Fourth vertex")
        self.vertices.append(self.vertex1)
        self.vertices.append(self.vertex2)
        self.vertices.append(self.vertex3)
        self.vertices.append(self.vertex4)
        self.edges = list()
        self.graph = MachineGraph(self.vertices, self.edges)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(self.vertices)
    def test_local_verts_go_to_local_lpgs(self):
        machine = VirtualMachine(width=12, height=12, with_wrap_arounds=True)
        graph = MachineGraph("Test")

        default_params = {
            'use_prefix': False,
            'key_prefix': None,
            'prefix_type': None,
            'message_type': EIEIOType.KEY_32_BIT,
            'right_shift': 0,
            'payload_as_time_stamps': True,
            'use_payload_prefix': True,
            'payload_prefix': None,
            'payload_right_shift': 0,
            'number_of_packets_sent_per_time_step': 0,
            'hostname': None,
            'port': None,
            'strip_sdp': None,
            'board_address': None,
            'tag': None}

        # data stores needed by algorithm
        live_packet_gatherers = dict()
        extended = dict(default_params)
        extended.update({'partition_id': "EVENTS"})
        default_params_holder = LivePacketGatherParameters(**extended)
        live_packet_gatherers[default_params_holder] = list()

        live_packet_gatherers_to_vertex_mapping = dict()
        live_packet_gatherers_to_vertex_mapping[default_params_holder] = dict()

        placements = Placements()

        # add LPG's (1 for each Ethernet connected chip)
        for chip in machine.ethernet_connected_chips:
            extended = dict(default_params)
            extended.update({'label': 'test'})
            vertex = LivePacketGatherMachineVertex(**extended)
            graph.add_vertex(vertex)
            placements.add_placement(
                Placement(x=chip.x, y=chip.y, p=2, vertex=vertex))
            live_packet_gatherers_to_vertex_mapping[
                default_params_holder][chip.x, chip.y] = vertex

        # tracker of wirings
        verts_expected = defaultdict(list)
        positions = list()
        positions.append([0, 0, 0, 0])
        positions.append([4, 4, 0, 0])
        positions.append([1, 1, 0, 0])
        positions.append([2, 2, 0, 0])
        positions.append([8, 4, 8, 4])
        positions.append([11, 4, 8, 4])
        positions.append([4, 11, 4, 8])
        positions.append([4, 8, 4, 8])
        positions.append([0, 11, 8, 4])
        positions.append([11, 11, 4, 8])
        positions.append([8, 8, 4, 8])
        positions.append([4, 0, 0, 0])
        positions.append([7, 7, 0, 0])

        # add graph vertices which reside on areas of the machine to ensure
        #  spread over boards.
        for x, y, eth_x, eth_y in positions:
            vertex = SimpleMachineVertex(resources=ResourceContainer())
            graph.add_vertex(vertex)
            live_packet_gatherers[default_params_holder].append(vertex)
            verts_expected[eth_x, eth_y].append(vertex)
            placements.add_placement(Placement(x=x, y=y, p=5, vertex=vertex))

        # run edge inserter that should go boom
        edge_inserter = InsertEdgesToLivePacketGatherers()
        edge_inserter(
            live_packet_gatherer_parameters=live_packet_gatherers,
            placements=placements,
            live_packet_gatherers_to_vertex_mapping=(
                live_packet_gatherers_to_vertex_mapping),
            machine=machine, machine_graph=graph, application_graph=None,
            graph_mapper=None)

        # verify edges are in the right place
        for chip in machine.ethernet_connected_chips:
            edges = graph.get_edges_ending_at_vertex(
                live_packet_gatherers_to_vertex_mapping[
                    default_params_holder][chip.x, chip.y])
            for edge in edges:
                self.assertIn(edge.pre_vertex, verts_expected[chip.x, chip.y])
    def _do_dynamic_routing(
            self, fixed_route_tables, placements, ethernet_connected_chip,
            destination_class, machine, board_version):
        """ Uses a router to route fixed routes

        :param fixed_route_tables: fixed route tables entry holder
        :param placements: placements
        :param ethernet_connected_chip: the chip to consider for this routing
        :param destination_class: the class at the Ethernet connected chip\
            for receiving all these routes.
        :param machine: SpiNNMachine instance
        :param board_version: The version of the machine
        :rtype: None
        """
        graph = MachineGraph(label="routing graph")
        fake_placements = Placements()

        # build fake setup for the routing
        eth_x = ethernet_connected_chip.x
        eth_y = ethernet_connected_chip.y
        down_links = set()
        for (chip_x, chip_y) in machine.get_chips_on_board(
                ethernet_connected_chip):
            vertex = RoutingMachineVertex()
            graph.add_vertex(vertex)
            rel_x = chip_x - eth_x
            if rel_x < 0:
                rel_x += machine.max_chip_x + 1
            rel_y = chip_y - eth_y
            if rel_y < 0:
                rel_y += machine.max_chip_y + 1

            free_processor = 0
            while ((free_processor < machine.MAX_CORES_PER_CHIP) and
                   fake_placements.is_processor_occupied(
                       self.FAKE_ETHERNET_CHIP_X,
                       y=self.FAKE_ETHERNET_CHIP_Y,
                       p=free_processor)):
                free_processor += 1

            fake_placements.add_placement(Placement(
                x=rel_x, y=rel_y, p=free_processor, vertex=vertex))
            down_links.update({
                (rel_x, rel_y, link) for link in range(
                    Router.MAX_LINKS_PER_ROUTER)
                if not machine.is_link_at(chip_x, chip_y, link)})

        # Create a fake machine consisting of only the one board that
        # the routes should go over
        fake_machine = machine
        if (board_version in machine.BOARD_VERSION_FOR_48_CHIPS and
                (machine.max_chip_x > machine.MAX_CHIP_X_ID_ON_ONE_BOARD or
                 machine.max_chip_y > machine.MAX_CHIP_Y_ID_ON_ONE_BOARD)):
            down_chips = {
                (x, y) for x, y in zip(
                    range(machine.SIZE_X_OF_ONE_BOARD),
                    range(machine.SIZE_Y_OF_ONE_BOARD))
                if not machine.is_chip_at(
                    (x + eth_x) % (machine.max_chip_x + 1),
                    (y + eth_y) % (machine.max_chip_y + 1))}

            # build a fake machine which is just one board but with the missing
            # bits of the real board
            fake_machine = VirtualMachine(
                machine.SIZE_X_OF_ONE_BOARD, machine.SIZE_Y_OF_ONE_BOARD,
                False, down_chips=down_chips, down_links=down_links)

        # build destination
        verts = graph.vertices
        vertex_dest = RoutingMachineVertex()
        graph.add_vertex(vertex_dest)
        destination_processor = self._locate_destination(
            ethernet_chip_x=ethernet_connected_chip.x,
            ethernet_chip_y=ethernet_connected_chip.y,
            destination_class=destination_class,
            placements=placements)
        fake_placements.add_placement(Placement(
            x=self.FAKE_ETHERNET_CHIP_X, y=self.FAKE_ETHERNET_CHIP_Y,
            p=destination_processor, vertex=vertex_dest))

        # deal with edges
        for vertex in verts:
            graph.add_edge(
                MachineEdge(pre_vertex=vertex, post_vertex=vertex_dest),
                self.FAKE_ROUTING_PARTITION)

        # route as if using multicast
        router = BasicDijkstraRouting()
        routing_tables_by_partition = router(
            placements=fake_placements, machine=fake_machine,
            machine_graph=graph, use_progress_bar=False)

        # convert to fixed route entries
        for (chip_x, chip_y) in routing_tables_by_partition.get_routers():
            mc_entries = routing_tables_by_partition.get_entries_for_router(
                chip_x, chip_y)
            # only want the first entry, as that will all be the same.
            mc_entry = next(itervalues(mc_entries))
            fixed_route_entry = FixedRouteEntry(
                link_ids=mc_entry.link_ids,
                processor_ids=mc_entry.processor_ids)
            x = (chip_x + eth_x) % (machine.max_chip_x + 1)
            y = (chip_y + eth_y) % (machine.max_chip_y + 1)
            key = (x, y)
            if key in fixed_route_tables:
                raise PacmanAlreadyExistsException(
                    "fixed route entry", str(key))
            fixed_route_tables[key] = fixed_route_entry
def test_convert_to_file_machine_graph(tmpdir):
    # Construct the sample graph
    graph = MachineGraph("foo")
    v0 = SimpleMachineVertex(ResourceContainer())
    graph.add_vertex(v0)
    tag = IPtagResource("1.2.3.4", 5, False, tag="footag")
    v1 = SimpleMachineVertex(ResourceContainer(iptags=[tag]))
    graph.add_vertex(v1)
    t1id = md5("%s_tag" % ident(v1))
    tag = ReverseIPtagResource(tag="bartag")
    v2 = SimpleMachineVertex(ResourceContainer(reverse_iptags=[tag]))
    graph.add_vertex(v2)
    t2id = md5("%s_tag" % ident(v2))
    graph.add_edge(MachineEdge(v1, v0), "part1")
    p1 = graph.get_outgoing_edge_partition_starting_at_vertex(v1, "part1")
    graph.add_edge(MachineEdge(v0, v2, label="foobar"), "part2")
    p2 = graph.get_outgoing_edge_partition_starting_at_vertex(v0, "part2")

    # Convert it to JSON
    algo = ConvertToFileMachineGraph()
    fn = tmpdir.join("foo.json")
    filename, _vertex_by_id, _partition_by_id = algo(
        graph, plan_n_timesteps=None, file_path=str(fn))
    assert filename == str(fn)

    # Rebuild and compare; simplest way of checking given that order is not
    # preserved in the underlying string and altering that is hard
    obj = json.loads(fn.read())
    baseline = {
        "vertices_resources": {
            ident(v0): {"cores": 1, "sdram": 0},
            ident(v1): {"cores": 1, "sdram": 0},
            t1id:      {"cores": 0, "sdram": 0},
            ident(v2): {"cores": 1, "sdram": 0},
            t2id:      {"cores": 0, "sdram": 0}},
        "edges": {
            ident(p1): {
                "source": ident(v1), "sinks": [ident(v0)],
                "type": "multicast", "weight": 1},
            ident(p2): {
                "source": ident(v0), "sinks": [ident(v2)],
                "type": "multicast", "weight": 1},
            t1id: {
                "source": ident(v1), "sinks": [t1id],
                "weight": 1.0, "type": "FAKE_TAG_EDGE"},
            t2id: {
                "source": ident(v2), "sinks": [t2id],
                "weight": 1.0, "type": "FAKE_TAG_EDGE"}}}
    assert obj == baseline
    def test_write_synaptic_matrix_and_master_population_table(self):
        MockSimulator.setup()
        # Add an sdram so maxsdram is high enough
        SDRAM(10000)

        default_config_paths = os.path.join(
            os.path.dirname(abstract_spinnaker_common.__file__),
            AbstractSpiNNakerCommon.CONFIG_FILE_NAME)

        config = conf_loader.load_config(
            AbstractSpiNNakerCommon.CONFIG_FILE_NAME, default_config_paths)
        config.set("Simulation", "one_to_one_connection_dtcm_max_bytes", 40)

        machine_time_step = 1000.0

        pre_app_vertex = SimpleApplicationVertex(10)
        pre_vertex = SimpleMachineVertex(resources=None)
        pre_vertex_slice = Slice(0, 9)
        post_app_vertex = SimpleApplicationVertex(10)
        post_vertex = SimpleMachineVertex(resources=None)
        post_vertex_slice = Slice(0, 9)
        post_slice_index = 0
        one_to_one_connector_1 = OneToOneConnector(None)
        one_to_one_connector_1.set_projection_information(
            pre_app_vertex, post_app_vertex, None, machine_time_step)
        one_to_one_connector_2 = OneToOneConnector(None)
        one_to_one_connector_2.set_projection_information(
            pre_app_vertex, post_app_vertex, None, machine_time_step)
        all_to_all_connector = AllToAllConnector(None)
        all_to_all_connector.set_projection_information(
            pre_app_vertex, post_app_vertex, None, machine_time_step)
        direct_synapse_information_1 = SynapseInformation(
            one_to_one_connector_1, SynapseDynamicsStatic(), 0, 1.5, 1.0)
        direct_synapse_information_2 = SynapseInformation(
            one_to_one_connector_2, SynapseDynamicsStatic(), 1, 2.5, 2.0)
        all_to_all_synapse_information = SynapseInformation(
            all_to_all_connector, SynapseDynamicsStatic(), 0, 4.5, 4.0)
        app_edge = ProjectionApplicationEdge(
            pre_app_vertex, post_app_vertex, direct_synapse_information_1)
        app_edge.add_synapse_information(direct_synapse_information_2)
        app_edge.add_synapse_information(all_to_all_synapse_information)
        machine_edge = ProjectionMachineEdge(
            app_edge.synapse_information, pre_vertex, post_vertex)
        partition_name = "TestPartition"

        graph = MachineGraph("Test")
        graph.add_vertex(pre_vertex)
        graph.add_vertex(post_vertex)
        graph.add_edge(machine_edge, partition_name)

        graph_mapper = GraphMapper()
        graph_mapper.add_vertex_mapping(
            pre_vertex, pre_vertex_slice, pre_app_vertex)
        graph_mapper.add_vertex_mapping(
            post_vertex, post_vertex_slice, post_app_vertex)
        graph_mapper.add_edge_mapping(machine_edge, app_edge)

        weight_scales = [4096.0, 4096.0]

        key = 0
        routing_info = RoutingInfo()
        routing_info.add_partition_info(PartitionRoutingInfo(
            [BaseKeyAndMask(key, 0xFFFFFFF0)],
            graph.get_outgoing_edge_partition_starting_at_vertex(
                pre_vertex, partition_name)))

        temp_spec = tempfile.mktemp()
        spec_writer = FileDataWriter(temp_spec)
        spec = DataSpecificationGenerator(spec_writer, None)
        master_pop_sz = 1000
        master_pop_region = 0
        all_syn_block_sz = 2000
        synapse_region = 1
        direct_region = 2
        spec.reserve_memory_region(master_pop_region, master_pop_sz)
        spec.reserve_memory_region(synapse_region, all_syn_block_sz)

        synaptic_manager = SynapticManager(
            n_synapse_types=2, ring_buffer_sigma=5.0,
            spikes_per_second=100.0, config=config)
        # UGLY but the mock transceiver NEED generate_on_machine be False
        abstract_generate_connector_on_machine.IS_PYNN_8 = False
        synaptic_manager._write_synaptic_matrix_and_master_population_table(
            spec, [post_vertex_slice], post_slice_index, post_vertex,
            post_vertex_slice, all_syn_block_sz, weight_scales,
            master_pop_region, synapse_region, direct_region, routing_info,
            graph_mapper, graph, machine_time_step)
        spec.end_specification()
        spec_writer.close()

        spec_reader = FileDataReader(temp_spec)
        executor = DataSpecificationExecutor(
            spec_reader, master_pop_sz + all_syn_block_sz)
        executor.execute()

        master_pop_table = executor.get_region(0)
        synaptic_matrix = executor.get_region(1)
        direct_matrix = executor.get_region(2)

        all_data = bytearray()
        all_data.extend(master_pop_table.region_data[
            :master_pop_table.max_write_pointer])
        all_data.extend(synaptic_matrix.region_data[
            :synaptic_matrix.max_write_pointer])
        all_data.extend(direct_matrix.region_data[
            :direct_matrix.max_write_pointer])
        master_pop_table_address = 0
        synaptic_matrix_address = master_pop_table.max_write_pointer
        direct_synapses_address = (
            synaptic_matrix_address + synaptic_matrix.max_write_pointer)
        direct_synapses_address += 4
        indirect_synapses_address = synaptic_matrix_address
        placement = Placement(None, 0, 0, 1)
        transceiver = MockTransceiverRawData(all_data)

        # Get the master population table details
        items = synaptic_manager._poptable_type\
            .extract_synaptic_matrix_data_location(
                key, master_pop_table_address, transceiver,
                placement.x, placement.y)

        # The first entry should be direct, but the rest should be indirect;
        # the second is potentially direct, but has been restricted by the
        # restriction on the size of the direct matrix
        assert len(items) == 3
        assert items[0][2]
        assert not items[1][2]
        assert not items[2][2]

        data_1, row_len_1 = synaptic_manager._retrieve_synaptic_block(
            transceiver=transceiver, placement=placement,
            master_pop_table_address=master_pop_table_address,
            indirect_synapses_address=indirect_synapses_address,
            direct_synapses_address=direct_synapses_address, key=key,
            n_rows=pre_vertex_slice.n_atoms, index=0,
            using_extra_monitor_cores=False)
        connections_1 = synaptic_manager._synapse_io.read_synapses(
            direct_synapse_information_1, pre_vertex_slice, post_vertex_slice,
            row_len_1, 0, 2, weight_scales, data_1, None,
            app_edge.n_delay_stages, machine_time_step)

        # The first matrix is a 1-1 matrix, so row length is 1
        assert row_len_1 == 1

        # Check that all the connections have the right weight and delay
        assert len(connections_1) == post_vertex_slice.n_atoms
        assert all([conn["weight"] == 1.5 for conn in connections_1])
        assert all([conn["delay"] == 1.0 for conn in connections_1])

        data_2, row_len_2 = synaptic_manager._retrieve_synaptic_block(
            transceiver=transceiver, placement=placement,
            master_pop_table_address=master_pop_table_address,
            indirect_synapses_address=indirect_synapses_address,
            direct_synapses_address=direct_synapses_address, key=key,
            n_rows=pre_vertex_slice.n_atoms, index=1,
            using_extra_monitor_cores=False)
        connections_2 = synaptic_manager._synapse_io.read_synapses(
            direct_synapse_information_2, pre_vertex_slice, post_vertex_slice,
            row_len_2, 0, 2, weight_scales, data_2, None,
            app_edge.n_delay_stages, machine_time_step)

        # The second matrix is a 1-1 matrix, so row length is 1
        assert row_len_2 == 1

        # Check that all the connections have the right weight and delay
        assert len(connections_2) == post_vertex_slice.n_atoms
        assert all([conn["weight"] == 2.5 for conn in connections_2])
        assert all([conn["delay"] == 2.0 for conn in connections_2])

        data_3, row_len_3 = synaptic_manager._retrieve_synaptic_block(
            transceiver=transceiver, placement=placement,
            master_pop_table_address=master_pop_table_address,
            indirect_synapses_address=indirect_synapses_address,
            direct_synapses_address=direct_synapses_address, key=key,
            n_rows=pre_vertex_slice.n_atoms, index=2,
            using_extra_monitor_cores=False)
        connections_3 = synaptic_manager._synapse_io.read_synapses(
            all_to_all_synapse_information, pre_vertex_slice,
            post_vertex_slice, row_len_3, 0, 2, weight_scales, data_3, None,
            app_edge.n_delay_stages, machine_time_step)

        # The third matrix is an all-to-all matrix, so length is n_atoms
        assert row_len_3 == post_vertex_slice.n_atoms

        # Check that all the connections have the right weight and delay
        assert len(connections_3) == \
            post_vertex_slice.n_atoms * pre_vertex_slice.n_atoms
        assert all([conn["weight"] == 4.5 for conn in connections_3])
        assert all([conn["delay"] == 4.0 for conn in connections_3])
class TestRadialPlacer(unittest.TestCase):
    def setUp(self):
        #######################################################################
        # Setting up vertices, edges and graph                                #
        #######################################################################
        self.vert1 = T_AppVertex(100, "New AbstractConstrainedVertex 1")
        self.vert2 = T_AppVertex(5, "New AbstractConstrainedVertex 2")
        self.vert3 = T_AppVertex(3, "New AbstractConstrainedVertex 3")
        self.edge1 = ApplicationEdge(self.vert1, self.vert2, "First edge")
        self.edge2 = ApplicationEdge(self.vert2, self.vert1, "Second edge")
        self.edge3 = ApplicationEdge(self.vert1, self.vert3, "Third edge")
        self.verts = [self.vert1, self.vert2, self.vert3]
        self.edges = [self.edge1, self.edge2, self.edge3]
        self.graph = ApplicationGraph("Graph", self.verts, self.edges)

        #######################################################################
        # Setting up machine                                                  #
        #######################################################################
        flops = 1000
        (_, _, n, _, _, s) = range(6)

        processors = list()
        for i in range(18):
            processors.append(Processor(i, flops))

        _sdram = SDRAM(128*(2**20))

        ip = "192.168.240.253"
        chips = list()
        for x in range(10):
            for y in range(10):
                links = list()

                links.append(Link(x, y, 0, (x + 1) % 10, y, n, n))
                links.append(Link(x, y, 1, (x + 1) % 10, (y + 1) % 10, s, s))
                links.append(Link(x, y, 2, x, (y + 1) % 10, n, n))
                links.append(Link(x, y, 3, (x - 1) % 10, y, s, s))
                links.append(Link(x, y, 4, (x - 1) % 10, (y - 1) % 10, n, n))
                links.append(Link(x, y, 5, x, (y - 1) % 10, s, s))

                r = Router(links, False, 100, 1024)
                chips.append(Chip(x, y, processors, r, _sdram, ip))

        self.machine = Machine(chips)
        #######################################################################
        # Setting up graph and graph_mapper                                   #
        #######################################################################
        self.vertices = list()
        self.vertex1 = T_MachineVertex(
            0, 1, get_resources_used_by_atoms(0, 1, []), "First vertex")
        self.vertex2 = T_MachineVertex(
            1, 5, get_resources_used_by_atoms(1, 5, []), "Second vertex")
        self.vertex3 = T_MachineVertex(
            5, 10, get_resources_used_by_atoms(5, 10, []), "Third vertex")
        self.vertex4 = T_MachineVertex(
            10, 100, get_resources_used_by_atoms(10, 100, []),
            "Fourth vertex")
        self.vertices.append(self.vertex1)
        self.vertices.append(self.vertex2)
        self.vertices.append(self.vertex3)
        self.vertices.append(self.vertex4)
        self.edges = list()
        self.graph = MachineGraph(self.vertices, self.edges)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(self.vertices)

    @unittest.skip("demonstrating skipping")
    def test_new_basic_placer(self):
        self.bp = RadialPlacer(self.machine, self.graph)
        self.assertEqual(self.bp._machine, self.machine)
        self.assertEqual(self.bp._graph, self.graph)

    @unittest.skip("demonstrating skipping")
    def test_place_where_vertices_dont_have_vertex(self):
        self.bp = RadialPlacer(self.machine, self.graph)
        placements = self.bp.place(self.graph, self.graph_mapper)
        for placement in placements.placements:
            print(placement.vertex.label, placement.vertex.n_atoms,
                  'x:', placement.x, 'y:', placement.y, 'p:', placement.p)

    @unittest.skip("demonstrating skipping")
    def test_place_where_vertices_have_vertices(self):
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(self.vertices, self.vert1)
        placements = self.bp.place(self.graph, self.graph_mapper)
        for placement in placements.placements:
            print(placement.vertex.label, placement.vertex.n_atoms,
                  'x:', placement.x, 'y:', placement.y, 'p:', placement.p)

    @unittest.skip("demonstrating skipping")
    def test_place_vertex_too_big_with_vertex(self):
        large_vertex = T_AppVertex(500, "Large vertex 500")
        large_machine_vertex = large_vertex.create_machine_vertex(
            0, 499, get_resources_used_by_atoms(0, 499, []))
        self.graph.add_vertex(large_vertex)
        self.graph = ApplicationGraph("Graph", [large_vertex])
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices([large_machine_vertex], large_vertex)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=[large_machine_vertex])
        with self.assertRaises(PacmanPlaceException):
            self.bp.place(self.graph, self.graph_mapper)

    @unittest.skip("demonstrating skipping")
    def test_try_to_place(self):
        self.assertEqual(True, False, "Test not implemented yet")

    @unittest.skip("demonstrating skipping")
    def test_deal_with_constraint_placement_vertices_dont_have_vertex(self):
        self.bp = RadialPlacer(self.machine, self.graph)
        self.vertex1.add_constraint(ChipAndCoreConstraint(8, 3, 2))
        self.assertIsInstance(self.vertex1.constraints[0],
                              ChipAndCoreConstraint)
        self.vertex2.add_constraint(ChipAndCoreConstraint(3, 5, 7))
        self.vertex3.add_constraint(ChipAndCoreConstraint(2, 4, 6))
        self.vertex4.add_constraint(ChipAndCoreConstraint(6, 4, 16))
        self.vertices = list()
        self.vertices.append(self.vertex1)
        self.vertices.append(self.vertex2)
        self.vertices.append(self.vertex3)
        self.vertices.append(self.vertex4)
        self.edges = list()
        self.graph = MachineGraph(self.vertices, self.edges)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(self.vertices)
        placements = self.bp.place(self.graph, self.graph_mapper)
        for placement in placements.placements:
            print(placement.vertex.label, placement.vertex.n_atoms,
                  'x:', placement.x, 'y:', placement.y, 'p:', placement.p)

    @unittest.skip("demonstrating skipping")
    def test_deal_with_constraint_placement_vertices_have_vertices(self):
        self.bp = RadialPlacer(self.machine, self.graph)
        self.vertex1.add_constraint(ChipAndCoreConstraint(1, 5, 2))
        self.assertIsInstance(self.vertex1.constraints[0],
                              ChipAndCoreConstraint)
        self.vertex2.add_constraint(ChipAndCoreConstraint(3, 5, 7))
        self.vertex3.add_constraint(ChipAndCoreConstraint(2, 4, 6))
        self.vertex4.add_constraint(ChipAndCoreConstraint(6, 7, 16))
        self.vertices = list()
        self.vertices.append(self.vertex1)
        self.vertices.append(self.vertex2)
        self.vertices.append(self.vertex3)
        self.vertices.append(self.vertex4)
        self.edges = list()
        self.graph = MachineGraph(self.vertices, self.edges)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(self.vertices, self.vert1)
        placements = self.bp.place(self.graph, self.graph_mapper)
        for placement in placements.placements:
            print(placement.vertex.label, placement.vertex.n_atoms,
                  'x:', placement.x, 'y:', placement.y, 'p:', placement.p)

    @unittest.skip("demonstrating skipping")
    def test_unsupported_non_placer_constraint(self):
        self.assertEqual(True, False, "Test not implemented yet")

    @unittest.skip("demonstrating skipping")
    def test_unsupported_placer_constraint(self):
        self.assertEqual(True, False, "Test not implemented yet")

    @unittest.skip("demonstrating skipping")
    def test_unsupported_placer_constraints(self):
        self.assertEqual(True, False, "Test not implemented yet")

    @unittest.skip("demonstrating skipping")
    def test_many_vertices(self):
        vertices = list()
        for i in range(20 * 17):  # 51 atoms per each processor on 20 chips
            vertices.append(SimpleMachineVertex(
                0, 50, get_resources_used_by_atoms(0, 50, []),
                "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        placements = self.bp.place(self.graph, self.graph_mapper)

        unorderdered_info = list()
        for placement in placements.placements:
            unorderdered_info.append(
                (placement.vertex.label.split(" ")[0],
                 "{:<4}".format(placement.vertex.label.split(" ")[1]),
                 placement.vertex.n_atoms, 'x: ',
                 placement.x, 'y: ', placement.y, 'p: ', placement.p))

        sorted_info = sorted(unorderdered_info, key=itemgetter(4, 6, 8))
        pp(sorted_info)

        pp("{}".format("=" * 50))
        sorted_info = sorted(unorderdered_info, key=lambda x: int(x[1]))
        pp(sorted_info)

    @unittest.skip("demonstrating skipping")
    def test_too_many_vertices(self):
        vertices = list()
        for i in range(100 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(SimpleMachineVertex(
                0, 50, get_resources_used_by_atoms(0, 50, []),
                "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        with self.assertRaises(PacmanPlaceException):
            self.bp.place(self.graph, self.graph_mapper)

    @unittest.skip("demonstrating skipping")
    def test_fill_machine(self):
        vertices = list()
        for i in range(99 * 17):  # 50 atoms per each processor on 20 chips
            vertices.append(SimpleMachineVertex(
                0, 50, get_resources_used_by_atoms(0, 50, []),
                "vertex " + str(i)))

        self.graph = ApplicationGraph("Graph", vertices)
        self.graph_mapper = GraphMapper()
        self.graph_mapper.add_vertices(vertices)
        self.bp = RadialPlacer(self.machine, self.graph)
        self.graph = MachineGraph(vertices=vertices)
        self.bp.place(self.graph, self.graph_mapper)