-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathperformance_study.py
484 lines (405 loc) · 14 KB
/
performance_study.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
#!/usr/bin/env python3
# Shared functions to use across analyses.
import json
import os
import re
from matplotlib.ticker import FormatStrFormatter
import matplotlib.pylab as plt
import pandas
import seaborn as sns
import yaml
sns.set_theme(style="whitegrid", palette="muted")
sns.set_style("whitegrid", {"legend.frameon": True})
# Manually created color palette
cloud_prefixes = [
"azure/aks",
"aws/eks",
"google/gke",
"google/compute-engine",
"aws/parallel-cluster",
"on-premises/dane",
"azure/cyclecloud",
"on-premises/lassen",
]
cloud_prefixes.sort()
colors = sns.color_palette("muted", len(cloud_prefixes))
hexcolors = colors.as_hex()
colors = {}
for cloud in cloud_prefixes:
colors[cloud] = hexcolors.pop(0)
def match_color(cloud):
"""
Match the color for an environment
"""
# We assume the environ name (e.g., azure/aks) is shorter than
# the one provided (e.g., azure/aks/cpu)
for environ, color in colors.items():
if environ in cloud:
return color
raise ValueError(f"Did not find color for {cloud}")
def read_yaml(filename):
with open(filename, "r") as fd:
content = yaml.safe_load(fd)
return content
def read_json(filename):
with open(filename, "r") as fd:
content = json.loads(fd.read())
return content
def recursive_find(base, pattern="*.*"):
"""
Recursively find and yield directories matching a glob pattern.
"""
for root, dirnames, filenames in os.walk(base):
for dirname in dirnames:
if not re.search(pattern, dirname):
continue
yield os.path.join(root, dirname)
def recursive_find_files(base, pattern="*.*"):
"""
Recursively find and yield directories matching a glob pattern.
"""
for root, _, filenames in os.walk(base):
for filename in filenames:
if not re.search(pattern, filename):
continue
yield os.path.join(root, filename)
def find_inputs(input_dir, pattern="*.*", include_on_premises=False):
"""
Find inputs (times results files)
"""
files = []
for filename in recursive_find(input_dir, pattern):
# We only have data for small
if not include_on_premises and "on-premises" in filename:
continue
files.append(filename)
return files
def get_outfiles(base, pattern="[.]out"):
"""
Recursively find and yield directories matching a glob pattern.
"""
for root, _, filenames in os.walk(base):
for filename in filenames:
if not re.search(pattern, filename):
continue
yield os.path.join(root, filename)
def read_file(filename):
with open(filename, "r") as fd:
content = fd.read()
return content
def write_json(obj, filename):
with open(filename, "w") as fd:
fd.write(json.dumps(obj, indent=4))
def write_file(text, filename):
with open(filename, "w") as fd:
fd.write(text)
class ExperimentNameParser:
"""
Shared parser to convert directory path into components.
"""
def __init__(self, filename, indir):
self.filename = filename
self.indir = indir
self.parse()
def show(self):
print(self.cloud, self.env, self.env_type, self.size)
def parse(self):
parts = self.filename.replace(self.indir + os.sep, "").split(os.sep)
# These are consistent across studies
self.cloud = parts.pop(0)
self.env = parts.pop(0)
self.env_type = parts.pop(0)
size = parts.pop(0)
# Experiment is the plot label
self.experiment = os.path.join(self.cloud, self.env, self.env_type)
# Prefix is an identifier for parsed flux metadata, jobspec and events
self.prefix = os.path.join(self.experiment, size)
# If these are in the size, they are additional identifiers to indicate the
# environment type. Add to it instead of the size. I could skip some of these
# but I want to see the differences.
if "-" in size:
size, _ = size.split("-", 1)
self.size = int(size.replace("size", ""))
class ResultParser:
"""
Helper class to parse results into a data frame.
"""
def __init__(self, app):
self.init_df()
self.idx = 0
self.app = app
def init_df(self):
"""
Initialize an empty data frame for the application
"""
self.df = pandas.DataFrame(
columns=[
"experiment",
"cloud",
"env",
"env_type",
"nodes",
"application",
"metric",
"value",
"gpu_count",
]
)
def set_context(self, cloud, env, env_type, size, qualifier=None, gpu_count=0):
"""
Set the context for the next stream of results.
These are just fields to add to the data frame.
"""
self.cloud = cloud
self.env = env
self.env_type = env_type
self.size = size
# Extra metadata to add to experiment name
self.qualifier = qualifier
self.gpu_count = gpu_count
def add_result(self, metric, value):
"""
Add a result to the table
"""
# Unique identifier for the experiment plot
# is everything except for size
experiment = os.path.join(self.cloud, self.env, self.env_type)
if self.qualifier is not None:
experiment = os.path.join(experiment, self.qualifier)
self.df.loc[self.idx, :] = [
experiment,
self.cloud,
self.env,
self.env_type,
self.size,
self.app,
metric,
value,
self.gpu_count,
]
self.idx += 1
class ProblemSizeParser(ResultParser):
"""
Extended ResultParser that includes a problem size.
"""
def init_df(self):
"""
Initialize an empty data frame for the application
"""
self.df = pandas.DataFrame(
columns=[
"experiment",
"cloud",
"env",
"env_type",
"nodes",
"application",
"problem_size",
"metric",
"value",
"gpu_count",
]
)
def add_result(self, metric, value, problem_size):
"""
Add a result to the table
"""
# Unique identifier for the experiment plot
# is everything except for size
experiment = os.path.join(self.cloud, self.env, self.env_type)
if self.qualifier is not None:
experiment = os.path.join(experiment, self.qualifier)
self.df.loc[self.idx, :] = [
experiment,
self.cloud,
self.env,
self.env_type,
self.size,
self.app,
problem_size,
metric,
value,
self.gpu_count,
]
self.idx += 1
def parse_flux_metadata(item):
"""
Jobs run with flux have a jobspec and event metadata at the end
"""
item, metadata = item.split("START OF JOBSPEC")
metadata, events = metadata.split("START OF EVENTLOG")
jobspec = json.loads(metadata)
events = [json.loads(x) for x in events.split("\n") if x]
# QUESTION: is this the correct event, shell.start? I chose over init
# Note that I assume we want to start at init and done.
# This unit is in seconds
start = [x for x in events if x["name"] == "shell.start"][0]["timestamp"]
done = [x for x in events if x["name"] == "done"][0]["timestamp"]
duration = done - start
metadata = {"jobspec": jobspec, "events": events}
return item, duration, metadata
def parse_slurm_duration(item):
"""
Get the start and end time from the slurm output.
We want this to throwup if it is missing from the output.
"""
start = int(
[x for x in item.split("\n") if "Start time" in x][0].rsplit(" ", 1)[-1]
)
done = int([x for x in item.split("\n") if "End time" in x][0].rsplit(" ", 1)[-1])
return done - start
def remove_slurm_duration(item):
"""
Remove the start and end time from the slurm output.
"""
keepers = [x for x in item.split("\n") if not re.search("^(Start|End) time", x)]
return "\n".join(keepers)
def skip_result(dirname, filename):
"""
Return true if we should skip the result path.
"""
# I don't know if these are results or testing, skipping for now
# They are from aws parallel-cluster CPU
if os.path.join("experiment", "data") in filename:
return True
if "compute-toolkit" in filename:
return True
# These are OK
if "aks/cpu/size" in filename and "kripke" in filename:
return False
# These were redone with a placement group
if (
"aks/cpu/size" in filename
and "placement" not in filename
and "256" not in filename
and "128" not in filename
):
return True
return (
dirname.startswith("_")
or "configs" in filename
or "no-add" in filename
or "noefa" in filename
or "attempt-1" in filename
or "no-placement" in filename
or "shared-memory" in filename
)
def set_group_color_properties(plot_name, color_code, label):
# https://www.geeksforgeeks.org/how-to-create-boxplots-by-group-in-matplotlib/
for k, v in plot_name.items():
plt.setp(plot_name.get(k), color=color_code)
# use plot function to draw a small line to name the legend.
plt.plot([], c=color_code, label=label)
plt.legend()
def make_plot(
df,
title,
ydimension,
xdimension,
xlabel,
ylabel,
palette=None,
ext="png",
plotname="lammps",
hue=None,
outdir="img",
log_scale=False,
do_round=False,
round_by=3,
):
"""
Helper function to make common plots.
This also adds the normalized version
Speedup: typically we do it in a way that takes into account serial/parallel.
Speedup definition - normalize by performance at smallest size tested.
This means taking each value and dividing by result at smallest test size (relative speed up)
to see if conveys information better.
"""
ext = ext.strip(".")
plt.figure(figsize=(7, 6))
sns.set_style("whitegrid")
ax = sns.stripplot(
x=xdimension,
y=ydimension,
hue=hue,
data=df,
palette=palette,
)
plt.title(title, fontsize=16)
ax.set_xlabel(xlabel, fontsize=16)
ax.set_ylabel(ylabel, fontsize=16)
if log_scale is True:
plt.gca().yaxis.set_major_formatter(
plt.ScalarFormatter(useOffset=True, useMathText=True)
)
if do_round is True:
ax.yaxis.set_major_formatter(FormatStrFormatter(f"%.{round_by}f"))
plt.legend(facecolor="white")
plt.tight_layout()
plt.savefig(os.path.join(outdir, f"{plotname}.{ext}"))
plt.clf()
# If we have more than one node size, normalize by smallest
if len(df.experiment.unique()) <= 1:
return
# We will assemble a column of speedups.
# 1. Take smallest size for the experiment environment
# 2. Calculate median metric for that size
# 3. Calculate how many times that size goes into larger sizes (e.g., 32 goes into 64 twice)
# 4. Multiply the median metric by that multiplier to get expected speedup
# First, normalize the index so we can reliably reference it later
df.index = list(range(0, df.shape[0]))
df.loc[:, "speedup"] = [None] * df.shape[0]
# We have to do by each experiment separately, because they don't all have the smallest size...
# We can reassemble after with the indices
for experiment in df.experiment.unique():
# Subset of data frame just for that cloud and environment
subset = df[df.experiment == experiment]
# The smallest size (important, not all cloud/environments have the very smallest)
# And get multiplier for all sizes based on the smallest. "column" would be a column
# of multipliers, one specific to each row (that has a size)
smallest = sorted(subset[xdimension].unique())[0]
multipliers = {x: x / smallest for x in subset[xdimension].unique()}
original_values = subset[ydimension].values
# xdimension here is usually nodes or gpu_count
column = [multipliers[x] for x in subset[xdimension].values]
# Get the median for the smallest sizes, organized by experiment
medians = (
subset[subset[xdimension] == smallest]
.groupby("experiment")[ydimension]
.median()
)
medians = medians.to_dict()
experiments = list(subset.experiment.values)
# And we want to divide each experiment by its median at the smallest size
speedup = []
for i, experiment in enumerate(experiments):
multiplier = column[i]
original_value = original_values[i]
# divide by the median of the smallest size, not the multiplier
speedup.append(original_value / medians[experiment])
# Fill the speedup in back at the experiment indices
df.loc[subset.index, "speedup"] = speedup
# Replace the initial value of interest with the speedup (this gets thrown away after plot)
df[ydimension] = df["speedup"]
plt.figure(figsize=(7, 6))
sns.set_style("whitegrid")
ax = sns.stripplot(
x=xdimension,
y=ydimension,
hue=hue,
data=df,
palette=palette,
)
plt.title(title + " Speedup", fontsize=16)
ax.set_xlabel(xlabel, fontsize=14)
ax.set_ylabel(ylabel + " (Speedup)", fontsize=14)
if log_scale is True:
plt.gca().yaxis.set_major_formatter(
plt.ScalarFormatter(useOffset=True, useMathText=True)
)
if do_round is True:
ax.yaxis.set_major_formatter(FormatStrFormatter(f"%.{round_by}f"))
plt.legend(facecolor="white")
plt.tight_layout()
plt.savefig(os.path.join(outdir, f"{plotname}-speedup-scaled.{ext}"))
plt.clf()