Skip to content

Commit

Permalink
cleanup(autotuner): minor style improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lukasrothenberger committed Jul 23, 2024
1 parent 6aabbd6 commit 9d81aae
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 23 deletions.
59 changes: 52 additions & 7 deletions discopop_library/EmpiricalAutotuning/Autotuner.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,10 @@ def run(arguments: AutotunerArguments) -> None:
# get untuned reference result
reference_configuration = CodeConfiguration(arguments.project_path, arguments.dot_dp_path)
reference_configuration.execute(timeout=None, is_initial=True)
statistics_graph.set_root(reference_configuration)
statistics_graph.set_root(reference_configuration.get_statistics_graph_label(), color=reference_configuration.get_statistics_graph_color(), shape=NodeShape.BOX)
timeout_after = cast(ExecutionResult, reference_configuration.execution_result).runtime * 2
debug_stats.append(([], cast(ExecutionResult, reference_configuration.execution_result).runtime))


# load hotspots
hsl_arguments = HotspotLoaderArguments(
arguments.log_level, arguments.write_log, False, arguments.dot_dp_path, True, False, True, True, True
Expand All @@ -71,7 +70,19 @@ def run(arguments: AutotunerArguments) -> None:
loop_tuples = hotspot_information[hotspot_type]
sorted_loop_tuples = sorted(loop_tuples, key=lambda x: x[4], reverse=True)
for loop_tuple in sorted_loop_tuples:
loop_str = "" + str(loop_tuple[0]) + "@" + str(loop_tuple[1]) + " - "+ str(loop_tuple[2]) + " " + loop_tuple[3] + " " + str(round(loop_tuple[4], 3))+"s"
loop_str = (
""
+ str(loop_tuple[0])
+ "@"
+ str(loop_tuple[1])
+ " - "
+ str(loop_tuple[2])
+ " "
+ loop_tuple[3]
+ " "
+ str(round(loop_tuple[4], 3))
+ "s"
)
statistics_graph.add_child(loop_str)
statistics_graph.update_current_node(loop_str)
# identify all applicable suggestions for this loop
Expand All @@ -88,7 +99,16 @@ def run(arguments: AutotunerArguments) -> None:
tmp_config = reference_configuration.create_copy(get_unique_configuration_id)
tmp_config.apply_suggestions(arguments, current_config)
tmp_config.execute(timeout=timeout_after)
statistics_graph.add_child("step " + str(statistics_step_num) + "\n" + str(current_config) + "\n" + tmp_config.get_statistics_graph_label(), shape=NodeShape.BOX, color=tmp_config.get_statistics_graph_color())
statistics_graph.add_child(
"step "
+ str(statistics_step_num)
+ "\n"
+ str(current_config)
+ "\n"
+ tmp_config.get_statistics_graph_label(),
shape=NodeShape.BOX,
color=tmp_config.get_statistics_graph_color(),
)
# only consider valid code
if (
cast(ExecutionResult, tmp_config.execution_result).result_valid
Expand All @@ -101,7 +121,16 @@ def run(arguments: AutotunerArguments) -> None:
tmp_config.deleteFolder()
# add current best configuration for reference / to detect "no suggestions is beneficial"
suggestion_effects.append(best_suggestion_configuration)
statistics_graph.add_child("step " + str(statistics_step_num) + "\n" +str(best_suggestion_configuration[0]) + "\n" + best_suggestion_configuration[1].get_statistics_graph_label(), shape=NodeShape.BOX, color=best_suggestion_configuration[1].get_statistics_graph_color())
statistics_graph.add_child(
"step "
+ str(statistics_step_num)
+ "\n"
+ str(best_suggestion_configuration[0])
+ "\n"
+ best_suggestion_configuration[1].get_statistics_graph_label(),
shape=NodeShape.BOX,
color=best_suggestion_configuration[1].get_statistics_graph_color(),
)

logger.debug(
"Suggestion effects:\n" + str([(str(t[0]), str(t[1].execution_result)) for t in suggestion_effects])
Expand All @@ -120,8 +149,24 @@ def run(arguments: AutotunerArguments) -> None:
+ " stored at "
+ best_suggestion_configuration[1].root_path
)
statistics_graph.add_child("step " + str(statistics_step_num) + "\n" +str(best_suggestion_configuration[0]) + "\n" + best_suggestion_configuration[1].get_statistics_graph_label(), shape=NodeShape.BOX, color=best_suggestion_configuration[1].get_statistics_graph_color())
statistics_graph.update_current_node("step " + str(statistics_step_num) + "\n" +str(best_suggestion_configuration[0]) + "\n" + best_suggestion_configuration[1].get_statistics_graph_label())
statistics_graph.add_child(
"step "
+ str(statistics_step_num)
+ "\n"
+ str(best_suggestion_configuration[0])
+ "\n"
+ best_suggestion_configuration[1].get_statistics_graph_label(),
shape=NodeShape.BOX,
color=best_suggestion_configuration[1].get_statistics_graph_color(),
)
statistics_graph.update_current_node(
"step "
+ str(statistics_step_num)
+ "\n"
+ str(best_suggestion_configuration[0])
+ "\n"
+ best_suggestion_configuration[1].get_statistics_graph_label()
)
statistics_graph.output()
statistics_step_num += 1
# cleanup other configurations (excluding original version)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def __init__(self, root_path: str, config_dot_dp_path: str):
def __str__(self) -> str:
return self.root_path

def execute(self, timeout: Optional[float], is_initial: bool =False) -> None:
def execute(self, timeout: Optional[float], is_initial: bool = False) -> None:
# create timeout string
timeout_string = "" if timeout is None else "timeout " + str(timeout) + " "
if is_initial:
Expand Down Expand Up @@ -140,14 +140,18 @@ def apply_suggestions(self, arguments: AutotunerArguments, suggestion_ids: List[

def get_statistics_graph_label(self) -> str:
res_str = "" + self.root_path + "\n"
res_str += str(round(self.execution_result.runtime, 3)) + "s"
if self.execution_result is None:
res_str += "Not executed."
else:
res_str += str(round(self.execution_result.runtime, 3)) + "s"

return res_str

return res_str

def get_statistics_graph_color(self) -> NodeColor:
if self.execution_result is None:
return NodeColor.ORANGE
if self.execution_result.result_valid and self.execution_result.return_code == 0:
return NodeColor.GREEN
if self.execution_result.return_code == 0 and not self.execution_result.result_valid:
return NodeColor.ORANGE
return NodeColor.RED

26 changes: 15 additions & 11 deletions discopop_library/EmpiricalAutotuning/Statistics/StatisticsGraph.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

logger = logging.getLogger("StatisticsGraph")


class NodeShape(Enum):
SQUARE = "square"
CIRCLE = "circle"
Expand Down Expand Up @@ -46,30 +47,33 @@ def output(self) -> None:
self.dump_to_dot()
self.create_svg_from_dot()

def set_root(self, label: str) -> None:
self.G.add_node(label)
def set_root(self, label: str, color: NodeColor = NodeColor.WHITE, shape: NodeShape = NodeShape.NONE) -> None:
if shape == NodeShape.NONE:
self.G.add_node(label, color="black", fillcolor=color.value, style="filled")
else:
self.G.add_node(label, color="black", fillcolor=color.value, style="filled", shape=shape.value)
self.current_node = label

def update_current_node(self, label: str)->None:
def update_current_node(self, label: str) -> None:
self.current_node = label

def add_child(self, child: str, color:NodeColor = NodeColor.WHITE, shape: NodeShape = NodeShape.NONE) -> None:
def add_child(self, child: str, color: NodeColor = NodeColor.WHITE, shape: NodeShape = NodeShape.NONE) -> None:
if shape == NodeShape.NONE:
self.G.add_node(child, color="black", fillcolor=color.value, style="filled")
else:
self.G.add_node(child, color="black", fillcolor=color.value, shape=shape.value, style="filled")
self.G.add_edge(self.current_node, child)

def create_svg_from_dot(self) -> None:

cmd = "dot -Tsvg dp_autotuner_statistics.dot -o dp_autotuner_statistics.svg"
res = subprocess.run(
cmd,
cwd=os.getcwd(),
executable="/bin/bash",
shell=True,
cmd,
cwd=os.getcwd(),
executable="/bin/bash",
shell=True,
)
if res.returncode != 0:
if res.returncode != 0:
logger.warning("Failed: dot -Tsvg dp_autotuner_statistics.dot -o dp_autotuner_statistics.svg")
else:
logger.info("Updated dp_autotuner_statistics.svg")
logger.info("Updated dp_autotuner_statistics.svg")

0 comments on commit 9d81aae

Please sign in to comment.