dragonfly-plus-random-topo-gen.py 25.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright 2018 - Neil McGlohon
# mcglon@rpi.edu
# An Object Oriented approach to topology generation - Allows for a logical generation that may be more complicated than v1 which used assumptions
# In hindsight this was a lot more complicated than I intended. It was looking to solve a complex problem that turned out to be invalid from the beginning.

### USAGE ###
# Correct usage: python3 script.py <num_groups> <num_spine_pg> <num_leaf_pg> <router_radix> <num_terminal_per_leaf> <intra-file> <inter-file>
###       ###

import sys
from enum import Enum
import struct
import numpy as np
import random
Neil McGlohon's avatar
Neil McGlohon committed
15
argv = sys.argv
16 17 18 19 20 21 22 23 24 25 26 27

class Loudness(Enum):
    DEBUG = 0 #prints all output
    EXTRA_LOUD = 1 #prints every single connection too
    LOUD = 2 #prints group connections and other similarly verbose things
    STANDARD = 3 #prints what step its working on
    QUIET = 4 #print no output

global DRYRUN
global LOUDNESS
global SHOW_ADJACENCY
global NO_OUTPUT_FILE
Neil McGlohon's avatar
Neil McGlohon committed
28
global TRUE_RANDOM
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160

LOUDNESS = Loudness.STANDARD
DRYRUN = 0
SHOW_ADJACENCY = 0
NO_OUTPUT_FILE = 0

global desired_num_gc_bg


if sys.version_info[0] < 3:
    raise Exception("Python 3 or a more recent version is required.")

class ConnType(Enum):
    LOCAL = 0
    GLOBAL = 1

class RouterType(Enum):
    LEAF = 0
    SPINE = 1

class AdjacencyType(Enum):
    LOCAL_LOCAL = 0 # size = (num_routers_pg, num_routers_pg)
    LOCAL_ONLY_GID = 1 # size = (total_routers, total_routers) but only the submatrix for the group is filled in. i.e. local connections only by gid
    GLOBAL_ONLY = 2 # size = (total_routers, total_routers) but only the global connecitons are included
    ALL_CONNS = 3 # size = (total_routers, total_routers) with global connections and local connections. Actual FULL adjacency matrix


def log(mystr, loudness):
    global LOUDNESS
    if loudness.value >= LOUDNESS.value:
        print(mystr)

def sortBySource(conn): #returns the key by which connections should be sorted (source id, local or global)
    if conn.connType is ConnType.LOCAL:
        return conn.src_router.local_id
    else:
        return conn.src_router.gid

def sortByDest(conn):
    if conn.connType is ConnType.LOCAL:
        return conn.dest_router.local_id
    else:
        return conn.dest_router.gid

def convertLocalIDtoGID(router_lid, router_group_num, routers_per_group):
    return router_lid + router_group_num * routers_per_group

def convertGIDtoLocalID(router_gid, routers_per_group):
    return router_gid % routers_per_group

