-
Notifications
You must be signed in to change notification settings - Fork 0
/
testbench_generator.py
executable file
·425 lines (382 loc) · 17.9 KB
/
testbench_generator.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
#!/usr/bin/python3
from network_generator import clog2
import pdb
import random
from math import ceil, log
import os
def clog2(num):
return int(ceil(log(num, 2)))
def create_packet_creator(num_leaves, injection_rate, traffic_pattern, injection_rate_slow):
import random
from datetime import datetime
random.seed(datetime.now())
num_msg_file = "./data/bench/" + traffic_pattern + "/" + str(num_leaves) + "/num_msg.txt"
num_msg_list = []
with open(num_msg_file,"r") as file:
for line in file:
num_msg = int(line.strip())
num_msg_list.append(num_msg)
assert(len(num_msg_list) == num_leaves)
code_string = ""
num_leaves_power_of_two = 2**clog2(num_leaves)
for i in range(num_leaves):
if(traffic_pattern.startswith("test_9") and i >= num_leaves_power_of_two//2): # slow injection rate in subtree2,3
injection_rate = injection_rate_slow
num_sent_per_leaf = num_msg_list[i]
code_packet_creator = '\n'.join([
'module packet_creator_' + str(i) + ' (',
'\tinput clk, ',
'\tinput reset,',
'\tinput resend,',
'\toutput reg [p_sz-1:0] bus_o',
'\t);',
'',
'\tparameter num_leaves= ' + str(num_leaves) + ';',
'\tparameter payload_sz = 1; // contains source address',
'\tparameter addr= 1\'b0;',
'\tparameter p_sz= 1 + $clog2(num_leaves) + payload_sz; // packet size',
'\tparameter num_sent_per_leaf= ' + str(num_sent_per_leaf) + ';',
'\t',
'\tparameter send_order_sz= $clog2(num_sent_per_leaf);',
'\treg [send_order_sz:0] i;',
'\treg send_out;',
'\tinteger seed= 123456;', # chagned to deterministic
# '\tinteger seed= ' + str(random.randint(0,2**32-1)) + ';',
'\treg [p_sz-1:0] data_to_be_sent[num_sent_per_leaf-1:0];', # changed
'',
'\tinitial begin',
'\t\t{i , send_out} <= 0;',
'\t\t$readmemb("./data/bench/' + traffic_pattern + '/' + str(num_leaves) + '/autogen_' + str(i) +'.trace", data_to_be_sent);',
'\tend',
'\talways @(posedge clk) begin',
'\t\tif (reset)',
'\t\t\t{i, send_out, bus_o} <= 0;',
'\t\telse if (!resend && send_out) begin',
'\t\t\tbus_o <= data_to_be_sent[i];',
'\t\t\ti <= i + 1;',
'\t\t\tif (i < num_sent_per_leaf-1) begin',
'\t\t\t\tsend_out <= (($random(seed) % ' + str(100 // injection_rate) +
') == 1\'b0);',
'\t\t\tend',
'\t\t\telse',
'\t\t\t\tsend_out <= 0;',
'\t\tend',
'\t\telse if (resend && send_out) begin',
'\t\t\tbus_o <= 0;',
'\t\tend',
'\t\telse begin',
'\t\t\tbus_o <= 0;',
'\t\t\tif (i < num_sent_per_leaf) begin',
'\t\t\t\tsend_out <= (($random(seed) % ' + str(100 // injection_rate) +
') == 1\'b0);',
'\t\t\tend',
'\t\t\telse',
'\t\t\t\tsend_out <= 0;',
'\t\tend',
'\tend',
'endmodule',
'',
''])
code_string = code_string + code_packet_creator
return code_string
def create_tb(traffic_pattern, num_leaves, injection_rate, addr_width, payload_sz):
num_leaves_power_of_two = 2**clog2(num_leaves)
num_msg_file = "./data/bench/" + traffic_pattern + "/" + str(num_leaves) + "/num_msg.txt"
num_msg_list = []
with open(num_msg_file,"r") as file:
for line in file:
num_msg = int(line.strip())
num_msg_list.append(num_msg)
assert(len(num_msg_list) == num_leaves)
avg_num_sent_per_leaf = sum(num_msg_list)//len(num_msg_list)
if(injection_rate < 50):
# wait_time = 50 + round(20*avg_num_sent_per_leaf*90*3) # relax the conservative wait time
wait_time = 50 + round(20*avg_num_sent_per_leaf*90*3) # relax the conservative wait time
else:
wait_time = 50 + round(20*avg_num_sent_per_leaf*90*100/injection_rate)
code_string = '\n'.join([
'module test;',
'',
'\tparameter num_leaves= ' + str(num_leaves) + ';',
'\tparameter payload_sz= ' + str(payload_sz) + ';',
'\tparameter p_sz= 1 + ' + str(addr_width) + ' + payload_sz;',
'',
'\tinteger k;',
'',
'\tinitial begin',
'\t\t$dumpfile("gen_nw.vcd");',
'\t\t$dumpvars(0, gen_nw_test);',
'\t\t$dumpvars(0, test);',
'\t\t$dumpvars(0, gen_pe[0]);',
'\t\tfor (k= 0; k < num_leaves; k= k + 1) begin',
'\t\t\t$dumpvars(0, test.pe_interface_arr[k]);',
'\t\t\t$dumpvars(0, test.interface_pe_arr[k]);',
'\t\t\tend',
'\t\t#' + str(wait_time),
'\t\t$finish;',
'\tend',
'',
'\treg clk;',
'\treg reset;',
'\t',
'\twire [num_leaves*p_sz-1:0] pe_interface;',
'\twire [num_leaves*p_sz-1:0] interface_pe;',
'\twire [p_sz-1:0] pe_interface_arr [num_leaves-1:0];',
'\twire [p_sz-1:0] interface_pe_arr [num_leaves-1:0];',
'\twire [num_leaves-1:0] resend;',
'',
'\tinteger i;',
'\tinitial begin',
'\t\t{clk, reset}= 2\'b01;',
'\t\t#50',
'\t\tfor (i= 0; i < 2^(100); i= i + 1) begin',
'\t\t\tif (i == 1) reset <= 0;',
'\t\t\tclk<= ~clk;',
'\t\t\t#10;',
'\t\tend',
'\tend',
'',
'\tgen_nw #(',
'\t\t.num_leaves(num_leaves),',
'\t\t.payload_sz(payload_sz),',
'\t\t.p_sz(p_sz),',
'\t\t.addr(1\'b0),',
'\t\t.level(0))',
'\t\tgen_nw_test(',
'\t\t\t.clk(clk),',
'\t\t\t.reset(reset),',
'\t\t\t.pe_interface(pe_interface),',
'\t\t\t.interface_pe(interface_pe),',
'\t\t\t.resend(resend));',
'',
'\tgenvar j;',
'\tgenerate',
'\tfor (j= 0; j < num_leaves; j=j+1) begin : gen_pe',
'\t\tassign pe_interface[j*p_sz+:p_sz]= pe_interface_arr[j];',
'\t\tassign interface_pe_arr[j]= interface_pe[j*p_sz+:p_sz];',
'\tend',
'\tendgenerate',
'',
'' ])
for i in range(num_leaves):
num_sent_per_leaf = num_msg_list[i]
code_tb = '\n'.join([
'\tpacket_creator_' + str(i) + ' #(',
'\t\t.num_leaves(num_leaves),',
'\t\t.payload_sz(payload_sz),',
'\t\t.addr(' + str(i) + '),',
'\t\t.p_sz(p_sz))',
'\t\tpc_' + str(i) + '(',
'\t\t\t.clk(clk),',
'\t\t\t.reset(reset),',
'\t\t\t.bus_o(pe_interface_arr[' + str(i) + ']),',
'\t\t\t.resend(resend[' + str(i) + ']));',
'',
'' ])
code_string = code_string + code_tb
code_end = '\n'.join([
'\tinitial $display("type\\tPE\\tpacket\\t\\ttime");',
'\tgenvar m;',
'\tgenerate',
'\tfor(m= 0; m < num_leaves; m= m + 1) begin',
'\t\talways @(posedge clk) begin',
'\t\t\tif (pe_interface_arr[m][p_sz-1])',
'\t\t\t\t$display("sent\\t%05d\\t%b\\t%0d", m, pe_interface_arr[m], $time);',
'\t\t\tif (interface_pe_arr[m][p_sz-1])',
'\t\t\t\t$display("rcvd\\t%05d\\t%b\\t%0d", m, interface_pe_arr[m], $time);',
#'\t\t\tif (gen_nw_test.subtree_left.subtree_left.subtree_left.bus_o[p_sz-1] == 1)',
#'\t\t\t\t$display("000_bus_o:\\t%05d\\t%b\\t%0d", m, gen_nw_test.subtree_left.subtree_left.subtree_left.bus_o, $time);',
'\t\tend',
'\tend',
'\tendgenerate',
'endmodule ',
''])
code_string = code_string + code_end
return code_string
def binary_output(width, data_in):
char_out = ''
for _ in range(0, width):
if(data_in % 2):
char_out += '1'
else:
char_out += '0'
data_in = data_in >> 1
return char_out[::-1]
# For now...
def get_local_subtree_lvl(num_leaves):
if(num_leaves<32):
return 2
elif(num_leaves<512):
return 3
# Create test pattern for Random traffic
def make_test_pattern(traffic_pattern, num_leaves, num_sent_per_leaf, addr_width, payload_width, injection_rate_slow):
num_leaves_power_of_two = 2**clog2(num_leaves)
data_width = payload_width - 11
dummy_width = 11 - addr_width
os.system("mkdir -p ./data/bench/" + traffic_pattern + "/" + str(num_leaves))
# write .trace files
for addr_src in range(num_leaves):
filedata = ""
file_name = "./data/bench/" + traffic_pattern + "/" + str(num_leaves) + "/autogen_" + str(addr_src) + ".trace"
if(traffic_pattern.startswith("test_9")):
# Below lines are for when N=24, etc, not power of two
num_valid_leaves_sparse = num_leaves - num_leaves_power_of_two//2
valid_leaves_s2s3 = get_valid_leaves_s2s3(num_leaves, num_valid_leaves_sparse)
# dense part, subtree_0 and subtree_1
if(addr_src < int(num_leaves_power_of_two/2)):
for data in range(0, num_sent_per_leaf):
addr_des = test_9(addr_src, num_leaves_power_of_two)
valid = "1"
packet = valid + binary_output(addr_width, addr_des) + binary_output(addr_width, addr_src) + \
binary_output(dummy_width, 0) + binary_output(data_width, data)
filedata += packet + "\n"
# sparse part, subtree_2 and subtree_3
else:
num_sent_per_leaf_slow = int(num_sent_per_leaf//(100/injection_rate_slow))
for data in range(0, num_sent_per_leaf_slow):
if(addr_src in valid_leaves_s2s3): # send to subtree_0/1
addr_des = test_9(addr_src, num_leaves_power_of_two)
valid = "1"
else:
addr_des = addr_src
valid = "0"
packet = valid + binary_output(addr_width, addr_des) + binary_output(addr_width, addr_src) + \
binary_output(dummy_width, 0) + binary_output(data_width, data)
filedata += packet + "\n"
elif(traffic_pattern.startswith("test_10")):
# num_valid_leaves_sparse = num_leaves - num_leaves_power_of_two//2
# valid_leaves_s2s3 = get_valid_leaves_s2s3(num_leaves, num_valid_leaves_sparse)
# densest part, subtree_0
if(addr_src < int(num_leaves_power_of_two//4)):
for data in range(0, num_sent_per_leaf):
valid_leaves_des = list(range(0, num_leaves_power_of_two//4))
valid_leaves_des.remove(addr_src)
addr_des = random.choice(valid_leaves_des)
valid = "1"
packet = valid + binary_output(addr_width, addr_des) + binary_output(addr_width, addr_src) + \
binary_output(dummy_width, 0) + binary_output(data_width, data)
filedata += packet + "\n"
# dense part, subtree_1
elif(int(num_leaves_power_of_two//4) <= addr_src and addr_src < int(num_leaves_power_of_two/2)):
num_sent_per_leaf_slow = int(num_sent_per_leaf//(100/injection_rate_slow))
for data in range(0, num_sent_per_leaf_slow):
valid_leaves_des = list(range(0, num_leaves_power_of_two//4))
# valid_leaves_des.remove(addr_src)
addr_des = random.choice(valid_leaves_des)
valid = "1"
packet = valid + binary_output(addr_width, addr_des) + binary_output(addr_width, addr_src) + \
binary_output(dummy_width, 0) + binary_output(data_width, data)
filedata += packet + "\n"
# sparse part, subtree_2 and subtree_3
else:
num_sent_per_leaf_slow = int(num_sent_per_leaf//(100/injection_rate_slow))
for data in range(0, num_sent_per_leaf_slow):
valid_leaves_des = list(range(0, num_leaves_power_of_two//4))
# valid_leaves_des.remove(addr_src)
addr_des = random.choice(valid_leaves_des)
valid = "1"
packet = valid + binary_output(addr_width, addr_des) + binary_output(addr_width, addr_src) + \
binary_output(dummy_width, 0) + binary_output(data_width, data)
filedata += packet + "\n"
else:
for data in range(0, num_sent_per_leaf):
if traffic_pattern == "test_0": # RANDOM
addr_des = test_0(addr_src,num_leaves)
valid = "1"
elif traffic_pattern == "test_3":
if(addr_src < num_leaves_power_of_two/2):
addr_des = test_3(addr_src, num_leaves_power_of_two)
valid = "1"
else:
addr_des = addr_src # invalid
valid = "0"
elif traffic_pattern == "test_5": # LOCAL, within 2**level subtree
local_subtree_level = get_local_subtree_lvl(num_leaves)
addr_des = test_5(addr_src, local_subtree_level)
valid = "1"
elif(traffic_pattern == 'test_7'): # RANDOM, 1/4 nodes in subtree_2,3 active
num_remaining_leaves = num_leaves - num_leaves_power_of_two/2
# only 1/4 active
valid_leaves_s2s3 = get_valid_leaves_s2s3(num_leaves, num_remaining_leaves//4)
valid_leaves_all = list(range(0,num_leaves_power_of_two//2)) + valid_leaves_s2s3
if(addr_src in valid_leaves_all):
valid_leaves_all.remove(addr_src) # except self
addr_des = random.choice(valid_leaves_all)
valid = "1"
else:
addr_des = addr_src
valid = "0"
packet = valid + binary_output(addr_width, addr_des) + binary_output(addr_width, addr_src) + \
binary_output(dummy_width, 0) + binary_output(data_width, data)
filedata += packet + "\n"
with open(file_name,"w") as file:
file.write(filedata)
# write num_msg.txt
data_lengths = ""
for addr_src in range(num_leaves):
file_name = "./data/bench/" + traffic_pattern + "/" + str(num_leaves) + "/autogen_" + str(addr_src) + ".trace"
max_data = 2**data_width-1
data = 0
with open(file_name,"r") as file:
for line in file:
val = line.strip()
if(val.startswith('1')): # valid data
data += 1
assert(data <= max_data) # if fails, change PACKET_SIZE
data_lengths += str(data) + "\n"
num_msg_file = "./data/bench/" + traffic_pattern + "/" + str(num_leaves) + "/num_msg.txt"
with open(num_msg_file,"w") as file:
file.write(data_lengths)
# It will return a list of valid leaves in subtree_2 and subtree_3
# that are the same distance away from each other, that
# is determined by the "num_valid_leaves_sparse"
def get_valid_leaves_s2s3(num_leaves, num_valid_leaves_sparse):
num_leaves_power_of_two = 2**clog2(num_leaves)
num_leaves_subtree_2 = num_leaves - num_leaves_power_of_two//2
n = 2**clog2(num_valid_leaves_sparse)
distance = num_leaves_subtree_2 // n
valid_leaves_list = []
idx_leaves = num_leaves_power_of_two//2
while(len(valid_leaves_list) < num_valid_leaves_sparse):
valid_leaves_list.append(idx_leaves)
idx_leaves = idx_leaves + distance
assert(len(valid_leaves_list) == num_valid_leaves_sparse)
for idx_leaves in valid_leaves_list:
assert(idx_leaves < num_leaves)
return valid_leaves_list
# test_0: random
def test_0(addr_src, num_leaves):
valid_leaves_des = list(range(0, num_leaves))
valid_leaves_des.remove(addr_src) # except self
return random.choice(valid_leaves_des)
# test_3: random, no node in subtree_2 and subtree_3
def test_3(addr_src, num_leaves_power_of_two):
valid_leaves_des = list(range(0, num_leaves_power_of_two//2))
valid_leaves_des.remove(addr_src) # except self
return random.choice(valid_leaves_des)
# test_5: local, communicate within only in 2^level subtree
def test_5(addr_src, level):
subtree_size = 2**level
for i in range(addr_src+1, addr_src+subtree_size+1):
if(i%subtree_size == 0):
idx_end = i
idx_start = idx_end-subtree_size
valid_leaves_des = list(range(idx_start,idx_end)) # leaves in subtree
valid_leaves_des.remove(addr_src) # except self
# print(valid_leaves_des)
return random.choice(valid_leaves_des)
# test_7: random, but 1/4 nodes in subtree_2/3 active
# test_9: random in subtree_0/1, and nodes in subtree_2/3 send to subtree_0/1 in low injection rate
# ok.. this function turns out to be the same as test_3
def test_9(addr_src, num_leaves_power_of_two):
valid_leaves_des = list(range(0, num_leaves_power_of_two//2))
if(addr_src in valid_leaves_des): # subtree_0/1 case
valid_leaves_des.remove(addr_src) # except self
return random.choice(valid_leaves_des)
# test_10: subtree_0 sends to subtree_0
# subtree_1,2,3 sends to subtree_0 in low injection rate
# def test_10(addr_src, num_leaves_power_of_two):
# valid_leaves_des = list(range(0, num_leaves_power_of_two//2))
# if(addr_src in valid_leaves_des): # subtree_0/1 case
# valid_leaves_des.remove(addr_src) # except self
# return random.choice(valid_leaves_des)