diff --git a/vtr_flow/scripts/python_libs/vtr/__init__.py b/vtr_flow/scripts/python_libs/vtr/__init__.py index 5fce29c9d63..9237e03be89 100644 --- a/vtr_flow/scripts/python_libs/vtr/__init__.py +++ b/vtr_flow/scripts/python_libs/vtr/__init__.py @@ -15,12 +15,12 @@ argparse_use_previous, argparse_str2bool, get_existing_run_dir, - get_latest_run_dir, - get_latest_run_number, + get_active_run_dir, get_next_run_dir, verify_file, pretty_print_table, find_task_dir, + RunDir, ) from .log_parse import ( determine_lut_size, diff --git a/vtr_flow/scripts/python_libs/vtr/parse_vtr_task.py b/vtr_flow/scripts/python_libs/vtr/parse_vtr_task.py index 32df9b17e07..0eff7bbb75c 100755 --- a/vtr_flow/scripts/python_libs/vtr/parse_vtr_task.py +++ b/vtr_flow/scripts/python_libs/vtr/parse_vtr_task.py @@ -7,6 +7,7 @@ from pathlib import Path from pathlib import PurePath import sys +import os import argparse import textwrap import shutil @@ -18,13 +19,12 @@ from vtr import ( load_list_file, RawDefaultHelpFormatter, - get_latest_run_dir, + get_active_run_dir, load_task_config, find_task_config_file, load_pass_requirements, load_parse_results, parse_vtr_flow, - get_latest_run_number, pretty_print_table, find_task_dir, CommandError, @@ -32,6 +32,7 @@ VtrError, create_jobs, paths, + RunDir, ) # pylint: enable=wrong-import-position @@ -130,7 +131,12 @@ def vtr_command_argparser(prog=None): help="QoR geomeans are not computed by default", ) - parser.add_argument("-run", default=None, type=str, help="") + parser.add_argument( + "-run", + default=None, + type=str, + help="Parse the specified run directory. Defaults to the latest.", + ) parser.add_argument("-revision", default="", help="Revision number") @@ -144,6 +150,8 @@ def vtr_command_main(arg_list, prog=None): """ # Load the arguments args = vtr_command_argparser(prog).parse_args(arg_list) + if args.run is not None: + RunDir.set_user_run_dir_name(args.run) try: task_names = args.task @@ -208,7 +216,7 @@ def parse_task(config, config_jobs, flow_metrics_basename=FIRST_PARSE_FILE, alt_ max_arch_len = len("architecture") max_circuit_len = len("circuit") for job in config_jobs: - work_dir = job.work_dir(get_latest_run_dir(find_task_dir(config, alt_tasks_dir))) + work_dir = job.work_dir(get_active_run_dir(find_task_dir(config, alt_tasks_dir))) job.parse_command()[0] = work_dir # job.second_parse_command()[0] = work_dir job.qor_parse_command()[0] = work_dir @@ -525,7 +533,7 @@ def calc_geomean(args, configs): first = False lines = summary.readlines() print( - get_latest_run_number(find_task_dir(configs[0], args.alt_tasks_dir)), + os.path.basename(get_active_run_dir(find_task_dir(configs[0], args.alt_tasks_dir))), file=out, end="\t", ) @@ -571,7 +579,7 @@ def find_latest_run_dir(config, alt_tasks_dir=None): """Find the latest run directory for given configuration""" task_dir = find_task_dir(config, alt_tasks_dir) - run_dir = get_latest_run_dir(task_dir) + run_dir = get_active_run_dir(task_dir) if not run_dir: raise InspectError( diff --git a/vtr_flow/scripts/python_libs/vtr/task.py b/vtr_flow/scripts/python_libs/vtr/task.py index be2559446fe..3d063d05202 100644 --- a/vtr_flow/scripts/python_libs/vtr/task.py +++ b/vtr_flow/scripts/python_libs/vtr/task.py @@ -16,8 +16,8 @@ load_list_file, load_parse_results, get_existing_run_dir, - get_latest_run_dir, get_next_run_dir, + get_active_run_dir, find_task_dir, load_script_param, paths, @@ -585,7 +585,7 @@ def create_jobs(args, configs, after_run=False) -> List[Job]: work_dir = get_work_dir_addr(arch, circuit, noc_traffic) run_dir = ( - str(Path(get_latest_run_dir(find_task_dir(config, args.alt_tasks_dir))) / work_dir) + str(Path(get_active_run_dir(find_task_dir(config, args.alt_tasks_dir))) / work_dir) if after_run else str( Path(get_next_run_dir(find_task_dir(config, args.alt_tasks_dir))) / work_dir diff --git a/vtr_flow/scripts/python_libs/vtr/util.py b/vtr_flow/scripts/python_libs/vtr/util.py index 7d1c7dbab11..a80521f07e8 100644 --- a/vtr_flow/scripts/python_libs/vtr/util.py +++ b/vtr_flow/scripts/python_libs/vtr/util.py @@ -22,6 +22,31 @@ from vtr import paths +class RunDir: + """ + A class for representing a run directory. + """ + + # The run directory name passed by set_global_run_dir + # is the run directory name to parse. + # If it is None, the latest run directory will be parsed. + g_run_dir_name = None + + @classmethod + def set_user_run_dir_name(cls, current_run_dir_name): + """ + Set the run directory name passed by the user. + """ + cls.g_run_dir_name = current_run_dir_name + + @classmethod + def get_user_run_dir_name(cls): + """ + Get the run directory name passed by the user. + """ + return cls.g_run_dir_name + + class RawDefaultHelpFormatter( argparse.ArgumentDefaultsHelpFormatter, argparse.RawDescriptionHelpFormatter ): @@ -521,16 +546,18 @@ def find_task_dir(config, alt_tasks_dir=None): return str(task_dir) -def get_latest_run_dir(base_dir): +def get_active_run_dir(base_dir): """ - Returns the run directory with the highest run number in base_dir + Returns full path to the active run directory + locatedin base_dir """ - latest_run_number = get_latest_run_number(base_dir) + active_run_dir_name = get_active_run_dir_name(base_dir) - if latest_run_number is None: - return None + run_dir = None + if active_run_dir_name: + run_dir = str(PurePath(base_dir) / active_run_dir_name) - return str(PurePath(base_dir) / run_dir_name(latest_run_number)) + return run_dir def get_existing_run_dir(base_dir: str, run_dir: str) -> str: @@ -549,34 +576,40 @@ def get_next_run_number(base_dir): """ Returns the next available (i.e. non-existing) run number in base_dir """ - latest_run_number = get_latest_run_number(base_dir) - - if latest_run_number is None: - next_run_number = 1 - else: + latest_run_dir_name = get_active_run_dir_name(base_dir) + match = re.match(r"^run(\d{3})$", latest_run_dir_name) + next_run_number = 1 + if match: + latest_run_number = int(match.group(1)) next_run_number = latest_run_number + 1 return next_run_number -def get_latest_run_number(base_dir): +def get_active_run_dir_name(base_dir): """ - Returns the highest run number of all run directories with in base_dir + Returns the active run directory name. If the user has specified + a run directory name, it will be returned. Otherwise, the + highest run number of all run directories within in base_dir + will be returned. """ - run_number = 1 - run_dir = Path(base_dir) / run_dir_name(run_number) + active_run_dir_name = "" + if RunDir.get_user_run_dir_name() is not None: + active_run_dir_name = RunDir.get_user_run_dir_name() + else: + run_number = 1 + run_dir = Path(base_dir) / run_dir_name(run_number) - if not run_dir.exists(): - # No existing run directories - return None + if run_dir.exists(): + while run_dir.exists(): + run_number += 1 + run_dir = Path(base_dir) / run_dir_name(run_number) - while run_dir.exists(): - run_number += 1 - run_dir = Path(base_dir) / run_dir_name(run_number) + # Currently one-past the last existing run dir, + # to get latest existing, subtract one + active_run_dir_name = run_dir_name(run_number - 1) - # Currently one-past the last existing run dir, - # to get latest existing, subtract one - return run_number - 1 + return active_run_dir_name def run_dir_name(run_num):