def exec_scenariotreeserver(options): modules_imported = {} for module_name in options.import_module: if module_name in sys.modules: modules_imported[module_name] = sys.modules[module_name] else: modules_imported[module_name] = \ load_external_module(module_name, clear_cache=True)[0] try: # spawn the daemon TaskWorkerServer(ScenarioTreeServerPyro, host=options.pyro_host, port=options.pyro_port, verbose=options.verbose, modules_imported=modules_imported) except: # if an exception occurred, then we probably want to shut down # all Pyro components. otherwise, the PH client may have # forever while waiting for results that will never # arrive. there are better ways to handle this at the PH # client level, but until those are implemented, this will # suffice for cleanup. #NOTE: this should perhaps be command-line driven, so it can # be disabled if desired. print("ScenarioTreeServerPyro aborted. Sending shutdown request.") shutdown_pyro_components(host=options.pyro_host, port=options.pyro_port, num_retries=0) raise
def exec_pysp2smps(options): import pyomo.environ start_time = time.time() try: run_pysp2smps(options) # This context will shutdown the pyro nameserver if requested. # Ideally, pyro workers can be reused without restarting the # nameserver finally: # if an exception is triggered, and we're running with # pyro, shut down everything - not doing so is # annoying, and leads to a lot of wasted compute # time. but don't do this if the shutdown-pyro option # is disabled => the user wanted if ((options.solver_manager_type == "pyro") or \ (options.solver_manager_type == "phpyro")) and \ options.shutdown_pyro: print("\n") print("Shutting down Pyro solver components.") shutdown_pyro_components(num_retries=0) print("") print("Total execution time=%.2f seconds" % (time.time() - start_time))
def _close_impl(self): if (self._manager is not None) and \ (self._manager.preprocessor is not None): assert self.preprocessor is self._manager.preprocessor for bundle in self.manager.scenario_tree._scenario_bundles: self._preprocessor.remove_bundle(bundle) for scenario in self.manager.scenario_tree._scenarios: assert scenario._instance is not None self._preprocessor.remove_scenario(scenario) self._manager.preprocessor = None self._preprocessor = None else: assert self._preprocessor is None if self._solver_manager is not None: self._solver_manager.deactivate() self._solver_manager = None if self.get_option("solver_manager_pyro_shutdown"): print("Shutting down Pyro components for solver manager.") shutdown_pyro_components( host=self.get_option("solver_manager_pyro_host"), port=self.get_option("solver_manager_pyro_port"), num_retries=0, caller_name=self.__class__.__name__) for solver in self._scenario_solvers.values(): solver.deactivate() self._scenario_solvers = {} for solver in self._bundle_solvers.values(): solver.deactivate() self._bundle_solvers = {} self._preprocessor = None self._objective_sense = None
def exec_pysp2smps(options): import pyomo.environ start_time = time.time() try: run_pysp2smps(options) # This context will shutdown the pyro nameserver if requested. # Ideally, pyro workers can be reused without restarting the # nameserver finally: # if an exception is triggered, and we're running with # pyro, shut down everything - not doing so is # annoying, and leads to a lot of wasted compute # time. but don't do this if the shutdown-pyro option # is disabled => the user wanted if ((options.solver_manager_type == "pyro") or \ (options.solver_manager_type == "phpyro")) and \ options.shutdown_pyro: print("\n") print("Shutting down Pyro solver components.") shutdown_pyro_components(num_retries=0) print("") print("Total execution time=%.2f seconds" % (time.time() - start_time))
def exec_scenariotreeserver(options): modules_imported = {} for module_name in options.import_module: if module_name in sys.modules: modules_imported[module_name] = sys.modules[module_name] else: modules_imported[module_name] = \ load_external_module(module_name, clear_cache=True)[0] try: # spawn the daemon TaskWorkerServer(ScenarioTreeServerPyro, host=options.pyro_host, port=options.pyro_port, verbose=options.verbose, modules_imported=modules_imported) except: # if an exception occurred, then we probably want to shut down # all Pyro components. otherwise, the PH client may have # forever while waiting for results that will never # arrive. there are better ways to handle this at the PH # client level, but until those are implemented, this will # suffice for cleanup. #NOTE: this should perhaps be command-line driven, so it can # be disabled if desired. print("ScenarioTreeServerPyro aborted. Sending shutdown request.") shutdown_pyro_components(host=options.pyro_host, port=options.pyro_port, num_retries=0) raise
def _close_impl(self): if (self._manager is not None) and \ (self._manager.preprocessor is not None): assert self.preprocessor is self._manager.preprocessor for bundle in self.manager.scenario_tree._scenario_bundles: self._preprocessor.remove_bundle(bundle) for scenario in self.manager.scenario_tree._scenarios: assert scenario._instance is not None self._preprocessor.remove_scenario(scenario) self._manager.preprocessor = None self._preprocessor = None else: assert self._preprocessor is None if self._solver_manager is not None: #self._solver_manager.deactivate() self._solver_manager = None if self.get_option("solver_manager_pyro_shutdown"): print("Shutting down Pyro components for solver manager.") shutdown_pyro_components( host=self.get_option("solver_manager_pyro_host"), port=self.get_option("solver_manager_pyro_port"), num_retries=0, caller_name=self.__class__.__name__) #for solver in self._scenario_solvers.values(): # solver.deactivate() self._scenario_solvers = {} #for solver in self._bundle_solvers.values(): # solver.deactivate() self._bundle_solvers = {} self._preprocessor = None self._objective_sense = None
def main(args=None): import pyomo.environ exception = False try: run(args=args) except IOError: print("ScenarioTreeServer encountered an I/O error") exception = True except pyutilib.common.ApplicationError: print("ScenarioTreeServer encountered a pyutilib " "application error") exception = True except RuntimeError: msg = sys.exc_info()[1] print("ScenarioTreeServer encountered a runtime " "error - message: %s" % str(msg)) exception = True # pyutilib.pyro tends to throw SystemExit exceptions if things # cannot be found or hooked up in the appropriate fashion. the # name is a bit odd, but we have other issues to worry # about. we are dumping the trace in case this does happen, so # we can figure out precisely who is at fault. except SystemExit: print("ScenarioTreeServer encountered a system error") exception = True except: print("ScenarioTreeServer encountered an unhandled " "exception") exception = True finally: if exception: print("@@@@@@@ Stack Trace @@@@@@@@") traceback.print_exc() print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@") # If an exception occurred, then we probably want to shut # down all Pyro components. Otherwise, the client might # wait forever for results that will never arrive. There # are better ways to handle this at the client level, but # until those are implemented, this will suffice for # cleanup. print("ScenarioTreeServer aborted. Sending shutdown request.") shutdown_pyro_components(num_retries=0) return 1 return 0
def main(args=None): import pyomo.environ exception = False try: run(args=args) except IOError: print("ScenarioTreeServer encountered an I/O error") exception = True except pyutilib.common.ApplicationError: print("ScenarioTreeServer encountered a pyutilib " "application error") exception = True except RuntimeError: msg = sys.exc_info()[1] print("ScenarioTreeServer encountered a runtime " "error - message: %s" % str(msg)) exception = True # pyutilib.pyro tends to throw SystemExit exceptions if things # cannot be found or hooked up in the appropriate fashion. the # name is a bit odd, but we have other issues to worry # about. we are dumping the trace in case this does happen, so # we can figure out precisely who is at fault. except SystemExit: print("ScenarioTreeServer encountered a system error") exception = True except: print("ScenarioTreeServer encountered an unhandled " "exception") exception = True finally: if exception: print("@@@@@@@ Stack Trace @@@@@@@@") traceback.print_exc() print("@@@@@@@@@@@@@@@@@@@@@@@@@@@@") # If an exception occurred, then we probably want to shut # down all Pyro components. Otherwise, the client might # wait forever for results that will never arrive. There # are better ways to handle this at the client level, but # until those are implemented, this will suffice for # cleanup. print("ScenarioTreeServer aborted. Sending shutdown request.") shutdown_pyro_components(num_retries=0) return 1 return 0
def exec_runef(options): import pyomo.solvers.plugins.smanager.phpyro import pyomo.solvers.plugins.smanager.pyro start_time = time.time() if options.verbose: print("Importing model and scenario tree files") scenario_instance_factory = ScenarioTreeInstanceFactory( options.model_directory, options.instance_directory, options.verbose) if options.output_times: print("Time to import model and scenario tree structure files=%.2f seconds" %(time.time() - start_time)) ef = None try: scenario_tree = GenerateScenarioTreeForEF(options, scenario_instance_factory) ef = EFAlgorithmBuilder(options, scenario_tree) run_ef(options, ef) finally: if ef is not None: if ef._solver_manager is not None: if isinstance(ef._solver_manager, pyomo.solvers.plugins.smanager.\ phpyro.SolverManager_PHPyro): ef._solver_manager.release_servers( shutdown=option.shutdown_pyro_workers) if isinstance(ef._solver_manager, pyomo.solvers.plugins.smanager.\ pyro.SolverManager_Pyro): if options.shutdown_pyro_workers: ef._solver_manager.shutdown_workers() ef._solver_manager.deactivate() if ef._solver is not None: ef._solver.deactivate() if (isinstance(ef._solver_manager, pyomo.solvers.plugins.smanager.\ pyro.SolverManager_Pyro) or \ isinstance(ef._solver_manager, pyomo.solvers.plugins.smanager.phpyro.\ SolverManager_PHPyro)) and \ (options.shutdown_pyro): print("Shutting down Pyro solver components") shutdown_pyro_components(host=options.pyro_host, port=options.pyro_port, num_retries=0) if scenario_instance_factory is not None: scenario_instance_factory.close() print("") print("Total EF execution time=%.2f seconds" %(time.time() - start_time)) print("") return 0