|
| 1 | +import os |
| 2 | +import re |
| 3 | +from abc import ABC, abstractmethod |
| 4 | +from pathlib import Path |
| 5 | +from typing import Optional, Tuple |
| 6 | + |
| 7 | +import psutil |
| 8 | + |
| 9 | +from src.grammar.cnf_grammar_template import CnfGrammarTemplate |
| 10 | +from src.graph.label_decomposed_graph import LabelDecomposedGraph |
| 11 | +from src.problems.Base.template_cfg.utils import explode_indices |
| 12 | + |
| 13 | + |
| 14 | +def get_all_pairs_cflr_command_manager( |
| 15 | + algo_settings: str, |
| 16 | + graph_path: Path, |
| 17 | + grammar_path: Path |
| 18 | +) -> "AllPairsCflrCommandManager": |
| 19 | + return { |
| 20 | + "pocr": PocrAllPairsCflrCommandManager, |
| 21 | + "pearl": PearlAllPairsCflrCommandManager, |
| 22 | + "gigascale": GigascaleAllPairsCflrCommandManager, |
| 23 | + "graspan": GraspanAllPairsCflrCommandManager |
| 24 | + }.get(algo_settings, PyAlgoAllPairsCflrCommandManager)( |
| 25 | + algo_settings, graph_path, grammar_path |
| 26 | + ) |
| 27 | + |
| 28 | + |
| 29 | +class AllPairsCflrCommandManager(ABC): |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + algo_settings: str, |
| 33 | + graph_path: Path, |
| 34 | + grammar_path: Path |
| 35 | + ): |
| 36 | + self.algo_settings = algo_settings |
| 37 | + self.graph_path = graph_path |
| 38 | + self.grammar_path = grammar_path |
| 39 | + |
| 40 | + @abstractmethod |
| 41 | + def create_command(self) -> str: |
| 42 | + pass |
| 43 | + |
| 44 | + # noinspection PyMethodMayBeStatic |
| 45 | + def discard_stderr(self) -> bool: |
| 46 | + return False |
| 47 | + |
| 48 | + @property |
| 49 | + def work_dir(self) -> Optional[Path]: |
| 50 | + return None |
| 51 | + |
| 52 | + # noinspection PyMethodMayBeStatic |
| 53 | + def get_analysis_time(self, output: str) -> float: |
| 54 | + return float(re.search(r"AnalysisTime\s+([\d.]+|NaN)", output).group(1)) |
| 55 | + |
| 56 | + # noinspection PyMethodMayBeStatic |
| 57 | + def get_edge_count(self, output: str) -> int: |
| 58 | + return re.search(r"#(SEdges|CountEdges)\s+([\d.]+|NaN)", output).group(2) |
| 59 | + |
| 60 | + |
| 61 | +class PyAlgoAllPairsCflrCommandManager(AllPairsCflrCommandManager): |
| 62 | + def __init__(self, *args, **kwargs): |
| 63 | + super().__init__(*args, **kwargs) |
| 64 | + |
| 65 | + def create_command(self) -> Optional[str]: |
| 66 | + return f"python3 -m cli.run_all_pairs_cflr {self.algo_settings} {self.graph_path} {self.grammar_path}" |
| 67 | + |
| 68 | + |
| 69 | +class PocrAllPairsCflrCommandManager(AllPairsCflrCommandManager): |
| 70 | + def __init__(self, *args, **kwargs): |
| 71 | + super().__init__(*args, **kwargs) |
| 72 | + |
| 73 | + def create_command(self) -> Optional[str]: |
| 74 | + return ( |
| 75 | + f'{self.grammar_path.stem} -pocr {self.graph_path}' |
| 76 | + if self.grammar_path.stem in {"aa", "vf"} |
| 77 | + else f'cfl -pocr {self.grammar_path} {self.graph_path}' |
| 78 | + ) |
| 79 | + |
| 80 | + |
| 81 | +class PearlAllPairsCflrCommandManager(AllPairsCflrCommandManager): |
| 82 | + def __init__(self, *args, **kwargs): |
| 83 | + super().__init__(*args, **kwargs) |
| 84 | + |
| 85 | + def create_command(self) -> Optional[str]: |
| 86 | + return ( |
| 87 | + f'./{self.grammar_path.stem} {self.graph_path} -pearl -scc=false -gf=false' |
| 88 | + if self.grammar_path.stem in {"aa", "vf"} |
| 89 | + else None |
| 90 | + ) |
| 91 | + |
| 92 | + @property |
| 93 | + def work_dir(self) -> Optional[Path]: |
| 94 | + return Path(os.environ['PEARL_DIR']) |
| 95 | + |
| 96 | + def get_edge_count(self, output: str) -> int: |
| 97 | + vedges_search = re.search(r"#VEdges\s+(\d+)", output) |
| 98 | + if vedges_search: |
| 99 | + return vedges_search.group(1) |
| 100 | + return re.search(r"#AEdges\s+(\d+)", output).group(1) |
| 101 | + |
| 102 | + |
| 103 | +class GigascaleAllPairsCflrCommandManager(AllPairsCflrCommandManager): |
| 104 | + def __init__(self, *args, **kwargs): |
| 105 | + super().__init__(*args, **kwargs) |
| 106 | + |
| 107 | + def create_command(self) -> Optional[str]: |
| 108 | + return ( |
| 109 | + f'./run.sh -wdlrb -i datasets/dacapo9/{self.graph_path.stem}' |
| 110 | + if self.grammar_path.stem in {"java_points_to"} |
| 111 | + else None |
| 112 | + ) |
| 113 | + |
| 114 | + @property |
| 115 | + def work_dir(self) -> Optional[Path]: |
| 116 | + return Path(os.environ['GIGASCALE_DIR']) |
| 117 | + |
| 118 | + # Gigascale sends [INFO] logs to stderr |
| 119 | + def discard_stderr(self) -> bool: |
| 120 | + return True |
| 121 | + |
| 122 | + def get_analysis_time(self, output: str) -> float: |
| 123 | + return self._get_analysis_time_and_edge_count(output)[0] |
| 124 | + |
| 125 | + def get_edge_count(self, output: str) -> int: |
| 126 | + return self._get_analysis_time_and_edge_count(output)[1] |
| 127 | + |
| 128 | + @staticmethod |
| 129 | + def _get_analysis_time_and_edge_count(output: str) -> Tuple[float, int]: |
| 130 | + pattern = (r"benchmark\s+TC-time\s+TC-mem\s+v\s+e\s+vpt\s+avg\s+max\s+load/f\s+store/f\s*\n" |
| 131 | + r"\w+\s+" |
| 132 | + r"(\d+\.\d+)\s+" |
| 133 | + r"\d+(?:\.\d+)?\s+" |
| 134 | + r"\d+\s+" |
| 135 | + r"\d+\s+" |
| 136 | + r"(\d+)\s+" |
| 137 | + r"\d+(?:\.\d+)?\s+" |
| 138 | + r"\d+\s+" |
| 139 | + r"\d+\s+" |
| 140 | + r"\d+") |
| 141 | + |
| 142 | + match = re.search(pattern, output) |
| 143 | + |
| 144 | + tc_time, vpt = match.groups() |
| 145 | + return float(tc_time), int(vpt) |
| 146 | + |
| 147 | + |
| 148 | +class GraspanAllPairsCflrCommandManager(AllPairsCflrCommandManager): |
| 149 | + def __init__(self, *args, **kwargs): |
| 150 | + super().__init__(*args, **kwargs) |
| 151 | + |
| 152 | + def create_command(self) -> Optional[str]: |
| 153 | + grammar = CnfGrammarTemplate.read_from_pocr_cnf_file(self.grammar_path) |
| 154 | + graph = LabelDecomposedGraph.read_from_pocr_graph_file(self.graph_path) |
| 155 | + |
| 156 | + # Graspan doesn't support indexed symbols, we need to concat labels and indices |
| 157 | + if graph.block_matrix_space.block_count > 1: |
| 158 | + graph, grammar = explode_indices(graph, grammar) |
| 159 | + graph_path = self.graph_path.parent / "graspan" / self.graph_path.name |
| 160 | + os.makedirs(graph_path.parent, exist_ok=True) |
| 161 | + graph.write_to_pocr_graph_file(graph_path) |
| 162 | + else: |
| 163 | + graph_path = self.graph_path |
| 164 | + |
| 165 | + # Graspan doesn't support grammars with over 255 symbols, because |
| 166 | + # each symbol is encoded with one byte and one symbol is reserved for epsilon |
| 167 | + if len(grammar.symbols) > 255: |
| 168 | + return None |
| 169 | + |
| 170 | + grammar_path = self.grammar_path.parent / "graspan" / self.grammar_path.name |
| 171 | + os.makedirs(grammar_path.parent, exist_ok=True) |
| 172 | + grammar.write_to_pocr_cnf_file(grammar_path, include_starting=False) |
| 173 | + |
| 174 | + return ( |
| 175 | + f'./run {graph_path} {grammar_path} 1 ' |
| 176 | + f'{int(psutil.virtual_memory().total / 10**9 * 0.9)} ' |
| 177 | + f'{os.cpu_count() * 2}' |
| 178 | + ) |
| 179 | + |
| 180 | + @property |
| 181 | + def work_dir(self) -> Optional[Path]: |
| 182 | + return Path(os.environ['GRASPAN_DIR']) / "src" |
| 183 | + |
| 184 | + def get_analysis_time(self, output: str) -> float: |
| 185 | + return float(re.search(r"COMP TIME:\s*([\d.]+|NaN)", output).group(1)) |
| 186 | + |
| 187 | + def get_edge_count(self, output: str) -> int: |
| 188 | + final_file = re.search(r"finalFile:\s*(.*)", output).group(1) |
| 189 | + start_nonterm = CnfGrammarTemplate.read_from_pocr_cnf_file(self.grammar_path).start_nonterm |
| 190 | + with open(final_file, "r") as file: |
| 191 | + edges = set() |
| 192 | + for line in file: |
| 193 | + if line.split()[-1] == start_nonterm.label: |
| 194 | + edges.add((line.split()[0], line.split()[1])) |
| 195 | + return len(edges) |
0 commit comments