def calculate_num_of_loops(self): fragments = self.__fragmentation.fragment_code() with open(self.__input_file_path, 'r') as input_file: input_file_text = input_file.read() e.assert_file_is_empty(input_file_text) self.set_number_of_loops(len(fragments)) return input_file_text, fragments
def inject_rtl_params_to_loop(self, file_dict: dict, omp_rtl_params: list): if not omp_rtl_params: return c_file_path = file_dict['file_full_path'] e.assert_file_exist(c_file_path) with open(c_file_path, 'r') as input_file: c_code = input_file.read() e.assert_file_is_empty(c_code) for loop_id in range(1, self.files_loop_dict[file_dict['file_id_by_rel_path']][0] + 1): start_loop_marker = f'{Fragmentator.get_start_label()}{loop_id}' end_loop_marker = f'{Fragmentator.get_end_label()}{loop_id}' start_marker_pattern = rf'{start_loop_marker}[ \t]*\n' loop_pattern = rf'{start_marker_pattern}.*{end_loop_marker}[ \t]*\n' loop_before_changes = re.search(loop_pattern, c_code, re.DOTALL).group() loop_prefix_pattern = rf'{start_marker_pattern}.*?(?=[\t ]*for[ \t]*\()' loop_prefix = re.search(loop_prefix_pattern, loop_before_changes, re.DOTALL).group() loop_body_and_suffix = re.sub(re.escape(loop_prefix), '', loop_before_changes) params_code = f'{start_loop_marker}\n' for param in omp_rtl_params: omp_function_name = re.search(r'.+(?=\(.*\))', param).group() if omp_function_name in loop_prefix: loop_prefix = re.sub(rf'{omp_function_name}[^;]*;[^\n]*\n', '', loop_prefix, re.DOTALL) params_code += f'{param}\n' loop_prefix = re.sub(rf'{start_marker_pattern}', params_code, loop_prefix) new_loop = f'{loop_prefix}{loop_body_and_suffix}' c_code = c_code.replace(loop_before_changes, new_loop) try: with open(c_file_path, 'w') as output_file: output_file.write(c_code) except OSError as err: raise e.FileError(str(err))
def inject_line_in_code(file_full_path: str, new_line: str): with open(file_full_path, 'r') as input_file: c_code = input_file.read() e.assert_file_is_empty(c_code) c_code = new_line + c_code try: with open(file_full_path, 'w') as output_file: output_file.write(c_code) except OSError as err: raise e.FileError(str(err))
def inject_c_code_to_loop(c_file_path: str, loop_id: str, c_code_to_inject: str): e.assert_file_exist(c_file_path) with open(c_file_path, 'r') as input_file: c_code = input_file.read() e.assert_file_is_empty(c_code) loop_id_with_inject_code = loop_id + '\n' + c_code_to_inject c_code = re.sub(loop_id + '[ ]*\n', loop_id_with_inject_code, c_code) try: with open(c_file_path, 'w') as output_file: output_file.write(c_code) except OSError as err: raise e.FileError(str(err))
def add_to_loop_details_about_comp_and_combination(file_path: str, start_label: str, combination_id: str, comp_name: str): e.assert_file_exist(file_path) e.assert_file_is_empty(file_path) with open(file_path, 'r') as file: file_text = file.read() to_replace = '' to_replace += f'{start_label}\n{ComparConfig.COMBINATION_ID_C_COMMENT}{combination_id}\n' to_replace += f'{ComparConfig.COMPILER_NAME_C_COMMENT}{comp_name}\n' file_text = re.sub(f'{start_label}[ ]*\\n', to_replace, file_text) try: with open(file_path, 'w') as file: file.write(file_text) except OSError as err: raise e.FileError(str(err))
def inject_directive_params_to_loop(self, file_dict: dict, omp_directive_params: list): if not omp_directive_params: return c_file_path = file_dict['file_full_path'] e.assert_file_exist(c_file_path) format_c_code([c_file_path, ]) # mainly to arrange directives in a single line with open(c_file_path, 'r') as input_file: c_code = input_file.read() e.assert_file_is_empty(c_code) for loop_id in range(1, self.files_loop_dict[file_dict['file_id_by_rel_path']][0] + 1): start_loop_marker = f'{Fragmentator.get_start_label()}{loop_id}' end_loop_marker = f'{Fragmentator.get_end_label()}{loop_id}' start_marker_pattern = rf'{start_loop_marker}[ \t]*\n' loop_pattern = rf'{start_marker_pattern}.*{end_loop_marker}[ \t]*\n' loop_before_changes = re.search(loop_pattern, c_code, re.DOTALL).group() loop_prefix_pattern = rf'{start_marker_pattern}.*?(?=[\t ]*for[ \t]*\()' loop_prefix = re.search(loop_prefix_pattern, loop_before_changes, re.DOTALL).group() loop_body_and_suffix = re.sub(re.escape(loop_prefix), '', loop_before_changes) pragma_pattern = r'#pragma omp[^\n]+\n' pragma = re.search(rf'{pragma_pattern}', loop_prefix) if pragma: pragma = pragma.group().replace('\n', '') new_directives = '' for directive in omp_directive_params: pragma_type, directive = directive.split('_', 1) pragma_name = re.search(r'[^(]+', directive).group() if not re.search(rf' {pragma_type} ?', pragma): if pragma_type == CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX: pragma = re.sub(r'pragma omp', f'pragma omp {pragma_type}', pragma) if pragma_type == CombinatorConfig.FOR_DIRECTIVE_PREFIX: if re.search(rf' {CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX} ?', pragma): new_text = f'pragma omp {CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX} {pragma_type} ' pragma = re.sub(rf'pragma omp {CombinatorConfig.PARALLEL_DIRECTIVE_PREFIX} ?', new_text, pragma) else: pragma = re.sub(r'pragma omp', f'pragma omp {pragma_type}', pragma) if pragma_name in pragma: pragma = re.sub(rf'{"pragma_name"}(?:\([^)]+\))? ?', '', pragma) new_directives += f' {directive}' new_pragma = f'{pragma} {new_directives}\n' new_prefix = re.sub(rf'{pragma_pattern}', new_pragma, loop_prefix) c_code = c_code.replace(loop_before_changes, f'{new_prefix}{loop_body_and_suffix}') try: with open(c_file_path, 'w') as output_file: output_file.write(c_code) except OSError as err: raise e.FileError(str(err))