-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathstats2gnuplot.cc
709 lines (600 loc) · 21.1 KB
/
stats2gnuplot.cc
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
// -*- mode: c++; fill-column: 79 -*-
/******************************************************************************
* stats2gnuplot.cc
*
* Convert stats.txt to a gnuplot script.
*
* This program will read a stats.txt file created by pmbw and output a gnuplot
* script, which generates a PDF. The program is used as following:
*
* "./stats2gnuplot stats.txt | gnuplot"
*
******************************************************************************
* Copyright (C) 2013 Timo Bingmann <[email protected]>
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <unistd.h>
// *** Warnings Output Function
bool gopt_warnings = false;
#define ERR(x) do { std::cerr << x << std::endl; } while(0)
#define ERRX(x) do { std::cerr << x; } while(0)
#define WARN(x) do { if (gopt_warnings) { std::cerr << x << std::endl; } } while(0)
// *** List of Function Name Processed (and Their Order)
static const char* funclist[] =
{
"ScanWrite256PtrSimpleLoop",
"ScanWrite256PtrUnrollLoop",
"ScanRead256PtrSimpleLoop",
"ScanRead256PtrUnrollLoop",
"ScanWrite128PtrSimpleLoop",
"ScanWrite128PtrUnrollLoop",
"ScanRead128PtrSimpleLoop",
"ScanRead128PtrUnrollLoop",
"cScanWrite128PtrSimpleLoop",
"ScanWrite64PtrSimpleLoop",
"ScanWrite64PtrUnrollLoop",
"ScanRead64PtrSimpleLoop",
"ScanRead64PtrUnrollLoop",
"ScanWrite64IndexSimpleLoop",
"ScanWrite64IndexUnrollLoop",
"ScanRead64IndexSimpleLoop",
"ScanRead64IndexUnrollLoop",
"cScanWrite64PtrSimpleLoop",
"cScanWrite64IndexSimpleLoop",
"ScanWrite32PtrSimpleLoop",
"ScanWrite32PtrUnrollLoop",
"ScanWrite32PtrMultiLoop",
"ScanRead32PtrSimpleLoop",
"ScanRead32PtrUnrollLoop",
"ScanRead32PtrMultiLoop",
"ScanWrite32IndexSimpleLoop",
"ScanWrite32IndexUnrollLoop",
"ScanRead32IndexSimpleLoop",
"ScanRead32IndexUnrollLoop",
"cScanWrite32PtrSimpleLoop",
"cScanWrite32IndexSimpleLoop",
"ScanWrite16PtrSimpleLoop",
"ScanWrite16PtrUnrollLoop",
"ScanRead16PtrSimpleLoop",
"ScanRead16PtrUnrollLoop",
"PermRead64SimpleLoop",
"PermRead64UnrollLoop",
"cPermRead64SimpleLoop",
"PermRead32SimpleLoop",
"PermRead32UnrollLoop",
"cPermRead32SimpleLoop",
NULL
};
/// global: function names not found in funclist, in the order of appearance
std::vector<std::string> g_unknown_funclist;
// ****************************************************************************
// *** Functions to read RESULT key-value files into Result vector
struct Result
{
// *** contains the field read from each RESULT line
std::string datetime;
std::string host;
std::string funcname;
size_t nthreads;
size_t areasize;
size_t threadsize;
size_t testsize;
size_t repeats;
size_t testvol;
size_t testaccess;
double time;
double bandwidth;
double rate;
size_t funcname_id; // index of funcname in funclist (for nicer order)
Result()
: nthreads(0), areasize(0), threadsize(0), testsize(0), repeats(0),
testvol(0), testaccess(0),
time(0), bandwidth(0), rate(0)
{
}
/// parse a single RESULT key-value and save its information
bool process_line_keyvalue(const std::string& key, const std::string& value);
/// sort order of results is: (funcname_id,nthreads,testsize)
bool operator< (const Result& b) const
{
if (funcname_id == b.funcname_id) {
if (nthreads == b.nthreads) {
return testsize < b.testsize;
}
return nthreads < b.nthreads;
}
return funcname_id < b.funcname_id;
}
};
/// global: hostname read from results
std::string g_hostname;
/// global: output filename of gnuplot pdf
std::string g_gnuplot_output;
/// global: the sorted results array
std::vector<Result> g_results;
/// parse a number as size_t with error detection
static inline bool
parse_sizet(const std::string& value, size_t& out)
{
char* endp;
out = strtoull(value.c_str(), &endp, 10);
return (endp && *endp == 0);
}
/// parse a number as double with error detection
static inline bool
parse_double(const std::string& value, double& out)
{
char* endp;
out = strtod(value.c_str(), &endp);
return (endp && *endp == 0);
}
/// parse a funcname into funcname_id with error detection
static inline bool
find_funcname(const std::string& funcname, size_t& funcname_id)
{
size_t i;
for (i = 0; funclist[i]; ++i) {
if (funcname == funclist[i]) {
funcname_id = i;
return true;
}
}
for (std::vector<std::string>::const_iterator it = g_unknown_funclist.begin(); it != g_unknown_funclist.end(); ++it, ++i) {
if (funcname == *it) {
funcname_id = i;
return true;
}
}
std::cerr << "Unknown funcname=" << funcname << "\n";
g_unknown_funclist.push_back(funcname);
funcname_id = i;
return false;
}
/// parse a single RESULT key-value and save its information
bool Result::process_line_keyvalue(const std::string& key, const std::string& value)
{
if (key == "datetime") {
datetime = value;
return true;
}
else if (key == "host") {
host = value;
return true;
}
else if (key == "funcname") {
funcname = value;
return find_funcname(funcname, funcname_id);
}
else if (key == "nthreads") {
return parse_sizet(value, nthreads);
}
else if (key == "areasize") {
return parse_sizet(value, areasize);
}
else if (key == "threadsize") {
return parse_sizet(value, threadsize);
}
else if (key == "testsize") {
return parse_sizet(value, testsize);
}
else if (key == "repeats") {
return parse_sizet(value, repeats);
}
else if (key == "testvol") {
return parse_sizet(value, testvol);
}
else if (key == "testaccess") {
return parse_sizet(value, testaccess);
}
else if (key == "time") {
return parse_double(value, time);
}
else if (key == "bandwidth") {
return parse_double(value, bandwidth);
}
else if (key == "rate") {
return parse_double(value, rate);
}
else {
return false;
}
}
/// process a single line containing RESULT key-value pairs
bool process_line(const std::string& line)
{
std::string::size_type splitpos = line.find('\t');
if (splitpos == std::string::npos) return false;
if (line.substr(0,splitpos) != "RESULT") return false;
struct Result result;
do
{
std::string::size_type splitprev = splitpos+1;
splitpos = line.find('\t', splitprev);
std::string keyvalue = line.substr(splitprev, splitpos - splitprev);
std::string::size_type equalpos = keyvalue.find('=');
if (equalpos != std::string::npos)
{
if (!result.process_line_keyvalue( keyvalue.substr(0,equalpos),
keyvalue.substr(equalpos+1) ))
{
WARN("Invalid key-value pair: " << keyvalue);
}
}
else
{
WARN("Invalid key-value pair: " << keyvalue);
}
}
while(splitpos != std::string::npos);
g_results.push_back(result);
return true;
}
/// read a stream of RESULT lines
void process_stream(std::istream& in)
{
std::string line;
while (std::getline(in,line) )
{
if (!process_line(line)) {
WARN("Non-RESULT line: " << line);
}
}
}
/// open a file as a stream
void process_file(const char* path)
{
std::ifstream in(path);
if (!in.good()) {
ERR("Error opening file " << path << ": " << strerror(errno));
return;
}
return process_stream(in);
}
/// check for multiple hosts
bool check_multiple_hosts()
{
std::set<std::string> hostnames;
g_hostname = g_results[0].host;
for (size_t i = 0; i < g_results.size(); ++i)
{
hostnames.insert( g_results[i].host );
}
if (hostnames.size() > 1)
{
ERRX("Multiple different hostnames found in results:");
for(std::set<std::string>::const_iterator hi = hostnames.begin();
hi != hostnames.end(); ++hi)
{
ERRX(" " << *hi);
}
ERR("");
return false;
}
return true;
}
// ****************************************************************************
// *** Output various plots generated from results
#define P(x) do { os << x << std::endl; } while(0)
/// join a vector of saved plot lines and output data stream afterwards
void join_plotlines(std::ostream& os, const std::vector<std::string>& plotlines, const std::ostringstream& datass)
{
if (plotlines.size() == 0) return;
P("plot \\");
for (size_t i = 0; i < plotlines.size(); ++i) {
os << plotlines[i];
if (i != plotlines.size()-1) os << ", \\" << std::endl;
else os << std::endl;
}
os << datass.str() << std::endl;
}
/// use STL stringstream to stringify a value
template <typename T>
static inline std::string toStr(const T& v)
{
std::ostringstream s; s << v; return s.str();
}
/// filter functional type used in plot procedures
typedef bool (*filter_type)(const Result& r);
/// data formatting functional used in plot procedures
typedef void (*data_print_func)(std::ostream& datass, const Result& r);
/// Plot procedure: iterate over results, filter them and output a plot
/// containing funcname plotlines
void plot_funcname_iteration(std::ostream& os, filter_type filter, data_print_func print_func)
{
std::ostringstream datass;
std::vector<std::string> plotlines;
std::string cfuncname; // current funcname in iteration
size_t ctestsize = 0;
// iterate over all results in order and collect plotlines
for (size_t i = 0; i < g_results.size(); ++i)
{
const Result& r = g_results[i];
if (!filter(r)) continue;
if (cfuncname == r.funcname && ctestsize == r.testsize)
{
WARN("Multiple results found for " << cfuncname
<< " testsize " << ctestsize << ", ignoring second.");
continue;
}
if (cfuncname != r.funcname) // start new plot line
{
if (datass.str().size()) datass << "e" << std::endl;
plotlines.push_back("'-' using 1:2 title '" + r.funcname + "' with linespoints");
cfuncname = r.funcname;
}
print_func(datass, r);
ctestsize = r.testsize;
}
if (datass.str().size()) datass << "e\n";
join_plotlines(os, plotlines, datass);
}
/// plot the bandwidth in GiB/s for each Result
void plot_data_bandwidth(std::ostream& datass, const Result& r)
{
datass << std::setprecision(20)
<< log(r.testsize) / log(2) << "\t"
<< r.bandwidth / 1024/1024/1024 << "\n";
}
/// plot the latency (access time) in nanoseconds for each Result
void plot_data_latency(std::ostream& datass, const Result& r)
{
datass << std::setprecision(20)
<< log(r.testsize) / log(2) << "\t"
<< r.rate * 1e9 << "\n";
}
/// show only sequential results with procs = 1
bool filter_sequential(const Result& r) {
return (r.nthreads == 1);
}
bool filter_sequential_nonpermutation(const Result& r) {
return (r.nthreads == 1) && (r.funcname.find("Perm") == std::string::npos);
}
bool filter_sequential_64bit_reads(const Result& r) {
return (r.nthreads == 1) && (r.funcname.find("Read64") == std::string::npos);
}
/// Plots showing just sequential memory bandwidth and latency
void plot_sequential(std::ostream& os)
{
P("set key top right");
P("set title '" << g_hostname << " - One Thread Memory Bandwidth'");
P("set ylabel 'Bandwidth [GiB/s]'");
P("set yrange [0:*]");
plot_funcname_iteration(os, filter_sequential, plot_data_bandwidth);
P("set key top left");
P("set title '" << g_hostname << " - One Thread Memory Latency (Access Time)'");
P("set ylabel 'Access Time [ns]'");
plot_funcname_iteration(os, filter_sequential, plot_data_latency);
P("set key top left");
P("set title '" << g_hostname << " - One Thread Memory Latency (excluding Permutation)'");
P("set ylabel 'Access Time [ns]'");
plot_funcname_iteration(os, filter_sequential_nonpermutation, plot_data_latency);
P("set key top right");
P("set title '" << g_hostname << " - One Thread Memory Bandwidth (only 64-bit Reads)'");
P("set ylabel 'Bandwidth [GiB/s]'");
plot_funcname_iteration(os, filter_sequential_64bit_reads, plot_data_bandwidth);
}
/// Plot procedure: iterate over results, filter them to show only one funcname
/// and output a plot containing plotlines for each nthreads
void plot_parallel_iteration(std::ostream& os, const std::string& funcname, data_print_func print_func)
{
std::ostringstream datass;
std::vector<std::string> plotlines;
size_t cnthreads = 0; // current nthreads
size_t ctestsize = 0; // current testsize
// iterate over all results in order, separate funcnames and collect plotlines for each funcname
for (size_t i = 0; i < g_results.size(); ++i)
{
const Result& r = g_results[i];
if (r.funcname != funcname) continue;
if (cnthreads == r.nthreads && ctestsize == r.testsize)
{
WARN("Multiple results found for " << funcname << " nthreads " << cnthreads
<< " testsize " << ctestsize << ", ignoring second.");
continue;
}
if (cnthreads != r.nthreads) // start new plot line
{
if (datass.str().size()) datass << "e\n";
plotlines.push_back("'-' using 1:2 title 'p=" + toStr(r.nthreads) + "' with linespoints");
cnthreads = r.nthreads;
}
print_func(datass, r);
ctestsize = r.testsize;
}
if (datass.str().size()) datass << "e\n";
join_plotlines(os, plotlines, datass);
}
/// Plot procedure: iterate over results, filter them to show only one funcname
/// and output a plot containing plotlines for each nthreads. Calculate the
/// speedup of memory bandwidth over the nthreads=1 entry.
void plot_parallel_speedup_bandwidth(std::ostream& os, const std::string& funcname, double& avgspeedup)
{
std::ostringstream datass;
std::vector<std::string> plotlines;
size_t cnthreads = 0; // current nthreads
size_t ctestsize = 0; // current testsize
avgspeedup = 0;
size_t cntspeedup = 0;
std::map<size_t,double> seqbandwidth; // map areasize -> sequential bandwidth (nthreads=1)
// areasize is used instead of testsize, because testsize may depend on rounding due to nthreads.
// iterate over all results in order, separate funcnames and collect plotlines for each funcname
for (size_t i = 0; i < g_results.size(); ++i)
{
const Result& r = g_results[i];
if (r.funcname != funcname) continue;
if (cnthreads == r.nthreads && ctestsize == r.testsize)
{
WARN("Multiple results found for " << funcname << " nthreads " << cnthreads
<< " testsize " << ctestsize << ", ignoring second.");
continue;
}
if (cnthreads != r.nthreads) // start new plot line
{
if (datass.str().size()) datass << "e\n";
plotlines.push_back("'-' using 1:2 title 'p=" + toStr(r.nthreads) + "' with linespoints");
cnthreads = r.nthreads;
}
if (r.nthreads == 1) {
seqbandwidth[r.areasize] = r.bandwidth;
}
if (seqbandwidth[r.areasize] == 0)
{
WARN("Missing sequential bandwidth in speedup plot for " << funcname << " nthreads " << cnthreads
<< " testsize " << ctestsize << ", skipping.");
}
else
{
datass << std::setprecision(20)
<< log(r.testsize) / log(2) << "\t"
<< r.bandwidth / seqbandwidth[r.areasize] << "\n";
avgspeedup += r.bandwidth / seqbandwidth[r.areasize];
cntspeedup++;
}
ctestsize = r.testsize;
}
if (datass.str().size()) datass << "e\n";
join_plotlines(os, plotlines, datass);
// divide by number of speedup values found
if (cntspeedup == 0)
avgspeedup = 0;
else
avgspeedup /= cntspeedup;
}
void plot_parallel_funcname(std::ostream& os, const std::string& funcname)
{
P("set key top right");
P("set title '" << g_hostname << " - Parallel Memory Bandwidth - " << funcname);
P("set ylabel 'Bandwidth [GiB/s]'");
plot_parallel_iteration(os, funcname, plot_data_bandwidth);
P("set key top left");
P("set title '" << g_hostname << " - Parallel Memory Access Time - " << funcname);
P("set ylabel 'Access Time [ns]'");
plot_parallel_iteration(os, funcname, plot_data_latency);
P("set key top right");
P("set title '" << g_hostname << " - Speedup of Parallel Memory Bandwidth - " << funcname);
P("set ylabel 'Bandwidth Speedup [1]'");
double avgspeedup;
plot_parallel_speedup_bandwidth(os, funcname, avgspeedup);
// replot last plot with other yrange scale
P("set title '" << g_hostname << " - Speedup of Parallel Memory Bandwidth (enlarged) - " << funcname);
P("set yrange [*:" << avgspeedup << "]");
plot_parallel_speedup_bandwidth(os, funcname, avgspeedup);
P("set yrange [*:*]");
P("");
P("##############################");
}
void plot_parallel(std::ostream& os)
{
for (size_t i = 0; funclist[i]; ++i)
{
plot_parallel_funcname(os,funclist[i]);
}
for (std::vector<std::string>::const_iterator it = g_unknown_funclist.begin(); it != g_unknown_funclist.end(); ++it) {
plot_parallel_funcname(os, *it);
}
}
void output_gnuplot(std::ostream& os)
{
P("set terminal pdf size 28cm,19.6cm linewidth 2.0 font \"Arial,18\" enhanced");
P("set output '" << g_gnuplot_output << "'");
P("");
P("set pointsize 0.7");
P("set style line 6 lc rgb '#f0b000'");
P("set style line 15 lc rgb '#f0b000'");
P("set style line 24 lc rgb '#f0b000'");
P("set style line 33 lc rgb '#f0b000'");
P("set style line 42 lc rgb '#f0b000'");
P("set style line 51 lc rgb '#f0b000'");
P("set style line 60 lc rgb '#f0b000'");
P("set style increment user");
P("");
P("set grid xtics ytics");
P("set xtics 1");
P("set xlabel 'Array Size log_2 [B]'");
P("set label 1 'pmbw " VERSION "' right at screen 0.98, screen 0.02");
plot_sequential(os);
plot_parallel(os);
}
/// main: read stdin or from all files on the command line
int main(int argc, char* argv[])
{
std::string opt_hostname_override;
std::string opt_gnuplot_output_override;
if (argc == 1) {
process_stream(std::cin);
}
else
{
// *** parse command line options
int opt;
while ( (opt = getopt(argc, argv, "vh:o:")) != -1 )
{
switch (opt) {
case 'h':
opt_hostname_override = optarg;
ERR("Setting hostname override to '" << opt_hostname_override << "'");
break;
case 'o':
opt_gnuplot_output_override = optarg;
ERR("Setting gnuplot output override to '" << opt_gnuplot_output_override << "'");
break;
case 'v':
gopt_warnings = true;
ERR("Outputting verbose warnings when processing plots.");
break;
default: /* '?' */
ERR("Usage: " << argv[0] << " [-v] [-h hostname] [files...]");
exit(EXIT_FAILURE);
}
}
while (optind < argc) { // process files
process_file(argv[optind++]);
}
}
if (g_results.size() == 0) {
ERR("No RESULT lines found in input.");
return 0;
}
else {
ERR("Parsed " << g_results.size() << " RESULT lines in input.");
}
if (!check_multiple_hosts())
{
if (!opt_hostname_override.size()) {
ERR("Use -h <hostname> to override the hostnames if this is intentional.");
return 0;
}
}
if (opt_hostname_override.size())
g_hostname = opt_hostname_override;
g_gnuplot_output = "plots-" + g_hostname + ".pdf";
if (opt_gnuplot_output_override.size())
g_gnuplot_output = opt_gnuplot_output_override;
std::sort(g_results.begin(), g_results.end());
output_gnuplot(std::cout);
return 0;
}