|
| 1 | +from EventManager.Models.RunnerEvents import RunnerEvents |
| 2 | +from EventManager.EventSubscriptionController import EventSubscriptionController |
| 3 | +from ConfigValidator.Config.Models.RunTableModel import RunTableModel |
| 4 | +from ConfigValidator.Config.Models.FactorModel import FactorModel |
| 5 | +from ConfigValidator.Config.Models.RunnerContext import RunnerContext |
| 6 | +from ConfigValidator.Config.Models.OperationType import OperationType |
| 7 | +from ProgressManager.Output.OutputProcedure import OutputProcedure as output |
| 8 | +from Plugins.Profilers.EnergiBridge import EnergiBridge |
| 9 | + |
| 10 | +from typing import Dict, List, Any, Optional |
| 11 | +from pathlib import Path |
| 12 | +from os.path import dirname, realpath |
| 13 | + |
| 14 | + |
| 15 | +class RunnerConfig: |
| 16 | + ROOT_DIR = Path(dirname(realpath(__file__))) |
| 17 | + |
| 18 | + # ================================ USER SPECIFIC CONFIG ================================ |
| 19 | + """The name of the experiment.""" |
| 20 | + name: str = "new_runner_experiment" |
| 21 | + |
| 22 | + """The path in which Experiment Runner will create a folder with the name `self.name`, in order to store the |
| 23 | + results from this experiment. (Path does not need to exist - it will be created if necessary.) |
| 24 | + Output path defaults to the config file's path, inside the folder 'experiments'""" |
| 25 | + results_output_path: Path = ROOT_DIR / 'experiments' |
| 26 | + |
| 27 | + """Experiment operation type. Unless you manually want to initiate each run, use `OperationType.AUTO`.""" |
| 28 | + operation_type: OperationType = OperationType.AUTO |
| 29 | + |
| 30 | + """The time Experiment Runner will wait after a run completes. |
| 31 | + This can be essential to accommodate for cooldown periods on some systems.""" |
| 32 | + time_between_runs_in_ms: int = 1000 |
| 33 | + |
| 34 | + # Dynamic configurations can be one-time satisfied here before the program takes the config as-is |
| 35 | + # e.g. Setting some variable based on some criteria |
| 36 | + def __init__(self): |
| 37 | + """Executes immediately after program start, on config load""" |
| 38 | + |
| 39 | + EventSubscriptionController.subscribe_to_multiple_events([ |
| 40 | + (RunnerEvents.BEFORE_EXPERIMENT, self.before_experiment), |
| 41 | + (RunnerEvents.BEFORE_RUN , self.before_run ), |
| 42 | + (RunnerEvents.START_RUN , self.start_run ), |
| 43 | + (RunnerEvents.START_MEASUREMENT, self.start_measurement), |
| 44 | + (RunnerEvents.INTERACT , self.interact ), |
| 45 | + (RunnerEvents.STOP_MEASUREMENT , self.stop_measurement ), |
| 46 | + (RunnerEvents.STOP_RUN , self.stop_run ), |
| 47 | + (RunnerEvents.POPULATE_RUN_DATA, self.populate_run_data), |
| 48 | + (RunnerEvents.AFTER_EXPERIMENT , self.after_experiment ) |
| 49 | + ]) |
| 50 | + self.run_table_model = None # Initialized later |
| 51 | + |
| 52 | + output.console_log("Custom config loaded") |
| 53 | + |
| 54 | + def create_run_table_model(self) -> RunTableModel: |
| 55 | + """Create and return the run_table model here. A run_table is a List (rows) of tuples (columns), |
| 56 | + representing each run performed""" |
| 57 | + factor1 = FactorModel("fib_type", ['iter', 'mem', 'rec']) |
| 58 | + factor2 = FactorModel("problem_size", [10, 20, 30]) |
| 59 | + self.run_table_model = RunTableModel( |
| 60 | + factors=[factor1, factor2], |
| 61 | + exclude_variations=[ |
| 62 | + {factor2: [10]}, # all runs having treatment "10" will be excluded |
| 63 | + {factor1: ['iter'], factor2: [30]}, # all runs having the combination ("iter", 30) will be excluded |
| 64 | + ], |
| 65 | + repetitions = 3, |
| 66 | + data_columns=["total_power (J)", "runtime (sec)", "avg_mem (bytes)"] |
| 67 | + ) |
| 68 | + return self.run_table_model |
| 69 | + |
| 70 | + def before_experiment(self) -> None: |
| 71 | + """Perform any activity required before starting the experiment here |
| 72 | + Invoked only once during the lifetime of the program.""" |
| 73 | + pass |
| 74 | + |
| 75 | + def before_run(self) -> None: |
| 76 | + """Perform any activity required before starting a run. |
| 77 | + No context is available here as the run is not yet active (BEFORE RUN)""" |
| 78 | + pass |
| 79 | + |
| 80 | + def start_run(self, context: RunnerContext) -> None: |
| 81 | + """Perform any activity required for starting the run here. |
| 82 | + For example, starting the target system to measure. |
| 83 | + Activities after starting the run should also be performed here.""" |
| 84 | + pass |
| 85 | + |
| 86 | + def start_measurement(self, context: RunnerContext) -> None: |
| 87 | + """Perform any activity required for starting measurements.""" |
| 88 | + fib_type = context.run_variation["fib_type"] |
| 89 | + problem_size = context.run_variation["problem_size"] |
| 90 | + |
| 91 | + self.profiler = EnergiBridge(target_program=f"python examples/hello-world-fibonacci/fibonacci_{fib_type}.py {problem_size}", |
| 92 | + out_file=context.run_dir / "energibridge.csv") |
| 93 | + |
| 94 | + self.profiler.start() |
| 95 | + |
| 96 | + def interact(self, context: RunnerContext) -> None: |
| 97 | + """Perform any interaction with the running target system here, or block here until the target finishes.""" |
| 98 | + pass |
| 99 | + |
| 100 | + def stop_measurement(self, context: RunnerContext) -> None: |
| 101 | + """Perform any activity here required for stopping measurements.""" |
| 102 | + stdout = self.profiler.stop(wait=True) |
| 103 | + |
| 104 | + def stop_run(self, context: RunnerContext) -> None: |
| 105 | + """Perform any activity here required for stopping the run. |
| 106 | + Activities after stopping the run should also be performed here.""" |
| 107 | + pass |
| 108 | + |
| 109 | + def populate_run_data(self, context: RunnerContext) -> Optional[Dict[str, Any]]: |
| 110 | + """Parse and process any measurement data here. |
| 111 | + You can also store the raw measurement data under `context.run_dir` |
| 112 | + Returns a dictionary with keys `self.run_table_model.data_columns` and their values populated""" |
| 113 | + |
| 114 | + eb_log, eb_summary = self.profiler.parse_log(self.profiler.logfile, |
| 115 | + self.profiler.summary_logfile) |
| 116 | + |
| 117 | + return {"total_power (J)": eb_summary["total_joules"], |
| 118 | + "runtime (sec)": eb_summary["runtime_seconds"], |
| 119 | + "total_mem (bytes)": list(eb_log["TOTAL_MEMORY"].values())[-1]} |
| 120 | + |
| 121 | + def after_experiment(self) -> None: |
| 122 | + """Perform any activity required after stopping the experiment here |
| 123 | + Invoked only once during the lifetime of the program.""" |
| 124 | + pass |
| 125 | + |
| 126 | + # ================================ DO NOT ALTER BELOW THIS LINE ================================ |
| 127 | + experiment_path: Path = None |
0 commit comments