-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathca_router.py
103 lines (95 loc) · 3.37 KB
/
ca_router.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
"""
Module: CA Router
Desp: Congestion-Aware Routing Algorithm
version: 0.0.2
requirements: router.py
Changelog: 0.0.1 - router
0.0.2 - bug fix for arbiter
0.1.0 - moved set_neighbour_routers to baseRouter
"""
from router import BaseRouter
class CARouter(BaseRouter):
def __init__(self, id, coordinates, rx_address):
super().__init__(id, coordinates, rx_address)
def get_busy_index(self, channel):
"""
Func: calculate the busy index using look up table
"""
full = 0
half_full = 0
for buffer in self.buffer:
if len(buffer) == self.buffer_size:
full += 1
if len(buffer) >= (self.buffer_size / 2):
half_full += 1
c_full = True if len(self.buffer[channel]) == self.buffer_size else False
busy_index = None
# look up table
if not c_full and half_full <= 2:
busy_index = 0
if not c_full and half_full > 2:
busy_index = 1
if not c_full and full > 0 and full < 2:
busy_index = 2
if c_full and half_full < 3:
busy_index = 3
if c_full and half_full >= 3:
busy_index = 4
if not c_full and full > 2:
busy_index = 5
if c_full and full > 1:
busy_index = 6
if c_full and full > 2:
busy_index = 7
return busy_index
def arbiter(self, dest_coordinates):
"""
Algo: Congestion Aware router
Details: Decide on whether to go X direction or Y direction based on
which one is free
"""
direction = None
ex = dest_coordinates[1] - self.coordinates[1]
ey = dest_coordinates[0] - self.coordinates[0]
if ex > 0:
if ey > 0:
if self.neighbour_routers[self.SOUTH].get_busy_index(
self.SOUTH
) >= self.neighbour_routers[self.EAST].get_busy_index(self.EAST):
direction = self.EAST
else:
direction = self.SOUTH
elif ey < 0:
if self.neighbour_routers[self.NORTH].get_busy_index(
self.NORTH
) >= self.neighbour_routers[self.EAST].get_busy_index(self.EAST):
direction = self.EAST
else:
direction = self.NORTH
else:
direction = self.EAST
elif ex < 0:
if ey > 0:
if self.neighbour_routers[self.SOUTH].get_busy_index(
self.SOUTH
) >= self.neighbour_routers[self.WEST].get_busy_index(self.WEST):
direction = self.WEST
else:
direction = self.SOUTH
elif ey < 0:
if self.neighbour_routers[self.NORTH].get_busy_index(
self.NORTH
) >= self.neighbour_routers[self.WEST].get_busy_index(self.WEST):
direction = self.WEST
else:
direction = self.NORTH
else:
direction = self.WEST
else: # ex==0
if ey > 0:
direction = self.SOUTH
elif ey < 0:
direction = self.NORTH
else: # reached destination
direction = self.SELF
return direction