xref: /petsc/src/benchmarks/streams/process.py (revision 5b6bfdb9644f185dbf5e5a09b808ec241507e1e7)
1d3ae85c4SBarry Smith#!/usr/bin/env python
2d3ae85c4SBarry Smith#!/bin/env python
3d3ae85c4SBarry Smith#
4a6cca095SBarry Smith#    Computers speed up of Streams benchmark results generated by make streams and plots
5a6cca095SBarry Smith#
6a6cca095SBarry Smith#    matplotlib can switch between different backends hence this needs to be run
7a6cca095SBarry Smith#    twice to first generate a file and then display a window
8d3ae85c4SBarry Smith#
9*5b6bfdb9SJed Brownfrom __future__ import print_function
10d3ae85c4SBarry Smithimport os
11d3ae85c4SBarry Smith#
12a6cca095SBarry Smithdef process(fileoutput = 1):
13d3ae85c4SBarry Smith  import re
14a6cca095SBarry Smith  ff = open('scaling.log')
15d3ae85c4SBarry Smith  data = ff.read()
16d3ae85c4SBarry Smith  ff.close()
17d3ae85c4SBarry Smith
185e71baefSBarry Smith  s = data.split('\n')
195e71baefSBarry Smith  ss = []
205e71baefSBarry Smith  for i in s:
215e71baefSBarry Smith    if not i.startswith('MPI rank'):
225e71baefSBarry Smith      ss.append(i)
235e71baefSBarry Smith  data = '\n'.join(ss)
24d3ae85c4SBarry Smith  hosts  = {}
25d3ae85c4SBarry Smith  triads = {}
26d3ae85c4SBarry Smith  speedups = {}
27d3ae85c4SBarry Smith  match = data.split('Number of MPI processes ')
28d3ae85c4SBarry Smith  for i in match:
29d3ae85c4SBarry Smith    if i:
30d3ae85c4SBarry Smith      fields = i.split('\n')
31436f0324SBarry Smith      size = int(fields[0].split()[0])
32436f0324SBarry Smith      hosts[size] = fields[0].split()[3:]
33436f0324SBarry Smith      triads[size] = float(fields[1].split()[1])
34d3ae85c4SBarry Smith
35293a2e3aSBarry Smith  if len(hosts) < 2: return
36293a2e3aSBarry Smith
37a6cca095SBarry Smith  ff = open('scaling.log','a')
38*5b6bfdb9SJed Brown  if fileoutput: print('np  speedup')
39a6cca095SBarry Smith  if fileoutput: ff.write('np  speedup\n')
40d3ae85c4SBarry Smith  for sizes in hosts:
41d3ae85c4SBarry Smith    speedups[sizes] = triads[sizes]/triads[1]
42*5b6bfdb9SJed Brown    if fileoutput: print(sizes,round(triads[sizes]/triads[1],2))
43a6cca095SBarry Smith    if fileoutput: ff.write(str(sizes)+' '+str(round(triads[sizes]/triads[1],2))+'\n')
441df1832dSBarry Smith
45*5b6bfdb9SJed Brown  if fileoutput: print("Estimation of possible speedup of MPI programs based on Streams benchmark.")
461df1832dSBarry Smith  if fileoutput: ff.write("Estimation of possible speedup of MPI programs based on Streams benchmark.\n")
471df1832dSBarry Smith
481df1832dSBarry Smith  if fileoutput:
491df1832dSBarry Smith    import re
501df1832dSBarry Smith    last = max(hosts.keys())
511df1832dSBarry Smith    lasthosts = hosts[last]
521df1832dSBarry Smith    for i in range(0,len(lasthosts)):
531df1832dSBarry Smith      lasthosts[i] = re.sub(r"Process [0-9]*", "", lasthosts[i])
541df1832dSBarry Smith    ulasthosts = list(set(lasthosts))
55*5b6bfdb9SJed Brown    print("It appears you have "+str(len(ulasthosts))+" node(s)")
5619623ac0SBarry Smith    ff.write("It appears you have "+str(len(ulasthosts))+" node(s)\n")
571df1832dSBarry Smith
5819623ac0SBarry Smith    if len(ulasthosts) < 1:
591df1832dSBarry Smith      testhosts = []
601df1832dSBarry Smith      for i in range(0,len(lasthosts)):
611df1832dSBarry Smith        testhosts.append(ulasthosts[i % len(ulasthosts)])
621df1832dSBarry Smith      if testhosts == lasthosts:
63*5b6bfdb9SJed Brown        print("   distributed in a round robin order")
641df1832dSBarry Smith        ff.write("   distributed in a round robin order\n")
651df1832dSBarry Smith      else:
66*5b6bfdb9SJed Brown        print("   NOT distributed in a round robin order")
671df1832dSBarry Smith        ff.write("   NOT distributed in a round robin order\n")
68d3ae85c4SBarry Smith
69d3ae85c4SBarry Smith  try:
70d3ae85c4SBarry Smith    import matplotlib
71d3ae85c4SBarry Smith  except:
72*5b6bfdb9SJed Brown    print("Unable to open matplotlib to plot speedup")
73d3ae85c4SBarry Smith    return
74d3ae85c4SBarry Smith
75d3ae85c4SBarry Smith  try:
76a6cca095SBarry Smith    if fileoutput: matplotlib.use('Agg')
77d3ae85c4SBarry Smith    import matplotlib.pyplot as plt
786a90b735SBarry Smith  except:
79*5b6bfdb9SJed Brown    print("Unable to open matplotlib to plot speedup")
806a90b735SBarry Smith    return
81d3ae85c4SBarry Smith
82182d2d36SBarry Smith  try:
837137e648SBarry Smith    fig, ax1 = plt.subplots()
847137e648SBarry Smith    plt.title('MPI Perfect and Streams Speedup')
857137e648SBarry Smith    ax2 = ax1.twinx()
867137e648SBarry Smith    ax1.set_autoscaley_on(False)
878473a99dSBarry Smith
888473a99dSBarry Smith    # make sure that actual bandwidth values (as opposed to perfect speedup) takes
898473a99dSBarry Smith    # at least a third of the y axis
908473a99dSBarry Smith    ymax = min(max(hosts.keys()), 3*max(triads.values())/min(triads.values()) - 2)
918473a99dSBarry Smith
927137e648SBarry Smith    ax1.set_xlim([min(hosts.keys()),max(hosts.keys())])
938473a99dSBarry Smith    ax1.set_ylim([min(hosts.keys()),ymax])
947137e648SBarry Smith    ax1.set_xlabel('Number of MPI processes')
957137e648SBarry Smith    ax1.set_ylabel('Memory Bandwidth Speedup')
96576f62e6SBarry Smith    ax1.plot(hosts.keys(),hosts.keys(),'b',hosts.keys(),speedups.values(),'r-o')
977137e648SBarry Smith    ax2.set_autoscaley_on(False)
987137e648SBarry Smith    ax2.set_xlim([min(hosts.keys()),max(hosts.keys())])
998473a99dSBarry Smith    ax2.set_ylim([min(triads.values())/1000.,min(triads.values())*ymax/1000.])
1007137e648SBarry Smith    ax2.set_ylabel("Achieved Bandwidth. Gigabytes per Second")
1017137e648SBarry Smith
102d3ae85c4SBarry Smith    plt.show()
103a6cca095SBarry Smith    if fileoutput: plt.savefig('scaling.png')
104*5b6bfdb9SJed Brown    if fileoutput: print("See graph in the file src/benchmarks/streams/scaling.png")
1051df1832dSBarry Smith    if fileoutput: ff.write("See graph in the file src/benchmarks/streams/scaling.png\n")
106*5b6bfdb9SJed Brown  except Exception as e:
107*5b6bfdb9SJed Brown    if fileoutput: print("Unable to plot speedup to a file")
108*5b6bfdb9SJed Brown    else: print("Unable to display speedup plot")
109182d2d36SBarry Smith    return
110d3ae85c4SBarry Smith
1111df1832dSBarry Smith  ff.close()
1121df1832dSBarry Smith
113d3ae85c4SBarry Smith#
114d3ae85c4SBarry Smith#
115d3ae85c4SBarry Smithif __name__ ==  '__main__':
116d3ae85c4SBarry Smith  import sys
117a6cca095SBarry Smith  process(len(sys.argv)-1)
118d3ae85c4SBarry Smith
119d3ae85c4SBarry Smith
120