Debian Codegraph @

src/c-blosc2/bench/plot-speeds.py

raw
1 """
2 Copyright (c) 2021 Blosc Development Team <[email protected]>
3 https://blosc.org
4 License: BSD 3-Clause (see LICENSE.txt)
5
6 Script for plotting the results of the 'suite' benchmark.
7 Invoke without parameters for usage hints.
8 """
9 import matplotlib as mpl
10 from pylab import *
11
12 KB_ = 1024
13 MB_ = 1024*KB_
14 GB_ = 1024*MB_
15 NCHUNKS = 128 # keep in sync with bench.c
16
17 linewidth=2
18 #markers= ['+', ',', 'o', '.', 's', 'v', 'x', '>', '<', '^']
19 #markers= [ 'x', '+', 'o', 's', 'v', '^', '>', '<', ]
20 markers= [ 's', 'o', 'v', '^', '+', 'x', '>', '<', '.', ',' ]
21 markersize = 8
22
23 def get_values(filename):
24 f = open(filename)
25 values = {"memcpyw": [], "memcpyr": []}
26
27 for line in f:
28 if line.startswith('-->'):
29 tmp = line.split('-->')[1]
30 nthreads, size, elsize, sbits, codec, shuffle = [i for i in tmp.split(', ')]
31 nthreads, size, elsize, sbits = map(int, (nthreads, size, elsize, sbits))
32 values["size"] = size / MB_
33 values["elsize"] = elsize
34 values["sbits"] = sbits
35 values["codec"] = codec
36 values["shuffle"] = shuffle
37 # New run for nthreads
38 (ratios, speedsw, speedsr) = ([], [], [])
39 # Add a new entry for (ratios, speedw, speedr)
40 values[nthreads] = (ratios, speedsw, speedsr)
41 #print("-->", nthreads, size, elsize, sbits)
42 elif line.startswith('memcpy(write):'):
43 tmp = line.split(',')[1]
44 memcpyw = float(tmp.split(' ')[1]) / 1024.
45 values["memcpyw"].append(memcpyw)
46 elif line.startswith('memcpy(read):'):
47 tmp = line.split(',')[1]
48 memcpyr = float(tmp.split(' ')[1]) / 1024.
49 values["memcpyr"].append(memcpyr)
50 elif line.startswith('comp(write):'):
51 tmp = line.split(',')[1]
52 speedw = float(tmp.split(' ')[1]) / 1024.
53 ratio = float(line.split(':')[-1])
54 speedsw.append(speedw)
55 ratios.append(ratio)
56 elif line.startswith('decomp(read):'):
57 tmp = line.split(',')[1]
58 speedr = float(tmp.split(' ')[1]) / 1024.
59 speedsr.append(speedr)
60 if "OK" not in line:
61 print("WARNING! OK not found in decomp line!")
62
63 f.close()
64 return nthreads, values
65
66
67 def show_plot(plots, yaxis, legends, gtitle, xmax=None, ymax=None):
68 xlabel('Compresssion ratio')
69 ylabel('Speed (GB/s)')
70 title(gtitle)
71 xlim(0, xmax)
72 ylim(0, ymax)
73 grid(True)
74
75 # legends = [f[f.find('-'):f.index('.out')] for f in filenames]
76 # legends = [l.replace('-', ' ') for l in legends]
77 #legend([p[0] for p in plots], legends, loc = "upper left")
78 legend([p[0] for p in plots
79 if not isinstance(p, mpl.lines.Line2D)],
80 legends, loc = "best")
81
82
83 #subplots_adjust(bottom=0.2, top=None, wspace=0.2, hspace=0.2)
84 if outfile:
85 print("Saving plot to:", outfile)
86 savefig(outfile, dpi=100)
87 else:
88 show()
89
90 if __name__ == '__main__':
91
92 from optparse import OptionParser
93
94 usage = "usage: %prog [-r] [-o outfile] [-t title ] [-d|-c] filename"
95 compress_title = 'Compression speed'
96 decompress_title = 'Decompression speed'
97 yaxis = 'No axis name'
98
99 parser = OptionParser(usage=usage)
100 parser.add_option('-o',
101 '--outfile',
102 dest='outfile',
103 help=('filename for output (many extensions '
104 'supported, e.g. .png, .jpg, .pdf)'))
105
106 parser.add_option('-t',
107 '--title',
108 dest='title',
109 help='title of the plot',)
110
111 parser.add_option('-l',
112 '--limit',
113 dest='limit',
114 help='expression to limit number of threads shown',)
115
116 parser.add_option('-x',
117 '--xmax',
118 dest='xmax',
119 help='limit the x-axis',
120 default=None)
121
122 parser.add_option('-y',
123 '--ymax',
124 dest='ymax',
125 help='limit the y-axis',
126 default=None)
127
128 parser.add_option('-r', '--report', action='store_true',
129 dest='report',
130 help='generate file for reporting ',
131 default=False)
132
133 parser.add_option('-d', '--decompress', action='store_true',
134 dest='dspeed',
135 help='plot decompression data',
136 default=False)
137 parser.add_option('-c', '--compress', action='store_true',
138 dest='cspeed',
139 help='plot compression data',
140 default=False)
141
142 (options, args) = parser.parse_args()
143 if len(args) == 0:
144 parser.error("No input arguments")
145 elif len(args) > 1:
146 parser.error("Too many input arguments")
147 else:
148 pass
149
150 if options.report and options.outfile:
151 parser.error("Can only select one of [-r, -o]")
152
153 if options.dspeed and options.cspeed:
154 parser.error("Can only select one of [-d, -c]")
155 elif options.cspeed:
156 options.dspeed = False
157 plot_title = compress_title
158 else: # either neither or dspeed
159 options.dspeed = True
160 plot_title = decompress_title
161
162 filename = args[0]
163 cspeed = options.cspeed
164 dspeed = options.dspeed
165 if options.outfile:
166 outfile = options.outfile
167 elif options.report:
168 if cspeed:
169 outfile = filename[:filename.rindex('.')] + '-compr.png'
170 else:
171 outfile = filename[:filename.rindex('.')] + '-decompr.png'
172 else:
173 outfile = None
174
175 plots = []
176 legends = []
177 nthreads, values = get_values(filename)
178 #print("Values:", values)
179
180 if options.limit:
181 thread_range = eval(options.limit)
182 else:
183 thread_range = range(1, nthreads+1)
184
185 if options.title:
186 plot_title = options.title
187 else:
188 plot_title += " (%(size).1f MB, %(elsize)d bytes, %(sbits)d bits), %(codec)s, %(shuffle)s" % values
189
190 gtitle = plot_title
191
192 for nt in thread_range:
193 #print("Values for %s threads --> %s" % (nt, values[nt]))
194 (ratios, speedw, speedr) = values[nt]
195 if cspeed:
196 speed = speedw
197 else:
198 speed = speedr
199 #plot_ = semilogx(ratios, speed, linewidth=2)
200 plot_ = plot(ratios, speed, linewidth=2)
201 plots.append(plot_)
202 nmarker = nt
203 if nt >= len(markers):
204 nmarker = nt%len(markers)
205 setp(plot_, marker=markers[nmarker], markersize=markersize,
206 linewidth=linewidth)
207 legends.append("%d threads" % nt)
208
209 # Add memcpy lines
210 # Take the first value found in order to avoid cache effects
211 if cspeed:
212 # memcpy_speed = np.mean(values["memcpyw"])
213 memcpy_speed = values["memcpyw"][0]
214 message = "memcpy (write to memory)"
215 else:
216 # memcpy_speed = np.mean(values["memcpyr"])
217 memcpy_speed = values["memcpyr"][0]
218 message = "memcpy (read from memory)"
219 plot_ = axhline(memcpy_speed, linewidth=3, linestyle='-.', color='black')
220 text(4.0, memcpy_speed + .5, message)
221 plots.append(plot_)
222 show_plot(plots, yaxis, legends, gtitle,
223 xmax=int(options.xmax) if options.xmax else None,
224 ymax=int(options.ymax) if options.ymax else None)
225