-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplot_profiles.py
executable file
·333 lines (256 loc) · 12 KB
/
plot_profiles.py
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
"""
Tool for ploting profiling results from Dedalus script runs.
Usage:
plot_profiles.py [options]
Options:
--profile=<profile> Profile data to plot (e.g., runtime, setup, warmup) [default: runtime]
--thresh=<thresh> Theshold for trimming output, as a fraction of total time [default: 0.02]
--max_profiles=<max> Maximum number of profiles to output [default: 50]
--directory=<dir> Location of profile data [default: profiles]
--verbose Display text verbose output to screen
"""
import os
import pstats
import numpy as np
import pathlib
import pickle
import matplotlib as mpl
mpl.use('Agg')
import matplotlib.pyplot as plt
def make_graph(profile, output_png_file, node_thresh=0.5):
import subprocess as sub
proc_graph = sub.Popen([f'python3 -m gprof2dot --skew 0.5 -n {node_thresh:f} -f pstats {profile}'],
stdout=sub.PIPE, stderr=sub.PIPE,
universal_newlines=True,
shell=True)
# the directed graph is produced by proc_graph.stdout
proc_dot = sub.Popen([f'dot -Tpng -o {output_png_file}'],
stdin=proc_graph.stdout,
stdout=sub.PIPE, stderr=sub.PIPE, universal_newlines=True,
shell=True)
stdout, stderr = proc_dot.communicate()
def sort_dict(dict_to_sort):
sorted_list = sorted(dict_to_sort.items(), key=lambda data_i: test_criteria(data_i[1]), reverse=True)
return sorted_list
def sort_by_total(joined_stat):
return sorted(joined_stat.items(), key=lambda kv: np.sum(kv[1]), reverse=True)
def test_criteria(data):
return np.max(data)
def natural_sort(l):
import re
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(l, key=alphanum_key)
def clean_display(ax):
# from http://nbviewer.ipython.org/gist/anonymous/5357268
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['left'].set_visible(False)
ax.yaxis.set_ticks_position('none')
ax.xaxis.set_ticks_position('none')
def plot_per_core_performance(stats_pdf_dict,
label='', N_profiles=50,
thresh=0.02, verbose=False,
dir=pathlib.Path('.')):
cmap = mpl.colormaps['tab20']
cmap_group = mpl.colormaps['tab10']
#set_plot_defaults(rcParams)
sorted_list = sort_by_total(stats_pdf_dict)
composite_data_set = []
composite_label = []
composite_key_label = []
total_time = []
for item in tottime.items():
total_time.append(np.mean(item[1]))
total_time = np.sum(total_time)
print(total_time)
fig_stacked = plt.figure(figsize=[8,8/2])
ax_stacked = fig_stacked.add_subplot(1,1,1)
fig_group = plt.figure(figsize=[8,8/2])
ax_group = fig_group.add_subplot(1,1,1)
group = {'linear algebra':["gssv", "apply_sparse", "superlu", "linalg"],
'MPI':["mpi4py.MPI", "fftw.fftw_wrappers.Transpose", "fftw_RL_to_CL", "fftw_CL_to_RL", "localize_columns", "localize_rows", "RL_fftw", "CL_fftw"],
'FFT':["ifft", "_dct", "rfft", "unpack_rescale", 'repack_rescale', "forward", "backward"],
'arithmetic':["(operate)", "einsum", "arithmetic"],
'copy':["copyto", "gather_inputs", "gather_outputs", 'scatter_inputs', "scatter_outputs"],
'exclude': ["load_dynamic", "__init__", "<frozen", 'importlib']}
group_data = {key:{} for key in group}
group_data['other'] = {}
N_cores = 0
for i_sort, (func, data) in enumerate(sorted_list):
if i_sort+1 == N_profiles:
break
#print(i_sort, func, data)
found_category = False
data = np.array(data)
for key in group:
tests = [item.lower() for item in group[key]]
if (any(item.lower() in func[0].lower() for item in group[key]) or any(item.lower() in func[2].lower() for item in group[key])) and test_criteria(data)/total_time > thresh:
if verbose:
print(f"found {key:s} call: {func[2]} in {tests} at {i_sort:d}")
group_data[key][func] = data
found_category = True
if not found_category and test_criteria(data)/total_time > thresh:
group_data['other'][func] = data
N_cores = max(N_cores, data.size)
N_profiles = 0
for key in group_data:
N_profiles += len(group_data[key])
if verbose:
for func in group_data['exclude']:
print(f"found excluded call: {func[2]}, popping...")
#excluded = group_data.pop('exclude', None)
#if verbose: print(excluded)
routine_text = "top {:d} routines for {:s}".format(N_profiles, label)
if verbose:
print()
print("{:60s}".format(routine_text)," min mean max (mean%total) (m%t cum.)")
print(120*"-")
def percent_time(sub_time):
sub_string = "{:4.2f}%".format(100*sub_time/total_time)
return sub_string
if N_cores > 200:
N_bins = 100
logscale = True
else:
N_bins = int(N_cores/4)
if N_bins == 0 : N_bins = 1
logscale = False
i_fig = 0
running = 0
previous_group_data = np.zeros(N_cores)
previous_data = np.zeros(N_cores)
for i_group, key in enumerate(group_data):
group_time = np.zeros(N_cores)
first_item = True
for func, data_list in group_data[key].items():
data = np.array(data_list)
N_missing = previous_data.size - data.size
if N_missing != 0:
if verbose:
print("missing {:d} values; setting to zero".format(N_missing))
for i in range(N_missing):
data_list.insert(N_missing*(i+1)-1, 0)
data = np.array(data_list)
group_time += data
if func[0] == '~':
title_string = func[2]
else:
title_string = "{:s}:{:d}:{:s}".format(*func)
running += np.mean(data)
timing_data_string = "{:8.2g} |{:8.2g} |{:8.2g} ({:s}) ({:s})".format(np.min(data), np.mean(data), np.max(data), percent_time(np.mean(data)), percent_time(running))
if verbose:
if first_item:
print(f'{key:>60s} :')
first_item = False
print("{:60s} = {:s}".format(title_string, timing_data_string))
timing_data_string = "min {:s} | {:s} | {:s} max".format(percent_time(np.min(data)), percent_time(np.mean(data)), percent_time(np.max(data)))
title_string += "\n{:s}".format(timing_data_string)
key_label = "{:s} {:s}".format(percent_time(np.mean(data)),func[2])
short_label = "{:s}".format(percent_time(np.mean(data)))
composite_data_set.append([data])
composite_label.append(short_label)
composite_key_label.append(key_label)
q_color = cmap(i_fig) #next(ax_stacked._get_lines.prop_cycler)['color']
fig = plt.figure(figsize=[8,8/2])
# pdf plot over many cores
ax1 = fig.add_subplot(1,2,1)
#hist_values, bin_edges = np.histogram(data, bins=N_bins)
#ax1.barh(hist_values, bin_edges[1:])
ax1.hist(data, bins=N_bins, orientation='horizontal', log=logscale, linewidth=0, color=q_color)
ax1.set_xlabel("N cores/bin")
ax1.set_ylabel("time (sec)")
ax1.grid(axis = 'x', color ='white', linestyle='-')
# bar plot for each core
ax2 = fig.add_subplot(1,2,2)
ax2.bar(np.arange(N_cores)+1, data, linewidth=0, width=1, color=q_color)
ax2.set_xlim(0.5, N_cores+0.5)
ax2.set_xlabel("core #")
clean_display(ax2)
ax2.grid(axis = 'y', color ='white', linestyle='-')
# end include
ax1.set_ylim(0, 1.1*np.max(data))
ax2.set_ylim(0, 1.1*np.max(data))
fig.suptitle(title_string)
fig.tight_layout()
fig.savefig(dir / f'{label:s}_{i_fig+1:06d}.png', dpi=200)
plt.close(fig)
ax_stacked.bar(np.arange(N_cores)+1, data, bottom=previous_data, label=short_label, linewidth=0,
width=1, color=q_color)
previous_data += data
i_fig += 1
ax_group.bar(np.arange(N_cores)+1, group_time, bottom=previous_group_data, label=key, linewidth=0,
width=1, color=cmap_group(i_group))
previous_group_data += group_time
clean_display(ax_stacked)
ax_stacked.set_xlim(0.5, N_cores+0.5)
ax_stacked.set_xlabel('core #')
ax_stacked.set_ylabel('total time (sec)')
#ax_stacked.legend(loc='upper left', bbox_to_anchor=(1.,1.), fontsize=10)
ax_stacked.set_title("per core timings for routines above {:g}% total time".format(thresh*100))
ax_stacked.grid(axis = 'y', color ='white', linestyle='-')
#ax_stacked.set_aspect(N_data/total_time)
points_per_data = 10
fig_x_size = 8
fig_stacked.tight_layout()
fig_stacked.savefig(dir / f"{label:s}_per_core_timings.png", dpi=max(200, N_cores*points_per_data/fig_x_size))
plt.close(fig_stacked)
clean_display(ax_group)
ax_group.legend()
ax_group.set_xlim(0.5, N_cores+0.5)
ax_group.set_xlabel('core #')
ax_group.set_ylabel('total time (sec)')
#ax_stacked.legend(loc='upper left', bbox_to_anchor=(1.,1.), fontsize=10)
ax_group.set_title("per core timings for groups, routines above {:g}% total time".format(thresh*100))
ax_group.grid(axis = 'y', color ='white', linestyle='-')
fig_group.tight_layout()
fig_group.savefig(dir / f"{label:s}_group_per_core_timings.png", dpi=max(200, N_cores*points_per_data/fig_x_size))
plt.close(fig_group)
# pdf plot over many cores
fig_composite = plt.figure(figsize=[8,8/2])
ax_composite = fig_composite.add_subplot(1,1,1)
# print(composite_data_set)
# n, bins, patches = ax_composite.hist(composite_data_set, bins=N_bins, orientation='vertical', log=logscale, linewidth=0, stacked=True,
# label=composite_label)
#
# clean_display(ax_composite)
# ax_composite.grid(axis = 'y', color ='white', linestyle='-')
#
# ax_composite.set_ylabel("N cores/bin")
# ax_composite.set_xlabel("total time (sec)")
# ax_composite.set_ylim(0, 1.1*np.max(composite_data_set))
# ax_composite.legend(loc='upper left', bbox_to_anchor=(1.,1.), fontsize=10)
#
# fig_composite.suptitle("composite PDF for routines above {:g}% total time".format(thresh*100))
# fig_composite.savefig(label+'_composite.png', dpi=200)
# plt.close(fig_composite)
#
# fig_key = plt.figure()
# plt.figlegend(patches, composite_key_label, 'center')
# #ax_key.legend(loc='center')
# fig_key.savefig(label+"_composite_key.png")
# plt.close(fig_key)
def read_database(file):
with (open(file, "rb")) as f:
data = pickle.load(f)
primcalls = data['primcalls']
totcalls = data['totcalls']
tottime = data['tottime']
cumtime = data['cumtime']
#average_runtime = shelf['average_runtime']
#n_processes = shelf['n_processes']
return primcalls, totcalls, tottime, cumtime
if __name__ == "__main__":
from docopt import docopt
args = docopt(__doc__)
dir = pathlib.Path(args['--directory'])
joint_file = str(args['--profile'])+'.prof'
profiles_file = str(args['--profile'])+'_parallel.pickle'
summed_stats = pstats.Stats(str(dir / joint_file))
primcalls, totcalls, tottime, cumtime = read_database(dir / profiles_file)
# per-core plots
plot_per_core_performance(tottime, label="tt", thresh=float(args['--thresh']), verbose=args['--verbose'], N_profiles=int(float(args['--max_profiles'])), dir=dir)
# Graphs
make_graph(str(dir / joint_file),
str(dir / f'graph_above_{args["--thresh"]}.png'),
node_thresh=100*float(args["--thresh"]))