-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmult.py
88 lines (71 loc) · 2.8 KB
/
mult.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
from fxpmath import Fxp
import random as r
def transpose(b):
result = [[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0],
[0, 0, 0, 0]]
# iterate through rows
for i in range(len(b)):
# iterate through columns
for j in range(len(b[0])):
result[j][i] = b[i][j]
return result
def file_Write(f,a):
# write matrix a
for i in range(4):
for j in range(4):
f.write(bin(a[i][j])[2:].zfill(8) + "")
f.write("\n")
#open file to write
f = open("matmul_dpu_tb.txt", "w") #input vector file
f1 = open("output_dpu_tb.txt", "w") #output file / Results file
for test in range(0,20,1):
#create a random 4 by 4 array with each value 8 bits max
a = Fxp([[r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)], [r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)], [r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)], [r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)]], signed=False, n_word=8, n_frac=0)
b = Fxp([[r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)], [r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)], [r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)], [r.randint(0, 255), r.randint(0, 255), r.randint(0, 255), r.randint(0, 255)]], signed=False, n_word=8, n_frac=0)
#multiply matrix a and b
c = a.dot(b)
#only save the integer part of the result
a = a.astype(int)
b = b.astype(int)
c = c.astype(int)
#save matrix a 1 row per line ... 4 values in row ... also converted into 8 bit respective value
file_Write(f,a)
#Tacking transpose of b, as on dpu side we dont have to deal with [row * col] we can just [row * row]
tran_b = transpose(b)
file_Write(f, tran_b)
#write matrix c : Each Line has only 1 result, out of 16, of a 4 by 4 matrix
for i in range(4):
for j in range(4):
f1.write(bin(c[i][j])[2:].zfill(25) + "\n")
# f1.write("\n")
f.close()
f1.close()
"""
print("Matrix A B C")
print(a)
print(b)
print(c)
# # write matrix a
# for i in range(4):
# for j in range(4):
# f.write(bin(a[i][j])[2:].zfill(8) + "")
# f.write("\n")
#Tacking transpose of matrix b
# result = [[0,0,0,0],
# [0,0,0,0],
# [0,0,0,0],
# [0,0,0,0] ]
#
# # iterate through rows
# for i in range(len(b)):
# # iterate through columns
# for j in range(len(b[0])):
# result[j][i] = b[i][j]
# # write matrix b
# for i in range(4):
# for j in range(4):
# f.write(bin(tran_b[i][j])[2:].zfill(8) + "")
# f.write("\n")
"""