-
Notifications
You must be signed in to change notification settings - Fork 3
/
matmult.py
executable file
·89 lines (74 loc) · 2.59 KB
/
matmult.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
#!/usr/bin/python
#
# (C) 2017 Riad S. Wahby <rsw@cs.nyu.edu>
#
# generate a matrix multiply PWS
import sys
def print_dp(r, c, matdim, voffset, fh):
# dot product in row-major format
dpvals = []
matsize = matdim * matdim
fh.write("// row %d, col %d\n" % (r, c))
for dpval in xrange(0, matdim):
rowidx = r * matdim + dpval
colidx = dpval * matdim + c + matsize
fh.write("P V%d = V%d * V%d E\n" % (voffset, rowidx, colidx))
dpvals.append(voffset)
voffset += 1
# add tree
while len(dpvals) > 1:
outs = []
for idx in xrange(0, len(dpvals)//2):
fh.write("P V%d = V%d + V%d E\n" % (voffset, dpvals[2*idx], dpvals[2*idx+1]))
outs.append(voffset)
voffset += 1
if len(dpvals) % 2 != 0:
outs.append(dpvals[-1])
dpvals = outs
return (dpvals[0], voffset)
def print_pws(matdim, fh):
# inputs
voffset = 2 * matdim * matdim
for inidx in xrange(0, voffset):
fh.write("P V%d = I%d E\n" % (inidx, inidx))
outs = []
for r in xrange(0, matdim):
for c in xrange(0, matdim):
(out, voffset) = print_dp(r, c, matdim, voffset, fh)
outs.append(out)
# write output
for outidx in outs:
fh.write("P O%d = V%d E\n" % (voffset, outidx))
voffset += 1
def print_rdl(matdim, fh):
# RDL routes inputs (in row-major format)
# inputs
voffset = 2 * matdim * matdim
for inidx in xrange(0, voffset):
fh.write("P V%d = I%d E\n" % (inidx, inidx))
# wire inputs to subckts
for nrow in xrange(0, matdim):
row_offset = nrow * matdim
for ncol in xrange(0, matdim):
# write the row inputs
for ridx in xrange(0, matdim):
row_ent = row_offset + ridx
fh.write("P V%d = V%d PASS V%d E\n" % (voffset, row_ent, row_ent))
voffset += 1
# write the column inputs
for cidx in xrange(0, matdim):
col_ent = ncol + cidx * matdim
fh.write("P V%d = V%d PASS V%d E\n" % (voffset, col_ent, col_ent))
voffset += 1
# outputs
assert voffset == 2 * matdim * matdim + 2 * matdim * matdim * matdim
for oval in xrange(0, 2 * matdim * matdim * matdim):
fh.write("P O%d = V%d E\n" % (voffset, oval + 2 * matdim * matdim))
voffset += 1
if __name__ == "__main__":
if len(sys.argv) < 2:
print "Usage: %s <n>" % sys.argv[0]
sys.exit(-1)
md = int(sys.argv[1])
with open("matmult_%d.pws" % md, 'w') as f:
print_pws(md, f)