|
| 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.PowerMetrics import PowerMetrics |
| 9 | + |
| 10 | +from typing import Dict, List, Any, Optional |
| 11 | +from pathlib import Path |
| 12 | +from os.path import dirname, realpath |
| 13 | +import time |
| 14 | +import numpy as np |
| 15 | + |
| 16 | +class RunnerConfig: |
| 17 | + ROOT_DIR = Path(dirname(realpath(__file__))) |
| 18 | + |
| 19 | + # ================================ USER SPECIFIC CONFIG ================================ |
| 20 | + """The name of the experiment.""" |
| 21 | + name: str = "new_runner_experiment" |
| 22 | + |
| 23 | + """The path in which Experiment Runner will create a folder with the name `self.name`, in order to store the |
| 24 | + results from this experiment. (Path does not need to exist - it will be created if necessary.) |
| 25 | + Output path defaults to the config file's path, inside the folder 'experiments'""" |
| 26 | + results_output_path: Path = ROOT_DIR / 'experiments' |
| 27 | + |
| 28 | + """Experiment operation type. Unless you manually want to initiate each run, use `OperationType.AUTO`.""" |
| 29 | + operation_type: OperationType = OperationType.AUTO |
| 30 | + |
| 31 | + """The time Experiment Runner will wait after a run completes. |
| 32 | + This can be essential to accommodate for cooldown periods on some systems.""" |
| 33 | + time_between_runs_in_ms: int = 1000 |
| 34 | + |
| 35 | + # Dynamic configurations can be one-time satisfied here before the program takes the config as-is |
| 36 | + # e.g. Setting some variable based on some criteria |
| 37 | + def __init__(self): |
| 38 | + """Executes immediately after program start, on config load""" |
| 39 | + |
| 40 | + EventSubscriptionController.subscribe_to_multiple_events([ |
| 41 | + (RunnerEvents.BEFORE_EXPERIMENT, self.before_experiment), |
| 42 | + (RunnerEvents.BEFORE_RUN , self.before_run ), |
| 43 | + (RunnerEvents.START_RUN , self.start_run ), |
| 44 | + (RunnerEvents.START_MEASUREMENT, self.start_measurement), |
| 45 | + (RunnerEvents.INTERACT , self.interact ), |
| 46 | + (RunnerEvents.STOP_MEASUREMENT , self.stop_measurement ), |
| 47 | + (RunnerEvents.STOP_RUN , self.stop_run ), |
| 48 | + (RunnerEvents.POPULATE_RUN_DATA, self.populate_run_data), |
| 49 | + (RunnerEvents.AFTER_EXPERIMENT , self.after_experiment ) |
| 50 | + ]) |
| 51 | + self.run_table_model = None # Initialized later |
| 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 | + |
| 58 | + # Create the experiment run table with factors, and desired data columns |
| 59 | + factor1 = FactorModel("test_factor", [1, 2]) |
| 60 | + self.run_table_model = RunTableModel( |
| 61 | + factors = [factor1], |
| 62 | + data_columns=["joules", "avg_cpu", "avg_gpu"]) |
| 63 | + |
| 64 | + return self.run_table_model |
| 65 | + |
| 66 | + def before_experiment(self) -> None: |
| 67 | + """Perform any activity required before starting the experiment here |
| 68 | + Invoked only once during the lifetime of the program.""" |
| 69 | + pass |
| 70 | + |
| 71 | + def before_run(self) -> None: |
| 72 | + """Perform any activity required before starting a run. |
| 73 | + No context is available here as the run is not yet active (BEFORE RUN)""" |
| 74 | + pass |
| 75 | + |
| 76 | + def start_run(self, context: RunnerContext) -> None: |
| 77 | + """Perform any activity required for starting the run here. |
| 78 | + For example, starting the target system to measure. |
| 79 | + Activities after starting the run should also be performed here.""" |
| 80 | + pass |
| 81 | + |
| 82 | + def start_measurement(self, context: RunnerContext) -> None: |
| 83 | + """Perform any activity required for starting measurements.""" |
| 84 | + |
| 85 | + # Create the powermetrics object we will use to collect data |
| 86 | + self.meter = PowerMetrics(out_file=context.run_dir / "powermetrics.plist") |
| 87 | + # Start measuring useing powermetrics |
| 88 | + self.meter.start() |
| 89 | + |
| 90 | + def interact(self, context: RunnerContext) -> None: |
| 91 | + """Perform any interaction with the running target system here, or block here until the target finishes.""" |
| 92 | + |
| 93 | + # Wait (block) for a bit to collect some data |
| 94 | + time.sleep(20) |
| 95 | + |
| 96 | + def stop_measurement(self, context: RunnerContext) -> None: |
| 97 | + """Perform any activity here required for stopping measurements.""" |
| 98 | + |
| 99 | + # Stop measuring at the end of a run |
| 100 | + stdout = self.meter.stop() |
| 101 | + |
| 102 | + def stop_run(self, context: RunnerContext) -> None: |
| 103 | + """Perform any activity here required for stopping the run. |
| 104 | + Activities after stopping the run should also be performed here.""" |
| 105 | + pass |
| 106 | + |
| 107 | + def populate_run_data(self, context: RunnerContext) -> Optional[Dict[str, Any]]: |
| 108 | + """Parse and process any measurement data here. |
| 109 | + You can also store the raw measurement data under `context.run_dir` |
| 110 | + Returns a dictionary with keys `self.run_table_model.data_columns` and their values populated""" |
| 111 | + |
| 112 | + # Retrieve data from run |
| 113 | + run_results = self.meter.parse_log(context.run_dir / "powermetrics.plist") |
| 114 | + |
| 115 | + # Parse it as required for your experiment and add it to the run table |
| 116 | + return { |
| 117 | + "joules": sum(map(lambda x: x["processor"]["package_joules"], run_results)), |
| 118 | + "avg_cpu": np.mean(list(map(lambda x: x["processor"]["packages"][0]["cores_active_ratio"], run_results))), |
| 119 | + "avg_gpu": np.mean(list(map(lambda x: x["processor"]["packages"][0]["gpu_active_ratio"], run_results))), |
| 120 | + } |
| 121 | + |
| 122 | + def after_experiment(self) -> None: |
| 123 | + """Perform any activity required after stopping the experiment here |
| 124 | + Invoked only once during the lifetime of the program.""" |
| 125 | + pass |
| 126 | + |
| 127 | + # ================================ DO NOT ALTER BELOW THIS LINE ================================ |
| 128 | + experiment_path: Path = None |
0 commit comments