This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 150
/
dram_trace.py
212 lines (159 loc) · 6.86 KB
/
dram_trace.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
import os.path
import math
from tqdm import tqdm
def prune(input_list):
l = []
for e in input_list:
e = e.strip()
if e != '' and e != ' ':
l.append(e)
return l
def dram_trace_read_v2(
sram_sz = 512 * 1024,
word_sz_bytes = 1,
min_addr = 0, max_addr=1000000,
default_read_bw = 10, # this is arbitrary
sram_trace_file = "sram_log.csv",
dram_trace_file = "dram_log.csv"
):
t_fill_start = -1
t_drain_start = 0
init_bw = default_read_bw # Taking an arbitrary initial bw of 4 bytes per cycle
sram = set()
sram_requests = open(sram_trace_file, 'r')
dram = open(dram_trace_file, 'w')
#for entry in tqdm(sram_requests):
for entry in sram_requests:
elems = entry.strip().split(',')
elems = prune(elems)
elems = [float(x) for x in elems]
clk = elems[0]
for e in range(1, len(elems)):
if (elems[e] not in sram) and (elems[e] >= min_addr) and (elems[e] < max_addr):
# Used up all the unique data in the SRAM?
if len(sram) + word_sz_bytes > sram_sz:
if t_fill_start == -1:
t_fill_start = t_drain_start - math.ceil(len(sram) / (init_bw * word_sz_bytes))
# Generate the filling trace from time t_fill_start to t_drain_start
cycles_needed = t_drain_start - t_fill_start
words_per_cycle = math.ceil(len(sram) / (cycles_needed * word_sz_bytes))
c = t_fill_start
while len(sram) > 0:
trace = str(c) + ", "
for _ in range(words_per_cycle):
if len(sram) > 0:
p = sram.pop()
trace += str(p) + ", "
trace += "\n"
dram.write(trace)
c += 1
t_fill_start = t_drain_start
t_drain_start = clk
# Add the new element to sram
sram.add(elems[e])
if len(sram) > 0:
if t_fill_start == -1:
t_fill_start = t_drain_start - math.ceil(len(sram) / (init_bw * word_sz_bytes))
# Generate the filling trace from time t_fill_start to t_drain_start
cycles_needed = t_drain_start - t_fill_start
words_per_cycle = math.ceil(len(sram) / (cycles_needed * word_sz_bytes))
c = t_fill_start
while len(sram) > 0:
trace = str(c) + ", "
for _ in range(words_per_cycle):
if len(sram) > 0:
p = sram.pop()
trace += str(p) + ", "
trace += "\n"
dram.write(trace)
c += 1
sram_requests.close()
dram.close()
def dram_trace_write(ofmap_sram_size = 64,
data_width_bytes = 1,
default_write_bw = 10, # this is arbitrary
sram_write_trace_file = "sram_write.csv",
dram_write_trace_file = "dram_write.csv"):
traffic = open(sram_write_trace_file, 'r')
trace_file = open(dram_write_trace_file, 'w')
last_clk = 0
clk = 0
sram_buffer = [set(), set()]
filling_buf = 0
draining_buf = 1
for row in traffic:
elems = row.strip().split(',')
elems = prune(elems)
elems = [float(x) for x in elems]
clk = elems[0]
# If enough space is in the filling buffer
# Keep filling the buffer
if (len(sram_buffer[filling_buf]) + (len(elems) - 1) * data_width_bytes ) < ofmap_sram_size:
for i in range(1,len(elems)):
sram_buffer[filling_buf].add(elems[i])
# Filling buffer is full, spill the data to the other buffer
else:
# If there is data in the draining buffer
# drain it
#print("Draining data. CLK = " + str(clk))
if len(sram_buffer[draining_buf]) > 0:
delta_clks = clk - last_clk
data_per_clk = math.ceil(len(sram_buffer[draining_buf]) / delta_clks)
#print("Data per clk = " + str(data_per_clk))
# Drain the data
c = last_clk + 1
while len(sram_buffer[draining_buf]) > 0:
trace = str(c) + ", "
c += 1
for _ in range(int(data_per_clk)):
if len(sram_buffer[draining_buf]) > 0:
addr = sram_buffer[draining_buf].pop()
trace += str(addr) + ", "
trace_file.write(trace + "\n")
#Swap the ids for drain buffer and fill buffer
tmp = draining_buf
draining_buf = filling_buf
filling_buf = tmp
#Set the last clk value
last_clk = clk
#Fill the new data now
for i in range(1,len(elems)):
sram_buffer[filling_buf].add(elems[i])
#Drain the last fill buffer
reasonable_clk = clk
if len(sram_buffer[draining_buf]) > 0:
#delta_clks = clk - last_clk
#data_per_clk = math.ceil(len(sram_buffer[draining_buf]) / delta_clks)
data_per_clk = default_write_bw
#print("Data per clk = " + str(data_per_clk))
# Drain the data
c = last_clk + 1
while len(sram_buffer[draining_buf]) > 0:
trace = str(c) + ", "
c += 1
for _ in range(int(data_per_clk)):
if len(sram_buffer[draining_buf]) > 0:
addr = sram_buffer[draining_buf].pop()
trace += str(addr) + ", "
trace_file.write(trace + "\n")
reasonable_clk = max(c, clk)
if len(sram_buffer[filling_buf]) > 0:
data_per_clk = default_write_bw
# Drain the data
c = reasonable_clk + 1
while len(sram_buffer[filling_buf]) > 0:
trace = str(c)+ ", "
c += 1
for _ in range(int(data_per_clk)):
if len(sram_buffer[filling_buf]) > 0:
addr = sram_buffer[filling_buf].pop()
trace += str(addr) + ", "
trace_file.write(trace + "\n")
#All traces done
traffic.close()
trace_file.close()
if __name__ == "__main__":
dram_trace_read_v2(min_addr=0, max_addr=1000000, dram_trace_file="ifmaps_dram_read.csv")
dram_trace_read_v2(min_addr=1000000, max_addr=100000000, dram_trace_file="filter_dram_read.csv")
#dram_trace_read(filter_sram_sz=1024, ifmap_sram_sz=1024, sram_trace_file="sram_read.csv")
#dram_trace_write(ofmap_sram_size=1024,sram_write_trace_file="yolo_tiny_layer1_write.csv", dram_write_trace_file="yolo_tiny_layer1_dram_write.csv")