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

Last change on this file since 2326 was 2326, checked in by jwatson, 9 years ago

Adding import of cPickle instead of pickle, if it exists.

File size: 4.4 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
14try:
15    import cPickle as pickle
16except ImportError:
17    import pickle
18
19try:
20    import Pyro.core
21    import pyutilib.pyro
22    using_pyro=True
23except ImportError:
24    using_pyro=False
25
26import pyutilib.misc
27import pyutilib.component.core
28from coopr.opt.parallel.manager import *
29from coopr.opt.parallel.solver import *
30from coopr.opt.results import SolverResults
31
32class SolverManager_Pyro(AsynchronousSolverManager):
33
34    def clear(self):
35        """
36        Clear manager state
37        """
38        AsynchronousSolverManager.clear(self)
39        self.client = pyutilib.pyro.Client()
40        self._opt = None
41        self._ah = {}
42
43    def _perform_queue(self, ah, *args, **kwds):
44        """
45        Perform the queue operation.  This method returns the ActionHandle,
46        and the ActionHandle status indicates whether the queue was successful.
47        """
48       
49        if 'opt' in kwds:
50            self._opt = kwds['opt']
51            del kwds['opt']
52        else:
53            raise ActionManagerError, "No solver passed to SolverManager_Pyro, method=_perform_queue; use keyword option \"opt\""
54
55        #
56        # Force coopr.opt to ignore tests for availability, at least locally
57        #
58        kwds['available'] = True
59        self._opt._presolve(*args, **kwds)
60        problem_file_string = open(self._opt._problem_files[0],'r').read()
61
62        #
63        # Delete this option, to ensure that the remote worker does the check for
64        # availability.
65        #
66        del kwds['available']
67
68        #
69        # We can't pickl the options object itself - so extract a simple
70        # dictionary of solver options and re-construct it on the other end.
71        #
72        solver_options = {}
73        for key in self._opt.options:
74           solver_options[key]=self._opt.options[key]
75
76        # pick up the warm-start file, if available.
77        warm_start_file_string = None
78        warm_start_file_name = None
79        if hasattr(self._opt,  "warm_start_solve"):
80           if (self._opt.warm_start_solve is True) and (self._opt.warm_start_file_name is not None):
81              warm_start_file_name = self._opt.warm_start_file_name
82              warm_start_file_string = open(warm_start_file_name, 'r').read()
83
84        #
85        # Pickl everything into one big data object via the "Bunch" command
86        # and post the task!
87        #
88        data=pyutilib.misc.Bunch(opt=self._opt.type, \
89                                 file=problem_file_string, filename=self._opt._problem_files[0], \
90                                 warmstart_file=warm_start_file_string, warmstart_filename=warm_start_file_name, \
91                                 kwds=kwds, solver_options=solver_options, mipgap=self._opt.mipgap, suffixes=self._opt.suffixes)
92        task = pyutilib.pyro.Task(data=data, id=ah.id)
93        self.client.add_task(task)
94        self._ah[task.id] = ah
95       
96        return ah
97
98    def _perform_wait_any(self):
99        """
100        Perform the wait_any operation.  This method returns an
101        ActionHandle with the results of waiting.  If None is returned
102        then the ActionManager assumes that it can call this method again.
103        Note that an ActionHandle can be returned with a dummy value,
104        to indicate an error.
105        """
106        if self.client.num_results() > 0:
107            # this protects us against the case where we get an action
108            # handle that we didn't know about or expect.
109            while(True):
110               task = self.client.get_result()
111               if task.id in self._ah:
112                  ah = self._ah[task.id]
113                  self._ah[task.id] = None
114                  ah.status = ActionStatus.done
115                  #print "HERE",ah.id, task.result
116                  self.results[ah.id] = pickle.loads(task.result)
117                  #self.results[ah.id].write()
118                  return ah
119
120if using_pyro:
121    SolverManagerRegistration("pyro", SolverManager_Pyro)
122
Note: See TracBrowser for help on using the repository browser.