xref: /petsc/src/benchmarks/benchmarkAssembly.py (revision 3849a283593cc4a74cd3b462ea7e8a7f625140b9)
1c87d2d20SMatthew G Knepley#!/usr/bin/env python
2c87d2d20SMatthew G Knepleyimport os
3c87d2d20SMatthew G Knepleyfrom benchmarkExample import PETScExample
4c87d2d20SMatthew G Knepley
55ec3c338SMatthew G KnepleysavedTiming = {'baconost': {'ElemAssembly': [(0.040919999999999998, 0.0), (0.1242, 0.0), (0.24410000000000001, 0.0), (0.374, 0.0), (0.56259999999999999, 0.0), (0.79049999999999998, 0.0), (1.0880000000000001, 0.0), (1.351, 0.0), (1.6930000000000001, 0.0), (2.0609999999999999, 0.0), (2.4820000000000002, 0.0), (3.0640000000000001, 0.0)],
65ec3c338SMatthew G Knepley                            'MatCUSPSetValBch': [(0.0123, 0.0), (0.023429999999999999, 0.0), (0.043540000000000002, 0.0), (0.06608, 0.0), (0.09579, 0.0), (0.12920000000000001, 0.0), (0.17169999999999999, 0.0), (0.2172, 0.0), (0.27179999999999999, 0.0), (0.48309999999999997, 0.0), (0.44180000000000003, 0.0), (0.51529999999999998, 0.0)]}
75ec3c338SMatthew G Knepley               }
85ec3c338SMatthew G Knepley
96cbfa02cSMatthew G Knepleydef calculateNonzeros(n):
106cbfa02cSMatthew G Knepley  num = 0
116cbfa02cSMatthew G Knepley  # corners
126cbfa02cSMatthew G Knepley  num += 2*3 + 2*4
136cbfa02cSMatthew G Knepley  # edges
146cbfa02cSMatthew G Knepley  num += 4*(n-2)*5
156cbfa02cSMatthew G Knepley  # interior
166cbfa02cSMatthew G Knepley  num += (n-2)*(n-2)*7
176cbfa02cSMatthew G Knepley  return num
186cbfa02cSMatthew G Knepley
19c87d2d20SMatthew G Knepleydef processSummary(moduleName, times, events):
20c87d2d20SMatthew G Knepley  '''Process the Python log summary into plot data'''
21c87d2d20SMatthew G Knepley  m = __import__(moduleName)
22c87d2d20SMatthew G Knepley  reload(m)
23c87d2d20SMatthew G Knepley  # Total Time
24c87d2d20SMatthew G Knepley  times.append(m.Time[0])
25c87d2d20SMatthew G Knepley  # Common events
26c87d2d20SMatthew G Knepley  #   Add the time and flop rate
270cfd297dSMatthew G Knepley  for stageName, eventName in [('GPU_Stage','MatCUSPSetValBch'), ('CPU_Stage','ElemAssembly')]:
280cfd297dSMatthew G Knepley    s = getattr(m, stageName)
290cfd297dSMatthew G Knepley    if not eventName in events:
300cfd297dSMatthew G Knepley      events[eventName] = []
310cfd297dSMatthew G Knepley    events[eventName].append((s.event[eventName].Time[0], s.event[eventName].Flops[0]/(s.event[eventName].Time[0] * 1e6)))
32c87d2d20SMatthew G Knepley  return
33c87d2d20SMatthew G Knepley
346cbfa02cSMatthew G Knepleydef plotSummary(library, num, sizes, nonzeros, times, events):
356cbfa02cSMatthew G Knepley  from pylab import legend, plot, show, title, xlabel, ylabel, ylim
36c87d2d20SMatthew G Knepley  import numpy as np
37194dd558SMatthew G Knepley  showEventTime      = True
386cbfa02cSMatthew G Knepley  showTimePerRow     = False
396cbfa02cSMatthew G Knepley  showTimePerNonzero = True
40c87d2d20SMatthew G Knepley  print events
41c87d2d20SMatthew G Knepley  if showEventTime:
42c87d2d20SMatthew G Knepley    data  = []
43c87d2d20SMatthew G Knepley    names = []
44c87d2d20SMatthew G Knepley    for event, style in [('MatCUSPSetValBch', 'b-'), ('ElemAssembly', 'b:')]:
45c87d2d20SMatthew G Knepley      names.append(event)
46c87d2d20SMatthew G Knepley      data.append(sizes)
47c87d2d20SMatthew G Knepley      data.append(np.array(events[event])[:,0])
48c87d2d20SMatthew G Knepley      data.append(style)
49c87d2d20SMatthew G Knepley    plot(*data)
50c87d2d20SMatthew G Knepley    title('Performance on '+library+' Example '+str(num))
51c87d2d20SMatthew G Knepley    xlabel('Number of Dof')
52c87d2d20SMatthew G Knepley    ylabel('Time (s)')
53c87d2d20SMatthew G Knepley    legend(names, 'upper left', shadow = True)
54c87d2d20SMatthew G Knepley    show()
556cbfa02cSMatthew G Knepley  if showTimePerRow:
566cbfa02cSMatthew G Knepley    data  = []
576cbfa02cSMatthew G Knepley    names = []
586cbfa02cSMatthew G Knepley    for event, style in [('MatCUSPSetValBch', 'b-'), ('ElemAssembly', 'b:')]:
596cbfa02cSMatthew G Knepley      names.append(event)
606cbfa02cSMatthew G Knepley      data.append(sizes)
616cbfa02cSMatthew G Knepley      rows = np.sqrt(sizes)
626cbfa02cSMatthew G Knepley      data.append(np.array(events[event])[:,0]/rows/3)
636cbfa02cSMatthew G Knepley      data.append(style)
646cbfa02cSMatthew G Knepley    plot(*data)
656cbfa02cSMatthew G Knepley    title('Performance on '+library+' Example '+str(num))
666cbfa02cSMatthew G Knepley    xlabel('Number of Dof')
676cbfa02cSMatthew G Knepley    ylabel('Time/Row (s)')
686cbfa02cSMatthew G Knepley    legend(names, 'upper left', shadow = True)
696cbfa02cSMatthew G Knepley    show()
706cbfa02cSMatthew G Knepley  if showTimePerNonzero:
716cbfa02cSMatthew G Knepley    data  = []
726cbfa02cSMatthew G Knepley    names = []
736cbfa02cSMatthew G Knepley    for event, style in [('MatCUSPSetValBch', 'b-'), ('ElemAssembly', 'b:')]:
746cbfa02cSMatthew G Knepley      names.append(event)
756cbfa02cSMatthew G Knepley      data.append(sizes)
766cbfa02cSMatthew G Knepley      data.append(np.array(events[event])[:,0]/nonzeros * 10**9)
776cbfa02cSMatthew G Knepley      data.append(style)
786cbfa02cSMatthew G Knepley    plot(*data)
796cbfa02cSMatthew G Knepley    title('Performance on '+library+' Example '+str(num))
806cbfa02cSMatthew G Knepley    xlabel('Number of Dof')
816cbfa02cSMatthew G Knepley    ylabel('Time/Nonzero (ns)')
826cbfa02cSMatthew G Knepley    legend(names, 'center right', shadow = True)
836cbfa02cSMatthew G Knepley    show()
84c87d2d20SMatthew G Knepley  return
85c87d2d20SMatthew G Knepley
86c87d2d20SMatthew G Knepleyif __name__ == '__main__':
875ec3c338SMatthew G Knepley  import argparse
885ec3c338SMatthew G Knepley
895ec3c338SMatthew G Knepley  parser = argparse.ArgumentParser(description     = 'PETSc Benchmarking',
905ec3c338SMatthew G Knepley                                   epilog          = 'This script runs src/<library>/examples/tutorials/ex<num>, For more information, visit http://www.mcs.anl.gov/petsc',
915ec3c338SMatthew G Knepley                                   formatter_class = argparse.ArgumentDefaultsHelpFormatter)
925ec3c338SMatthew G Knepley  parser.add_argument('--library', default='KSP',           help='The PETSc library used in this example')
935ec3c338SMatthew G Knepley  parser.add_argument('--num',     type = int, default='4', help='The example number')
945ec3c338SMatthew G Knepley  parser.add_argument('--module',  default='summary',       help='The module for timing output')
955ec3c338SMatthew G Knepley  parser.add_argument('--saved',                            help='Name of saved data')
9619d5f70aSMatthew G Knepley  parser.add_argument('--scaling',                          help='Run parallel scaling test')
97194dd558SMatthew G Knepley  parser.add_argument('--small',   action='store_true', default=False, help='Use small sizes')
9819d5f70aSMatthew G Knepley  parser.add_argument('--batch',   action='store_true', default=False, help='Generate batch files for the runs instead')
995ec3c338SMatthew G Knepley
1005ec3c338SMatthew G Knepley  args = parser.parse_args()
1015ec3c338SMatthew G Knepley  print(args)
1025ec3c338SMatthew G Knepley  ex       = PETScExample(args.library, args.num, log_summary_python=args.module+'.py', preload='off')
103c87d2d20SMatthew G Knepley  sizes    = []
1046cbfa02cSMatthew G Knepley  nonzeros = []
105c87d2d20SMatthew G Knepley  times    = []
1065ec3c338SMatthew G Knepley  if args.saved is None:
107c87d2d20SMatthew G Knepley    events   = {}
10819d5f70aSMatthew G Knepley    if args.scaling == 'strong':
10919d5f70aSMatthew G Knepley      procs  = [1, 2, 4, 8]
11019d5f70aSMatthew G Knepley      if args.small:
11119d5f70aSMatthew G Knepley        grid = [10]*len(procs)
11219d5f70aSMatthew G Knepley      else:
11319d5f70aSMatthew G Knepley        grid = [1250]*len(procs)
11419d5f70aSMatthew G Knepley    else:
115194dd558SMatthew G Knepley      if args.small:
116194dd558SMatthew G Knepley        grid = [100, 150, 200, 250, 300]
117194dd558SMatthew G Knepley      else:
118194dd558SMatthew G Knepley        grid = range(150, 1350, 100)
11919d5f70aSMatthew G Knepley      procs  = [1]*len(grid)
12019d5f70aSMatthew G Knepley    for n, p in zip(grid, procs):
12119d5f70aSMatthew G Knepley      ex.run(p, da_grid_x=n, da_grid_y=n, cusp_synchronize=1, batch=args.batch)
122c87d2d20SMatthew G Knepley      sizes.append(n*n)
1236cbfa02cSMatthew G Knepley      nonzeros.append(calculateNonzeros(n))
12419d5f70aSMatthew G Knepley      if not args.batch: processSummary(args.module, times, events)
12521c1d55bSMatthew G Knepley  else:
126*3849a283SMatthew G Knepley    if args.batch: raise RuntimeException('Cannot use batch option with saved data')
1275ec3c338SMatthew G Knepley    events = savedTiming[args.saved]
12821c1d55bSMatthew G Knepley    for n in range(150, 1350, 100):
12921c1d55bSMatthew G Knepley      sizes.append(n*n)
1306cbfa02cSMatthew G Knepley      nonzeros.append(calculateNonzeros(n))
131*3849a283SMatthew G Knepley  if not args.batch: plotSummary(args.library, args.num, sizes, nonzeros, times, events)
132