class DragonflyPlusNetwork(object):
    def __init__(self,num_groups=5, num_spine_pg=2, num_leaf_pg=2, router_radix=4, num_hosts_per_leaf=2):
        log("New Dragonfly Plus Network", Loudness.STANDARD)
        self.num_groups = num_groups
        self.num_spine_pg = num_spine_pg
        self.num_leaf_pg = num_leaf_pg
        self.router_radix = router_radix

        self.num_global_links_per_spine = router_radix - num_leaf_pg
        self.num_global_links_pg = self.num_global_links_per_spine * num_spine_pg

        largest_num_groups = (router_radix//2)**2 + 1
        global desired_num_gc_bg
        self.desired_num_global_links_between_groups = desired_num_gc_bg
        self.num_global_links_between_groups = (largest_num_groups-1)//(num_groups-1)

        self.num_hosts_per_leaf = num_hosts_per_leaf #just for stats, not used in the actual topology export

        self.localConnectionsOutput = []
        self.globalConnectionsOutput = []
        self.groups = []

        self.precheckForErrors()
        if not DRYRUN:
            self.initializeGroups()
            self.generateLocalGroupConnections()
            self.generateGlobalGroupConnections()
            self.verifyTopology()

    def getSummary(self):
        outStr = "\nDragonfly Plus Network:\n"
        outStr += "\tNumber of Groups:          %d\n" % self.num_groups
        outStr += "\tRouter Radix:              %d\n" % self.router_radix
        outStr += "\tNumber Spine Per Group:    %d\n" % self.num_spine_pg
        outStr += "\tNumber Leaf Per Group:     %d\n" % self.num_leaf_pg
        outStr += "\tNumber Terminal Per Leaf:  %d\n" % self.num_hosts_per_leaf
        outStr += "\n"
        outStr += "\tNumber GC per Spine:       %d\n" % self.num_global_links_per_spine
        outStr += "\tNumber GC per Group:       %d\n" % (self.num_global_links_per_spine * self.num_spine_pg)
        outStr += "\tNumber GC between Groups:  %d\n" % (self.num_global_links_between_groups)
        outStr += "\n"
        outStr += "\tTotal Spine:               %d\n" % (self.num_spine_pg * self.num_groups)
        outStr += "\tTotal Leaf:                %d\n" % (self.num_leaf_pg * self.num_groups)
        outStr += "\tTotal Routers:             %d\n" % ((self.num_leaf_pg + self.num_spine_pg) * self.num_groups)
        outStr += "\tTotal Number Terminals:    %d\n" % (self.num_leaf_pg * self.num_hosts_per_leaf * self.num_groups)
        outStr += "\t"
        return outStr


    def initializeGroups(self):
        groups = []
        num_routers_pg = self.num_spine_pg + self.num_leaf_pg
        for new_group_id in range(self.num_groups):
            newGroup = Group(new_group_id, self.num_global_links_pg, self)

            for local_id in range(num_routers_pg):
                router_gid = local_id + (new_group_id * num_routers_pg)
                if local_id < self.num_leaf_pg:
                    intra_radix = self.num_spine_pg
                    inter_radix = 0
                    rt = RouterType.LEAF

                elif local_id >= self.num_leaf_pg:
                    intra_radix = self.num_leaf_pg
                    inter_radix = self.getNumGlobalConnsPerSpine()
                    rt = RouterType.SPINE

                newRouter = Router(router_gid, local_id, new_group_id, intra_radix, inter_radix, rt, self)
                newGroup.addRouter(newRouter)

            groups.append(newGroup)
            log("Added New Group. ID: %d, Size: %d" % ( new_group_id, len(newGroup.group_routers) ) , Loudness.LOUD)

        self.groups = groups

    def generateLocalGroupConnections(self):
        log("Dragonfly Plus Network: Generating Local Group Connections", Loudness.STANDARD)
        for group in self.groups:
            group.generateLocalConnections()

    def generateGlobalGroupConnections(self):
        log("Dragonfly Plus Network: Generating Global Group Connections", Loudness.STANDARD)
Neil McGlohon's avatar
Neil McGlohon committed
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183

        pair_set = set()
        for i in range(self.num_groups):
            for j in range(self.num_groups):
                if i != j:
                    pair_set.add(frozenset((i,j)))

        for pair in pair_set:
            groups = [-1,-1]
            for i,group_id in enumerate(pair):
                groups[i] = group_id

            if groups[0] == -1 or groups[1] == -1:
                raise Exception("DragonflyPlusNetwork: Bad Generation of Group Pairs")
            
            group1 = self.groups[groups[0]]
            group2 = self.groups[groups[1]]

            for i in range(self.num_global_links_between_groups):
                the_group_connection = GroupConnection(group1,group2)
                group1.addGlobalConnection(the_group_connection)
                group2.addGlobalConnection(the_group_connection)

184
        for group in self.groups:
Neil McGlohon's avatar
Neil McGlohon committed
185
            group.assignRoutersToGlobalConnections()
186

Neil McGlohon's avatar
Neil McGlohon committed
187 188
        for group in self.groups:
            group.bakeGlobalConnections()
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280


    def getNumGlobalConnsPerSpine(self):
        return self.num_global_links_pg // self.num_spine_pg

    def getTotalRouters(self):
        return self.num_groups * (self.num_spine_pg + self.num_leaf_pg)

    def getTotalNodes(self):
        return self.num_hosts_per_leaf * self.num_leaf_pg * self.num_groups

    def getAllSpineRouters(self):
        spine_rtrs = []
        for g in self.groups:
            for rtr in g.group_routers:
                if rtr.routerType is RouterType.SPINE:
                    spine_rtrs.append(rtr)
        return spine_rtrs

    def getAllLeafRouters(self):
        leaf_rtrs = []
        for g in self.groups:
            for rtr in g.group_routers:
                if rtr.routerType is RouterType.LEAF:
                    leaf_rtrs.append(rtr)
        return leaf_rtrs

    def getRouter(self,gid):
        router_group_num = gid // (self.num_spine_pg + self.num_leaf_pg)
        offset = gid % (self.num_spine_pg + self.num_leaf_pg)

        rtr = self.groups[router_group_num].group_routers[offset]
        assert(rtr.gid == gid)
        return rtr

    def precheckForErrors(self):
        if self.num_global_links_pg % self.desired_num_global_links_between_groups != 0:
            raise Exception("DragonflyPlusNetwork: Global Connection Fairness Violation. num_global_links_pg % num_global_links_between_groups != 0")
        if self.num_global_links_pg % self.num_spine_pg != 0: #are we fair?
            raise Exception("DragonflyPlusNetwork: Global Connection Fairness Violation. num_global_links_pg % num_spine_pg != 0!!!!")
        if self.num_leaf_pg > (self.router_radix - self.getNumGlobalConnsPerSpine()): #do we have enough ports left to connect to all the leafs in the group after connecting global conns?
            raise Exception("DragonflyPlusNetwork: Inadequate radix for number of global connections per spine router")

    def verifyTopology(self):
        A = self.getAdjacencyMatrix(AdjacencyType.ALL_CONNS)

        #verify symmetry. 1 <-> 2 == 2 <-> 1
        log("Verifying Symmetry...", Loudness.STANDARD)
        for i in range(A.shape[0]):
            for j in range(A.shape[1]):
                if A[i][j] != A[j][i]:
                    raise Exception("DragonflyPlusNetwork: Failed Verification: Topology not symmetric")

        #verify safe radix
        log("Verifying Radix Usage...", Loudness.STANDARD)
        for row in A:
            # log("%d > %d: %s" % (sum(row), self.router_radix, str(sum(row) > self.router_radix) ) )
            if sum(row) > self.router_radix:
                raise Exception("DragonflyPlusNetwork: Failed Verification: Router Radix Exceeded")

        #verify fairness
        log("Verifying Fairness...", Loudness.STANDARD)
        spine_rtrs = self.getAllSpineRouters()
        leaf_rtrs = self.getAllLeafRouters()

        #all spine routers have same local and global connections sizes
        loc_conns = len(spine_rtrs[0].local_connections)
        glob_conns = len(spine_rtrs[0].global_connections)
        for s in spine_rtrs:
            loc = len(s.local_connections)
            glob = len(s.global_connections)
            failed = False
            if loc is not loc_conns:
                failed = True
            if glob is not glob_conns:
                failed = True

        #all leaf routers have same local and global connections sizes
        #all leaf routers have 0 global connections
        loc_conns = len(leaf_rtrs[0].local_connections)
        glob_conns = 0
        for l in leaf_rtrs:
            loc = len(l.local_connections)
            glob = len(l.global_connections)
            failed = False
            if loc is not loc_conns:
                failed = True
            if glob is not glob_conns:
                failed = True

        log("Verifying Dragonfly Nature...", Loudness.STANDARD)

Neil McGlohon's avatar
Neil McGlohon committed
281 282 283

        if failed:
            raise Exception("DragonflyPlusNetwork: Failed Verification: Fairness")
284 285 286

        for g in self.groups:
            if len(set(g.groupConns)) != self.num_global_links_pg:
Neil McGlohon's avatar
Neil McGlohon committed
287
                raise Exception("DragonflyPlusNetwork: Not Enough Group Connections")
288 289 290 291 292 293 294

        log("Verifying Inter Group Connection Uniformity...", Loudness.STANDARD)
        num_gc_between_0_1 = len(self.groups[0].getConnectionsToGroup(1))
        for g in self.groups:
            other_groups = g.getOtherGroupIDsStartingAfterMe(self.num_groups)
            for other_group_id in other_groups:
                if len(g.getConnectionsToGroup(other_group_id)) != num_gc_between_0_1:
Neil McGlohon's avatar
Neil McGlohon committed
295 296 297 298 299 300 301 302 303 304 305 306
                    raise Exception("DragonflyPlusNetwork: Failed Verification: InterGroup Connection Uniformity: %d != %d")


        log("Verifying Number of Links Generated...", Loudness.STANDARD)
        link_sum = 0
        for row in A:
            for item in row:
                link_sum += item
            
        if link_sum != (self.router_radix * ((self.num_leaf_pg + self.num_spine_pg)*self.num_groups)) - (self.num_leaf_pg * self.num_hosts_per_leaf * self.num_groups):  #number of links per router - number of terminals (because those links weren't generated)
            raise Exception("DragonflyPlusNetwork: Failed Verification: Number of links generated doesn't match expected")

307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377

    def commitConnection(self,conn, connType):
        if connType is ConnType.LOCAL:
            self.localConnectionsOutput.append(conn)
        else:
            self.globalConnectionsOutput.append(conn)

    def writeInterconnectionFile(self, inter_filename):
        log("\nWriting out InterConnection File '%s': " % inter_filename, Loudness.STANDARD)
        with open(inter_filename, "wb") as fd:
            for conn in sorted(self.globalConnectionsOutput,key=sortBySource):
                src_gid = conn.src_router.gid
                dest_gid = conn.dest_router.gid
                fd.write(struct.pack("2i",src_gid,dest_gid))

                log("INTER %d %d sgrp %d dgrp %d" % (src_gid, dest_gid, conn.src_router.group_id, conn.dest_router.group_id), Loudness.EXTRA_LOUD)

    def writeIntraconnectionFile(self, intra_filename):
        log("\nWriting out IntraConnection File '%s': " % intra_filename, Loudness.STANDARD)
        with open(intra_filename, "wb") as fd:
            for conn in sorted(self.localConnectionsOutput,key=sortBySource):
                if conn.src_router.group_id == 0:
                    src_id = conn.src_router.local_id
                    dest_id = conn.dest_router.local_id
                    fd.write(struct.pack("2i",src_id,dest_id))
                    log("INTRA %d %d" % (src_id, dest_id), Loudness.EXTRA_LOUD)

    def getAdjacencyMatrix(self, adj_type):
        conns = []
        if adj_type is AdjacencyType.LOCAL_LOCAL:
            conns.extend(self.localConnectionsOutput)
            size = self.num_spine_pg + self.num_leaf_pg
        elif adj_type is AdjacencyType.LOCAL_ONLY_GID:
            conns.extend(self.localConnectionsOutput)
            size = self.num_groups * (self.num_spine_pg + self.num_leaf_pg)
        elif adj_type is AdjacencyType.GLOBAL_ONLY:
            conns.extend(self.globalConnectionsOutput)
            size = self.num_groups * (self.num_spine_pg + self.num_leaf_pg)
        elif adj_type is AdjacencyType.ALL_CONNS:
            conns.extend(self.localConnectionsOutput)
            conns.extend(self.globalConnectionsOutput)
            size = self.num_groups * (self.num_spine_pg + self.num_leaf_pg)

        log("A = np.zeros((%d,%d))" % (size,size), Loudness.DEBUG)
        A = np.zeros((size,size))

        for conn in conns:
            if adj_type is AdjacencyType.LOCAL_LOCAL:
                if conn.src_router.group_id is 0:
                    A[conn.src_router.local_id][conn.dest_router.local_id] += 1
            else:
                A[conn.src_router.gid][conn.dest_router.gid] += 1

        return A


class Group(object):
    def __init__(self, group_id, group_global_radix, network):
        self.group_id = group_id
        self.group_routers = [] #list of routers in this group
        self.group_global_radix = group_global_radix #number of connections from this group to other groups
        self.used_radix = 0 #used number of connections from this group to other groups
        self.network = network

        self.groupConns = []


    def addRouter(self,router):
        self.group_routers.append(router)
        self.used_radix += router.inter_radix

Neil McGlohon's avatar
Neil McGlohon committed
378 379 380 381 382 383 384 385
    def addGlobalConnection(self, group_conn):
        if group_conn.src_group == self:
            other_group_id = group_conn.dest_group.group_id
        else:
            other_group_id = group_conn.src_group.group_id

        log("Group %d -> Group %d" % (self.group_id, other_group_id), Loudness.LOUD)
        self.groupConns.append(group_conn)
386 387 388 389 390 391 392 393 394 395 396 397 398

    def getSpineRouters(self):
        return [r for r in self.group_routers if r.routerType is RouterType.SPINE]

    def getLeafRouters(self):
        return [r for r in self.group_routers if r.routerType is RouterType.LEAF]

    def getOtherGroupIDsStartingAfterMe(self,num_groups):
        my_group_id = self.group_id
        all_group_ids = [i for i in range(num_groups) if i != my_group_id]
        return np.roll(all_group_ids, -1*my_group_id)

    def getConnectionsToGroup(self,other_group_id):
Neil McGlohon's avatar
Neil McGlohon committed
399
        return [conn for conn in self.groupConns if conn.dest_group.group_id == other_group_id or conn.src_group.group_id == other_group_id]
400 401 402 403 404 405 406 407 408 409 410

    def generateLocalConnections(self):
        log("Group %d: generating local connections" % self.group_id, Loudness.LOUD)

        spine_routers = [rtr for rtr in self.group_routers if rtr.routerType == RouterType.SPINE]
        leaf_routers = [rtr for rtr in self.group_routers if rtr.routerType == RouterType.LEAF]

        for srtr in spine_routers:
            for lrtr in leaf_routers:
                srtr.connectTo(lrtr, ConnType.LOCAL)

Neil McGlohon's avatar
Neil McGlohon committed
411
    def assignRoutersToGlobalConnections(self):
412 413
        log("Group %d: assigning global connections" % self.group_id, Loudness.LOUD)

Neil McGlohon's avatar
Neil McGlohon committed
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
        my_spine_routers = self.getSpineRouters()

        if TRUE_RANDOM:
            random.shuffle(self.groupConns)

        group_conns_used = 0
        while (group_conns_used < len(self.groupConns)):
            my_spine_routers = random.sample(my_spine_routers, len(my_spine_routers))
            for router in my_spine_routers:
                router.num_unbaked_global_connections += 1
                self.groupConns[group_conns_used].setEndpoint(router)
                group_conns_used += 1

    def bakeGlobalConnections(self):
        log("Group %d: baking global connections" % self.group_id, Loudness.LOUD)

        for i, group_conn in enumerate(self.groupConns):
            if group_conn.routers[0].group_id == self.group_id:
                group_conn.routers[0].connectToOneWay(group_conn.routers[1], ConnType.GLOBAL)
            elif group_conn.routers[1].group_id == self.group_id:
                group_conn.routers[1].connectToOneWay(group_conn.routers[0], ConnType.GLOBAL)
            else:
                raise Exception("BakeGlobalConnections: Something went wrong...")
437 438 439 440 441 442 443 444 445 446 447 448 449 450




class Router(object):
    def __init__(self, self_gid, self_local_id, group_id, intra_radix, inter_radix, routerType, network):
        self.gid = self_gid
        self.local_id = self_local_id
        self.group_id = group_id
        self.intra_radix = intra_radix
        self.inter_radix = inter_radix
        self.routerType = routerType
        self.local_connections = []
        self.global_connections = []
Neil McGlohon's avatar
Neil McGlohon committed
451
        self.num_unbaked_global_connections = 0
452 453 454 455
        self.network = network

        log("New Router: GID: %d    LID: %d    Group %d" % (self.gid, self.local_id, self.group_id), Loudness.DEBUG)

Neil McGlohon's avatar
Neil McGlohon committed
456 457 458 459 460 461 462 463 464
        # local_spinal_id = self.local_id - self.network.num_leaf_pg #what is my ID in terms of num spine
        # other_groups = self.network.groups[group_id].getOtherGroupIDsStartingAfterMe(self.network.num_groups)
        # other_groups_i_connect_to_std = [g for i,g in enumerate(other_groups) if i]
        # other_groups_i_connect_to_start_index_std = (local_spinal_id * self.network.num_spine_pg) % len(other_groups)
        # other_groups_i_connect_to_start_index_addtl_gc = (other_groups_i_connect_to_start_index_std - (self.network.num_global_links_per_spine - self.network.num_spine_pg)) % len(other_groups)




465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524
    def connectTo(self, other_rtr, connType):
        if connType is ConnType.GLOBAL:
            assert(self.routerType == RouterType.SPINE)
            assert(other_rtr.routerType == RouterType.SPINE)
        conn = Connection(self,other_rtr,connType)
        oconn = Connection(other_rtr, self,connType)

        self.addConnection(conn, connType)
        other_rtr.addConnection(oconn, connType)

    def connectToOneWay(self, other_rtr, connType): #connects without connecting backward - for use if you know your loop will double count
        if connType is ConnType.GLOBAL:
            assert(self.routerType == RouterType.SPINE)
            assert(other_rtr.routerType == RouterType.SPINE)
        conn = Connection(self, other_rtr, connType)
        self.addConnection(conn, connType)

    def addConnection(self,conn, conntype):
        if conntype is ConnType.LOCAL:
            if len(self.local_connections) >= self.intra_radix:
                raise Exception("Router %d: Cannot add connection, exceeds intra_radix"% self.gid)
            self.local_connections.append(conn)
            log("New: INTRA %d %d" % (conn.src_router.local_id, conn.dest_router.local_id), Loudness.EXTRA_LOUD)

        elif conntype is ConnType.GLOBAL:
            if len(self.global_connections) >= self.inter_radix:
                raise Exception("Router %d: Cannot add connection, exceeds inter_radix"% self.gid)
            self.global_connections.append(conn)
            log("New: INTER %d %d  | Group: %d -> %d" % (conn.src_router.gid, conn.dest_router.gid, conn.src_router.group_id, conn.dest_router.group_id), Loudness.EXTRA_LOUD)
        else:
            raise Exception("Invalid Connection Type")

        self.network.commitConnection(conn, conntype)


class Connection(object):
    def __init__(self, src_router, dest_router, connType, shifted_by=0):
        self.src_router = src_router
        self.dest_router = dest_router
        self.connType = connType
        self.shifted_by = shifted_by

    def __getitem__(self, key):
        if type(key) is str:
            if key == "src":
                return self.src_router
            if key == "dest":
                return self.dest_router

        elif type(key) is int:
            if key is 0:
                return self.src_router
            elif key is 1:
                return self.dest_router
            else:
                raise IndexError("Connection: index out of range")

        else:
            raise KeyError("Connection: Invalid __getitem__() key")

Neil McGlohon's avatar
Neil McGlohon committed
525 526
class GroupConnection(object):
    def __init__(self, src_group, dest_group):
527 528
        self.src_group = src_group
        self.dest_group = dest_group
Neil McGlohon's avatar
Neil McGlohon committed
529
        self.routers = []
530

Neil McGlohon's avatar
Neil McGlohon committed
531 532 533 534 535
    def setEndpoint(self, rtr):
        if len(self.routers) == 2:
            raise Exception("GroupConnection: Can't supply more than 2 endpoints to a group connection")
        
        self.routers.append(rtr)
536 537 538 539 540 541

def parseOptionArguments():
    global DRYRUN
    global LOUDNESS
    global SHOW_ADJACENCY
    global NO_OUTPUT_FILE
Neil McGlohon's avatar
Neil McGlohon committed
542 543 544 545 546 547
    global TRUE_RANDOM

    if "--true-random" in argv:
        TRUE_RANDOM = True
    else:
        TRUE_RANDOM = False
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633

    if "--debug" in argv:
        LOUDNESS = Loudness.DEBUG

    if "--extra-loud" in argv:
        LOUDNESS = Loudness.EXTRA_LOUD

    if "--loud" in argv:
        LOUDNESS = Loudness.LOUD

    if "--quiet" in argv:
        LOUDNESS = Loudness.QUIET

    if "--dry-run" in argv:
        DRYRUN = 1

    if "--no-output" in argv:
        NO_OUTPUT_FILE = 1

    if "--show-adjacency" in argv:
        SHOW_ADJACENCY = 1


#mainV3() assumes things about the network:
#   num_spine_pg == num_leaf_pg
#   term_per_leaf = num_spine_pg
#   num_router_pg = router_radix (half up half down)
#   total_gc_per_group = router_radix / 2
def mainV3():
    parseOptionArguments()

    if DRYRUN or NO_OUTPUT_FILE:
        if DRYRUN and NO_OUTPUT_FILE:
            option = '--dry-run'
        elif DRYRUN:
            option = '--dry-run'
        elif NO_OUTPUT_FILE:
            option = '--no-output'
        if(len(argv) < 4):
            raise Exception("Correct usage: python %s <router_radix> <num_gc_between_groups> %s" % (sys.argv[0], option))

    elif(len(argv) < 5):
        raise Exception("Correct usage:  python %s <router_radix> <num_gc_between_groups> <intra-file> <inter-file>" % sys.argv[0])

    router_radix = int(argv[1])
    num_gc_between_groups = int(argv[2])

    global desired_num_gc_bg
    desired_num_gc_bg = num_gc_between_groups

    largest_num_groups = (router_radix//2)**2 + 1
    num_groups = (largest_num_groups - 1) // num_gc_between_groups + 1

    num_router_pg = int(router_radix)
    num_spine_pg = int(num_router_pg / 2)
    num_leaf_pg = num_spine_pg
    term_per_leaf = num_spine_pg

    if not (DRYRUN or NO_OUTPUT_FILE):
        intra_filename = argv[3]
        inter_filename = argv[4]


    try:
        dfp_network = DragonflyPlusNetwork(num_groups, num_spine_pg, num_leaf_pg, router_radix, num_hosts_per_leaf=term_per_leaf)
    except Exception as err:
        print("ERROR: ",err)
        exit(1)
        

    if not DRYRUN:
        if not NO_OUTPUT_FILE:
            dfp_network.writeIntraconnectionFile(intra_filename)
            dfp_network.writeInterconnectionFile(inter_filename)

    if LOUDNESS is not Loudness.QUIET:
        print(dfp_network.getSummary())

    if SHOW_ADJACENCY == 1:
        print("\nPrinting Adjacency Matrix:")

        np.set_printoptions(linewidth=400,threshold=10000,edgeitems=200)
        A = dfp_network.getAdjacencyMatrix(AdjacencyType.ALL_CONNS)
        print(A.astype(int))


Neil McGlohon's avatar
Neil McGlohon committed
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665
# def mainV2():
#     if(len(argv) < 8):
#         raise Exception("Correct usage:  python %s <num_groups> <num_spine_pg> <num_leaf_pg> <router_radix> <terminals-per-leaf> <intra-file> <inter-file>" % sys.argv[0])

#     num_groups = int(argv[1])
#     num_spine_pg = int(argv[2])
#     num_leaf_pg = int(argv[3])
#     router_radix = int(argv[4])
#     term_per_leaf = int(argv[5])
#     intra_filename = argv[6]
#     inter_filename = argv[7]

#     parseOptionArguments()

#     dfp_network = DragonflyPlusNetwork(num_groups, num_spine_pg, num_leaf_pg, router_radix, num_hosts_per_leaf=term_per_leaf)

#     if not DRYRUN:
#         dfp_network.writeIntraconnectionFile(intra_filename)
#         dfp_network.writeInterconnectionFile(inter_filename)

#     if LOUDNESS is not Loudness.QUIET:
#         print("\nNOTE: THIS STILL CAN'T DO THE MED-LARGE TOPOLOGY RIGHT\n")

#         print(dfp_network.getSummary())

#     if SHOW_ADJACENCY == 1:
#         print("\nPrinting Adjacency Matrix:")

#         np.set_printoptions(linewidth=400,threshold=10000,edgeitems=200)
#         A = dfp_network.getAdjacencyMatrix(AdjacencyType.ALL_CONNS)
#         print(A.astype(int))

666 667
if __name__ == '__main__':
    mainV3()