-
Notifications
You must be signed in to change notification settings - Fork 2
/
heatmap.py
345 lines (290 loc) · 11.9 KB
/
heatmap.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
"""
Usage:
heatmap.py (-d <dir>) [-c <dir>] [-p <file>] [-n <file>] [-r <max-res>] [--3D]
heatmap.py (--help|-h)
Options:
-d <path> Path to design folder
-c <dir> Sub dir from <path> to a folder containing ClustersInstances.out
-p <file> Partition file with lines such as '<cell name> <O/1>' (.part)
-n <file> File with nets cut by partition (connectivity_partition.txt)
-r <max-res> Maximum pixel resolution. A 4x4 display uses max-res/2, /5 and /10 as well.
Default: 300
--3D Display 3D pins as yellow pixels, FRONT_BUMP instances
-h --help Print this help
"""
from math import *
import locale
import os
import datetime
import errno
import random
from docopt import docopt
import logging, logging.config
import numpy as np
import sys
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import statistics
import math
from alive_progress import alive_bar
try:
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
except locale.Error:
locale.setlocale(locale.LC_ALL, 'en_GB.UTF-8')
RANDOM_SEED = 0 # Set to 0 if no seed is used, otherwise set to seed value.
def loadDesign(display3DPins):
"""
CellSizes.out:
<cell name> <width [float]> <height [float]>
CellCoord.out:
<net name>, <cell name>, <x [float]>, <y [float]>
Parameters:
-----------
display3DPins : boolean
If true, load pins
Return
------
cells : {}
maxX : float
maxY : float
pins : []
"""
cells = {} # {cell name: [x,y,width, height]}
maxX = 0
maxY = 0
with open("CellCoord.out", 'r') as f:
lines = f.readlines()
for line in lines:
cells[line.split(',')[1]] = [float(line.split(',')[2]), float(line.split(',')[3])]
with open("CellSizes.out", 'r') as f:
lines = f.readlines()
for line in lines[1:]:
cellName = line.split(' ')[0]
if cellName in cells:
# Continue only if the cellName has a coordinate in the cells dictionary.
# It might happen that is does not, such as 3D bump in 3D ICs.
cells[cellName].extend([cells[cellName][0] + float(line.split(' ')[1]), cells[cellName][1] + float(line.split(' ')[2])])
maxX = max(maxX, cells[cellName][2])
maxY = max(maxY, cells[cellName][3])
pins = []
if display3DPins:
with open("uBumps.out", 'r') as f:
lines = f.readlines()
for line in lines[1:]:
pins.append([float(line.split(' ')[1]), float(line.split(' ')[2])])
return cells, maxX, maxY, pins
def generateHeatmap(cells, maxX, maxY, dimension=300, display3DPins=False, pins=None):
"""
Parameters
----------
cells : {cell name : [x1, y1, x2, y2]}
maxX : float
maxY : float
display3DPins : boolean
pins : [[x, y]]
"""
fig, axs = plt.subplots(2,2)
for counter, dimension in enumerate([dimension/10, dimension/5, dimension/2, dimension]):
imgW = math.floor(dimension)
imgH = int(imgW * (maxY/maxX))
data = np.zeros(shape=(imgW+2,imgH+2))
pinsX = []
pinsY = []
for coordinates in cells.values():
xl = coordinates[0]
xl = xl * imgW / maxX # Normalization
xl = math.floor(xl)
xu = math.ceil(coordinates[2] * imgW / maxX)
yl = math.floor(coordinates[1] * imgH / maxY)
yu = math.ceil(coordinates[3] * imgH / maxY)
for i in range(xl, xu+1):
for j in range(yl, yu+1):
data[i,j] += 1
if display3DPins:
for pin in pins:
pinsX.append(math.floor(pin[0] * imgW / maxX))
pinsY.append(math.floor(pin[1] * imgH / maxY))
logger.debug("max in data: {}".format(data.max()))
# https://matplotlib.org/gallery/images_contours_and_fields/pcolor_demo.html
x, y = np.mgrid[0:imgW+2:1, 0:imgH+2:1]
if counter == 0:
ax = axs[0, 0]
if counter == 1:
ax = axs[0, 1]
elif counter == 2:
ax = axs[1, 0]
elif counter == 3:
ax = axs[1, 1]
ax.set_xlim([0,imgW+2])
ax.set_ylim([0,imgH+2])
c = ax.pcolormesh(x, y, data, cmap='Reds', shading='auto', vmin=data.min(), vmax=data.max())
# ax.plot([10], marker='', color='y')
if display3DPins:
ax.scatter(pinsX, pinsY, marker='.', color='y', s=1)
ax.set_title('Resolution: {}'.format(floor(dimension)))
ax.axis('equal')
fig.colorbar(c, ax=ax)
fig.set_size_inches(11, 9)
fig.tight_layout()
plt.savefig('{}_{}_heatmap.png'.format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), "_".join(os.getcwd().split(os.sep)[-1].split('_')[2:])), dpi=150)
plt.show()
def colorizeClusters(clustDir, cells, dimension=1000):
"""
ClustersInstances.out should be formatted as follow:
<cluster ID> <instance 1> <...> <instance n>\n
Parameters
----------
clustDir : str
folder containing ClustersInstances.out
cells : {cell name : [x1, y1, x2, y2]}
"""
imgW = math.floor(dimension)
imgH = int(imgW * (maxY/maxX))
data = np.zeros(shape=(imgW+2,imgH+2))
with open(os.path.join(clustDir,"ClustersInstances.out"), 'r') as f:
lines = f.readlines()
for clusterID, line in enumerate(lines):
line = line.strip()
for cellName in line.split(' ')[1:]:
coordinates = cells[cellName]
xl = math.floor(coordinates[0] * imgW / maxX)
xu = math.ceil(coordinates[2] * imgW / maxX)
yl = math.floor(coordinates[1] * imgH / maxY)
yu = math.ceil(coordinates[3] * imgH / maxY)
for i in range(xl, xu+1):
for j in range(yl, yu+1):
data[i,j] = clusterID + 1 # +1 because I want the value 0 to still mean "there is nothing there"
x, y = np.mgrid[0:imgW+2:1, 0:imgH+2:1]
fig, ax = plt.subplots()
ax.set_xlim([0,imgW+2])
ax.set_ylim([0,imgH+2])
c = ax.pcolormesh(x, y, data, cmap='nipy_spectral', shading='auto', vmin=data.min(), vmax=data.max())
ax.set_title('Resolution: {}'.format(floor(dimension)))
ax.axis('equal')
fig.colorbar(c, ax=ax)
fig.tight_layout()
plt.savefig(os.path.join(clustDir,'{}_{}_clusters_heatmap.png'.format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), "_".join(os.getcwd().split(os.sep)[-1].split('_')[2:]))))
plt.show()
def colorizePartitions(partFile, cells, maxX, maxY, netCutFile, dimension=1000):
"""
"""
imgW = math.floor(dimension)
imgH = int(imgW * (maxY/maxX))
data = np.zeros(shape=(imgW+2,imgH+2))
partInstances = dict() # { instance name : 0/1}
with open(partFile, 'r') as f:
lines = f.readlines()
for line in lines:
line = line.strip()
cellName = line.split()[0]
partID = int(line.split()[1])
partInstances[cellName] = partID
coordinates = cells[cellName]
xl = math.floor(coordinates[0] * imgW / maxX)
xu = math.ceil(coordinates[2] * imgW / maxX)
yl = math.floor(coordinates[1] * imgH / maxY)
yu = math.ceil(coordinates[3] * imgH / maxY)
for i in range(xl, xu+1):
for j in range(yl, yu+1):
data[i,j] = partID + 1 # +1 because I want the value 0 to still mean "there is nothing there"
netPoints = dict()
if netCutFile:
netInstances = dict() # {net name : [instance names]}
with open(netCutFile, 'r') as f:
lines = f.readlines()
for net in lines[0].split(',')[3:]:
netInstances[net] = list()
with open("InstancesPerNet.out", 'r') as f:
lines = f.readlines()
for line in lines:
netName = line.split()[0]
if netName in netInstances.keys():
netInstances[netName] = line.split()[1:]
netPoints = dict() # {net name : [ [partition ID, (x, y)], ...] }
logger.info("Extracting cut net end points")
with alive_bar(len(netInstances)) as bar:
for net in netInstances:
bar()
netPoints[net] = list()
for instance in netInstances[net]:
coordinates = cells[instance]
xl = math.floor(coordinates[0] * imgW / maxX)
xu = math.ceil(coordinates[2] * imgW / maxX)
yl = math.floor(coordinates[1] * imgH / maxY)
yu = math.ceil(coordinates[3] * imgH / maxY)
center = (xl + (xu - xl)/2, yl + (yu - yl)/2)
netPoints[net].append([partInstances[instance], center])
x, y = np.mgrid[0:imgW+2:1, 0:imgH+2:1]
partDir = os.sep.join(partFile.split(os.sep)[:-1])
designName = "_".join(os.getcwd().split(os.sep)[-1].split('_')[2:])
fig, ax = plt.subplots(figsize=(15, 10))
ax.set_xlim([0,imgW+2])
ax.set_ylim([0,imgH+2])
c = ax.pcolormesh(x, y, data, cmap='binary', shading='auto', vmin=data.min(), vmax=data.max(), zorder=1)
logger.info("Plotting 3D nets")
with alive_bar(len(netPoints)) as bar:
for net in netPoints:
bar()
# Ugly patch to ignore clock net
if not "clk" in net and not "rst" in net and not "clock" in net:
# logger.debug("Net: {}, len: {}".format(net, len(netPoints[net])))
points = netPoints[net]
color = random.choice(list(mcolors.XKCD_COLORS))
for i in range(len(points)):
for j in range(i+1, len(points)):
# If points are on different partitions
if points[i][0] != points[j][0]:
xs = points[i][1][0] # Source
xd = points[j][1][0] # Destination
ys = points[i][1][1] # Source
yd = points[j][1][1] # Destination
ax.plot([xs, xd], [ys, yd], linewidth=1, color=color, zorder=10)
ax.set_title('Resolution: {}, {}'.format(floor(dimension), designName))
ax.axis('equal')
fig.colorbar(c, ax=ax)
fig.tight_layout()
plt.savefig(os.path.join(partDir,'{}_{}_clusters_heatmap.png'.format(datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S"), designName)))
plt.show()
if __name__ == "__main__":
output_dir = ""
clustDir = None
partFile = None
netCutFile = None
dimension=300
display3DPins = False
args = docopt(__doc__)
if args["-d"]:
output_dir = args["-d"]
if args["-c"]:
clustDir = args["-c"]
if args["-p"]:
partFile = args["-p"]
if args["-n"]:
netCutFile = args["-n"]
if args["-r"]:
dimension = int(args["-r"])
if args["--3D"]:
display3DPins = True
# Load base config from conf file.
logging.config.fileConfig('log.conf')
# Load logger from config
logger = logging.getLogger('default')
# Create new file handler
fh = logging.FileHandler(os.path.join(output_dir, 'heatmap_' + datetime.datetime.now().strftime("%Y-%m-%d_%H-%M-%S") + '.log'))
# Set a format for the file handler
fh.setFormatter(logging.Formatter('%(asctime)s: %(message)s'))
# Add the handler to the logger
logger.addHandler(fh)
logger.debug(args)
os.chdir(output_dir)
if args["-c"]:
clustDir = os.path.join(os.getcwd(),args["-c"])
logger.info("Working inside {}".format(output_dir))
logger.info("Seed: {}".format(RANDOM_SEED))
cells, maxX, maxY, pins = loadDesign(display3DPins)
if clustDir:
colorizeClusters(clustDir, cells,dimension)
elif partFile:
colorizePartitions(partFile, cells, maxX, maxY, netCutFile,dimension)
else:
generateHeatmap(cells, maxX, maxY,dimension, display3DPins, pins)