source: trunk/coopr/plugins/smanager/pyro.py @ 1768

Last change on this file since 1768 was 1768, checked in by wehart, 11 years ago

Rework of Coopr to use the new PyUtilib? package decomposition.

NOTE: to use Coopr with this update, we need to work with a new version of coopr_install.

File size: 3.1 KB
Line 
1#  _________________________________________________________________________
2#
3#  Coopr: A COmmon Optimization Python Repository
4#  Copyright (c) 2008 Sandia Corporation.
5#  This software is distributed under the BSD License.
6#  Under the terms of Contract DE-AC04-94AL85000 with Sandia Corporation,
7#  the U.S. Government retains certain rights in this software.
8#  For more information, see the Coopr README.txt file.
9#  _________________________________________________________________________
10
11
12__all__ = []
13
14import pyutilib.misc
15import pyutilib.plugin.core
16from coopr.opt.parallel.manager import *
17from coopr.opt.parallel.solver import *
18from coopr.opt.base import SolverResults
19
20import pickle
21
22try:
23    import Pyro.core
24    import pyutilib.pyro
25    using_pyro=True
26except ImportError:
27    using_pyro=False
28       
29
30class SolverManager_Pyro(AsynchronousSolverManager):
31
32    def clear(self):
33        """
34        Clear manager state
35        """
36        AsynchronousSolverManager.clear(self)
37        self.client = pyutilib.pyro.Client()
38        self._opt = None
39        self._ah = {}
40
41    def _perform_queue(self, ah, *args, **kwds):
42        """
43        Perform the queue operation.  This method returns the ActionHandle,
44        and the ActionHandle status indicates whether the queue was successful.
45        """
46        if 'opt' in kwds:
47            self._opt = kwds['opt']
48            del kwds['opt']
49        else:
50            raise ActionManagerError, "Undefined solver"
51        #
52        # Force coopr.opt to ignore tests for availability, at least locally
53        #
54        kwds['available'] = True
55        self._opt._presolve(*args, **kwds)
56        filestr = open(self._opt._problem_files[0],'r').read()
57        #
58        # Delete this option, to ensure that the remote worker does the check for
59        # availability.
60        #
61        del kwds['available']
62        data=pyutilib.misc.Bunch(opt=self._opt.type, file=filestr, filename=self._opt._problem_files[0], kwds=kwds)
63        task = pyutilib.pyro.Task(data=data, id=ah.id)
64        self.client.add_task(task)
65        self._ah[task.id] = ah
66        return ah
67
68    def _perform_wait_any(self):
69        """
70        Perform the wait_any operation.  This method returns an
71        ActionHandle with the results of waiting.  If None is returned
72        then the ActionManager assumes that it can call this method again.
73        Note that an ActionHandle can be returned with a dummy value,
74        to indicate an error.
75        """
76        if self.client.num_results() > 0:
77            # this protects us against the case where we get an action
78            # handle that we didn't know about or expect.
79            while(True):
80               task = self.client.get_result()
81               if task.id in self._ah:
82                  ah = self._ah[task.id]
83                  self._ah[task.id] = None
84                  ah.status = ActionStatus.done
85                  #print "HERE",ah.id, task.result
86                  self.results[ah.id] = pickle.loads(task.result)
87                  #self.results[ah.id].write()
88                  return ah
89
90if using_pyro:
91    SolverManagerRegistration("pyro", SolverManager_Pyro)
92
Note: See TracBrowser for help on using the repository browser.