|
| 1 | +import os |
| 2 | +import subprocess |
| 3 | +import platform |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | + |
| 7 | +def init_cmd_args(): |
| 8 | + import argparse |
| 9 | + parser = argparse.ArgumentParser() |
| 10 | + parser.add_argument( |
| 11 | + "--running-type", |
| 12 | + required=True, |
| 13 | + choices=["threads", "processes", "performance", "performance-list"], |
| 14 | + help="Specify the execution mode. Choose 'threads' for multithreading or 'processes' for multiprocessing." |
| 15 | + ) |
| 16 | + parser.add_argument( |
| 17 | + "--additional-mpi-args", |
| 18 | + required=False, |
| 19 | + default="", |
| 20 | + help="Additional MPI arguments to pass to the mpirun command (optional)." |
| 21 | + ) |
| 22 | + args = parser.parse_args() |
| 23 | + _args_dict = vars(args) |
| 24 | + return _args_dict |
| 25 | + |
| 26 | + |
| 27 | +class PPCRunner: |
| 28 | + def __init__(self): |
| 29 | + self.work_dir = None |
| 30 | + self.valgrind_cmd = "valgrind --error-exitcode=1 --leak-check=full --show-leak-kinds=all" |
| 31 | + |
| 32 | + if platform.system() == "Windows": |
| 33 | + self.ocv_script_name = "setup_vars_opencv4.cmd" |
| 34 | + self.ocv_script_path = Path("build/ppc_opencv/install/") / self.ocv_script_name |
| 35 | + else: |
| 36 | + self.ocv_script_name = "setup_vars_opencv4.sh" |
| 37 | + self.ocv_script_path = Path("build/ppc_opencv/install/bin/") / self.ocv_script_name |
| 38 | + |
| 39 | + if platform.system() == "Windows": |
| 40 | + self.mpi_exec = "mpiexec" |
| 41 | + else: |
| 42 | + self.mpi_exec = "mpirun" |
| 43 | + |
| 44 | + @staticmethod |
| 45 | + def __get_project_path(): |
| 46 | + script_path = Path(__file__).resolve() # Absolute path of the script |
| 47 | + script_dir = script_path.parent # Directory containing the script |
| 48 | + return script_dir.parent |
| 49 | + |
| 50 | + @staticmethod |
| 51 | + def __source_script(script_path): |
| 52 | + if platform.system() == "Windows": |
| 53 | + return |
| 54 | + command = f"bash -c 'source {script_path} && env'" |
| 55 | + result = subprocess.run(command, stdout=subprocess.PIPE, shell=True, text=True) |
| 56 | + if result.returncode == 0: |
| 57 | + # Parse the output environment variables |
| 58 | + env_vars = {} |
| 59 | + for line in result.stdout.splitlines(): |
| 60 | + if '=' in line: |
| 61 | + key, value = line.split("=", 1) |
| 62 | + env_vars[key] = value |
| 63 | + return env_vars |
| 64 | + else: |
| 65 | + print(f"Failed to source script: {script_path}") |
| 66 | + return {} |
| 67 | + |
| 68 | + def setup_env(self): |
| 69 | + if os.path.isfile(Path(self.__get_project_path()) / self.ocv_script_path): |
| 70 | + _work_dir = Path(self.__get_project_path()) / "build/bin" |
| 71 | + env_vars = self.__source_script(Path(self.__get_project_path()) / self.ocv_script_path) |
| 72 | + else: |
| 73 | + _work_dir = Path(self.__get_project_path()) / "install/bin" |
| 74 | + env_vars = self.__source_script(Path(_work_dir) / self.ocv_script_name) |
| 75 | + |
| 76 | + self.work_dir = Path(_work_dir) |
| 77 | + if not platform.system() == "Windows": |
| 78 | + os.environ.update(env_vars) |
| 79 | + |
| 80 | + @staticmethod |
| 81 | + def __run_exec(command): |
| 82 | + result = subprocess.run(command, shell=True, env=os.environ) |
| 83 | + if result.returncode != 0: |
| 84 | + raise Exception(f"Subprocess return {result.returncode}.") |
| 85 | + |
| 86 | + @staticmethod |
| 87 | + def __get_gtest_settings(repeats_count): |
| 88 | + command = "--gtest_also_run_disabled_tests " |
| 89 | + command += f"--gtest_repeat={repeats_count} " |
| 90 | + command += "--gtest_recreate_environments_when_repeating " |
| 91 | + command += "--gtest_color=0 " |
| 92 | + return command |
| 93 | + |
| 94 | + def run_threads(self): |
| 95 | + if platform.system() == "Linux" and not os.environ.get("ASAN_RUN"): |
| 96 | + self.__run_exec(f"{self.valgrind_cmd} {self.work_dir / 'seq_func_tests'} {self.__get_gtest_settings(1)}") |
| 97 | + self.__run_exec(f"{self.valgrind_cmd} {self.work_dir / 'stl_func_tests'} {self.__get_gtest_settings(1)}") |
| 98 | + |
| 99 | + self.__run_exec(f"{self.work_dir / 'seq_func_tests'} {self.__get_gtest_settings(3)}") |
| 100 | + self.__run_exec(f"{self.work_dir / 'stl_func_tests'} {self.__get_gtest_settings(3)}") |
| 101 | + self.__run_exec(f"{self.work_dir / 'tbb_func_tests'} {self.__get_gtest_settings(3)}") |
| 102 | + |
| 103 | + if os.environ.get("CLANG_BUILD") == "1": |
| 104 | + return |
| 105 | + self.__run_exec(f"{self.work_dir / 'omp_func_tests'} {self.__get_gtest_settings(3)}") |
| 106 | + |
| 107 | + def run_core(self): |
| 108 | + if platform.system() == "Linux" and not os.environ.get("ASAN_RUN"): |
| 109 | + self.__run_exec(f"{self.valgrind_cmd} {self.work_dir / 'core_func_tests'} {self.__get_gtest_settings(1)}") |
| 110 | + self.__run_exec(f"{self.valgrind_cmd} {self.work_dir / 'ref_func_tests'} {self.__get_gtest_settings(1)}") |
| 111 | + |
| 112 | + self.__run_exec(f"{self.work_dir / 'core_func_tests'} {self.__get_gtest_settings(3)}") |
| 113 | + self.__run_exec(f"{self.work_dir / 'ref_func_tests'} {self.__get_gtest_settings(3)}") |
| 114 | + |
| 115 | + def run_processes(self, additional_mpi_args): |
| 116 | + if os.environ.get("CLANG_BUILD") == "1": |
| 117 | + return |
| 118 | + |
| 119 | + proc_count = os.environ.get("PROC_COUNT") |
| 120 | + if proc_count is None: |
| 121 | + raise EnvironmentError("Required environment variable 'PROC_COUNT' is not set.") |
| 122 | + |
| 123 | + mpi_running = f"{self.mpi_exec} {additional_mpi_args} -np {proc_count}" |
| 124 | + if not os.environ.get("ASAN_RUN"): |
| 125 | + self.__run_exec(f"{mpi_running} {self.work_dir / 'all_func_tests'} {self.__get_gtest_settings(10)}") |
| 126 | + self.__run_exec(f"{mpi_running} {self.work_dir / 'mpi_func_tests'} {self.__get_gtest_settings(10)}") |
| 127 | + |
| 128 | + def run_performance(self): |
| 129 | + if not os.environ.get("ASAN_RUN"): |
| 130 | + mpi_running = "" |
| 131 | + if platform.system() in ("Linux", "Windows"): |
| 132 | + mpi_running = f"{self.mpi_exec} -np 4" |
| 133 | + elif platform.system() == "Darwin": |
| 134 | + mpi_running = f"{self.mpi_exec} -np 2" |
| 135 | + self.__run_exec(f"{mpi_running} {self.work_dir / 'all_perf_tests'} {self.__get_gtest_settings(1)}") |
| 136 | + self.__run_exec(f"{mpi_running} {self.work_dir / 'mpi_perf_tests'} {self.__get_gtest_settings(1)}") |
| 137 | + |
| 138 | + self.__run_exec(f"{self.work_dir / 'omp_perf_tests'} {self.__get_gtest_settings(1)}") |
| 139 | + self.__run_exec(f"{self.work_dir / 'seq_perf_tests'} {self.__get_gtest_settings(1)}") |
| 140 | + self.__run_exec(f"{self.work_dir / 'stl_perf_tests'} {self.__get_gtest_settings(1)}") |
| 141 | + self.__run_exec(f"{self.work_dir / 'tbb_perf_tests'} {self.__get_gtest_settings(1)}") |
| 142 | + |
| 143 | + def run_performance_list(self): |
| 144 | + for task_type in ["all", "mpi", "omp", "seq", "stl", "tbb"]: |
| 145 | + self.__run_exec(f"{self.work_dir / f'{task_type}_perf_tests'} --gtest_list_tests") |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + args_dict = init_cmd_args() |
| 150 | + |
| 151 | + ppc_runner = PPCRunner() |
| 152 | + ppc_runner.setup_env() |
| 153 | + |
| 154 | + if args_dict["running_type"] in ["threads", "processes"]: |
| 155 | + ppc_runner.run_core() |
| 156 | + |
| 157 | + if args_dict["running_type"] == "threads": |
| 158 | + ppc_runner.run_threads() |
| 159 | + elif args_dict["running_type"] == "processes": |
| 160 | + ppc_runner.run_processes(args_dict["additional_mpi_args"]) |
| 161 | + elif args_dict["running_type"] == "performance": |
| 162 | + ppc_runner.run_performance() |
| 163 | + elif args_dict["running_type"] == "performance-list": |
| 164 | + ppc_runner.run_performance_list() |
| 165 | + else: |
| 166 | + raise Exception("running-type is wrong!") |
0 commit comments