xref: /petsc/src/benchmarks/benchmarkAssembly.py (revision ff218e97a57ed641f3ebc93f697e38ef0f3aa217)
1#!/usr/bin/env python
2import os
3from benchmarkExample import PETScExample
4
5def calculateNonzeros(n):
6  num = 0
7  # corners
8  num += 2*3 + 2*4
9  # edges
10  num += 4*(n-2)*5
11  # interior
12  num += (n-2)*(n-2)*7
13  return num
14
15def processSummary(moduleName, times, events):
16  '''Process the Python log summary into plot data'''
17  m = __import__(moduleName)
18  reload(m)
19  # Total Time
20  times.append(m.Time[0])
21  # Common events
22  #   Add the time and flop rate
23  for stageName, eventName in [('GPU_Stage','MatCUSPSetValBch'), ('CPU_Stage','ElemAssembly')]:
24    s = getattr(m, stageName)
25    if not eventName in events:
26      events[eventName] = []
27    events[eventName].append((s.event[eventName].Time[0], s.event[eventName].Flops[0]/(s.event[eventName].Time[0] * 1e6)))
28  return
29
30def plotSummary(library, num, sizes, nonzeros, times, events):
31  from pylab import legend, plot, show, title, xlabel, ylabel, ylim
32  import numpy as np
33  showEventTime      = False
34  showTimePerRow     = False
35  showTimePerNonzero = True
36  print events
37  if showEventTime:
38    data  = []
39    names = []
40    for event, style in [('MatCUSPSetValBch', 'b-'), ('ElemAssembly', 'b:')]:
41      names.append(event)
42      data.append(sizes)
43      data.append(np.array(events[event])[:,0])
44      data.append(style)
45    plot(*data)
46    title('Performance on '+library+' Example '+str(num))
47    xlabel('Number of Dof')
48    ylabel('Time (s)')
49    legend(names, 'upper left', shadow = True)
50    show()
51  if showTimePerRow:
52    data  = []
53    names = []
54    for event, style in [('MatCUSPSetValBch', 'b-'), ('ElemAssembly', 'b:')]:
55      names.append(event)
56      data.append(sizes)
57      rows = np.sqrt(sizes)
58      data.append(np.array(events[event])[:,0]/rows/3)
59      data.append(style)
60    plot(*data)
61    title('Performance on '+library+' Example '+str(num))
62    xlabel('Number of Dof')
63    ylabel('Time/Row (s)')
64    legend(names, 'upper left', shadow = True)
65    show()
66  if showTimePerNonzero:
67    data  = []
68    names = []
69    for event, style in [('MatCUSPSetValBch', 'b-'), ('ElemAssembly', 'b:')]:
70      names.append(event)
71      data.append(sizes)
72      data.append(np.array(events[event])[:,0]/nonzeros * 10**9)
73      data.append(style)
74    plot(*data)
75    title('Performance on '+library+' Example '+str(num))
76    xlabel('Number of Dof')
77    ylabel('Time/Nonzero (ns)')
78    legend(names, 'center right', shadow = True)
79    show()
80  return
81
82if __name__ == '__main__':
83  library = 'KSP'
84  num     = 4
85  ex      = PETScExample(library, num, log_summary_python='summary.py', preload='off')
86  if 0:
87    sizes    = []
88    nonzeros = []
89    times    = []
90    events   = {}
91    for n in [10, 20, 50, 100, 150, 200, 250, 300, 350]:
92      ex.run(da_grid_x=n, da_grid_y=n, cusp_synchronize=1)
93      sizes.append(n*n)
94      nonzeros.append(calculateNonzeros(n))
95      processSummary('summary', times, events)
96    plotSummary(library, num, sizes, nonzeros, times, events)
97  else:
98    sizes    = []
99    nonzeros = []
100    times    = []
101    for n in range(150, 1350, 100):
102      sizes.append(n*n)
103      nonzeros.append(calculateNonzeros(n))
104    baconostEvents = {'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)], '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)]}
105    plotSummary(library, num, sizes, nonzeros, times, baconostEvents)
106