From 2a46defb7ea66197775050b8332bce6553436a02 Mon Sep 17 00:00:00 2001 From: goerlibe Date: Mon, 11 Sep 2023 21:24:18 +0200 Subject: [PATCH] refactor(stronger mypy checks): remove redundant casts --- discopop_explorer/PETGraphX.py | 16 ++++---- discopop_explorer/generate_Data_CUInst.py | 2 +- .../combined_gpu_patterns/step_1.py | 6 +-- .../combined_gpu_patterns/step_2.py | 8 ++-- .../combined_gpu_patterns/step_3.py | 2 +- .../combined_gpu_patterns/step_4.py | 8 ++-- .../simple_gpu_patterns/GPULoop.py | 2 +- .../simple_gpu_patterns/GPURegions.py | 8 ++-- .../task_parallelism/filter.py | 17 +++------ .../task_parallelism/postprocessor.py | 37 ++++--------------- .../task_parallelism/suggesters/auxiliary.py | 26 ++++++------- .../task_parallelism/suggesters/barriers.py | 9 +---- .../suggesters/data_sharing_clauses.py | 4 +- .../suggesters/dependency_clauses.py | 19 ++++------ .../task_parallelism/suggesters/tasks.py | 6 +-- .../task_parallelism_detector.py | 4 +- .../task_parallelism/tp_utils.py | 14 +++---- discopop_explorer/utils.py | 2 +- .../scheduling/workload_delta.py | 12 +----- .../utilities/MOGUtilities.py | 6 +-- mypy.ini | 2 +- 21 files changed, 73 insertions(+), 137 deletions(-) diff --git a/discopop_explorer/PETGraphX.py b/discopop_explorer/PETGraphX.py index ecf08be18..261a7b315 100644 --- a/discopop_explorer/PETGraphX.py +++ b/discopop_explorer/PETGraphX.py @@ -300,9 +300,7 @@ def get_nesting_level(self, pet: PETGraphX, return_invert_result: bool = True) - parent_nesting_levels.append( min( 2, - cast(LoopNode, parent_node).get_nesting_level( - pet, return_invert_result=False - ), + parent_node.get_nesting_level(pet, return_invert_result=False), ) ) @@ -322,7 +320,7 @@ def get_entry_node(self, pet: PETGraphX) -> Optional[Node]: if pet.node_at(s) not in pet.direct_children(self) ] if len(predecessors_outside_loop_body) > 0: - return cast(Node, node) + return node return None @@ -361,7 +359,7 @@ def get_entry_cu_id(self, pet: PETGraphX) -> NodeID: def get_exit_cu_ids(self, pet: PETGraphX) -> Set[NodeID]: exit_cu_ids: Set[NodeID] = set() if self.children_cu_ids is not None: - for child_cu_id in cast(List[NodeID], self.children_cu_ids): + for child_cu_id in self.children_cu_ids: if ( len(pet.out_edges(child_cu_id, EdgeType.SUCCESSOR)) == 0 and len(pet.in_edges(child_cu_id, EdgeType.SUCCESSOR)) != 0 @@ -1154,7 +1152,7 @@ def unused_check_alias(self, s: NodeID, t: NodeID, d: Dependency, root_loop: Nod parent_func_source = self.get_parent_function(self.node_at(t)) res = False - d_var_name_str = cast(str, str(d.var_name)) + d_var_name_str = str(d.var_name) if self.unused_is_global(d_var_name_str, sub) and not ( self.is_passed_by_reference(d, parent_func_sink) @@ -1278,7 +1276,7 @@ def get_variables(self, nodes: Sequence[Node]) -> Dict[Variable, Set[MemoryRegio ): if dep.var_name == var_name.name: if dep.memory_region is not None: - res[var_name].add(cast(MemoryRegion, dep.memory_region)) + res[var_name].add(dep.memory_region) return res def get_undefined_variables_inside_loop( @@ -1559,7 +1557,7 @@ def check_reachability(self, target: Node, source: Node, edge_types: List[EdgeTy return True else: if e[0] not in visited: - queue.append(cast(Node, self.node_at(e[0]))) + queue.append(self.node_at(e[0])) return False def is_predecessor(self, source_id: NodeID, target_id: NodeID) -> bool: @@ -1696,7 +1694,7 @@ def get_memory_regions(self, nodes: List[CUNode], var_name: str) -> Set[MemoryRe for s, t, d in out_deps: if d.var_name == var_name: if d.memory_region is not None: - mem_regs.add(cast(MemoryRegion, d.memory_region)) + mem_regs.add(d.memory_region) return mem_regs def get_path_nodes_between( diff --git a/discopop_explorer/generate_Data_CUInst.py b/discopop_explorer/generate_Data_CUInst.py index 1e3a93518..afbcfed97 100644 --- a/discopop_explorer/generate_Data_CUInst.py +++ b/discopop_explorer/generate_Data_CUInst.py @@ -130,7 +130,7 @@ def __output_dependencies_of_type( + dep_identifier + cast(str, dep[2].source_line) + "|" - + cast(str, dep[2].var_name) + + dep[2].var_name + "," ) diff --git a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_1.py b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_1.py index 869dd4d3a..706eefb7a 100644 --- a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_1.py +++ b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_1.py @@ -95,10 +95,8 @@ def get_cu_and_varname_to_memory_regions( if dep.var_name is None or dep.memory_region is None or len(dep.memory_region) == 0: continue if dep.var_name not in result_dict[cu_id]: - result_dict[cu_id][VarName(cast(str, dep.var_name))] = set() - result_dict[cu_id][VarName(cast(str, dep.var_name))].add( - MemoryRegion(cast(str, dep.memory_region)) - ) + result_dict[cu_id][VarName(dep.var_name)] = set() + result_dict[cu_id][VarName(dep.var_name)].add(dep.memory_region) return result_dict diff --git a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_2.py b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_2.py index 793c888c5..b7c3a280a 100644 --- a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_2.py +++ b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_2.py @@ -313,16 +313,16 @@ def calculate_host_liveness( for _, target, dep in out_data_edges: if target in comb_gpu_reg.device_cu_ids: if dep.var_name is not None: - shared_variables.add(VarName(cast(str, dep.var_name))) + shared_variables.add(VarName(dep.var_name)) if dep.memory_region is not None: - shared_memory_regions.add(MemoryRegion(cast(str, dep.memory_region))) + shared_memory_regions.add(MemoryRegion(dep.memory_region)) for source, _, dep in in_data_edges: if source in comb_gpu_reg.device_cu_ids: if dep.var_name is not None: - shared_variables.add(VarName(cast(str, dep.var_name))) + shared_variables.add(VarName(dep.var_name)) if dep.memory_region is not None: - shared_memory_regions.add(MemoryRegion(cast(str, dep.memory_region))) + shared_memory_regions.add(dep.memory_region) for var_name in shared_variables: if var_name not in host_liveness_lists: host_liveness_lists[var_name] = [] diff --git a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_3.py b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_3.py index 56d4c9956..f0b60f6ee 100644 --- a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_3.py +++ b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_3.py @@ -121,7 +121,7 @@ def propagate_writes( pet_node = pet.node_at(cu_id) if type(pet_node) != CUNode: continue - cu_node = cast(CUNode, pet_node) + cu_node = pet_node if cu_node.return_instructions_count > 0: # propagate write to calling cus parent_function = pet.get_parent_function(cu_node) diff --git a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_4.py b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_4.py index 9c65269b8..3c61d088e 100644 --- a/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_4.py +++ b/discopop_explorer/pattern_detectors/combined_gpu_patterns/step_4.py @@ -399,7 +399,7 @@ def identify_updates( # get parent functions parent_functions: Set[NodeID] = set() for region in comb_gpu_reg.contained_regions: - parent_functions.add(cast(NodeID, pet.get_parent_function(pet.node_at(region.node_id)).id)) + parent_functions.add(pet.get_parent_function(pet.node_at(region.node_id)).id) for parent_function_id in parent_functions: print("IDENTIFY UPDATES FOR: ", pet.node_at(parent_function_id).name, file=sys.stderr) @@ -410,7 +410,7 @@ def identify_updates( ]: in_successor_edges = pet.in_edges(function_child_id, EdgeType.SUCCESSOR) if len(in_successor_edges) == 0 and pet.node_at(function_child_id).type == NodeType.CU: - entry_points.append(cast(NodeID, function_child_id)) + entry_points.append(function_child_id) for entry_point in entry_points: print( @@ -780,7 +780,7 @@ def identify_updates_in_unrolled_function_graphs( # get parent functions parent_functions: Set[NodeID] = set() for region in comb_gpu_reg.contained_regions: - parent_functions.add(cast(NodeID, pet.get_parent_function(pet.node_at(region.node_id)).id)) + parent_functions.add(pet.get_parent_function(pet.node_at(region.node_id)).id) for parent_function_id in parent_functions: print("IDENTIFY UPDATES FOR: ", pet.node_at(parent_function_id).name, file=sys.stderr) @@ -791,7 +791,7 @@ def identify_updates_in_unrolled_function_graphs( ]: in_successor_edges = pet.in_edges(function_child_id, EdgeType.SUCCESSOR) if len(in_successor_edges) == 0 and pet.node_at(function_child_id).type == NodeType.CU: - entry_points.append(cast(NodeID, function_child_id)) + entry_points.append(function_child_id) for entry_point in entry_points: print( diff --git a/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPULoop.py b/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPULoop.py index 5ec8dbf92..7381979b7 100644 --- a/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPULoop.py +++ b/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPULoop.py @@ -681,7 +681,7 @@ def setCollapseClause(self, pet: PETGraphX, node_id: NodeID, res): # information from the LLVM debug information # check if distance between first CU of node_id and cn_id is 2 steps on the successor graph potentials: Set[Node] = set() - for succ1 in pet.direct_successors(cast(Node, loop_entry_node)): + for succ1 in pet.direct_successors(loop_entry_node): for succ2 in pet.direct_successors(succ1): potentials.add(succ2) if cast(LoopNode, cn_id).get_entry_node(pet) in potentials: diff --git a/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPURegions.py b/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPURegions.py index 9e2f0a470..4e1c39d0e 100644 --- a/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPURegions.py +++ b/discopop_explorer/pattern_detectors/simple_gpu_patterns/GPURegions.py @@ -282,7 +282,7 @@ def determineDataMapping(self) -> None: if self.pet.node_at(source_cu_id) not in region_cus: if dep.dtype == DepType.RAW: if dep.var_name not in consumed_vars and dep.var_name is not None: - consumed_vars.append(cast(str, dep.var_name)) + consumed_vars.append(dep.var_name) # determine variables which are read afterwards and written in the region produced_vars: List[str] = [] @@ -297,7 +297,7 @@ def determineDataMapping(self) -> None: if self.pet.node_at(sink_cu_id) not in region_cus: if dep.dtype in [DepType.RAW, DepType.WAW]: if dep.var_name not in produced_vars and dep.var_name is not None: - produced_vars.append(cast(str, dep.var_name)) + produced_vars.append(dep.var_name) # gather consumed, produced, allocated and deleted variables from mapping information map_to_vars: List[str] = [] @@ -354,7 +354,7 @@ def old_mapData(self) -> None: tmp_result = self.findGPULoop(j) if tmp_result is None: continue - gpuLoop: GPULoopPattern = cast(GPULoopPattern, tmp_result) + gpuLoop: GPULoopPattern = tmp_result gpuLoop.printGPULoop() print(f"==============================================") for i in range(0, self.numRegions): @@ -378,7 +378,7 @@ def old_mapData(self) -> None: t -= 1 continue - loopIter: GPULoopPattern = cast(GPULoopPattern, tmp_result) + loopIter: GPULoopPattern = tmp_result varis: Set[Variable] = set([]) varis.update(loopIter.map_type_alloc) varis.update(loopIter.map_type_to) diff --git a/discopop_explorer/pattern_detectors/task_parallelism/filter.py b/discopop_explorer/pattern_detectors/task_parallelism/filter.py index 4cba6fbc6..97e2ec00a 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/filter.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/filter.py @@ -49,7 +49,6 @@ def __filter_data_sharing_clauses_suppress_shared_loop_index( # only consider task suggestions if type(suggestion) != TaskParallelismInfo: continue - suggestion = cast(TaskParallelismInfo, suggestion) if suggestion.type is not TPIType.TASK: continue # get parent loops of suggestion @@ -92,7 +91,6 @@ def __filter_data_sharing_clauses_by_function( # only consider task suggestions if type(suggestion) != TaskParallelismInfo: continue - suggestion = cast(TaskParallelismInfo, suggestion) if suggestion.type not in [TPIType.TASK, TPIType.TASKLOOP]: continue # get function containing the task cu @@ -302,7 +300,6 @@ def __filter_data_sharing_clauses_by_scope( # only consider task suggestions if type(suggestion) != TaskParallelismInfo: continue - suggestion = cast(TaskParallelismInfo, suggestion) if suggestion.type is not TPIType.TASK: continue # get function containing the task cu @@ -356,7 +353,7 @@ def __filter_sharing_clause( optional_var_def_cu = child_cu if optional_var_def_cu is None: continue - var_def_cu = cast(Node, optional_var_def_cu) + var_def_cu = optional_var_def_cu # 1. check control flow (reverse BFS from suggestion._node to parent_function if __reverse_reachable_w_o_breaker( pet, pet.node_at(suggestion.node_id), parent_function_cu, var_def_cu, [] @@ -434,16 +431,14 @@ def remove_duplicate_data_sharing_clauses(suggestions: List[PatternInfo]) -> Lis :param suggestions: List[PatternInfo] :return: Modified List of PatternInfos """ - result = [] + result: List[PatternInfo] = [] for s in suggestions: if not type(s) == TaskParallelismInfo: result.append(s) else: - s_tpi = cast(TaskParallelismInfo, s) - s_tpi.in_dep = list(set(s_tpi.in_dep)) - s_tpi.out_dep = list(set(s_tpi.out_dep)) - s_tpi.in_out_dep = list(set(s_tpi.in_out_dep)) - s = cast(PatternInfo, s_tpi) + s.in_dep = list(set(s.in_dep)) + s.out_dep = list(set(s.out_dep)) + s.in_out_dep = list(set(s.in_out_dep)) result.append(s) return result @@ -716,7 +711,6 @@ def filter_data_depend_clauses( # only consider task suggestions if type(suggestion) != TaskParallelismInfo: continue - suggestion = cast(TaskParallelismInfo, suggestion) if suggestion.type not in [TPIType.TASK, TPIType.TASKLOOP]: continue for var in suggestion.in_dep: @@ -741,7 +735,6 @@ def filter_data_depend_clauses( # only consider task suggestions if type(suggestion) != TaskParallelismInfo: continue - suggestion = cast(TaskParallelismInfo, suggestion) if suggestion.type not in [TPIType.TASK, TPIType.TASKLOOP]: continue # get function containing the task cu diff --git a/discopop_explorer/pattern_detectors/task_parallelism/postprocessor.py b/discopop_explorer/pattern_detectors/task_parallelism/postprocessor.py index d7726869a..834ca63a9 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/postprocessor.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/postprocessor.py @@ -32,16 +32,12 @@ def group_task_suggestions(pet: PETGraphX, suggestions: List[PatternInfo]) -> Li :return: Updated suggestions""" task_suggestions = [ s - for s in [ - cast(TaskParallelismInfo, e) for e in suggestions if type(e) == TaskParallelismInfo - ] + for s in [e for e in suggestions if type(e) == TaskParallelismInfo] if s.type is TPIType.TASK ] taskwait_suggestions = [ s - for s in [ - cast(TaskParallelismInfo, e) for e in suggestions if type(e) == TaskParallelismInfo - ] + for s in [e for e in suggestions if type(e) == TaskParallelismInfo] if s.type is TPIType.TASKWAIT ] # mark preceeding suggestions for each taskwait suggestion @@ -116,7 +112,7 @@ def group_task_suggestions(pet: PETGraphX, suggestions: List[PatternInfo]) -> Li ) # valid, overwrite sug.task_group if value is not None if value is not None: - sug.task_group = [cast(int, value)] + sug.task_group = [value] return suggestions @@ -130,30 +126,13 @@ def sort_output(suggestions: List[PatternInfo]) -> List[PatternInfo]: sorted_suggestions = [] tmp_dict: Dict[str, List[Tuple[str, PatternInfo]]] = dict() for sug in suggestions: - if type(sug) == ParallelRegionInfo: - sug_par = cast(ParallelRegionInfo, sug) - # Note: Code duplicated for type correctness - # get start_line and file_id for sug - if ":" not in sug_par.region_start_line: - start_line = sug_par.region_start_line - file_id = sug_par.start_line[0 : sug_par.start_line.index(":")] - else: - start_line = sug_par.region_start_line - file_id = start_line[0 : start_line.index(":")] - start_line = start_line[start_line.index(":") + 1 :] - # split suggestions by file-id - if file_id not in tmp_dict: - tmp_dict[file_id] = [] - tmp_dict[file_id].append((start_line, sug)) - elif type(sug) == TaskParallelismInfo: - sug_task = cast(TaskParallelismInfo, sug) - # Note: Code duplicated for type correctness + if isinstance(sug, (ParallelRegionInfo, TaskParallelismInfo)): # get start_line and file_id for sug - if ":" not in sug_task.region_start_line: - start_line = sug_task.region_start_line - file_id = sug_task.start_line[0 : sug_task.start_line.index(":")] + if ":" not in sug.region_start_line: + start_line = sug.region_start_line + file_id = sug.start_line[0 : sug.start_line.index(":")] else: - start_line = sug_task.region_start_line + start_line = sug.region_start_line file_id = start_line[0 : start_line.index(":")] start_line = start_line[start_line.index(":") + 1 :] # split suggestions by file-id diff --git a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/auxiliary.py b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/auxiliary.py index 48988725e..cef2a984d 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/auxiliary.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/auxiliary.py @@ -44,10 +44,10 @@ def suggest_parallel_regions( # remove duplicates parents = list(set(parents)) # get outer-most parents of suggested tasks - outer_parents = [] + outer_parents: List[Tuple[Node, Optional[Node]]] = [] # iterate over entries in parents. while len(parents) > 0: - (p, last_node) = parents.pop(0) + p, last_node = parents.pop(0) p_parents = get_parent_of_type(pet, p, NodeType.FUNC, EdgeType.CHILD, False) if not p_parents: # p_parents is empty # p is outer @@ -64,7 +64,7 @@ def suggest_parallel_regions( for parent, last_node in outer_parents: if last_node is None: continue - last_node = cast(Node, last_node) + last_node = last_node region_suggestions.append( ParallelRegionInfo( parent, TPIType.PARALLELREGION, last_node.start_position(), last_node.end_position() @@ -115,8 +115,7 @@ def set_task_contained_lines(suggestions: List[TaskParallelismInfo]) -> List[Tas cu_to_suggestions_map[cu][idx] = s # append suggestions to output for cu in cu_to_suggestions_map: - tmp: List[TaskParallelismInfo] = cast(List[TaskParallelismInfo], cu_to_suggestions_map[cu]) - for s in tmp: + for s in cu_to_suggestions_map[cu]: output.append(s) return output @@ -149,8 +148,8 @@ def detect_taskloop_reduction( # s not contained in reduction loop body output.append(s) else: - red_vars_entry = cast(Dict[str, str], red_vars_entry) - red_loop = cast(Node, red_loop) + red_vars_entry = red_vars_entry + red_loop = red_loop # s contained in reduction loop body # modify task s reduction_clause = "reduction(" @@ -184,17 +183,14 @@ def combine_omittable_cus(pet: PETGraphX, suggestions: List[PatternInfo]) -> Lis result: List[PatternInfo] = [] for single_suggestion in suggestions: if type(single_suggestion) == OmittableCuInfo: - omittable_suggestions.append(cast(OmittableCuInfo, single_suggestion)) + omittable_suggestions.append(single_suggestion) else: if type(single_suggestion) == TaskParallelismInfo: - single_suggestion_tpi: TaskParallelismInfo = cast( - TaskParallelismInfo, single_suggestion - ) try: - if single_suggestion_tpi.type is TPIType.TASK: - task_suggestions.append(single_suggestion_tpi) + if single_suggestion.type is TPIType.TASK: + task_suggestions.append(single_suggestion) else: - result.append(single_suggestion_tpi) + result.append(single_suggestion) except AttributeError: result.append(single_suggestion) else: @@ -259,7 +255,7 @@ def combine_omittable_cus(pet: PETGraphX, suggestions: List[PatternInfo]) -> Lis continue task_suggestions_dict[omit_s.combine_with_node][ omit_target_task_idx - ].out_dep.append(cast(str, omit_out_var)) + ].out_dep.append(omit_out_var) # omit_s.combine_with_node.out_dep.append(omit_out_var) # process in dependencies of omit_s for omit_in_var in omit_s.in_dep: diff --git a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/barriers.py b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/barriers.py index dc16df057..c578f753b 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/barriers.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/barriers.py @@ -158,7 +158,7 @@ def detect_barrier_suggestions(pet: PETGraphX, suggestions: List[PatternInfo]) - # find this suggestion and extract combine_with_node found_cwn = False for tmp_omit, tmp_cwn in omittable_nodes: - tmp_cwn_list = cast(List[Node], tmp_cwn) + tmp_cwn_list = tmp_cwn if pet.node_at(e[1]) == tmp_omit: if len(tmp_cwn_list) == 1: parent_task = tmp_cwn_list[0] @@ -303,7 +303,6 @@ def __split_suggestions( elif type(single_suggestion) == OmittableCuInfo: omittable_suggestions.append(single_suggestion) elif type(single_suggestion) == TaskParallelismInfo: - single_suggestion = cast(TaskParallelismInfo, single_suggestion) if single_suggestion.type is TPIType.TASKWAIT: taskwait_suggestions.append(single_suggestion) elif single_suggestion.type is TPIType.TASK: @@ -324,14 +323,12 @@ def suggest_barriers_for_uncovered_tasks_before_return( for suggestion in suggestions: if type(suggestion) != TaskParallelismInfo: continue - suggestion = cast(TaskParallelismInfo, suggestion) if suggestion.type is not TPIType.TASK: continue # if task is covered by a parallel region, ignore it due to the present, implicit barrier covered_by_parallel_region = False for tmp in suggestions: if type(tmp) == ParallelRegionInfo: - tmp = cast(ParallelRegionInfo, tmp) if line_contained_in_region( suggestion.start_line, tmp.region_start_line, tmp.region_end_line ): @@ -388,11 +385,10 @@ def validate_barriers(pet: PETGraphX, suggestions: List[PatternInfo]) -> List[Pa :param suggestions: List[PatternInfo] :return: List[PatternInfo] """ - barrier_suggestions = [] + barrier_suggestions: List[TaskParallelismInfo] = [] result: List[PatternInfo] = [] for single_suggestion in suggestions: if type(single_suggestion) == TaskParallelismInfo: - single_suggestion = cast(TaskParallelismInfo, single_suggestion) try: if single_suggestion.type is TPIType.TASKWAIT: barrier_suggestions.append(single_suggestion) @@ -476,7 +472,6 @@ def suggest_missing_barriers_for_global_vars( ): continue if type(single_suggestion) == TaskParallelismInfo: - single_suggestion = cast(TaskParallelismInfo, single_suggestion) if single_suggestion.type is TPIType.TASKWAIT: taskwait_suggestions.append(single_suggestion) elif single_suggestion.type is TPIType.TASK: diff --git a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/data_sharing_clauses.py b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/data_sharing_clauses.py index 8b30c326e..56c21dc56 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/data_sharing_clauses.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/data_sharing_clauses.py @@ -28,9 +28,7 @@ def suggest_shared_clauses_for_all_tasks_in_function_body( """ task_suggestions = [ s - for s in [ - cast(TaskParallelismInfo, t) for t in suggestions if type(t) == TaskParallelismInfo - ] + for s in [t for t in suggestions if type(t) == TaskParallelismInfo] if s.type is TPIType.TASK ] for ts in task_suggestions: diff --git a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/dependency_clauses.py b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/dependency_clauses.py index d825c816d..6f1d0aae6 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/dependency_clauses.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/dependency_clauses.py @@ -177,9 +177,7 @@ def get_alias_information( # iterate over task suggestions task_suggestions = [ s - for s in [ - cast(TaskParallelismInfo, e) for e in suggestions if type(e) == TaskParallelismInfo - ] + for s in [e for e in suggestions if type(e) == TaskParallelismInfo] if s.type is TPIType.TASK ] # collect alias information @@ -225,7 +223,6 @@ def get_alias_information( continue if called_function_cu_id is None: continue - called_function_cu_id_not_none = cast(NodeID, called_function_cu_id) current_alias = [ ( param, @@ -236,7 +233,7 @@ def get_alias_information( ] current_alias += get_alias_for_parameter_at_position( pet, - cast(FunctionNode, pet.node_at(called_function_cu_id_not_none)), + cast(FunctionNode, pet.node_at(called_function_cu_id)), idx, source_code_files, [], @@ -325,7 +322,6 @@ def identify_dependencies_for_different_functions( task_suggestions: List[TaskParallelismInfo] = [] for s in suggestions: if type(s) == TaskParallelismInfo: - s = cast(TaskParallelismInfo, s) if s.type is TPIType.TASK: task_suggestions.append(s) else: @@ -544,9 +540,8 @@ def identify_dependencies_for_same_functions( task_suggestions: List[TaskParallelismInfo] = [] for s in suggestions: if type(s) == TaskParallelismInfo: - s_tpi = cast(TaskParallelismInfo, s) - if s_tpi.type is TPIType.TASK: - task_suggestions.append(s_tpi) + if s.type is TPIType.TASK: + task_suggestions.append(s) else: result_suggestions.append(s) else: @@ -816,7 +811,7 @@ def check_dependence_of_task_pair( for parameter_potential_none in param_names_1: if parameter_potential_none is None: continue - parameter = cast(str, parameter_potential_none) + parameter = parameter_potential_none # get aliases for parameter for alias_entry in aliases[task_suggestion_1]: # skip wrong alias entries @@ -964,7 +959,7 @@ def get_function_call_parameter_rw_information( return None if called_function_name is None: raise ValueError("No valid called function could be found!") - called_function_name_not_none = cast(str, called_function_name) + called_function_name_not_none = called_function_name # 5.2. get R/W information for successive called function's parameters based on CUInstResult.txt called_function_cu = cast(FunctionNode, pet.node_at(called_function_cu_id)) # get raw info concerning the scope of called_function_cu @@ -985,7 +980,7 @@ def get_function_call_parameter_rw_information( for raw_entry in filtered_raw_info: if raw_entry["var"] is None: continue - raw_entry_var = cast(str, raw_entry["var"]) + raw_entry_var = raw_entry["var"] if raw_entry_var.replace(".addr", "") == arg_var.name.replace(".addr", ""): raw_reported = True raw_reported_for_param_positions.append((raw_reported, False)) diff --git a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/tasks.py b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/tasks.py index d44d6c931..9afee54c2 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/suggesters/tasks.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/suggesters/tasks.py @@ -161,9 +161,7 @@ def correct_task_suggestions_in_loop_body( :return: Updated suggestions""" task_suggestions = [ s - for s in [ - cast(TaskParallelismInfo, e) for e in suggestions if type(e) == TaskParallelismInfo - ] + for s in [e for e in suggestions if type(e) == TaskParallelismInfo] if s.type is TPIType.TASK ] for ts in task_suggestions: @@ -207,7 +205,6 @@ def find_taskwaits(cu_node: Node, visited: List[Node]): ) for s in suggestions: if type(s) == TaskParallelismInfo: - s = cast(TaskParallelismInfo, s) if s.type is TPIType.TASKWAIT and s._node == stws_cu: s.pragma_line = int(loop_cu.end_position().split(":")[1]) + 1 else: @@ -225,7 +222,6 @@ def find_taskwaits(cu_node: Node, visited: List[Node]): # move pragma taskwait line for s in suggestions: if type(s) == TaskParallelismInfo: - s = cast(TaskParallelismInfo, s) if s.type is TPIType.TASKWAIT and s._node == stws_cu: s.pragma_line = int(loop_cu.end_position().split(":")[1]) # move pragma task line to beginning of loop body (i.e. make the entire loop body a task) diff --git a/discopop_explorer/pattern_detectors/task_parallelism/task_parallelism_detector.py b/discopop_explorer/pattern_detectors/task_parallelism/task_parallelism_detector.py index ffa2180c2..f529f754c 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/task_parallelism_detector.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/task_parallelism_detector.py @@ -91,7 +91,7 @@ def build_preprocessed_graph_and_run_detection( if llvm_cxxfilt_path is None: __global_llvm_cxxfilt_path = "None" else: - __global_llvm_cxxfilt_path = cast(str, llvm_cxxfilt_path) + __global_llvm_cxxfilt_path = llvm_cxxfilt_path if discopop_build_path is None or discopop_build_path == "None": raise ValueError("Path to DiscoPoP build directory not specified!") set_global_llvm_cxxfilt_path(__global_llvm_cxxfilt_path) @@ -110,7 +110,7 @@ def build_preprocessed_graph_and_run_detection( file_mapping, dep_file, cu_inst_result_file, - cast(str, discopop_build_path), + discopop_build_path, ) return suggestions diff --git a/discopop_explorer/pattern_detectors/task_parallelism/tp_utils.py b/discopop_explorer/pattern_detectors/task_parallelism/tp_utils.py index bca2e7bfc..a9843bf13 100644 --- a/discopop_explorer/pattern_detectors/task_parallelism/tp_utils.py +++ b/discopop_explorer/pattern_detectors/task_parallelism/tp_utils.py @@ -44,12 +44,12 @@ def demangle(mangled_name: str) -> str: # set default llvm-cxxfilt executable llvm_cxxfilt_path = "llvm-cxxfilt" else: - llvm_cxxfilt_path = cast(str, __global_llvm_cxxfilt_path) + llvm_cxxfilt_path = __global_llvm_cxxfilt_path try: process = subprocess.Popen([llvm_cxxfilt_path, mangled_name], stdout=subprocess.PIPE) process.wait() if process.stdout is not None: - out_bytes = cast(IO[bytes], process.stdout).readline() + out_bytes = process.stdout.readline() out = out_bytes.decode("UTF-8") out = out.replace("\n", "") demangling_cache[mangled_name] = out @@ -358,7 +358,7 @@ def recursive_function_call_contained_in_worker_cu( tightest_worker_cu = cur_w if tightest_worker_cu is None: raise ValueError("No surrounding worker CU could be found.") - return cast(Node, tightest_worker_cu) + return tightest_worker_cu def task_contained_in_reduction_loop( @@ -431,9 +431,7 @@ def __get_word_prior_to_bracket(string): string = string[-1] return string - called_function_name_contained = False - if called_function_name is None: - called_function_name_contained = True + called_function_name_contained = called_function_name is None while ( function_call_string.count("(") > function_call_string.count(")") @@ -467,7 +465,7 @@ def __get_word_prior_to_bracket(string): function_call_string = function_call_string.replace("\n", "") # if called_function_name is set and contained more than once in function_call_string, split function_call_string if called_function_name is not None: - called_function_name_str = cast(str, called_function_name) + called_function_name_str = called_function_name while function_call_string.count(called_function_name_str) > 1: function_call_string = function_call_string[ : function_call_string.rfind(called_function_name_str) @@ -563,7 +561,7 @@ def get_called_function_and_parameter_names_from_function_call( else: # check if param in known variables: result_parameters.append(param.replace(" ", "")) - return cast(Optional[str], function_name), cast(List[Optional[str]], result_parameters) + return cast(Optional[str], function_name), result_parameters def set_global_llvm_cxxfilt_path(value: str): diff --git a/discopop_explorer/utils.py b/discopop_explorer/utils.py index ec741fc7a..00ad1eb62 100644 --- a/discopop_explorer/utils.py +++ b/discopop_explorer/utils.py @@ -108,7 +108,7 @@ def calculate_workload(pet: PETGraphX, node: Node) -> int: """ # check if value already present if node.workload is not None: - return cast(int, node.workload) + return node.workload res = 0 if node.type == NodeType.DUMMY: # store workload diff --git a/discopop_library/discopop_optimizer/scheduling/workload_delta.py b/discopop_library/discopop_optimizer/scheduling/workload_delta.py index 731a276a3..4e6c3a517 100644 --- a/discopop_library/discopop_optimizer/scheduling/workload_delta.py +++ b/discopop_library/discopop_optimizer/scheduling/workload_delta.py @@ -224,15 +224,7 @@ def __parse_node( # calculate the workload of the current node and register it in the stack # add workload of the current node to min / max if isinstance(node_data, Workload): - min_wl = ( - 0 - if cast(Workload, node_data).sequential_workload is None - else cast(int, cast(Workload, node_data).sequential_workload) - ) + ( - 0 - if cast(Workload, node_data).parallelizable_workload is None - else cast(int, cast(Workload, node_data).parallelizable_workload) - ) + min_wl = (node_data.sequential_workload or 0) + (node_data.parallelizable_workload or 0) max_wl = min_wl # apply iteration factor if requested min_wl *= iteration_factor @@ -241,7 +233,7 @@ def __parse_node( # register children if type(node_data) == Loop: - iteration_factor = cast(Loop, node_data).iterations + iteration_factor = node_data.iterations else: iteration_factor = 1 for child_id in get_children(experiment.optimization_graph, node_id): diff --git a/discopop_library/discopop_optimizer/utilities/MOGUtilities.py b/discopop_library/discopop_optimizer/utilities/MOGUtilities.py index 9e4e0e4a7..85a0c418f 100644 --- a/discopop_library/discopop_optimizer/utilities/MOGUtilities.py +++ b/discopop_library/discopop_optimizer/utilities/MOGUtilities.py @@ -156,16 +156,14 @@ def convert_temporary_edges(graph: nx.DiGraph): for edge in graph.edges: edge_data = graph.edges[edge]["data"] if isinstance(edge_data, TemporaryEdge): - graph.edges[edge]["data"] = cast(TemporaryEdge, edge_data).convert_to_successor_edge() + graph.edges[edge]["data"] = edge_data.convert_to_successor_edge() def convert_temporary_edge(graph: nx.DiGraph, source_id: int, target_id: int): """Converts a single temporary edge to a successor edge""" edge_data = graph.edges[(source_id, target_id)]["data"] if isinstance(edge_data, TemporaryEdge): - graph.edges[(source_id, target_id)]["data"] = cast( - TemporaryEdge, edge_data - ).convert_to_successor_edge() + graph.edges[(source_id, target_id)]["data"] = edge_data.convert_to_successor_edge() def get_all_function_nodes(graph: nx.DiGraph) -> List[int]: diff --git a/mypy.ini b/mypy.ini index 2287dd58d..65081c12c 100644 --- a/mypy.ini +++ b/mypy.ini @@ -18,7 +18,7 @@ files = discopop_explorer, discopop_library, discopop_profiler, discopop_wizard warn_unused_configs = True # the following options should be enabled one by one, then we will switch to `strict` type checking: -#warn_redundant_casts = True +warn_redundant_casts = True #disallow-any-generics #disallow-subclassing-any