forked from V3D3/prashaant-netgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimulator.py
1321 lines (1070 loc) · 41.3 KB
/
simulator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
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
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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/python
# monolithic simulator
### Network on Chip - Project - 2
### Simulating transfer of a flit in a two level network of nodes
### Authors : Shashank Nag (EE19B118), Vedaant Alok Arya (CS19B046), Pole Praneeth
### ----------------------------------------------------------------
### INPUT:
### L1Topology.txt will have single line with this data
### X,n,m where X = C, R, M, F, H, B
### C: Chain, R: Ring, M: Mesh, F: Folded Torus, H: Hypercube of dimension 3 (8 nodes), B: Butterfly network
### n: Number of nodes in first dimension
### m: Number of nodes in second dimension - it is 1 for C and R.
### n = m = 3 for H and
### n = m = 2^k (Power of 2) for B for some k > 1
###
### L2Topology.txt will have n X m lines with each line as the same syntax as above.
###
### The simulator runs as an Interactive application.
### ----------------------------------------------------------------
### OUTPUT:
### Network.Outer.dot - a DOT format file containing graphical representation of outer topology
### Network.<headID>.dot - a DOT format file for each inner topology's graph
###
### The simulator will output the routing in the format:
### Node <(Switch)>?: <NodeID>, VC: <VCID>
### For as many nodes as are encountered while routing.
###
### The Node IDs are defined in the respective *inner* DOT files.
### Note for outer nodes (Head nodes), the ID is not as indicated in the Outer DOT file,
### it is the ID in corresponding inner DOT file, the one which starts with "O" (Outer)
### Non-head IDs start with "I".
### -----------------------------------------------------------------
### DEPENDENCIES:
### networkx, pydot - for graph representation and DOT file generation respectively
### -----------------------------------------------------------------
### CONVENTION FOR NODE IDS:
### if connects to outside network
### O<outID><DELIMITER><inID>
### binary encoding: 1 <outID bits> <inID bits>
### example: outside max nodes = 16, inside max nodes = 16
### 1 oooo z iiii
### 1 0101 0011
### else
### I<outID><DELIMITER><inID>
### binary encoding: 0 <outID bits> <inID bits>
### example: continued
### 0 0000 0010
###
### Switches are given IDs but not treated as Nodes when routing
### The switches are implemented as psuedo nodes,
### in the sense that they are essentially a set of MUXes which are enabled by the scheduler
### inID and outID are tuples of integers (in mesh, butterfly, foldedtorus)
### -----------------------------------------------------------------
###################
#### IMPORTS ####
###################
# log2 used for calculating stage count in butterfly network
from math import log2
# pydot is imported by nx when calling its dot output function
import networkx as nx
##############################
#### SUPPORTING CLASSES ####
##############################
# Class which stores extra data for a Node
# - generateID: generates an ID for a node, given the parameters
class Node:
def __init__(self, isHead, headID, inClass, inID, isSwitch = False):
# is this node the head node of its tile?
self.isHead = isHead
# id of head node of the tile of this node in outer topology
self.headID = headID
# class of inner topology of which this node is a member
self.inClass = inClass
# id of node in inner topology
self.inID = inID
# is this node a switch or a tile?
self.isSwitch = isSwitch
# string id for this node, uniquely identifies it
self.id = self.generateID(isHead, headID, inClass, inID, isSwitch)
@staticmethod
def generateID(isHead, headID, inClass, inID, isSwitch = False):
if(isHead):
return 'O' + headID + DELIMITER + inID
else:
return 'I' + headID + DELIMITER + inID
# Class which stores data about a topology
class Topology:
def __init__(self, isOuter, headID, topoClass, topoParams, topoGraph):
# is it an outer topology or inner?
self.isOuter = isOuter
# id of the head node in this tile (if exists)
self.headID = headID
# class of this topography
self.topoClass = topoClass
# parameters for this topology
self.topoParams = topoParams
# store the graph of the topology
self.topoGraph = topoGraph
###################
#### GLOBALS ####
###################
# This object stores the outer topology
outerTopology = {}
# This dictionary stores inner topology objects
innerTopologies = {}
# The delimiter in string IDs
DELIMITER = 'Z'
# The input files
file1 = open(r"L1Topology.txt","r")
file2 = open(r"L2Topology.txt","r")
##############################################
#### INNER TOPOLOGY GENERATOR FUNCTIONS ####
##############################################
# Function to generate the tile for a chain type L2 connection
# n is the length of the chain
# id is the head id for this tile - note this is not the actual id for the head node
def chain_gen(n, id):
# Create topology object
thisClass = 'C'
thisParams = (n, )
thisGraph = nx.Graph();
# In order: not outer topology, no head node id defined, class is thisClass,
# params are thisParams, graph representation is thisGraph
thisTopology = Topology(False, '', thisClass, thisParams, thisGraph);
# Trivial case of one node. It isn't connected to anything
if n == 1:
# Special case of an ID: this node has a blank innerID
node = Node(True, id, thisClass, '')
# Add node to topology's graph rep
thisGraph.add_node(node.id, exdata=node)
# Set this node as the head node
thisTopology.headID = node.id
else :
# Add nodes
for i in range(0, n):
isHead = False
# If we encounter a head node
if(i == int(n/2)):
isHead = True
thisTopology.headID = Node.generateID(True, id, thisClass, str(i))
# Generate a node
cnode = Node(isHead, id, thisClass, str(i))
# Add it
thisGraph.add_node(cnode.id, exdata=cnode)
# Add edges between nodes
for i in range(0, n-1):
# Generate IDs for current and immediately next node
myID = Node.generateID((i == int(n/2)), id, thisClass, str(i))
nextID = Node.generateID(((i+1) == int(n/2)), id, thisClass, str(i+1))
# Add an undirected link (nx.Graph() is undirected)
thisGraph.add_edge(myID, nextID)
# Add this topology to the innerTopologies dict
innerTopologies[id] = thisTopology
# Function to generate the tile for a ring type L2 connection
# n is the length of the ring
# id is the head id for this tile
def ring_gen(n, id):
# Create topology object
thisClass = 'R'
thisParams = (n, )
thisGraph = nx.Graph()
thisTopology = Topology(False, 0, thisClass, thisParams, thisGraph)
# Trivial case of one node
if n == 1:
node = Node(True, id, thisClass, '')
thisGraph.add_node(node.id, exdata=node)
thisTopology.headID = node.id
# Ring with only two nodes. The nodes are connected to each other
if n == 2:
# Generate and add the two nodes
node1 = Node(False, id, thisClass, '0')
node2 = Node(True, id, thisClass, '1')
thisGraph.add_node(node1.id, exdata=node1)
thisGraph.add_node(node2.id, exdata=node2)
# Set head in topology object, as latter node
thisTopology.headID = node2.id
# Link them
thisGraph.add_edge(node1.id, node2.id)
# Non trivial case
else :
# Adding nodes, part 1: headnode
headnode = Node(True, id, thisClass, '0')
thisGraph.add_node(headnode.id, exdata=headnode)
thisTopology.headID = headnode.id
# Adding nodes, part 2: others
for i in range(1, n):
cnode = Node(False, id, thisClass, str(i))
thisGraph.add_node(cnode.id, exdata=cnode)
# Adding links except to head
for i in range(1, n-1):
myID = Node.generateID(False, id, thisClass, str(i))
nextID = Node.generateID(False, id, thisClass, str(i + 1))
thisGraph.add_edge(myID, nextID)
# Adding links to head
thisGraph.add_edge(headnode.id, Node.generateID(False, id, thisClass, str(n-1)))
thisGraph.add_edge(headnode.id, Node.generateID(False, id, thisClass, str(1)))
# Add this topology to inner topologies dict
innerTopologies[id] = thisTopology
# Function to generate the tile for a hypercube type L2 connection
# Dimension is forced as 3 (8 nodes)
# id is head id for the tile
def hypercube_gen(id):
thisClass = 'H'
thisParams = None
thisGraph = nx.Graph();
thisTopology = Topology(False, 0, thisClass, thisParams, thisGraph);
# Create and add nodes
nodes = [Node(True, id, thisClass, '0')]
thisGraph.add_node(nodes[0].id, exdata=nodes[0])
thisTopology.headID = nodes[0].id
for i in range(1, 8):
nodes.append(Node(False, id, thisClass, str(i)))
thisGraph.add_node(nodes[i].id, exdata=nodes[i])
# Now add links
for i in range(8):
# Edges are made by inverting 1 bit in each position
# 3 edges for our 3d hypercube
thisGraph.add_edge(nodes[i].id, nodes[i ^ 1].id)
thisGraph.add_edge(nodes[i].id, nodes[i ^ 2].id)
thisGraph.add_edge(nodes[i].id, nodes[i ^ 4].id)
# Add inner topology to dict
innerTopologies[id] = thisTopology
# Function to generate the tile for a mesh type L2 connection
# Dimension of n x m (n rows and m columns)
# id is head id for the tile
def mesh_gen(n, m, id):
thisClass = 'M'
thisParams = (n, m)
thisGraph = nx.Graph();
thisTopology = Topology(False, 0, thisClass, thisParams, thisGraph);
# Given i, j this function checks if it is a head node according to params
def checkHead(ix, jx):
return (ix == int(n/2)) and (jx == (int(m/2)))
# Add nodes
for i in range(n): # each row
for j in range(m): # each column
# Add node to graph
isHead = checkHead(i, j)
cnode = Node(isHead, id, thisClass, str(i) + DELIMITER + str(j))
thisGraph.add_node(cnode.id, exdata=cnode)
# If headnode, set topology property
if(isHead):
thisTopology.headID = cnode.id
# Checks if node i, j are valid
def checkNode(ix, jx):
if(ix < 0 or jx < 0):
return False
if(ix >= n or jx >= m):
return False
return True
# Safely adds edges - if dest node exists, and accounting for headID
def addEdgeSafe(isrc, jsrc, idest, jdest):
# If not safe, return
if(not checkNode(idest, jdest)):
return
# Generate IDs for source and destination
srcID = Node.generateID(checkHead(isrc, jsrc), id, thisClass, str(isrc) + DELIMITER + str(jsrc))
destID = Node.generateID(checkHead(idest, jdest), id, thisClass, str(idest) + DELIMITER + str(jdest))
# Add edge between src and dest
thisGraph.add_edge(srcID, destID)
# Add edges
for i in range(n):
for j in range(m):
# Add edge top, bottom, left and right. Function will take care of issues
addEdgeSafe(i, j, i - 1, j)
addEdgeSafe(i, j, i + 1, j)
addEdgeSafe(i, j, i, j - 1)
addEdgeSafe(i, j, i, j + 1)
# Add topology to dict
innerTopologies[id] = thisTopology
# Function to generate the tile for a butterfly type L2 connection
# n is the total number of nodes on left and right
# id is the head id for this tile
def butterfly_gen(n, id):
thisClass = 'B'
thisParams = (n, )
thisGraph = nx.Graph();
thisTopology = Topology(False, 0, thisClass, thisParams, thisGraph);
# node internal IDs: <num1> where
# num1 identifies the node index
# switch internal IDs: <num1><DELIMITER><num2> where
# num1 identifies the stage in the butterfly
# num2 identifies the switch in the stage
# Add headID (we know where head is)
thisTopology.headID = Node.generateID(True, id, thisClass, str(0))
# Check if this node is the head node
def checkHead(stage, index):
# We've selected the 0th node as head node
if(stage == 0 and index == 0):
return True
return False
# n/2 nodes on left, n/2 on right, n/4 switches in each stage
# hence, log2(n/4) + 1 = log2(n) - 1 layers of switches
# and 2 layers of nodes, total log2(n) + 1 layers
n_stages = int(log2(n)) + 1
# Add nodes
for i in range(0, n):
# Stage differs based on i, and index in stage too, send accordingly to checkHead
cnode = Node(checkHead(0 if i < int(n/2) else n_stages - 1, i % int(n/2)),
id, thisClass, str(i))
thisGraph.add_node(cnode.id, exdata=cnode)
# Add switches
for i in range(1, n_stages - 1):
# n/4 switches in each stage!
for j in range(int(n/4)):
# Note the last parameter is essential. We're giving IDs to switches too, but won't consider them as nodes
cswitch = Node(False, id, thisClass, str(i) + DELIMITER + str(j), True)
thisGraph.add_node(cswitch.id, exdata=cswitch)
# Add edges from input to first switch layer
for i in range(0, int(n/2)):
# Generate IDs of target node and switch
nodeID = Node.generateID(checkHead(0, i), id, thisClass, str(i))
switchID = Node.generateID(False, id, thisClass, str(1) + DELIMITER + str(int(i/2)), True)
thisGraph.add_edge(nodeID, switchID)
# Add edges from last switch layer to output
for i in range(0, int(n/4)):
# Generate IDs of target switch and nodes
switchID = Node.generateID(False, id, thisClass, str(n_stages - 2) + DELIMITER + str(i), True)
nodeID1 = Node.generateID(False, id, thisClass, str(int(n/2) + (2*i)))
nodeID2 = Node.generateID(False, id, thisClass, str(int(n/2) + (2*i + 1)))
# Add edges to both nodes from switch
thisGraph.add_edge(switchID, nodeID1)
thisGraph.add_edge(switchID, nodeID2)
# This method returns the switch index for the next layer indirect switch according to current switch
def nextSwitch(current, stage):
# Invert one bit according to stage
bit = 1 << (stage - 1)
return bit ^ current
# Add edges between switches in the inner layers
for i in range(1, n_stages - 2):
for j in range(0, int(n/4)):
# This switch's ID
myID = Node.generateID(False, id, thisClass, str(i) + DELIMITER + str(j), True)
# Next Switch IDs
nextIDDirect = Node.generateID(False, id, thisClass, str(i + 1) + DELIMITER + str(j), True)
nextIDIndirect = Node.generateID(False, id, thisClass, str(i + 1) + DELIMITER + str(nextSwitch(j, i)), True)
# Adding edges
thisGraph.add_edge(myID, nextIDDirect)
thisGraph.add_edge(myID, nextIDIndirect)
# Add topology to dict
innerTopologies[id] = thisTopology
# Function to generate the tile for a folded torus type L2 connection
# Dimension of n x m (n rows and m columns)
# id for the head id of this tile
def folded_torus_gen(n, m, id):
thisClass = 'F'
thisParams = (n, m)
thisGraph = nx.Graph();
thisTopology = Topology(False, 0, thisClass, thisParams, thisGraph);
# Node ID: <num1> <DELIM> <num2>
# num1 -> row of the node
# num2 -> column of the node
# This function checks if the i, j point to a head
def checkHead(i, j):
# Choosing first node as head for this topology
if(i == 0 and j == 0):
thisTopology.headID = Node.generateID(True, id, thisClass, '0Z0')
return True
return False
# Add nodes
for i in range(n):
for j in range(m):
cnode = Node(checkHead(i, j), id, thisClass, str(i) + DELIMITER + str(j))
thisGraph.add_node(cnode.id, exdata=cnode)
# Safely add an edge between source (i,j) and dest (i,j)
def safeAddEdge(isrc, jsrc, idest, jdest):
# Safety: keeping indices within limits
idest = (idest + n) % n
jdest = (jdest + m) % m
# Generate IDs and link the nodes
srcID = Node.generateID(checkHead(isrc, jsrc), id, thisClass, str(isrc) + DELIMITER + str(jsrc))
destID = Node.generateID(checkHead(idest, jdest), id, thisClass, str(idest) + DELIMITER + str(jdest))
thisGraph.add_edge(srcID, destID)
# Add edges in columns
for j in range(m):
# Add link to immediately next of first node
safeAddEdge(0, j, 1, j)
# Add links in internal nodes
for i in range(int(n / 2) - 1):
safeAddEdge(2*i, j, 2*i+2, j)
safeAddEdge(2*i+1, j, 2*i+3, j)
# Add link to prev of last node
safeAddEdge(n-1, j, n-2, j)
# Add edges in rows
for i in range(n):
# Add link to immediately next of first node
safeAddEdge(i, 0, i, 1)
# Add links in internal nodes
for j in range(int(m / 2) - 1):
safeAddEdge(i, 2*j, i, 2*j+2)
safeAddEdge(i, 2*j+1, i, 2*j+3)
# Add link to prev of last node
safeAddEdge(i, m-1, i, m-2)
# Add this topology to dict
innerTopologies[id] = thisTopology
###########################################
#### OUTER TOPOLOGY HELPER FUNCTIONS ####
###########################################
# Generates inner topology given the head id
def genInner(id):
# Read tile info from L2Topology file
tileInfo = file2.readline()
# Extract network type, dimensions
network_type, n, m = tileInfo.split(',')
n = int(n)
m = int(m)
# Generate according to network_type
# Note these generators automatically add the generated Topology object to innerTopologies dict
# So we can access them right away after genInner is done running
if network_type == 'R':
ring_gen(n, id)
elif network_type == 'C':
chain_gen(n, id)
elif network_type == 'M':
mesh_gen(n, m, id)
elif network_type == 'B':
butterfly_gen(n, id)
elif network_type == 'F':
folded_torus_gen(n, m, id)
elif network_type == 'H':
hypercube_gen(id)
# Gets the head node data for the topology with the given head id
def getHeadNode(index):
srcTopo = innerTopologies[index]
srcHeadID = srcTopo.headID
srcHeadNode = srcTopo.topoGraph.nodes[srcHeadID]
return srcHeadNode['exdata']
##############################################
#### OUTER TOPOLOGY GENERATOR FUNCTIONS ####
##############################################
# Each generator generates the inner topologies, then uses node data from the head nodes of these
# in the outer topology graph. They don't generate new nodes, except for Butterfly which generates
# Node objects for switches (however these aren't treated as nodes).
# These call genInner() for each "node addition" of their own, then fetch the head node for the
# topology just generated.
# Return values are the outerTopology object
# For ring type L1 topology
def ring_head_gen(n):
thisClass = 'R'
thisParams = (n, )
thisGraph = nx.Graph()
thisTopology = Topology(True, 0, thisClass, thisParams, thisGraph)
# Two node chain case
if n == 2:
# Generate the topologies
genInner('0')
node0 = getHeadNode('0')
thisGraph.add_node('0', exdata=node0)
genInner('1')
node1 = getHeadNode('1')
thisGraph.add_node('1', exdata=node1)
# Add edge between them
thisGraph.add_edge('0', '1')
# Non-trivial case
else :
# Adding nodes
for i in range(n):
# Generate each one, get its head node, add it to our graph
genInner(str(i))
node = getHeadNode(str(i))
thisGraph.add_node(str(i), exdata=node)
# Adding links
for i in range(n):
thisGraph.add_edge(str(i), str((i + 1) % n))
return thisTopology
# For chain type L1 topology
def chain_head_gen(n):
thisClass = 'C'
thisParams = (n, )
thisGraph = nx.Graph();
thisTopology = Topology(True, 0, thisClass, thisParams, thisGraph);
if n >= 2:
# Generate nodes
for i in range(n):
genInner(str(i))
node = getHeadNode(str(i))
thisGraph.add_node(str(i), exdata=node)
# Add links
for i in range(0, n-1):
thisGraph.add_edge(str(i), str(i+1))
else:
# Sanity check
print("Invalid dimensions for outer topology as chain")
exit()
return thisTopology
# For hypercube type L1 topology
def hypercube_head_gen():
thisClass = 'H'
thisParams = None
thisGraph = nx.Graph();
thisTopology = Topology(True, '', thisClass, thisParams, thisGraph);
n = 8 # Fixed dimensions for hypercube
# Generate nodes
for i in range(n):
genInner(str(i))
node = getHeadNode(str(i))
thisGraph.add_node(str(i), exdata=node)
# Add links
for i in range(n):
thisGraph.add_edge(str(i), str(i ^ 1))
thisGraph.add_edge(str(i), str(i ^ 2))
thisGraph.add_edge(str(i), str(i ^ 4))
return thisTopology
# For mesh type L1 topology
def mesh_head_gen(n,m):
thisClass = 'M'
thisParams = (n, m)
thisGraph = nx.Graph();
thisTopology = Topology(True, 0, thisClass, thisParams, thisGraph);
# This returns the head id given i, j (Note not head node id, that is generated by inner topo generators)
def genID(ix, jx):
return str(ix) + DELIMITER + str(jx)
# Generate nodes
for i in range(n):
for j in range(m):
genInner(genID(i, j))
node = getHeadNode(genID(i, j))
thisGraph.add_node(genID(i, j), exdata=node)
# Checks if node i, j are valid
def checkNode(ix, jx):
if(ix < 0 or jx < 0):
return False
if(ix >= n or jx >= m):
return False
return True
# Safely add an edge, just like in mesh_gen
def addEdgeSafe(isrc, jsrc, idest, jdest):
if(not checkNode(idest, jdest)):
return
thisGraph.add_edge(genID(isrc, jsrc), genID(idest, jdest))
# Add edges
for i in range(n):
for j in range(m):
addEdgeSafe(i, j, i - 1, j)
addEdgeSafe(i, j, i + 1, j)
addEdgeSafe(i, j, i, j - 1)
addEdgeSafe(i, j, i, j + 1)
return thisTopology
# For Butterfly type L1 topology
def butterfly_head_gen(n):
thisClass = 'B'
thisParams = (n, )
thisGraph = nx.Graph();
thisTopology = Topology(True, 0, thisClass, thisParams, thisGraph);
# Number of stages, like in butterfly_gen
n_stages = int(log2(n)) + 1
# Generate nodes only
for i in range(0,n):
genInner(str(i))
cnode = getHeadNode(str(i))
thisGraph.add_node(str(i), exdata=cnode)
# Returns an ID of a switch, given stage and index
# switch names: <num1> <DELIM> <num2> :
# num1 identifies the stage in the butterfly
# num2 identifies the switch in the stage
def switchID(stage, index):
return str(stage) + DELIMITER + str(index)
# Generate switches (Note creates Node objects)
for i in range(1, n_stages - 1):
for j in range(0, int(n/4)):
thisGraph.add_node(str(i), exdata=Node(True, switchID(i, j), thisClass, '', True))
# Add edges from input to first switch layer
for i in range(0, int(n/2)):
myID = Node.generateID(True, switchID(1, int(i/2)), thisClass, '', True)
thisGraph.add_edge(str(i), myID)
# Add edges from last switch layer to output
for i in range(0, int(n/4)):
myID = Node.generateID(True, switchID(n_stages - 2, i), thisClass, '', True)
nodeID1 = str(int(n/2) + 2*i)
nodeID2 = str(int(n/2) + 2*i + 1)
thisGraph.add_edge(myID, nodeID1)
thisGraph.add_edge(myID, nodeID2)
# Returns index of next stage indirect switch connected to given switch
def nextSwitch(current, stage):
bit = 1 << (stage - 1)
return bit ^ current
# Add edges between switches in the inner layers
for i in range(1, n_stages - 2):
for j in range(0, int(n/4)):
myID = Node.generateID(True, switchID(i, j), thisClass, '', True)
nextIDDirect = Node.generateID(True, switchID(i+1, j), thisClass, '', True)
nextIDIndirect = Node.generateID(True, switchID(i+1, nextSwitch(j, i)), thisClass, '', True)
thisGraph.add_edge(myID, nextIDDirect)
thisGraph.add_edge(myID, nextIDIndirect)
return thisTopology
# For folded torus type L1 topology
def folded_torus_head_gen(n,m):
thisClass = 'F'
thisParams = (n, m)
thisGraph = nx.Graph();
thisTopology = Topology(True, 0, thisClass, thisParams, thisGraph);
# Given i,j return ID of node
def genID(i, j):
return str(i) + DELIMITER + str(j)
# Add nodes
for i in range(n):
for j in range(m):
genInner(genID(i, j))
cnode = getHeadNode(genID(i, j))
thisGraph.add_node(genID(i, j), exdata=cnode)
# Safely add edge, like in folded_torus_gen
def safeAddEdge(isrc, jsrc, idest, jdest):
idest = (idest + n) % n
jdest = (jdest + m) % m
srcID = genID(isrc, jsrc)
destID = genID(idest, jdest)
thisGraph.add_edge(srcID, destID)
# Add edges in columns
for j in range(m):
# Add link to immediately next of first node
safeAddEdge(0, j, 1, j)
# Add links in internal nodes
for i in range(int(n / 2) - 1):
safeAddEdge(2*i, j, 2*i+2, j)
safeAddEdge(2*i+1, j, 2*i+3, j)
# Add link to prev of last node
safeAddEdge(n-1, j, n-2, j)
# Add edges in rows
for i in range(n):
# Add link to immediately next of first node
safeAddEdge(i, 0, i, 1)
# Add links in internal nodes
for j in range(int(m / 2) - 1):
safeAddEdge(i, 2*j, i, 2*j+2)
safeAddEdge(i, 2*j+1, i, 2*j+3)
# Add link to prev of last node
safeAddEdge(i, m-1, i, m-2)
return thisTopology
###########################
#### OUTPUT FUNCTION ####
###########################
## Function to print the network.dot file
def print_func():
nx.drawing.nx_pydot.write_dot(outerTopology.topoGraph, "Network.Outer.dot")
for q in innerTopologies:
nx.drawing.nx_pydot.write_dot(innerTopologies[q].topoGraph, "Network." + q + ".dot")
#########################################
#### NETWORK GENERATION INITIATION ####
#########################################
# Read the L1 topology file to find the top layer topology
L1 = file1.read()
# Extract values from the L1 topology
L1_network_type, L1_n, L1_m = L1.split(",")
L1_n = int(L1_n)
L1_m = int(L1_m)
# Generate head according to type
if L1_network_type == "R":
outerTopology = ring_head_gen(L1_n)
elif L1_network_type == "C":
outerTopology = chain_head_gen(L1_n)
elif L1_network_type == "M":
outerTopology = mesh_head_gen(L1_n,L1_m)
elif L1_network_type == "B":
outerTopology = butterfly_head_gen(L1_n)
elif L1_network_type == "F":
outerTopology = folded_torus_head_gen(L1_n,L1_m)
elif L1_network_type == "H":
outerTopology = hypercube_head_gen()
# Output the DOT files for the generated network
print_func()
####################################
#### ROUTING HELPER FUNCTIONS ####
####################################
# Get a Node object from any topology given its id
def getNode(id):
if(outerTopology.topoGraph.nodes.get(id) != None):
return outerTopology.topoGraph.nodes[id]
for q in innerTopologies:
if(innerTopologies[q].topoGraph.nodes.get(id) != None):
return innerTopologies[q].topoGraph.nodes[id]
return None
#############################
#### ROUTING FUNCTIONS ####
#############################
# Routing functions work in a tick-based fashion. When called, they route a flit once.
# Butterfly routing function does printing itself, for switches, considering practically they aren't treated as nodes
# Route a flit from src to dest, with src having received it in vcid, and routing done in (outside?outer:inner) topology.
# Mesh routing
def route_mesh(src:Node, dest:Node, outside:bool, vcid=''):
# Get ID according to routing being done outside or in
# inID for a node doesn't have its headID or isHead components, which its id does
idsrc = src.headID if outside else src.inID
iddest = dest.headID if outside else dest.inID
# i,j's for src, dest, found from IDs
isrc, jsrc = list(map(lambda x : int(x), idsrc.split(DELIMITER)))
idest, jdest = list(map(lambda x : int(x), iddest.split(DELIMITER)))
# Topology object of the source node
srcTopo = outerTopology if outside else (innerTopologies[src.headID])
# nextid, vcid will be returned
nextid = ''
# H indicates a VC of a head node, being used for outer routing
vcid = 'H0' if outside else '0'
# X routing
if(jsrc < jdest):
jsrc += 1 # move closer to destination j
elif(jsrc > jdest):
jsrc -= 1
# These statements checked if jsrc == jdest, hence X-Y routing
elif(isrc < idest):
isrc += 1
else:
isrc -= 1
# Determining nextID according to transformed isrc, jsrc
if(outside):
# If outside, find using innerTopologies' headIDs
nextid = innerTopologies[str(isrc) + DELIMITER + str(jsrc)].headID
else:
# If inside, assume it is not a head node
nextid = Node.generateID(False, src.headID, src.inClass, str(isrc) + DELIMITER + str(jsrc))
# If not found, our assumption was wrong. It is a head node, set nextID accordingly
if(srcTopo.topoGraph.nodes.get(nextid) == None):
nextid = Node.generateID(True, src.headID, src.inClass, str(isrc) + DELIMITER + str(jsrc))
# Return routed info
return nextid, str(vcid)
# Folded Torus routing
def route_folded_torus(src:Node, dest:Node, outside:bool, vcid=''):
# Get IDs
idsrc = src.headID if outside else src.inID
iddest = dest.headID if outside else dest.inID
# Extract location info
isrc, jsrc = list(map(lambda x : int(x), idsrc.split(DELIMITER)))
idest, jdest = list(map(lambda x : int(x), iddest.split(DELIMITER)))
# Get source topo
srcTopo = outerTopology if outside else (innerTopologies[src.headID])
nextid = ''
# vcset: if vcid has been shifted to 1, we don't want it being shifted back to 0
# if vcset is true, use vcid as given, else generate a new one
vcset = False
# Check if vcid supplied is valid
if(len(vcid) > 0):
# Check if it was shifted to 1
vcset = (vcid[-1] == '1')
if(vcset):
# If it was shifted:
# do not absorb outer vcid when going into an inner topology from outside
if((not outside) and vcid[0] == 'H'):
vcset = False
# do not absorb inner vcid when going outside the inner topology
if((outside) and vcid[0] != 'H'):
vcset = False
# If vcset is false, initialize vcid
if(not vcset):
vcid = 'H' if outside else ''
# Cache i,j to check later if they changed
icache, jcache = isrc, jsrc
# n, m are needed to determine shorter arc
n, m = srcTopo.topoParams
# X routing with shortest arc
if(jsrc < jdest):
# Inc/Dec according to shortest arc
if(jdest - jsrc > (jsrc + m - jdest)):
jsrc -= 1
else:
jsrc += 1
elif(jsrc > jdest):
if(jsrc - jdest > (jdest + n - jsrc)):
jsrc += 1
else:
jsrc -= 1
# Dateline routing
elif(isrc != idest):
isrc += 1
# Jumping from start to end of a ring/vice-versa
if(jsrc < 0):
jsrc = m - 1
elif(jsrc == m):
jsrc = 0
if(isrc == n):
isrc = 0
# Dateline routing: setting vc if not previously set
if(not vcset):
if(isrc != icache):
# If i was changed and is now 0, then change VC
if(isrc == 0):
vcid += '1'
else:
vcid += '0'
else:
vcid += '0'
# Getting nextID appropriately
if(outside):
nextid = innerTopologies[str(isrc) + DELIMITER + str(jsrc)].headID
else:
nextid = Node.generateID(False, src.headID, src.inClass, str(isrc) + DELIMITER + str(jsrc))
if(srcTopo.topoGraph.nodes.get(nextid) == None):
nextid = Node.generateID(True, src.headID, src.inClass, str(isrc) + DELIMITER + str(jsrc))
return nextid, str(vcid)
# Chain routing
def route_chain(src:Node, dest:Node, outside:bool, vcid):
# Get IDs
idsrc = int(src.headID if outside else src.inID)
iddest = int(dest.headID if outside else dest.inID)
# Get source topo
srcTopo = outerTopology if outside else (innerTopologies[src.headID])
# VC's only used directionally here
nextid = ''
vcid = 'H' if outside else ''
# If going right, VC 0, else VC 1
if(idsrc < iddest):
idsrc += 1; vcid += '0'
else:
idsrc -= 1; vcid += '1'
# Get nextID
if(outside):
nextid = innerTopologies[str(idsrc)].headID
else:
nextid = Node.generateID(False, src.headID, src.inClass, str(idsrc))
if(srcTopo.topoGraph.nodes.get(nextid) == None):
nextid = Node.generateID(True, src.headID, src.inClass, str(idsrc))
return nextid, str(vcid)
# Ring routing
def route_ring(src:Node, dest:Node, outside:bool, vcid):
# Get IDs
idsrc = int(src.headID if outside else src.inID)
iddest = int(dest.headID if outside else dest.inID)
# Get source topo
srcTopo = outerTopology if outside else (innerTopologies[src.headID])