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_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_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 _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
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_add_edge_with_no_existing_post_vertex_in_graph(self): """ test that adding a edge where the post 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(vertices[0], SimpleMachineVertex(None, ""))) with self.assertRaises(PacmanInvalidParameterException): graph = MachineGraph("foo") graph.add_vertices(vertices) graph.add_outgoing_edge_partition( MulticastEdgePartition(vertices[0], "bar")) graph.add_edges(edges, "bar")
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_outgoing_edge_partition( MulticastEdgePartition(vertices[0], "bar")) with self.assertRaises(PacmanAlreadyExistsException): graph.add_edges(edges, "bar")
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_outgoing_edge_partition( MulticastEdgePartition(identifier="part1", pre_vertex=v1)) machine_graph.add_outgoing_edge_partition( MulticastEdgePartition(identifier="part2", pre_vertex=v2)) machine_graph.add_outgoing_edge_partition( MulticastEdgePartition(identifier="part2", pre_vertex=v1)) 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 check_new_graph(self, app_graph, app_vertex): """ 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, "", app_vertex=app_vertex)) 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", application_graph=app_graph) graph.add_vertices(vertices) graph.add_outgoing_edge_partition( MulticastEdgePartition(vertices[0], "bar")) graph.add_outgoing_edge_partition( MulticastEdgePartition(vertices[5], "bar")) 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) for vert in vertices: self.assertEqual(vert, graph.vertex_by_label(vert.label)) edges_from_graph = list(graph.edges) for edge in edges_from_graph: self.assertIn(edge, edges) if app_graph: with self.assertRaises(PacmanInvalidParameterException): graph.clone() else: second = graph.clone() self.assertEqual(graph.n_vertices, second.n_vertices) vertices_from_graph = list(second.vertices) for vert in vertices_from_graph: self.assertIn(vert, vertices) for vert in vertices: self.assertEqual(vert, graph.vertex_by_label(vert.label)) self.assertEqual(graph.n_outgoing_edge_partitions, second.n_outgoing_edge_partitions) edges_from_graph = list(second.edges) for edge in edges_from_graph: self.assertIn(edge, edges) self.assertEqual(len(edges_from_graph), len(edges)) third = MachineGraphView(graph) self.assertEqual(graph.n_vertices, third.n_vertices) vertices_from_graph = list(third.vertices) for vert in vertices_from_graph: self.assertIn(vert, vertices) for vert in vertices: self.assertEqual(vert, graph.vertex_by_label(vert.label)) self.assertEqual(graph.n_outgoing_edge_partitions, third.n_outgoing_edge_partitions) edges_from_graph = list(third.edges) for edge in edges_from_graph: self.assertIn(edge, edges) self.assertEqual(len(edges_from_graph), len(edges)) with self.assertRaises(PacmanConfigurationException): third.add_edge("mock", "mock") with self.assertRaises(PacmanConfigurationException): third.add_vertex("mock") with self.assertRaises(PacmanConfigurationException): third.add_outgoing_edge_partition("mock")
def test_at_vertex_methods(self): graph = MachineGraph("foo") mach1 = MockMachineVertex("mach1", sdram_requirement=0) mach2 = MockMachineVertex("mach2", sdram_requirement=0) mach3 = MockMachineVertex("mach3", sdram_requirement=0) mach4 = SimpleMachineVertex("mach4") graph.add_vertices([mach1, mach2, mach3, mach4]) # Add partition then edge part_m_1 = MulticastEdgePartition(mach1, "spikes") graph.add_outgoing_edge_partition(part_m_1) edge_m_11 = MachineEdge(mach1, mach2, traffic_type=EdgeTrafficType.MULTICAST) graph.add_edge(edge_m_11, "spikes") # check clear error it you add the edge again with self.assertRaises(PacmanAlreadyExistsException): graph.add_edge(edge_m_11, "spikes") self.assertIn(edge_m_11, part_m_1.edges) edge_m_12 = MachineEdge(mach1, mach3, traffic_type=EdgeTrafficType.MULTICAST) graph.add_edge(edge_m_12, "spikes") edge_m_21 = MachineEdge(mach3, mach4, traffic_type=EdgeTrafficType.MULTICAST) graph.add_edge(edge_m_21, "spikes") part_m_2 = graph.get_outgoing_partition_for_edge(edge_m_21) edge_f_1 = MachineEdge(mach1, mach3, traffic_type=EdgeTrafficType.FIXED_ROUTE) graph.add_edge(edge_f_1, "Control") part_f = graph.get_outgoing_partition_for_edge(edge_f_1) part_s_1 = ConstantSDRAMMachinePartition("ram", mach1, "ram1") graph.add_outgoing_edge_partition(part_s_1) edge_s_11 = SDRAMMachineEdge(mach1, mach2, "s1") graph.add_edge(edge_s_11, "ram") edge_s_12 = SDRAMMachineEdge(mach1, mach3, "s2") graph.add_edge(edge_s_12, "ram") starting_at_mach1 = list( graph.get_outgoing_edge_partitions_starting_at_vertex(mach1)) self.assertIn(part_m_1, starting_at_mach1) self.assertIn(part_f, starting_at_mach1) self.assertIn(part_s_1, starting_at_mach1) self.assertEqual(3, len(starting_at_mach1)) starting_at_mach3 = list( graph.get_outgoing_edge_partitions_starting_at_vertex(mach3)) self.assertIn(part_m_2, starting_at_mach3) self.assertEqual(1, len(starting_at_mach3)) starting_at_mach4 = list( graph.get_outgoing_edge_partitions_starting_at_vertex(mach4)) self.assertEqual(0, len(starting_at_mach4)) ending_at_mach2 = list( graph.get_edge_partitions_ending_at_vertex(mach2)) self.assertIn(part_m_1, ending_at_mach2) self.assertIn(part_s_1, ending_at_mach2) self.assertEqual(2, len(ending_at_mach2)) ending_at_mach3 = list( graph.get_edge_partitions_ending_at_vertex(mach3)) self.assertIn(part_m_1, ending_at_mach3) self.assertIn(part_f, ending_at_mach3) self.assertIn(part_s_1, ending_at_mach3) self.assertEqual(3, len(ending_at_mach3)) ending_at_mach1 = list( graph.get_edge_partitions_ending_at_vertex(mach1)) self.assertEqual(0, len(ending_at_mach1))