Debian Codegraph @

src/scripts/perf/plotPerformance.py

raw
1 # ########################################################################
2 # Copyright 2013 Advanced Micro Devices, Inc.
3 #
4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
7 #
8 # http://www.apache.org/licenses/LICENSE-2.0
9 #
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
15 # ########################################################################
16
17 # to use this script, you will need to download and install the 32-BIT VERSION of:
18 # - Python 2.7 x86 (32-bit) - http://www.python.org/download/releases/2.7.1
19 #
20 # you will also need the 32-BIT VERSIONS of the following packages as not all the packages are available in 64bit at the time of this writing
21 # The ActiveState python distribution is recommended for windows
22 # (make sure to get the python 2.7-compatible packages):
23 # - NumPy 1.5.1 (32-bit, 64-bit unofficial, supports Python 2.4 - 2.7 and 3.1 - 3.2.) - http://sourceforge.net/projects/numpy/files/NumPy/
24 # - matplotlib 1.0.1 (32-bit & 64-bit, supports Python 2.4 - 2.7) - http://sourceforge.net/projects/matplotlib/files/matplotlib/
25 #
26 # For ActiveState Python, all that one should need to type is 'pypm install matplotlib'
27
28 import datetime
29 import sys
30 import argparse
31 import subprocess
32 import itertools
33 import os
34 import matplotlib
35 import pylab
36 from matplotlib.backends.backend_pdf import PdfPages
37 from fftPerformanceTesting import *
38
39 def plotGraph(dataForAllPlots, title, plottype, plotkwargs, xaxislabel, yaxislabel):
40 """
41 display a pretty graph
42 """
43 dh.write('Making graph\n')
44 colors = ['k','y','m','c','b','r','g']
45 #plottype = 'plot'
46 for thisPlot in dataForAllPlots:
47 getattr(pylab, plottype)(thisPlot.xdata, thisPlot.ydata,
48 '{}.-'.format(colors.pop()),
49 label=thisPlot.label, **plotkwargs)
50 if len(dataForAllPlots) > 1:
51 pylab.legend(loc='best')
52
53 pylab.title(title)
54 pylab.xlabel(xaxislabel)
55 pylab.ylabel(yaxislabel)
56 pylab.grid(True)
57
58 if args.outputFilename == None:
59 # if no pdf output is requested, spit the graph to the screen . . .
60 pylab.show()
61 else:
62 pylab.savefig(args.outputFilename,dpi=(1024/8))
63 # . . . otherwise, gimme gimme pdf
64 #pdf = PdfPages(args.outputFilename)
65 #pdf.savefig()
66 #pdf.close()
67
68 ######## plotFromDataFile() Function to plot from data file begins ########
69 def plotFromDataFile():
70 data = []
71 """
72 read in table(s) from file(s)
73 """
74 for thisFile in args.datafile:
75 if not os.path.isfile(thisFile):
76 print 'No file with the name \'{}\' exists. Please indicate another filename.'.format(thisFile)
77 quit()
78
79 results = open(thisFile, 'r')
80 resultsContents = results.read()
81 resultsContents = resultsContents.rstrip().split('\n')
82
83 firstRow = resultsContents.pop(0)
84 if firstRow != tableHeader:
85 print 'ERROR: input file \'{}\' does not match expected format.'.format(thisFile)
86 quit()
87
88 for row in resultsContents:
89 row = row.split(',')
90 row = TableRow(TestCombination(row[0],row[1],row[2],row[3],row[4],row[5],row[6],row[7],row[8],row[9]), row[10])
91 data.append(GraphPoint(row.parameters.x, row.parameters.y, row.parameters.z, row.parameters.batchsize, row.parameters.precision, row.parameters.device, row.parameters.label, row.gflops))
92
93 """
94 data sanity check
95 """
96 # if multiple plotvalues have > 1 value among the data rows, the user must specify which to plot
97 multiplePlotValues = []
98 for option in plotvalues:
99 values = []
100 for point in data:
101 values.append(getattr(point, option))
102 multiplePlotValues.append(len(set(values)) > 1)
103 if multiplePlotValues.count(True) > 1 and args.plot == None:
104 print 'ERROR: more than one parameter of {} has multiple values. Please specify which parameter to plot with --plot'.format(plotvalues)
105 quit()
106
107 # if args.graphxaxis is not 'problemsize', the user should know that the results might be strange
108 if args.graphxaxis != 'problemsize':
109 xaxisvalueSet = []
110 for option in xaxisvalues:
111 if option != 'problemsize':
112 values = []
113 for point in data:
114 values.append(getattr(point, option))
115 xaxisvalueSet.append(len(set(values)) > 1)
116 if xaxisvalueSet.count(True) > 1:
117 print 'WARNING: more than one parameter of {} is varied. unexpected results may occur. please double check your graphs for accuracy.'.format(xaxisvalues)
118
119 # multiple rows should not have the same input values
120 pointInputs = []
121 for point in data:
122 pointInputs.append(point.__str__().split(';')[0])
123 if len(set(pointInputs)) != len(data):
124 print 'ERROR: imported table has duplicate rows with identical input parameters'
125 quit()
126
127 """
128 figure out if we have multiple plots on this graph (and what they should be)
129 """
130 if args.plot != None:
131 multiplePlots = args.plot
132 elif multiplePlotValues.count(True) == 1:
133 multiplePlots = plotvalues[multiplePlotValues.index(True)]
134 else:
135 # default to device if none of the options to plot have multiple values
136 multiplePlots = 'device'
137
138 """
139 assemble data for the graphs
140 """
141 data.sort(key=lambda row: int(getattr(row, args.graphxaxis)))
142
143 # choose scale for x axis
144 if args.xaxisscale == None:
145 # user didn't specify. autodetect
146 if int(getattr(data[len(data)-1], args.graphxaxis)) > 2000: # big numbers on x-axis
147 args.xaxisscale = 'log2'
148 elif int(getattr(data[len(data)-1], args.graphxaxis)) > 10000: # bigger numbers on x-axis
149 args.xaxisscale = 'log10'
150 else: # small numbers on x-axis
151 args.xaxisscale = 'linear'
152
153 if args.yaxisscale == None:
154 args.yaxisscale = 'linear'
155
156 plotkwargs = {}
157 if args.xaxisscale == 'linear':
158 plottype = 'plot'
159 elif args.xaxisscale == 'log2':
160 plottype = 'semilogx'
161 if (args.yaxisscale=='log2'):
162 plottype = 'loglog'
163 plotkwargs = {'basex':2,'basey':2}
164 elif (args.yaxisscale=='log10'):
165 plottype = 'loglog'
166 plotkwargs = {'basex':2,'basey':10}
167 elif (args.yaxisscale=='linear'):
168 plottype = 'semilogx'
169 plotkwargs = {'basex':2}
170 elif args.xaxisscale == 'log10':
171 plottype = 'semilogx'
172 if (args.yaxisscale=='log2'):
173 plottype = 'loglog'
174 plotkwargs = {'basex':10,'basey':2}
175 elif (args.yaxisscale=='log10'):
176 plottype = 'loglog'
177 plotkwargs = {'basex':10,'basey':10}
178 else:
179 print 'ERROR: invalid value for x-axis scale'
180 quit()
181
182
183 plots = set(getattr(row, multiplePlots) for row in data)
184
185 class DataForOnePlot:
186 def __init__(self, inlabel, inxdata, inydata):
187 self.label = inlabel
188 self.xdata = inxdata
189 self.ydata = inydata
190
191 dataForAllPlots=[]
192 for plot in plots:
193 dataForThisPlot = itertools.ifilter( lambda x: getattr(x, multiplePlots) == plot, data)
194 dataForThisPlot = list(itertools.islice(dataForThisPlot, None))
195 if args.graphxaxis == 'problemsize':
196 xdata = [int(row.x) * int(row.y) * int(row.z) * int(row.batchsize) for row in dataForThisPlot]
197 else:
198 xdata = [getattr(row, args.graphxaxis) for row in dataForThisPlot]
199 ydata = [getattr(row, args.graphyaxis) for row in dataForThisPlot]
200 dataForAllPlots.append(DataForOnePlot(plot,xdata,ydata))
201
202 """
203 assemble labels for the graph or use the user-specified ones
204 """
205 if args.graphtitle:
206 # use the user selection
207 title = args.graphtitle
208 else:
209 # autogen a lovely title
210 title = 'Performance vs. ' + args.graphxaxis.capitalize()
211
212 if args.xaxislabel:
213 # use the user selection
214 xaxislabel = args.xaxislabel
215 else:
216 # autogen a lovely x-axis label
217 if args.graphxaxis == 'cachesize':
218 units = '(bytes)'
219 else:
220 units = '(datapoints)'
221
222 xaxislabel = args.graphxaxis + ' ' + units
223
224 if args.yaxislabel:
225 # use the user selection
226 yaxislabel = args.yaxislabel
227 else:
228 # autogen a lovely y-axis label
229 if args.graphyaxis == 'gflops':
230 units = 'GFLOPS'
231 yaxislabel = 'Performance (' + units + ')'
232
233 """
234 display a pretty graph
235 """
236 colors = ['k','y','m','c','b','g','r']
237 def getkey(item):
238 return str(item.label)
239 dataForAllPlots.sort(key=getkey)
240 #for thisPlot in sorted(dataForAllPlots,key=getkey):
241 for thisPlot in sorted(dataForAllPlots,key=getkey):
242 getattr(pylab, plottype)(thisPlot.xdata, thisPlot.ydata, '{}.-'.format(colors.pop()), label=thisPlot.label, **plotkwargs)
243
244 if len(dataForAllPlots) > 1:
245 pylab.legend(loc='best')
246
247 pylab.title(title)
248 pylab.xlabel(xaxislabel)
249 pylab.ylabel(yaxislabel)
250 pylab.grid(True)
251
252 if args.outputFilename == None:
253 # if no pdf output is requested, spit the graph to the screen . . .
254 pylab.show()
255 else:
256 # . . . otherwise, gimme gimme pdf
257 #pdf = PdfPages(args.outputFilename)
258 #pdf.savefig()
259 #pdf.close()
260 pylab.savefig(args.outputFilename,dpi=(1024/8))
261 ######### plotFromDataFile() Function to plot from data file ends #########
262
263
264
265 ######## "main" program begins #####
266 """
267 define and parse parameters
268 """
269
270 xaxisvalues = ['x','y','z','batchsize','problemsize']
271 yaxisvalues = ['gflops']
272 plotvalues = ['device', 'precision', 'label']
273
274
275
276 parser = argparse.ArgumentParser(description='Plot performance of the clfft\
277 library. clfft.plotPerformance.py reads in data tables from clfft.\
278 measurePerformance.py and plots their values')
279 fileOrDb = parser.add_mutually_exclusive_group(required=True)
280 fileOrDb.add_argument('-d', '--datafile',
281 dest='datafile', action='append', default=None, required=False,
282 help='indicate a file to use as input. must be in the format output by\
283 clfft.measurePerformance.py. may be used multiple times to indicate\
284 multiple input files. e.g., -d cypressOutput.txt -d caymanOutput.txt')
285 parser.add_argument('-x', '--x_axis',
286 dest='graphxaxis', default=None, choices=xaxisvalues, required=True,
287 help='indicate which value will be represented on the x axis. problemsize\
288 is defined as x*y*z*batchsize')
289 parser.add_argument('-y', '--y_axis',
290 dest='graphyaxis', default='gflops', choices=yaxisvalues,
291 help='indicate which value will be represented on the y axis')
292 parser.add_argument('--plot',
293 dest='plot', default=None, choices=plotvalues,
294 help='indicate which of {} should be used to differentiate multiple plots.\
295 this will be chosen automatically if not specified'.format(plotvalues))
296 parser.add_argument('--title',
297 dest='graphtitle', default=None,
298 help='the desired title for the graph generated by this execution. if\
299 GRAPHTITLE contains any spaces, it must be entered in \"double quotes\".\
300 if this option is not specified, the title will be autogenerated')
301 parser.add_argument('--x_axis_label',
302 dest='xaxislabel', default=None,
303 help='the desired label for the graph\'s x-axis. if XAXISLABEL contains\
304 any spaces, it must be entered in \"double quotes\". if this option\
305 is not specified, the x-axis label will be autogenerated')
306 parser.add_argument('--x_axis_scale',
307 dest='xaxisscale', default=None, choices=['linear','log2','log10'],
308 help='the desired scale for the graph\'s x-axis. if nothing is specified,\
309 it will be selected automatically')
310 parser.add_argument('--y_axis_scale',
311 dest='yaxisscale', default=None, choices=['linear','log2','log10'],
312 help='the desired scale for the graph\'s y-axis. if nothing is specified,\
313 linear will be selected')
314 parser.add_argument('--y_axis_label',
315 dest='yaxislabel', default=None,
316 help='the desired label for the graph\'s y-axis. if YAXISLABEL contains any\
317 spaces, it must be entered in \"double quotes\". if this option is not\
318 specified, the y-axis label will be autogenerated')
319 parser.add_argument('--outputfile',
320 dest='outputFilename', default=None,
321 help='name of the file to output graphs. Supported formats: emf, eps, pdf, png, ps, raw, rgba, svg, svgz.')
322
323 args = parser.parse_args()
324
325 if args.datafile != None:
326 plotFromDataFile()
327 else:
328 print "Atleast specify if you want to use text files or database for plotting graphs. Use -h or --help option for more details"
329 quit()
330