From b9d97fd32daef1f993f6c928b345187d5107817c Mon Sep 17 00:00:00 2001 From: Jonathan Lifflander Date: Mon, 12 Dec 2022 11:04:55 -0800 Subject: [PATCH] #324: memory: fix code style issues --- src/lbaf/Applications/LBAF_app.py | 2 +- .../lbsCentralizedPrefixOptimizerAlgorithm.py | 42 ++++++------------- 2 files changed, 13 insertions(+), 31 deletions(-) diff --git a/src/lbaf/Applications/LBAF_app.py b/src/lbaf/Applications/LBAF_app.py index f8b4f9b2a..5410ca862 100644 --- a/src/lbaf/Applications/LBAF_app.py +++ b/src/lbaf/Applications/LBAF_app.py @@ -351,7 +351,7 @@ def main(self): # Perform central prefix optimizer when requested if self.params.algorithm["name"] != "CentralizedPrefixOptimizer": - self.logger.info(f"Starting centralized prefix optimizer") + self.logger.info("Starting centralized prefix optimizer") # Instantiate and execute runtime qoi_name = self.params.__dict__.get("visualize_qoi") diff --git a/src/lbaf/Execution/lbsCentralizedPrefixOptimizerAlgorithm.py b/src/lbaf/Execution/lbsCentralizedPrefixOptimizerAlgorithm.py index 612f0d564..ec30b2c21 100644 --- a/src/lbaf/Execution/lbsCentralizedPrefixOptimizerAlgorithm.py +++ b/src/lbaf/Execution/lbsCentralizedPrefixOptimizerAlgorithm.py @@ -11,8 +11,7 @@ class CentralizedPrefixOptimizerAlgorithm(AlgorithmBase): - """ A concrete class for the centralized prefix memory-constrained optimizer - """ + """ A concrete class for the centralized prefix memory-constrained optimizer""" def __init__(self, work_model, parameters: dict, lgr: Logger, qoi_name: str): """ Class constructor @@ -25,8 +24,8 @@ def __init__(self, work_model, parameters: dict, lgr: Logger, qoi_name: str): work_model, parameters, lgr, qoi_name) def execute(self, phases: list, distributions: dict, statistics: dict, _): - """ Execute centralized prefix memory-constrained optimizer - """ + """ Execute centralized prefix memory-constrained optimizer""" + # Ensure that a list with at least one phase was provided if not phases or not isinstance(phases, list) or not isinstance( (phase := phases[0]), Phase): @@ -60,9 +59,7 @@ def execute(self, phases: list, distributions: dict, statistics: dict, _): # objects.sort(key=lambda x: x.get("id")) self._max_shared_ids = max_shared_ids + 1 - made_no_assignments = 0 - do_second_stage = False while made_no_assignments < 2: @@ -71,19 +68,14 @@ def execute(self, phases: list, distributions: dict, statistics: dict, _): for rank in phase_ranks: rank_max_heap.append(rank); - for i in range(len(rank_max_heap)): - self._logger.info("{} load {}".format(i, rank_max_heap[i].get_load())) - heapq._heapify_max(rank_max_heap) max_rank = heapq._heappop_max(rank_max_heap) diff = max_rank.get_load() - statistics["average load"] - self._logger.info("diff={}".format(diff)) + self._logger.info(f"diff={diff}") - shared_map = {} - obj_shared_map = {} - groupings = [] - groupings_sum = [] + shared_map, obj_shared_map = {}, {} + groupings, groupings_sum = [], [] for o in max_rank.get_migratable_objects(): if not o.get_shared_block_id() in obj_shared_map: @@ -99,14 +91,8 @@ def execute(self, phases: list, distributions: dict, statistics: dict, _): for sid in shared_map: groupings.append((shared_map[sid],sid)) - for x in groupings: - self._logger.info("grouping {}".format(x)) - groupings.sort() - for x in groupings: - self._logger.info("sorted grouping {}".format(x)) - for i in range(len(groupings)): groupings_sum.append(0) groupings_sum[0] = groupings[0][0] @@ -114,20 +100,18 @@ def execute(self, phases: list, distributions: dict, statistics: dict, _): groupings_sum[i] = groupings_sum[i-1] + groupings[i][0] for i in range(len(groupings)): - self._logger.info("i={} sum={}".format(i,groupings_sum[i])) + self._logger.info(f"i={i} sum={groupings_sum[i]}") pick_upper = 0 while (groupings_sum[pick_upper] < diff): - self._logger.info("add pick_upper") pick_upper += 1 if pick_upper-1 >= 0 and groupings_sum[pick_upper-1] >= diff * 1.05: pick_upper -= 1 - self._logger.info("subtract pick_upper") pick_lower = pick_upper-1 while (pick_lower-1 >= -1 and groupings_sum[pick_upper] - groupings_sum[pick_lower] < diff): pick_lower -= 1 - self._logger.info("pick=({},{})".format(pick_lower,pick_upper)) + self._logger.info(f"pick=({pick_lower},{pick_upper}]") made_assignment = False @@ -161,20 +145,18 @@ def execute(self, phases: list, distributions: dict, statistics: dict, _): self.report_final_mapping(self._logger) def tryBin(self, ranks, max_rank, tbin, size, sid, objs): - self._logger.info("tryBin size={}, max={}".format(size, self._max_shared_ids)) + self._logger.info(f"tryBin size={size}, max={self._max_shared_ids}") min_ranks = [] for rank in ranks: - self._logger.info("adding rank {}: len={} max={}".format(rank.get_id(),len(rank.get_shared_blocks()),self._max_shared_ids)) if sid in rank.get_shared_blocks() or len(rank.get_shared_blocks()) < self._max_shared_ids: min_ranks.append(rank) heapq.heapify(min_ranks) min_rank = None - tally_assigned = 0 - tally_rejected = 0 + tally_assigned, tally_rejected = 0, 0 for o in objs[sid]: if len(min_ranks) == 0: @@ -182,7 +164,7 @@ def tryBin(self, ranks, max_rank, tbin, size, sid, objs): sys.excepthook = exc_handler raise SystemExit(1) - if min_rank == None: + if min_rank is None: min_rank = heapq.heappop(min_ranks) if len(min_rank.get_shared_blocks()) >= self._max_shared_ids and not sid in min_rank.get_shared_blocks(): @@ -200,6 +182,6 @@ def tryBin(self, ranks, max_rank, tbin, size, sid, objs): tally_rejected += 1 - self._logger.info("try bin: {}, size={}, id={}; assigned={}, rejected={}\n".format(tbin, size, sid, tally_assigned, tally_rejected)) + self._logger.info(f"try bin: {tbin}, size={size}, id={sid}; assigned={tally_assigned}, rejected={tally_rejected}") return tally_assigned > 0