Skip to content

Commit

Permalink
implement basic time limit for PH
Browse files Browse the repository at this point in the history
  • Loading branch information
bknueven committed Dec 2, 2024
1 parent 9875bad commit e41c050
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 4 deletions.
3 changes: 2 additions & 1 deletion doc/src/hubs.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ in a ``Config`` object) is often set to a negative number so internal
convergence is ignored in favor of the threshhold on the gap between
upper and lower bounds as computed by the spokes (``rel_gap`` and
``abs_gap`` in ``Config`` object). Most hubs can be terminated
based on an iteration limit (``max_iterations`` in a ``Config`` object).
based on an iteration limit (``max_iterations`` in a ``Config`` object),
or a time limit in seconds (``time_limit`` in a ``Config`` object).

An additional gap-based termination option is supported by
``Config`` and ``cfg_vanilla.py``: ``max_stalled_iters``
Expand Down
3 changes: 2 additions & 1 deletion examples/afew.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,9 @@ def do_one(dirname, progname, np, argstring):
"--solver-name={} --rel-gap=0.0 "
" --xhatlshaped --max-solver-threads=1".format(solver_name))
do_one("hydro", "hydro_cylinders_pysp.py", 3,
"--bundles-per-rank=0 --max-iterations=100 "
"--bundles-per-rank=0 --max-iterations=10000 "
"--default-rho=1 --xhatshuffle --lagrangian "
"--abs-gap=0 --rel-gap=0 --time-limit=2 "
"--solver-name={}".format(solver_name))

if len(badguys) > 0:
Expand Down
10 changes: 9 additions & 1 deletion mpisppy/phbase.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,6 +821,9 @@ def options_check(self):
# Smoothed is optional, not required
if "smoothed" not in self.options:
self.options["smoothed"] = 0
# time_limit is optional, not required
if "time_limit" not in self.options:
self.options["time_limit"] = None


def Iter0(self):
Expand Down Expand Up @@ -1001,10 +1004,15 @@ def iterk_loop(self):
if self.convobject.is_converged():
global_toc("User-supplied converger determined termination criterion reached", self.cylinder_rank == 0)
break
elif self.conv is not None:
if self.conv is not None:
if self.conv < self.options["convthresh"]:
global_toc("Convergence metric=%f dropped below user-supplied threshold=%f" % (self.conv, self.options["convthresh"]), self.cylinder_rank == 0)
break
if self.options["time_limit"] is not None:
time_to_stop = self.allreduce_or( (time.perf_counter() - self.start_time) >= self.options["time_limit"] )
if time_to_stop:
global_toc(f"Time limit {self.options['time_limit']} seconds reached.", self.cylinder_rank == 0)
break

teeme = (
"tee-rank0-solves" in self.options
Expand Down
1 change: 1 addition & 0 deletions mpisppy/utils/cfg_vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def shared_options(cfg):
"defaultPHrho": cfg.default_rho,
"convthresh": 0,
"PHIterLimit": cfg.max_iterations, # not needed by all
"time_limit": cfg.time_limit, # not needed by all
"verbose": cfg.verbose,
"display_progress": cfg.display_progress,
"display_convergence_detail": cfg.display_convergence_detail,
Expand Down
7 changes: 6 additions & 1 deletion mpisppy/utils/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,10 +173,15 @@ def _common_args(self):

def popular_args(self):
self.add_to_config("max_iterations",
description="hub max iiterations (default 1)",
description="hub max iterations (default 1)",
domain=int,
default=1)

self.add_to_config("time_limit",
description="hub time limit in seconds (default None)",
domain=int,
default=None)

self.add_solver_specs(prefix="")

self.add_to_config("seed",
Expand Down

0 comments on commit e41c050

Please sign in to comment.