forked from learning-process/parallel_programming_course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_tests.py
More file actions
executable file
·181 lines (150 loc) · 6.28 KB
/
run_tests.py
File metadata and controls
executable file
·181 lines (150 loc) · 6.28 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
#!/usr/bin/env python3
import os
import shlex
import subprocess
import platform
from pathlib import Path
def init_cmd_args():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
"--running-type",
required=True,
choices=["threads", "processes", "performance"],
help="Specify the execution mode. Choose 'threads' for multithreading or 'processes' for multiprocessing."
)
parser.add_argument(
"--additional-mpi-args",
required=False,
default="",
help="Additional MPI arguments to pass to the mpirun command (optional)."
)
parser.add_argument(
"--counts",
nargs="+",
type=int,
help="List of process/thread counts to run sequentially"
)
args = parser.parse_args()
_args_dict = vars(args)
return _args_dict
class PPCRunner:
def __init__(self):
self.__ppc_num_threads = None
self.__ppc_num_proc = None
self.__ppc_env = None
self.work_dir = None
self.valgrind_cmd = "valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all"
if platform.system() == "Windows":
self.mpi_exec = "mpiexec"
else:
self.mpi_exec = "mpirun"
@staticmethod
def __get_project_path():
script_path = Path(__file__).resolve() # Absolute path of the script
script_dir = script_path.parent # Directory containing the script
return script_dir.parent
def setup_env(self, ppc_env):
self.__ppc_env = ppc_env
self.__ppc_num_threads = self.__ppc_env.get("PPC_NUM_THREADS")
if self.__ppc_num_threads is None:
raise EnvironmentError("Required environment variable 'PPC_NUM_THREADS' is not set.")
self.__ppc_env["OMP_NUM_THREADS"] = self.__ppc_num_threads
self.__ppc_num_proc = self.__ppc_env.get("PPC_NUM_PROC")
if self.__ppc_num_proc is None:
raise EnvironmentError("Required environment variable 'PPC_NUM_PROC' is not set.")
if (Path(self.__get_project_path()) / "install").exists():
self.work_dir = Path(self.__get_project_path()) / "install" / "bin"
else:
self.work_dir = Path(self.__get_project_path()) / "build" / "bin"
def __run_exec(self, command):
result = subprocess.run(command, shell=False, env=self.__ppc_env)
if result.returncode != 0:
raise Exception(f"Subprocess return {result.returncode}.")
@staticmethod
def __get_gtest_settings(repeats_count, type_task):
command = [
f"--gtest_repeat={repeats_count}",
"--gtest_recreate_environments_when_repeating",
"--gtest_color=0",
"--gtest_shuffle",
f"--gtest_filter=*{type_task}*",
]
return command
def run_threads(self):
if platform.system() == "Linux" and not self.__ppc_env.get("PPC_ASAN_RUN"):
for task_type in ["seq", "stl"]:
self.__run_exec(
shlex.split(self.valgrind_cmd)
+ [str(self.work_dir / 'ppc_func_tests')]
+ self.__get_gtest_settings(1, '_' + task_type + '_')
)
for task_type in ["omp", "seq", "stl", "tbb"]:
self.__run_exec(
[str(self.work_dir / 'ppc_func_tests')] + self.__get_gtest_settings(3, '_' + task_type + '_')
)
def run_core(self):
if platform.system() == "Linux" and not self.__ppc_env.get("PPC_ASAN_RUN"):
self.__run_exec(
shlex.split(self.valgrind_cmd)
+ [str(self.work_dir / 'core_func_tests')]
+ self.__get_gtest_settings(1, '*')
+ ["--gtest_filter=*:-*_disabled_valgrind"]
)
self.__run_exec(
[str(self.work_dir / 'core_func_tests')] + self.__get_gtest_settings(1, '*')
)
def run_processes(self, additional_mpi_args):
ppc_num_proc = self.__ppc_env.get("PPC_NUM_PROC")
if ppc_num_proc is None:
raise EnvironmentError("Required environment variable 'PPC_NUM_PROC' is not set.")
mpi_running = [self.mpi_exec] + shlex.split(additional_mpi_args) + ["-np", ppc_num_proc]
if not self.__ppc_env.get("PPC_ASAN_RUN"):
for task_type in ["all", "mpi"]:
self.__run_exec(
mpi_running
+ [str(self.work_dir / 'ppc_func_tests')]
+ self.__get_gtest_settings(10, '_' + task_type)
)
def run_performance(self):
if not self.__ppc_env.get("PPC_ASAN_RUN"):
mpi_running = [self.mpi_exec, "-np", self.__ppc_num_proc]
for task_type in ["all", "mpi"]:
self.__run_exec(
mpi_running
+ [str(self.work_dir / 'ppc_perf_tests')]
+ self.__get_gtest_settings(1, '_' + task_type)
)
for task_type in ["omp", "seq", "stl", "tbb"]:
self.__run_exec(
[str(self.work_dir / 'ppc_perf_tests')] + self.__get_gtest_settings(1, '_' + task_type)
)
def _execute(args_dict, env):
runner = PPCRunner()
runner.setup_env(env)
if args_dict["running_type"] in ["threads", "processes"]:
runner.run_core()
if args_dict["running_type"] == "threads":
runner.run_threads()
elif args_dict["running_type"] == "processes":
runner.run_processes(args_dict["additional_mpi_args"])
elif args_dict["running_type"] == "performance":
runner.run_performance()
else:
raise Exception("running-type is wrong!")
if __name__ == "__main__":
args_dict = init_cmd_args()
counts = args_dict.get("counts")
if counts:
for count in counts:
env_copy = os.environ.copy()
if args_dict["running_type"] == "threads":
env_copy["PPC_NUM_THREADS"] = str(count)
env_copy.setdefault("PPC_NUM_PROC", "1")
elif args_dict["running_type"] == "processes":
env_copy["PPC_NUM_PROC"] = str(count)
env_copy.setdefault("PPC_NUM_THREADS", "1")
print(f"Executing with {args_dict['running_type']} count: {count}")
_execute(args_dict, env_copy)
else:
_execute(args_dict, os.environ.copy())