def test_get_edge_from_machine_edge(self): """ test that tests getting a edge from a graph mapper """ vertices = list() vertices.append(SimpleMachineVertex(None, "")) vertices.append(SimpleMachineVertex(None, "")) edges = list() edges.append(MachineEdge(vertices[0], vertices[1])) edges.append(MachineEdge(vertices[1], vertices[1])) sube = MachineEdge(vertices[1], vertices[0]) edges.append(sube) # Create the graph mapper graph = GraphMapper() edge = SimpleTestEdge(SimpleTestVertex(10, "pre"), SimpleTestVertex(5, "post")) graph.add_edge_mapping(sube, edge) graph.add_edge_mapping(edges[0], edge) edge_from_machine_edge = graph.get_application_edge(sube) self.assertEqual(edge_from_machine_edge, edge) self.assertEqual( graph.get_application_edge(edges[0]), edge) self.assertIsNone(graph.get_application_edge(edges[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_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 test_get_vertex_from_vertex(self): """ test that the graph mapper can retrieve a vertex from a given vertex """ vertices = list() vertices.append(SimpleMachineVertex(None, "")) vertices.append(SimpleMachineVertex(None, "")) vertex1 = SimpleMachineVertex(None, "") vertex2 = SimpleMachineVertex(None, "") graph_mapper = GraphMapper() vert = SimpleTestVertex(10, "Some testing vertex") vertex_slice = Slice(0, 1) graph_mapper.add_vertex_mapping(vertex1, vertex_slice, vert) vertex_slice = Slice(2, 3) graph_mapper.add_vertex_mapping(vertex2, vertex_slice, vert) self.assertEqual( vert, graph_mapper.get_application_vertex(vertex1)) self.assertEqual( vert, graph_mapper.get_application_vertex(vertex2)) self.assertEqual( None, graph_mapper.get_application_vertex(vertices[0])) self.assertEqual( None, graph_mapper.get_application_vertex(vertices[1]))
def test_get_vertices_from_vertex(self): """ test getting the vertex from a graph mapper via the vertex """ vertices = list() vertices.append(SimpleMachineVertex(None, "")) vertices.append(SimpleMachineVertex(None, "")) vertex1 = SimpleMachineVertex(None, "") vertex2 = SimpleMachineVertex(None, "") edges = list() edges.append(MachineEdge(vertices[0], vertices[1])) edges.append(MachineEdge(vertices[1], vertices[1])) graph_mapper = GraphMapper() vert = SimpleTestVertex(4, "Some testing vertex") vertex_slice = Slice(0, 1) graph_mapper.add_vertex_mapping(vertex1, vertex_slice, vert) vertex_slice = Slice(2, 3) graph_mapper.add_vertex_mapping(vertex2, vertex_slice, vert) returned_vertices = graph_mapper.get_machine_vertices(vert) self.assertIn(vertex1, returned_vertices) self.assertIn(vertex2, returned_vertices) for v in vertices: self.assertNotIn(v, returned_vertices)
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)
def test_same_chip_as_constraint(self): v1 = SimpleMachineVertex(None, "v1") v2 = SimpleMachineVertex(None, "v2") c1 = SameChipAsConstraint(v1) c2 = SameChipAsConstraint(v1) c3 = SameChipAsConstraint(v2) c4 = SameChipAsConstraint(v2) self.assertEqual(c1.vertex, v1) self.assertEqual(str(c1), "SameChipAsConstraint(vertex=v1)") self.assertEqual(str(c4), "SameChipAsConstraint(vertex=v2)") self.assertEqual(c1, c2) self.assertEqual(c2, c1) self.assertEqual(c3, c4) self.assertNotEqual(c1, c3) self.assertNotEqual(c3, c1) self.assertNotEqual(c2, c4) d = {} d[c1] = 1 d[c2] = 2 d[c3] = 3 d[c4] = 4 self.assertEqual(len(d), 2) self.assertEqual(d[c1], 2) self.assertEqual(d[c3], 4)
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)
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)
def create_app_less(): app_graph = ApplicationGraph("Test") mac_graph = MachineGraph("Test", app_graph) n_keys_map = DictBasedMachinePartitionNKeysMap() # An output vertex to aim things at (to make keys required) out_mac_vertex = SimpleMachineVertex(None, None) mac_graph.add_vertex(out_mac_vertex) # Create 5 application vertices (3 bits) for app_index in range(5): # For each, create up to (5 x 4) + 1 = 21 machine vertices (5 bits) for mac_index in range((app_index * 4) + 1): mac_vertex = SimpleMachineVertex(None, None) mac_graph.add_vertex(mac_vertex) # For each machine vertex create up to # (20 x 2) + 1 = 81(!) partitions (6 bits) for mac_edge_index in range((mac_index * 2) + 1): mac_edge = MachineEdge(mac_vertex, out_mac_vertex) part_name = "Part{}".format(mac_edge_index) mac_graph.add_edge(mac_edge, part_name) # Give the partition up to (40 x 4) + 1 = 161 keys (8 bits) p = mac_graph.get_outgoing_edge_partition_starting_at_vertex( mac_vertex, part_name) n_keys_map.set_n_keys_for_partition(p, (mac_edge_index * 4) + 1) return app_graph, mac_graph, n_keys_map
def test_virtual_vertices_spreader(): """ 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) n_keys_map = DictBasedMachinePartitionNKeysMap() partition = machine_graph.get_outgoing_edge_partition_starting_at_vertex( virtual_vertex, "SPIKES") n_keys_map.set_n_keys_for_partition(partition, 1) # 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 = SpreaderPlacer()( machine_graph, extended_machine, n_keys_map, 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_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 test_none_have_app_vertex(self): app_graph = ApplicationGraph("Test") graph = MachineGraph("foo", app_graph) app1 = SimpleTestVertex(12, "app1") mach1 = SimpleMachineVertex("mach1", app_vertex=None) mach2 = SimpleMachineVertex("mach2", app_vertex=None) mach3 = SimpleMachineVertex("mach3", app_vertex=app1) graph.add_vertices([mach1, mach2]) with self.assertRaises(PacmanInvalidParameterException): graph.add_vertex(mach3)
def _do_test(self, placer): machine = virtual_machine(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)])) n_keys_map = DictBasedMachinePartitionNKeysMap() inputs = { "MemoryExtendedMachine": machine, "MemoryMachine": machine, "MemoryMachineGraph": graph, "PlanNTimeSteps": None, "MemoryMachinePartitionNKeysMap": n_keys_map } algorithms = [placer] xml_paths = [] executor = PACMANAlgorithmExecutor( algorithms, [], inputs, [], [], [], xml_paths) executor.execute_mapping() placements = executor.get_item("MemoryPlacements") 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.assertTrue( other_placement.x == placement.x and other_placement.y == placement.y, "Vertex was not placed on the same chip as requested")
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 _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 test_too_many_ip_tags_for_1_board(self): n_extra_vertices = 3 machine = virtual_machine(12, 12) eth_chips = machine.ethernet_connected_chips eth_chip = eth_chips[0] eth_chip_2 = machine.get_chip_at(eth_chip.x + 1, eth_chip.y + 1) eth_procs = [ proc.processor_id for proc in eth_chip.processors if not proc.is_monitor ] procs = [proc for proc in eth_chip_2.processors if not proc.is_monitor] eth2_procs = [proc.processor_id for proc in procs] proc = procs[-1] eth_vertices = [ SimpleMachineVertex(ResourceContainer( iptags=[IPtagResource("127.0.0.1", port=tag, strip_sdp=True)]), label="Ethernet Vertex {}".format(proc)) for tag in eth_chip.tag_ids ] eth2_vertices = [ SimpleMachineVertex(ResourceContainer(iptags=[ IPtagResource("127.0.0.1", port=10000 + tag, strip_sdp=True) ]), label="Ethernet 2 Vertex {}".format(proc)) for tag in range(n_extra_vertices) ] placements = Placements( Placement(vertex, eth_chip.x, eth_chip.y, proc) for proc, vertex in zip(eth_procs, eth_vertices)) placements.add_placements( Placement(vertex, eth_chip_2.x, eth_chip_2.y, proc) for proc, vertex in zip(eth2_procs, eth2_vertices)) allocator = BasicTagAllocator() _, _, tags = allocator(machine, plan_n_timesteps=None, placements=placements) tags_by_board = defaultdict(set) for vertices in (eth_vertices, eth2_vertices): for vertex in vertices: iptags = tags.get_ip_tags_for_vertex(vertex) self.assertEqual(len(iptags), 1, "Incorrect number of tags assigned") placement = placements.get_placement_of_vertex(vertex) print(placement, "has tag", iptags[0]) self.assertFalse( iptags[0].tag in tags_by_board[iptags[0].board_address], "Tag used more than once") tags_by_board[iptags[0].board_address].add(iptags[0].tag) self.assertEqual(len(tags_by_board[eth_chip.ip_address]), len(eth_chip.tag_ids), "Wrong number of tags assigned to first Ethernet")
def test_add_iptag_then_fail_to_locate(self): """ test that asking for a invalid iptag returns a None value """ tag_info = Tags() iptag = IPTag("", 0, 0, 1, "122.2.2.2", 1, False) machine_vertex = SimpleMachineVertex(None, "") machine_vertex_2 = SimpleMachineVertex(None, "") tag_info.add_ip_tag(iptag, machine_vertex) gotton_tag = tag_info.get_ip_tags_for_vertex(machine_vertex_2) self.assertEqual(gotton_tag, None)
def test_add_reverse_iptag_then_not_locate_tag(self): """ check that asking for a reverse iptag with a incorrect machine vertex will cause a none returned """ tag_info = Tags() reverse_iptag = ReverseIPTag("", 1, 23, 0, 0, 1, 1) machine_vertex = SimpleMachineVertex(None, "") machine_vertex2 = SimpleMachineVertex(None, "") tag_info.add_reverse_ip_tag(reverse_iptag, machine_vertex2) gotton_tag = tag_info.get_reverse_ip_tags_for_vertex(machine_vertex) self.assertEqual(gotton_tag, None)
def _do_test(self, placer): machine = virtual_machine(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)])) 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 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.assertTrue( other_placement.x == placement.x and other_placement.y == placement.y, "Vertex was not placed on the same chip as requested")
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")
def test_same_atoms_as_vertex_constraint(self): with self.assertRaises(NotImplementedError): v1 = SimpleMachineVertex(None, "v1") v2 = SimpleMachineVertex(None, "v2") c1 = SameAtomsAsVertexConstraint(v1) self.assertEqual(c1.vertex, v1) self.assertEqual(c1, SameAtomsAsVertexConstraint(v1)) self.assertEqual(str(c1), 'SameAtomsAsVertexConstraint(vertex=v1)') c2 = SameAtomsAsVertexConstraint(v2) self.assertNotEqual(c1, c2) self.assertNotEqual(c1, "1.2.3.4") d = {} d[c1] = 1 d[c2] = 2 self.assertEqual(len(d), 2)
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")
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_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(2, constraints=[ChipAndCoreConstraint(1, 1)]) v1.set_virtual_chip_coordinates(0, 2) graph.add_vertex(v1) v1_id = ident(v1) 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": "south", "vertex": v1_id }, { "type": "location", "location": [1, 0], "vertex": v1_id }] assert obj == baseline
def test_create_new_placements(self): """ test creating a placements object """ subv = SimpleMachineVertex(None, "") pl = Placement(subv, 0, 0, 1) Placements([pl])
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_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 create_machine_vertex(self, vertex_slice, resources_required, label=None, constraints=None): return SimpleMachineVertex(resources_required, label, constraints, self, vertex_slice)
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_ip_tags(self): machine = virtual_machine(12, 12) eth_chips = machine.ethernet_connected_chips vertices = [ SimpleMachineVertex(ResourceContainer( iptags=[IPtagResource("127.0.0.1", port=None, strip_sdp=True) ]), label="Vertex {}".format(i)) for i in range(len(eth_chips)) ] print("Created {} vertices".format(len(vertices))) placements = Placements( Placement(vertex, chip.x, chip.y, 1) for vertex, chip in zip(vertices, eth_chips)) allocator = BasicTagAllocator() _, _, tags = allocator(machine, plan_n_timesteps=None, placements=placements) for vertex, chip in zip(vertices, eth_chips): iptags = tags.get_ip_tags_for_vertex(vertex) self.assertEqual(len(iptags), 1, "Incorrect number of tags assigned") self.assertEqual(iptags[0].destination_x, chip.x, "Destination of tag incorrect") self.assertEqual(iptags[0].destination_y, chip.y, "Destination of tag incorrect") placement = placements.get_placement_of_vertex(vertex) print(placement, "has tag", iptags[0])
class TestBasicPlacer(unittest.TestCase): """ test for basic placement algorithm """ def setUp(self): ####################################################################### # Setting up vertices, edges and graph # ####################################################################### self.vert1 = SimpleTestVertex( 100, "New AbstractConstrainedTestVertex 1") self.vert2 = SimpleTestVertex(5, "New AbstractConstrainedTestVertex 2") self.vert3 = SimpleTestVertex(3, "New AbstractConstrainedTestVertex 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, 0, 0, ip)) self.machine = Machine(chips) ####################################################################### # Setting up graph and graph_mapper # ####################################################################### self.vertices = list() self.vertex1 = SimpleMachineVertex( 0, 1, self.vert1.get_resources_used_by_atoms(Slice(0, 1)), "First vertex") self.vertex2 = SimpleMachineVertex( 1, 5, get_resources_used_by_atoms(1, 5, []), "Second vertex") self.vertex3 = SimpleMachineVertex( 5, 10, get_resources_used_by_atoms(5, 10, []), "Third vertex") self.vertex4 = SimpleMachineVertex( 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 = BasicPlacer(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 = BasicPlacer(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 = BasicPlacer(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 = SimpleTestVertex(500, "Large vertex 500") large_machine_vertex = large_vertex.create_machine_vertex( 0, 499, get_resources_used_by_atoms(0, 499, [])) # SimpleMachineVertex(0, 499, "Large vertex") 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 = BasicPlacer(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 = BasicPlacer(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 = BasicPlacer(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): # 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) @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(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) 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(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 setUp(self): ####################################################################### # Setting up vertices, edges and graph # ####################################################################### self.vert1 = SimpleTestVertex( 100, "New AbstractConstrainedTestVertex 1") self.vert2 = SimpleTestVertex(5, "New AbstractConstrainedTestVertex 2") self.vert3 = SimpleTestVertex(3, "New AbstractConstrainedTestVertex 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, 0, 0, ip)) self.machine = Machine(chips) ####################################################################### # Setting up graph and graph_mapper # ####################################################################### self.vertices = list() self.vertex1 = SimpleMachineVertex( 0, 1, self.vert1.get_resources_used_by_atoms(Slice(0, 1)), "First vertex") self.vertex2 = SimpleMachineVertex( 1, 5, get_resources_used_by_atoms(1, 5, []), "Second vertex") self.vertex3 = SimpleMachineVertex( 5, 10, get_resources_used_by_atoms(5, 10, []), "Third vertex") self.vertex4 = SimpleMachineVertex( 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)