Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix minimize_register_usage objective #76

Merged
merged 2 commits into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 7 additions & 8 deletions slothy/core/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,6 @@ def __init__(self, config):
self._kernel_input_output = None
self._pre_core_post_dict = None
self._codesize_with_bubbles = None
self._register_used = None
self._optimization_wall_time = None
self._optimization_user_time = None

Expand Down Expand Up @@ -2201,7 +2200,7 @@ def _allow_renaming(_):
if self.config.constraints.minimize_register_usage is not None:
ty = self.config.constraints.minimize_register_usage
regs = self.arch.RegisterType.list_registers(ty)
self._register_used = { reg : self._NewBoolVar(f"reg_used[{reg}]") for reg in regs }
self._model._register_used = { reg : self._NewBoolVar(f"reg_used[{reg}]") for reg in regs }

# Create variables for register renaming

Expand Down Expand Up @@ -2506,9 +2505,9 @@ def _add_constraints_register_renaming(self):
for reg in self.arch.RegisterType.list_registers(ty):
arr = self._model.register_usage_vars.get(reg,[])
if len(arr) > 0:
self._model.AddMaxEquality(self._register_used[reg], arr)
self._AddMaxEquality(self._model._register_used[reg], arr)
else:
self._Add(self._register_used[reg] is False)
self._Add(self._model._register_used[reg] == False)

# Ensure that outputs are unambiguous
for t in self._get_nodes(allnodes=True):
Expand Down Expand Up @@ -3064,8 +3063,6 @@ def _add_objective(self, force_objective=False):
name = None
printer = None

# We only support objectives of the form: Maximize/Minimize the sum of a set of variables.

# If the number of stalls is variable, its minimization is our objective
if force_objective is False and self.config.variable_size:
name = "minimize cycles"
Expand All @@ -3090,8 +3087,8 @@ def _add_objective(self, force_objective=False):
maxlist = [ t.program_start_var for t in self._get_nodes() ]
name = "move stalls to top"
elif self.config.constraints.minimize_register_usage is not None:
# Minimize the number of registers used
minlist = list(self._register_used.values())
minlist = list(self._model._register_used.values())
name = "minimize number of registers used"
elif self.config.constraints.minimize_use_of_extra_registers is not None:
ty = self.config.constraints.minimize_use_of_extra_registers
minlist = []
Expand Down Expand Up @@ -3169,6 +3166,8 @@ def _AddHint(self,var,val): # pylint:disable=invalid-name
return self._model.cp_model.AddHint(var,val)
def _AddNoOverlap(self,interval_list): # pylint:disable=invalid-name
return self._model.cp_model.AddNoOverlap(interval_list)
def _AddMaxEquality(self,varlist,var): # pylint:disable=invalid-name
return self._model.cp_model.AddMaxEquality(varlist, var)

def _export_model(self):
if self.config.log_model is None:
Expand Down
20 changes: 17 additions & 3 deletions slothy/core/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,14 @@ def optimize_binsearch(source, logger, conf, **kwargs):
The Result object for the succceeding optimization with the smallest
number of stalls.
"""
flexible = not conf.constraints.functional_only

if conf.variable_size:
return Heuristics.optimize_binsearch_internal(source, logger, conf, **kwargs)
return Heuristics.optimize_binsearch_internal(source, logger, conf,
flexible=flexible, **kwargs)

return Heuristics.optimize_binsearch_external(source, logger, conf, **kwargs)
return Heuristics.optimize_binsearch_external(source, logger, conf,
flexible=flexible, **kwargs)

@staticmethod
def _log_reoptimization_failure(log):
Expand Down Expand Up @@ -201,7 +205,7 @@ def optimize_binsearch_external(source, logger, conf, flexible=True, **kwargs):
return core.result

@staticmethod
def optimize_binsearch_internal(source, logger, conf, **kwargs):
def optimize_binsearch_internal(source, logger, conf, flexible=True, **kwargs):
"""Internally optimize for minimum number of stalls, and potentially a secondary objective.

This finds the minimum number of stalls for which a one-shot SLOTHY optimization succeeds.
Expand All @@ -213,11 +217,21 @@ def optimize_binsearch_internal(source, logger, conf, **kwargs):
logger: The logger to be used.
conf: The configuration to apply. This is fixed for all one-shot SLOTHY
runs invoked by this call, except for variation of stall count.
flexible: Indicates whether the number of stalls should be minimized
through a binary search, or whether a single one-shot SLOTHY optimization
for a fixed number of stalls (encoded in the configuration) should be
conducted.

Returns:
A Result object representing the final optimization result.
"""

if not flexible:
core = SlothyBase(conf.arch, conf.target, logger=logger,config=conf)
if not core.optimize(source):
raise SlothyException("Optimization failed")
return core.result

logger.info("Perform internal binary search for minimal number of stalls...")

start_attempt = conf.constraints.stalls_first_attempt
Expand Down
Loading