-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.py
207 lines (182 loc) · 6.75 KB
/
Main.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
# Libraries
from copy import deepcopy
import networkx as nx
import matplotlib.pyplot as plt
# Output variable for file print
MainOutput = []
# Read from files
with open('Input_Matrix.txt', 'r') as f:
Inputs = [[int(num) for num in line.split(',')] for line in f]
f.close()
with open('Output_Matrix.txt', 'r') as f:
Outputs = [[int(num) for num in line.split(',')] for line in f]
f.close()
with open('Matrix_of_Edges.txt', 'r') as f:
Edges = [[int(num) for num in line.split(',')] for line in f]
f.close()
with open('Primary_Tokens.txt', 'r') as f:
PrimaryToken = int(f.readline())
PrimaryTokens = [[int(num) for num in line.split(',')] for line in f]
f.close()
with open('input.txt', 'r') as f:
f.readline()
ProcessingTimes = [int(num) for num in f.readline().split(',')]
f.readline()
f.readline()
NumberOfClocks = int(f.readline())
f.readline()
f.readline()
FrequencyOfClock = int(f.readline())
# Class of actor
class Actor:
def __init__(self, ID, ProcessingTime, Input, Output):
self.ID = ID
self.ProcessingTime = ProcessingTime
self.Input = deepcopy(Input)
self.Output = deepcopy(Output)
self.Busy = 0
# Making a list of Actors
Actors = []
for i in range(0, len(Edges)):
id = i
ProcessingTime = ProcessingTimes[i]
Input = []
for j in range(0, len(Inputs)):
if Inputs[j][2] == i:
if Inputs[j][0] == -1:
Input.append([Inputs[j][0], PrimaryToken, Inputs[j][1]])
else:
Input.append([Inputs[j][0], PrimaryTokens[Inputs[j][0]][Inputs[j][2]], Inputs[j][1]])
Busy = 0
Output = []
for j in range(0, len(Outputs)):
if Outputs[j][0] == i:
if Outputs[j][2] == -2:
Outputs[j][2] = len(Edges)
Output.append([Outputs[j][1], Outputs[j][2]])
AC = Actor(id, ProcessingTime, Input, Output)
Actors.append(AC)
outp = []
for o in Outputs:
if o[2] == -2:
outp = deepcopy(o)
AC = Actor(len(Edges), 100, [[o[0],0,1]], [[1, len(Edges)+1]])
Actors.append(AC)
# Drawing SDF graph
SDF = nx.DiGraph()
for j in range(0, len(Actors)-1):
SDF.add_node(str(Actors[j].ID))
for i in Actors[j].Input:
if(i[0]!=-1):
SDF.add_edge(str(i[0]), str(Actors[j].ID))
nx.draw(SDF, node_size=600, with_labels=True)
plt.savefig("path_graph1.png")
plt.show()
# Get from inputs
num = 0
def getInput(Actors, z, i):
global num
ContinueOrNot = 0
for j in range(0, len(Actors[z].Input)):
if Actors[z].Input[j][1] >= Actors[z].Input[j][2]:
num = num + 1
if num == len(Actors[z].Input) and Actors[z].Busy == 0 and Actors[z].ID != len(Edges):
if(Actors[z].ID == 0):
StartTime.append(i)
for c in Actors[z].Input:
c[1] = c[1] - c[2]
pt = Actors[z].ProcessingTime
Actors[z].Busy = pt
ContinueOrNot = 1
MainOutput.append("Clock Number "+str(i)+" : "+"Actor number "+str(z)+" is ready to get the inputs so inputs get into the actor.")
else:
if num != len(Actors[z].Input):
MainOutput.append("Clock Number "+str(i)+" : "+"All the inputs are not ready to enter the actor number "+str(z)+".")
if Actors[z].Busy != 0:
MainOutput.append("Clock Number "+str(i)+" : "+"Actor number "+str(z)+" is still busy.")
num = 0
return ContinueOrNot
# Main loop
EndTimes = []
StartTime = []
for i in range(0, NumberOfClocks):
for z in range(0, len(Actors)):
# Get from inputs
if getInput(Actors, z, i) == 1:
continue
# Shoot the answers
if z == len(Edges) and Actors[z].Input[0][1] == 1: # and Actors[kk-1].Input[0][1] > 0:
if i+4<NumberOfClocks:
EndTimes.append(i)
Actors[z].Input[0][1] = 0
if Actors[z].Busy == 1:
Actors[z].Busy = 0
for j in range(0, len(Actors[z].Output)):
kk = len(Actors)
index = Actors[z].Output[j][1]
if index == len(Actors):
continue
InputChangerIndex = 0
for ip in range(0, len(Actors[index].Input)):
if Actors[index].Input[ip][0] == Actors[z].ID:
InputChangerIndex = ip
Actors[index].Input[InputChangerIndex][1] = Actors[index].Input[InputChangerIndex][1] + Actors[z].Output[j][0]
# Get from inputs
if getInput(Actors, z, i) == 1:
continue
MainOutput.append("Clock Number "+str(i)+" : "+"Actor number "+str(z)+"shooted and now it is free.")
elif Actors[z].Busy == 0:
pass
MainOutput.append("Clock Number "+str(i)+" : "+"Actor number "+str(z)+" is still free.")
else:
Actors[z].Busy = Actors[z].Busy - 1
MainOutput.append("Clock Number "+str(i)+" : "+"Actor number "+str(z)+" is still busy and it can shoot in "+str(Actors[z].Busy)+" clocks later.")
# Make Start times right
num=0
print(len(StartTime))
print(len(EndTimes))
for i in PrimaryTokens:
for j in i:
if j>0:
num=num+j
for i in range(0, num):
StartTime.insert(0, -1)
for i in range(0, len(EndTimes)-len(StartTime)):
StartTime.insert(0, -1)
# Print the result
end = len(EndTimes)-1
for i in range(0, len(EndTimes)):
if(StartTime[i]==-1):
print("Token number " + str(i + 1) + " came out in Clock number " + str(EndTimes[i]) + " and after " + str(
EndTimes[i] / FrequencyOfClock) + " seconds based on the frequency you entered.")
else:
print("Token number " + str(i + 1) + " had got in, in clock number " + str(
StartTime[i]) + " and came out in Clock number " + str(EndTimes[i]) + " and after " + str(
EndTimes[i] / FrequencyOfClock) + " seconds based on the frequency you entered.")
if len(EndTimes)>num and PrimaryToken!=0:
print("Latency : "+str(EndTimes[num]-StartTime[num]))
if len(EndTimes)>num+1:
print("Throughput : 1/"+str(EndTimes[end] - EndTimes[end-1]))
else:
print("Throughput is not available for 1 token.")
else:
print("Latency and Throughput are not available for 0 token.")
# Chart of process of each Token
fig, gnt = plt.subplots()
gnt.set_ylim(0, len(StartTime))
gnt.set_xlim(0, NumberOfClocks-1)
gnt.set_xlabel('Number of clocks')
gnt.set_ylabel('Tokens')
gnt.grid(True)
for i in range(0, len(EndTimes)):
if(i % 2 == 0):
gnt.broken_barh([(StartTime[i], EndTimes[i]-StartTime[i])], (i, 0.9), facecolors=('tab:red'))
else:
gnt.broken_barh([(StartTime[i], EndTimes[i] - StartTime[i])], (i, 0.9))
plt.show()
# Write on file
with open('Output.txt','w') as f:pass
f.close()
with open('Output.txt','a') as f:
for i in MainOutput:
f.write(i+"\n")