From e5b41b791db09c29f4da2f0911d220eb3cd2b620 Mon Sep 17 00:00:00 2001 From: Lukas Rothenberger Date: Fri, 12 Jul 2024 16:50:54 +0200 Subject: [PATCH] fix(explorer): exception handling when profiling enabled --- discopop_explorer/discopop_explorer.py | 174 +++++++++++++------------ 1 file changed, 90 insertions(+), 84 deletions(-) diff --git a/discopop_explorer/discopop_explorer.py b/discopop_explorer/discopop_explorer.py index ee6cdf332..8c4c421ea 100644 --- a/discopop_explorer/discopop_explorer.py +++ b/discopop_explorer/discopop_explorer.py @@ -190,79 +190,102 @@ def run(arguments: ExplorerArguments) -> None: profile = cProfile.Profile() profile.enable() - if arguments.generate_data_cu_inst is not None: - # start generation of Data_CUInst and stop execution afterwards - from .generate_Data_CUInst import wrapper as generate_data_cuinst_wrapper + try: + if arguments.generate_data_cu_inst is not None: + # start generation of Data_CUInst and stop execution afterwards + from .generate_Data_CUInst import wrapper as generate_data_cuinst_wrapper + + generate_data_cuinst_wrapper( + arguments.cu_xml_file, + arguments.dep_file, + arguments.loop_counter_file, + arguments.reduction_file, + arguments.generate_data_cu_inst, + ) + sys.exit(0) + + print("Loading Hotspots...") + + hotspots = load_hotspots( + HotspotLoaderArguments( + verbose=True, + get_loops=True, + get_functions=True, + get_YES=True, + get_MAYBE=True, + get_NO=False, + log_level=arguments.log_level, + write_log=arguments.write_log, + ) + ) + + print("Done.") - generate_data_cuinst_wrapper( + start = time.time() + + res = __run( + arguments.project_path, arguments.cu_xml_file, arguments.dep_file, arguments.loop_counter_file, arguments.reduction_file, - arguments.generate_data_cu_inst, - ) - sys.exit(0) - - print("Loading Hotspots...") - - hotspots = load_hotspots( - HotspotLoaderArguments( - verbose=True, - get_loops=True, - get_functions=True, - get_YES=True, - get_MAYBE=True, - get_NO=False, - log_level=arguments.log_level, - write_log=arguments.write_log, + arguments.plugins, + file_mapping=arguments.file_mapping_file, + cu_inst_result_file=arguments.cu_inst_result_file, + llvm_cxxfilt_path=arguments.llvm_cxxfilt_path, + discopop_build_path=arguments.discopop_build_path, + enable_patterns=arguments.enable_patterns, + enable_task_pattern=arguments.enable_task_pattern, + enable_detection_of_scheduling_clauses=arguments.detect_scheduling_clauses, + hotspot_functions=hotspots, + load_existing_doall_and_reduction_patterns=arguments.load_existing_doall_and_reduction_patterns, ) - ) - - print("Done.") - - start = time.time() - - res = __run( - arguments.project_path, - arguments.cu_xml_file, - arguments.dep_file, - arguments.loop_counter_file, - arguments.reduction_file, - arguments.plugins, - file_mapping=arguments.file_mapping_file, - cu_inst_result_file=arguments.cu_inst_result_file, - llvm_cxxfilt_path=arguments.llvm_cxxfilt_path, - discopop_build_path=arguments.discopop_build_path, - enable_patterns=arguments.enable_patterns, - enable_task_pattern=arguments.enable_task_pattern, - enable_detection_of_scheduling_clauses=arguments.detect_scheduling_clauses, - hotspot_functions=hotspots, - load_existing_doall_and_reduction_patterns=arguments.load_existing_doall_and_reduction_patterns, - ) - - end = time.time() - - if arguments.enable_pet_dump_file is not None: - with open(arguments.enable_pet_dump_file, "w+") as f: - f.write(res.pet.dump_to_pickled_json()) - f.flush() - f.close() - - if arguments.enable_detection_result_dump_file is not None: - with open(arguments.enable_detection_result_dump_file, "w+") as f: - f.write(res.dump_to_pickled_json()) - f.flush() - f.close() - - if arguments.enable_json_file is None: - print(str(res)) - else: - # todo re-enable? - # print(str(res)) - # since PETGraphX is not JSON Serializable, delete the field prior to executing the serialization - del res.pet - with open(arguments.enable_json_file, "w+") as f: - json.dump(res, f, indent=2, cls=PatternBaseSerializer) + + end = time.time() + + if arguments.enable_pet_dump_file is not None: + with open(arguments.enable_pet_dump_file, "w+") as f: + f.write(res.pet.dump_to_pickled_json()) + f.flush() + f.close() + + if arguments.enable_detection_result_dump_file is not None: + with open(arguments.enable_detection_result_dump_file, "w+") as f: + f.write(res.dump_to_pickled_json()) + f.flush() + f.close() + + if arguments.enable_json_file is None: + print(str(res)) + else: + # todo re-enable? + # print(str(res)) + # since PETGraphX is not JSON Serializable, delete the field prior to executing the serialization + del res.pet + with open(arguments.enable_json_file, "w+") as f: + json.dump(res, f, indent=2, cls=PatternBaseSerializer) + + + + # initialize the line_mapping.json + initialize_line_mapping(load_file_mapping(arguments.file_mapping_file), arguments.project_path) + + print("Time taken for pattern detection: {0}".format(end - start)) + + # demonstration of Microbenchmark possibilities + if arguments.microbench_file is not None: + microbench_file = get_path( + arguments.project_path, arguments.microbench_file + ) # NOTE: the json file is not usually located in the project, this is just for demonstration purposes + if not os.path.isfile(microbench_file): + raise FileNotFoundError(f"Microbenchmark file not found: {microbench_file}") + extrapBench = ExtrapInterpolatedMicrobench(microbench_file) + sympyExpr = extrapBench.getFunctionSympy() + print(sympyExpr) + print(sympyExpr.free_symbols) + except BaseException: + # required to correctly write profiling data if the program terminates + pass if arguments.enable_profiling_dump_file is not None: profile.disable() @@ -271,20 +294,3 @@ def run(arguments: ExplorerArguments) -> None: with open(arguments.enable_profiling_dump_file, "w+") as f: stats = pstats2.Stats(profile, stream=f).sort_stats("time").reverse_order() stats.print_stats() - - # initialize the line_mapping.json - initialize_line_mapping(load_file_mapping(arguments.file_mapping_file), arguments.project_path) - - print("Time taken for pattern detection: {0}".format(end - start)) - - # demonstration of Microbenchmark possibilities - if arguments.microbench_file is not None: - microbench_file = get_path( - arguments.project_path, arguments.microbench_file - ) # NOTE: the json file is not usually located in the project, this is just for demonstration purposes - if not os.path.isfile(microbench_file): - raise FileNotFoundError(f"Microbenchmark file not found: {microbench_file}") - extrapBench = ExtrapInterpolatedMicrobench(microbench_file) - sympyExpr = extrapBench.getFunctionSympy() - print(sympyExpr) - print(sympyExpr.free_symbols)