-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplot.py
More file actions
executable file
·324 lines (272 loc) · 9.88 KB
/
plot.py
File metadata and controls
executable file
·324 lines (272 loc) · 9.88 KB
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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
import argparse
import sys
class CSV(object):
def __init__(self, name = None, **kwargs):
self.__name = name
self.__file = None
self.__header = kwargs.pop("header", [])
@property
def name(self):
return self.__name
@property
def header(self):
return self.__header
@staticmethod
def __guess_type(s):
if not s:
return None
try:
return int(s)
except ValueError:
pass
try:
return float(s)
except ValueError:
pass
return s
def __enter__(self):
if self.__file is not None:
raise RuntimeError("CSV \'{}\' is already opened".format(self.__name))
self.__file = open(self.name, 'r')
line = self.__file.readline()
if line[-1] == '\n':
line = line[:-1]
self.__header = line.split(',')
return self
def __exit__(self, type, value, traceback):
if self.__file is None:
raise RuntimeError("CSV \'{}\' is not opened and cannot be closed".format(self.__name))
self.__file.close()
self.__file = None
def __iter__(self):
if self.__file is None:
raise RuntimeError("CSV must be within a context block to be iterated on")
def create_dict(row):
d = dict()
for h, r in zip(self.header, row):
d[h] = r
return d
for line in self.__file:
if line[-1] == '\n':
line = line[:-1]
row = (CSV.__guess_type(e) for e in line.split(','))
yield create_dict(row)
def __repr__(self):
return "CSV({!r})".format(self.name)
filename = 'bw.csv'
inf = float("inf")
xmin = inf
xmax = -inf
ymin = inf
ymax = -inf
ylog = True
parser = argparse.ArgumentParser(description='Plots the bandwidth of a machine from CSV file')
parser.add_argument('filename', type=str, help='CSV file', action='store', nargs='?', default='/dev/stdin')
parser.add_argument('--L1', type=str, default="0", help='L1 cache size in bytes ("xx KB", "xx MB" & "xx GB" are accepted)')
parser.add_argument('--L2', type=str, default="0", help='L2 cache size in bytes ("xx KB", "xx MB" & "xx GB" are accepted)')
parser.add_argument('--L3', type=str, default="0", help='L3 cache size in bytes ("xx KB", "xx MB" & "xx GB" are accepted)')
parser.add_argument('--L4', type=str, default="0", help='L4 cache size in bytes ("xx KB", "xx MB" & "xx GB" are accepted)')
group = parser.add_mutually_exclusive_group()
group.add_argument('-l', '--linear', dest='linear', default=False, help='linear scale for Y axis', action='store_true')
group.add_argument('-L', '--log', dest='linear', help='log scale for Y axis', action='store_false')
group = parser.add_mutually_exclusive_group()
group.add_argument('--legend', dest='legend', default=True, help='Print the legend', action='store_true')
group.add_argument('--no-legend', dest='legend', help='Do not print the legend', action='store_false')
parser.add_argument('-m', '--machine', type=str, default='', help='Machine name')
parser.add_argument('-t', '--title', type=str, default='', help='Plot title')
parser.add_argument('-T', '--type', type=str, default='f32', help='plot only type')
parser.add_argument('-c', '--columns', type=str, default='', help='columns to plot')
parser.add_argument('--xscale', type=float, default=1., help='Scaling of the values in X')
parser.add_argument('--yscale', type=float, default=1., help='Scaling of the values in Y')
parser.add_argument('--xlabel', type=str, default='', help='')
parser.add_argument('--ylabel', type=str, default='', help='')
parser.add_argument('-o', '--out', type=str, default='', help='output filename')
parser.add_argument('-s', '--speedup', type=str, default='', help='show speedup for this column')
args = parser.parse_args()
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.ticker as ticker
#matplotlib.rcParams['axes.autolimit_mode'] = 'round_numbers'
matplotlib.rcParams['patch.force_edgecolor'] = True
matplotlib.rcParams['patch.facecolor'] = 'b'
matplotlib.rcParams['legend.fancybox'] = False
#matplotlib.rcParams['legend.loc'] = 'upper right'
matplotlib.rcParams['legend.numpoints'] = 1
matplotlib.rcParams['legend.fontsize'] = 'large'
matplotlib.rcParams['legend.framealpha'] = None
matplotlib.rcParams['legend.scatterpoints'] = 3
matplotlib.rcParams['legend.edgecolor'] = 'inherit'
#matplotlib.rcParams['axes.autolimit_mode'] = 'round_numbers'
#matplotlib.rcParams['axes.xmargin'] = 0
#matplotlib.rcParams['axes.ymargin'] = 0
#matplotlib.rcParams['xtick.direction'] = 'inout'
#matplotlib.rcParams['ytick.direction'] = 'inout'
matplotlib.rcParams['xtick.direction'] = 'in'
matplotlib.rcParams['ytick.direction'] = 'in'
matplotlib.rcParams['xtick.top'] = True
matplotlib.rcParams['ytick.right'] = True
matplotlib.rcParams['axes.grid'] = True
matplotlib.rcParams['grid.linewidth'] = 0.5
matplotlib.rcParams['grid.color'] = '#000000'
matplotlib.rcParams['grid.alpha'] = 2./15. # 0.133
#matplotlib.rcParams['font.family'] = 'Latin Modern Roman'
matplotlib.rcParams['mathtext.fontset'] = 'cm'
matplotlib.rcParams['axes.formatter.useoffset'] = False
def human2bytes(s):
res = s.split()
value = float(res[0])
if len(res) > 1:
if res[1] == "B":
value = value
elif res[1] == "KB":
value = value * 1024
elif res[1] == "MB":
value = value * 1024 * 1024
elif res[1] == "GB":
value = value * 1024 * 1024 * 1024
else:
raise RuntimeError("Unsupported format:'" + s + "'")
return value;
raw_caches = [0., human2bytes(args.L1)*args.xscale, human2bytes(args.L2)*args.xscale, human2bytes(args.L3)*args.xscale, human2bytes(args.L4)*args.xscale]
caches = [0.]
last_cache = 0
for i in range(1, len(raw_caches)):
c = raw_caches[i]
if not c:
continue
if last_cache != i-1:
sys.stderr.write('L{} cannot be set if L{} is not set\n'.format(i, i-1))
exit(1)
if c <= raw_caches[last_cache]:
sys.stderr.write('L{} must be bigger than L{}\n'.format(i, i-1))
exit(1)
last_cache = i
caches.append(c)
ylog = not args.linear
machine_name = args.machine
title = args.title or (args.machine and "{} bandwidth".format(machine_name)) or ""
series = dict()
class Rset(object):
def __init__(self, l):
self.__set = set(l)
def __contains__(self, v):
return v not in self.__set
def __repr__(self):
return 'Rset({!r})'.format(self.__set)
def __str__(self):
return repr(self)
csv = CSV(args.filename)
currenttype = args.type or None
ignore = {'type'}
if args.columns:
ignore = Rset(args.columns.split(","))
Xname = ''
X = []
with csv:
for n in csv.header:
series[n] = []
Xname = next((h for h in csv.header if h not in {'type'}))
for row in csv:
if currenttype is None:
currenttype = row["type"]
elif currenttype != row["type"]:
continue
x = row[Xname] * args.xscale
xmin = min(xmin, x)
xmax = max(xmax, x)
X.append(x)
for n in csv.header:
if n != Xname and n not in ignore and n in row:
val = row[n] * args.yscale
if val != 0:
ymin = min(ymin, val)
ymax = max(ymax, val)
series[n].append(val)
ram_bw = None
if args.speedup:
s = series[args.speedup][-10:]
if len(s) > 0:
ram_bw = sum(s) / len(s)
if args.out:
fig = plt.figure(figsize=[6.,4.])
else:
fig = plt.figure()
ax = plt.subplot(111)
if title:
plt.title(title)
if args.xlabel:
plt.xlabel(args.xlabel)
if args.ylabel:
plt.ylabel(args.ylabel)
if ram_bw:
ax.plot([xmin, xmax], [ram_bw, ram_bw], linewidth=1., linestyle=':', color='black')
for n in (h for h in csv.header if h not in ignore and h != Xname):
ax.plot(X, series[n], label=n)
ax.set_xlim(xmin, xmax)
plt.xscale("log")
_, top = ax.get_ylim()
if ylog:
ax.set_ylim(ymin/1.2, ymax*1.4)
top = ymax*1.4
plt.yscale("log")
else:
ax.set_ylim(bottom=0.)
def bytes_fmt(v, *args):
if v == 0.:
return "0 B"
letters = 'fpnum KMGTPE'
i = letters.index(' ')
while (i > 0 and v < 0.999):
v *= 1000.
i -= 1
while (i < len(letters)-1 and v > 999.):
v /= 1000.
i += 1
return "{:g} {}B".format(v, letters[i])
def y_formatter(v, *args):
if "{:g}".format(v)[0] in '689':
return ''
return bytes_fmt(v, *args) + "/s"
formatter = ticker.FuncFormatter
ax.xaxis.set_major_formatter(ticker.FuncFormatter(bytes_fmt))
ax.xaxis.set_minor_formatter(ticker.NullFormatter())
ax.yaxis.set_minor_formatter(ticker.FuncFormatter(y_formatter))
ax.yaxis.set_major_formatter(ticker.FuncFormatter(y_formatter))
if ram_bw:
import bisect
if len(caches) > 1:
lastx = xmin
if not ylog:
ymin /= 2
a = 0.1
colors = ['black', 'green', 'blue', 'yellow', 'orange', 'red']
for i in range(1, len(caches)):
x = caches[i]
xmid = (lastx*x)**0.5
ax.axvspan(lastx, x, alpha=a, color=colors[i], linewidth=0.)
ax.annotate("L{}".format(i), xy=(xmid, top), xycoords='data', xytext=(0., -5.), textcoords='offset points', horizontalalignment='center', verticalalignment='top')
if ram_bw:
i = bisect.bisect_left(X, xmid)
ys = series[args.speedup][i-5:i+5]
if len(ys) > 0:
y = sum(ys) / len(ys)
if ylog:
ymid = (ram_bw * y)**0.5
else:
ymid = (ram_bw + y)*0.5
ax.annotate("", (xmid, ram_bw), (xmid, y), arrowprops=dict(arrowstyle='<->', linewidth=0.5))
plt.annotate(u"×{:0.2g}".format(y / ram_bw), xy=(xmid, ymid), xycoords='data', xytext=(3., 0.), textcoords='offset points', horizontalalignment='left', verticalalignment='center')
lastx = x
ax.axvspan(lastx, xmax, alpha=a, color=colors[-1], linewidth=0.)
ax.annotate("RAM", xy=((lastx*xmax)**0.5, top), xycoords='data', xytext=(0., -5.), textcoords='offset points', horizontalalignment='center', verticalalignment='top')
ax.grid(visible=True, which='major', color="black", alpha=4./30., linewidth=0.5)
ax.grid(visible=True, which='minor', color="black", alpha=1.5/30., linewidth=0.5)
extra_artists = []
if args.legend:
extra_artists = [plt.legend()]
if args.out:
plt.savefig(args.out, bbox_extra_artists=extra_artists, bbox_inches='tight', transparent=True)
else:
plt.show()