forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_perf_table.py
More file actions
286 lines (251 loc) · 10.7 KB
/
create_perf_table.py
File metadata and controls
286 lines (251 loc) · 10.7 KB
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
import argparse
import csv
import os
import re
import xlsxwriter
# -------------------------------
# Helpers and configuration
# -------------------------------
# Known task types (used to pre-initialize tables)
list_of_type_of_tasks = ["all", "mpi", "omp", "seq", "stl", "tbb"]
# Compile patterns once
OLD_PATTERN = re.compile(r"tasks[\/|\\](\w*)[\/|\\](\w*):(\w*):(-*\d*\.\d*)")
NEW_PATTERN = re.compile(
r"(\w+_test_task_(threads|processes))_(\w+)_enabled:(\w*):(-*\d*\.\d*)"
)
# Example formats:
# example_threads_omp_enabled:task_run:0.4749
# example_processes_2_mpi_enabled:pipeline:0.0507
# Accept optional suffix after `_enabled` (e.g., `_enabled_size1000000`) before the colon
SIMPLE_PATTERN = re.compile(
r"(.+?)_(omp|seq|tbb|stl|all|mpi)_enabled[^:]*:(task_run|pipeline):(-*\d*\.\d*)"
)
def _ensure_task_tables(result_tables: dict, perf_type: str, task_name: str) -> None:
if perf_type not in result_tables:
result_tables[perf_type] = {}
if task_name not in result_tables[perf_type]:
result_tables[perf_type][task_name] = {t: -1.0 for t in list_of_type_of_tasks}
def _infer_category(task_name: str) -> str:
return "threads" if "threads" in task_name else "processes"
def _columns_for_category(category: str) -> list[str]:
return (
["seq", "omp", "tbb", "stl", "all"] if category == "threads" else ["seq", "mpi"]
)
def _write_excel_sheet(
workbook,
worksheet,
cpu_num: int,
tasks_list: list[str],
cols: list[str],
table: dict,
):
worksheet.set_column("A:Z", 23)
right_bold_border = workbook.add_format({"bold": True, "right": 2, "bottom": 2})
bottom_bold_border = workbook.add_format({"bold": True, "bottom": 2})
right_border = workbook.add_format({"right": 2})
worksheet.write(0, 0, "cpu_num = " + str(cpu_num), right_bold_border)
# Header (T_x, S, Eff) per column
col = 1
for ttype in cols:
worksheet.write(0, col, f"T_{ttype}({cpu_num})", bottom_bold_border)
col += 1
worksheet.write(
0,
col,
f"S({cpu_num}) = T_seq({cpu_num}) / T_{ttype}({cpu_num})",
bottom_bold_border,
)
col += 1
worksheet.write(
0, col, f"Eff({cpu_num}) = S({cpu_num}) / {cpu_num}", right_bold_border
)
col += 1
# Task rows
row = 1
for task_name in tasks_list:
worksheet.write(
row, 0, task_name, workbook.add_format({"bold": True, "right": 2})
)
row += 1
# Values
row = 1
for task_name in tasks_list:
col = 1
for ttype in cols:
if task_name not in table:
# no data for task at all
worksheet.write(row, col, "—")
col += 1
worksheet.write(row, col, "—")
col += 1
worksheet.write(row, col, "—", right_border)
col += 1
continue
par_time = table[task_name].get(ttype, -1.0)
seq_time = table[task_name].get("seq", -1.0)
if par_time in (0.0, -1.0) or seq_time in (0.0, -1.0):
speed_up = "—"
efficiency = "—"
else:
speed_up = seq_time / par_time
efficiency = speed_up / cpu_num
worksheet.write(row, col, par_time if par_time != -1.0 else "?")
col += 1
worksheet.write(row, col, speed_up)
col += 1
worksheet.write(row, col, efficiency, right_border)
col += 1
row += 1
def _write_csv(path: str, header: list[str], tasks_list: list[str], table: dict):
"""Write raw times (seconds) to CSV so downstream can derive speedups correctly."""
with open(path, "w", newline="") as csvfile:
writer = csv.writer(csvfile)
writer.writerow(header)
for task_name in tasks_list:
task_row = table.get(task_name, {})
seq_time = task_row.get("seq", -1.0)
row = [task_name, (seq_time if seq_time not in (0.0, -1.0) else "?")]
for col_name in header[2:]:
val = task_row.get(col_name.lower(), -1.0)
row.append(val if val != -1.0 else "?")
writer.writerow(row)
parser = argparse.ArgumentParser()
parser.add_argument(
"-i", "--input", help="Input file path (logs of perf tests, .txt)", required=True
)
parser.add_argument(
"-o", "--output", help="Output file path (path to .xlsx table)", required=True
)
args = parser.parse_args()
logs_path = os.path.abspath(args.input)
xlsx_path = os.path.abspath(args.output)
# For each perf_type (pipeline/task_run) store times per task
result_tables = {"pipeline": {}, "task_run": {}}
# Map task name -> category (threads|processes)
task_categories = {}
# Track tasks per category to split output
tasks_by_category = {"threads": set(), "processes": set()}
with open(logs_path, "r") as logs_file:
logs_lines = logs_file.readlines()
for line in logs_lines:
# Handle both old format: tasks/task_type/task_name:perf_type:time
# and new format: namespace_task_type_enabled:perf_type:time
old_result = OLD_PATTERN.findall(line)
new_result = NEW_PATTERN.findall(line)
simple_result = SIMPLE_PATTERN.findall(line)
if len(old_result):
task_name = old_result[0][1]
perf_type = old_result[0][2]
# legacy: track task in threads category by default
_ensure_task_tables(result_tables, perf_type, task_name)
# Unknown category in legacy format; default to threads
task_categories[task_name] = "threads"
tasks_by_category["threads"].add(task_name)
elif len(new_result):
# Extract task name from namespace format; keep it specific (no collapsing to example_*),
# so per-task-number data (processes_2, processes_3, etc.) is preserved.
base = new_result[0][0] # e.g., nesterov_a_test_task_processes
task_category = new_result[0][1] # "threads" or "processes"
task_type_token = new_result[0][2] # e.g., "all", "omp", or "2_mpi"
task_name = f"{base}_{task_type_token}"
perf_type = new_result[0][3]
_ensure_task_tables(result_tables, perf_type, task_name)
task_categories[task_name] = task_category
tasks_by_category[task_category].add(task_name)
elif len(simple_result):
# Extract task name in the current format (prefix already includes category suffix)
task_name = simple_result[0][0]
# Infer category by substring
task_category = "threads" if "threads" in task_name else "processes"
perf_type = simple_result[0][2]
# no set tracking needed; category mapping below
_ensure_task_tables(result_tables, perf_type, task_name)
task_categories[task_name] = task_category
tasks_by_category[task_category].add(task_name)
for line in logs_lines:
# Handle both old format: tasks/task_type/task_name:perf_type:time
# and new format: namespace_task_type_enabled:perf_type:time
old_result = OLD_PATTERN.findall(line)
new_result = NEW_PATTERN.findall(line)
simple_result = SIMPLE_PATTERN.findall(line)
if len(old_result):
task_type = old_result[0][0]
task_name = old_result[0][1]
perf_type = old_result[0][2]
perf_time = float(old_result[0][3])
result_tables[perf_type][task_name][task_type] = perf_time
elif len(new_result):
# Extract task details from namespace format (keep specific task name)
base = new_result[0][0]
task_category = new_result[0][1] # "threads" or "processes"
token = new_result[0][2] # "all", "omp", "seq", or tokens like "2_mpi"
perf_type = new_result[0][3]
perf_time = float(new_result[0][4])
# Split token like "2_mpi" into task suffix and impl to aggregate seq/mpi together
if "_" in token:
suffix, impl = token.rsplit("_", 1)
if impl in list_of_type_of_tasks:
task_name = f"{base}_{suffix}"
task_type = impl
else:
task_name = f"{base}_{token}"
task_type = token
else:
task_name = f"{base}_{token}"
task_type = token
_ensure_task_tables(result_tables, perf_type, task_name)
result_tables[perf_type][task_name][task_type] = perf_time
task_categories[task_name] = task_category
tasks_by_category[task_category].add(task_name)
elif len(simple_result):
# Extract details from the simplified pattern (current logs)
task_name = simple_result[0][0]
# Infer category by substring present in task_name
task_category = "threads" if "threads" in task_name else "processes"
task_type = simple_result[0][1]
perf_type = simple_result[0][2]
perf_time = float(simple_result[0][3])
if perf_type not in result_tables:
result_tables[perf_type] = {}
if task_name not in result_tables[perf_type]:
result_tables[perf_type][task_name] = {}
for ttype in list_of_type_of_tasks:
result_tables[perf_type][task_name][ttype] = -1.0
result_tables[perf_type][task_name][task_type] = perf_time
task_categories[task_name] = task_category
tasks_by_category[task_category].add(task_name)
for table_name, table_data in result_tables.items():
# Prepare two workbooks/CSVs: threads and processes
for category in ["threads", "processes"]:
tasks_list = sorted(tasks_by_category[category])
if not tasks_list:
continue
# Use appropriate env var per category
if category == "threads":
cpu_num_env = os.environ.get("PPC_NUM_THREADS")
if cpu_num_env is None:
raise EnvironmentError(
"Required environment variable 'PPC_NUM_THREADS' is not set."
)
else:
cpu_num_env = os.environ.get("PPC_NUM_PROC")
if cpu_num_env is None:
raise EnvironmentError(
"Required environment variable 'PPC_NUM_PROC' is not set."
)
cpu_num = int(cpu_num_env)
cols = _columns_for_category(category)
# Excel
wb_path = os.path.join(
xlsx_path, f"{category}_" + table_name + "_perf_table.xlsx"
)
workbook = xlsxwriter.Workbook(wb_path)
worksheet = workbook.add_worksheet()
_write_excel_sheet(workbook, worksheet, cpu_num, tasks_list, cols, table_data)
workbook.close()
# CSV
header = ["Task", "SEQ"] + [c.upper() for c in cols[1:]]
csv_path = os.path.join(
xlsx_path, f"{category}_" + table_name + "_perf_table.csv"
)
_write_csv(csv_path, header, tasks_list, table_data)