xref: /petsc/src/benchmarks/streams/process.py (revision abdd934af67c1cc360c4d0cfcf2de16e188d627e)
1#!/usr/bin/env python3
2#
3#    Computers speed up of Streams benchmark results generated by make streams and plots
4#
5#    matplotlib can switch between different backends hence this needs to be run
6#    twice to first generate a file and then display a window
7#
8from __future__ import print_function
9import os
10#
11def process(streamstype,fileoutput):
12  import re
13
14  ff = open('scaling.log')
15  data = ff.read()
16  ff.close()
17
18  s = data.split('\n')
19  triads = {} # stream triad bandwidth data points
20  speedups = {}
21  size = 0
22  for i in s[0:-1]:
23      i = i.split()
24      triads[size] = float(i[1])/1000 # MB/s to GB/s
25      size = size + 1
26
27  if size < 2: return
28
29  triads = list(triads.values())
30  speedups = {}
31  for i in range(0,size):
32    speedups[i] = triads[i]/triads[0]
33
34  try:
35    import matplotlib
36    from matplotlib.ticker import MaxNLocator
37  except:
38    print("Unable to open matplotlib to plot speedup")
39    return
40
41  try:
42    if fileoutput: matplotlib.use('Agg')
43    import matplotlib.pyplot as plt
44  except:
45    print("Unable to open matplotlib to plot speedup")
46    return
47
48  try:
49    fig, ax1 = plt.subplots(layout='constrained')
50    plt.title(streamstype+' Perfect and Streams Speedup')
51    ax2 = ax1.twinx()
52    ax1.set_autoscaley_on(False)
53
54    r = range(1,size+1)
55    speedups = speedups.values()
56
57    # make sure that actual bandwidth values (as opposed to perfect speedup) takes
58    # at least a third of the y axis
59    ymax = min(size, 3*max(speedups))
60    ymin = min(1, min(speedups))
61    if ymin < 1: ymin = 0
62
63    ax1.set_xlim(1,size)
64    ax1.xaxis.set_major_locator(MaxNLocator(integer=True))
65    ax1.set_ylim([ymin,ymax])
66    ax1.set_xlabel('Number of processes/threads')
67    ax1.set_ylabel('Achieved Speedup')
68    ax1.plot(r,r,'b',label='Ideal speedup')
69    ax1.plot(r,speedups,'r-o', label='Achieved speedup')
70    ax2.set_autoscaley_on(False)
71    ax2.set_xlim([1,size])
72    ax2.set_ylim([min(triads),max(triads)])
73    ax2.set_ylabel("Achieved Bandwidth (GB/s)")
74    ax2.plot(r,triads,'g-o', label='Achieved bandwidth')
75
76    lines1, labels1 = ax1.get_legend_handles_labels()
77    lines2, labels2 = ax2.get_legend_handles_labels()
78    ax1.legend(lines1 + lines2, labels1 + labels2, loc='best')
79
80    plt.show()
81    if fileoutput: plt.savefig(streamstype+'scaling.png')
82    if fileoutput: print("See graph in the file src/benchmarks/streams/"+streamstype+"scaling.png")
83  except Exception as e:
84    if fileoutput: print("Unable to plot speedup to a file")
85    else: print("Unable to display speedup plot")
86    return
87
88  ff.close()
89
90# plot bandwidth data in scaling.log under the current directory.
91#
92# ./process.py arg1 arg2
93#   arg1: stream type, e.g., MPI, OpenMP, CUDA etc
94#   arg2: optional, can be anything, to indicate if a <stream type>scaling.png should be generated
95if __name__ ==  '__main__':
96  import sys
97
98  process(sys.argv[1],len(sys.argv)-2)
99