Package multi :: Module uni_processor
[hide private]
[frames] | no frames]

Source Code for Module multi.uni_processor

  1  ############################################################################### 
  2  #                                                                             # 
  3  # Copyright (C) 2007 Gary S Thompson (https://gna.org/users/varioustoxins)    # 
  4  # Copyright (C) 2010-2012 Edward d'Auvergne                                   # 
  5  #                                                                             # 
  6  # This file is part of the program relax.                                     # 
  7  #                                                                             # 
  8  # relax is free software; you can redistribute it and/or modify               # 
  9  # it under the terms of the GNU General Public License as published by        # 
 10  # the Free Software Foundation; either version 2 of the License, or           # 
 11  # (at your option) any later version.                                         # 
 12  #                                                                             # 
 13  # relax is distributed in the hope that it will be useful,                    # 
 14  # but WITHOUT ANY WARRANTY; without even the implied warranty of              # 
 15  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               # 
 16  # GNU General Public License for more details.                                # 
 17  #                                                                             # 
 18  # You should have received a copy of the GNU General Public License           # 
 19  # along with relax; if not, write to the Free Software                        # 
 20  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA   # 
 21  #                                                                             # 
 22  ############################################################################### 
 23   
 24  # Module docstring. 
 25  """The uni-processor fabric for running on a single CPU.""" 
 26   
 27   
 28  # Python module imports. 
 29  import sys, os 
 30   
 31  # multi module imports. 
 32  from multi.misc import Result_string 
 33  from multi.processor import Processor 
 34  from multi.result_commands import Result_command 
 35   
 36   
37 -class Uni_processor(Processor):
38 """The uni-processor class.""" 39
40 - def __init__(self, processor_size, callback):
41 """Initialise the class. 42 43 @param processor_size: The number of processors. 44 @type processor_size: int 45 @param callback: The callback object. 46 @type callback: ? 47 """ 48 super(Uni_processor, self).__init__(processor_size=1, callback=callback) 49 50 if processor_size > 1: 51 print('warning: uniprocessor can only support 1 processor you requested %d' % processor_size) 52 print('continuing...\n') 53 54 self.command_queue = [] 55 self.memo_map = {}
56 57
58 - def add_to_queue(self, command, memo=None):
59 self.command_queue.append(command) 60 if memo != None: 61 command.set_memo_id(memo) 62 self.memo_map[memo.memo_id()] = memo
63 64
65 - def assert_on_master(self):
66 """Make sure that this is the master processor and not a slave. 67 68 As this is the Uni-processor, the processor is always the master. Hence this method does nothing. 69 """
70 71
72 - def get_intro_string(self):
73 """Return the string to append to the end of the relax introduction string. 74 75 @return: The string describing this Processor fabric. 76 @rtype: str 77 """ 78 79 # Return the string. 80 return "Uni-processor."
81 82
83 - def get_name(self):
84 # FIXME may need system dependent changes 85 return '%s-%s' % (os.getenv('HOSTNAME'), os.getpid())
86 87
88 - def on_master(self):
89 """For the uni-processor fabric, we are always on the master. 90 91 @return: The flag specifying if we are on the master or slave processors. 92 @rtype: bool 93 """ 94 95 # Always master. 96 return True
97 98
99 - def master_queue_command(self, command, dest):
100 """Slave to master processor data transfer - send the result command from the slave. 101 102 This mimics a slave to master data transfer initiated by a slave by holding the result command so that the matching self.master_receive_result(), which is called by the master processor, can return it. As the master and slave processors are one and the same, the command is just held as a private class variable. 103 104 105 @param command: The results command to send to the master. 106 @type command: Results_command instance 107 @param dest: The destination processor's rank. 108 @type dest: int 109 """ 110 111 # Hold the result command so that the matching self.master_receive_result() can return it. 112 self._result_command_queue = command
113 114
115 - def master_receive_result(self):
116 """Slave to master processor data transfer - receive the result command from the slave. 117 118 This mimics a slave to master data transfer initiated by a slave by holding the result command so that the matching self.master_receive_result(), which is called by the master processor, can return it. As the master and slave processors are one and the same, the command is just held as a private class variable. 119 120 121 @return: The result command sent by the slave. 122 @rtype: Result_command instance 123 """ 124 125 # Remove the command from the class namespace. 126 command = self._result_command_queue 127 del self._result_command_queue 128 129 # Return the command 130 return command
131 132
133 - def post_run(self):
134 """Dummy function for preventing the printing of the run time."""
135 136
137 - def processor_size(self):
138 """Return 1 as this is the uni-processor. 139 140 @return: The number of processors. 141 @rtype: int 142 """ 143 144 return 1
145 146
147 - def rank(self):
148 """The uni-processor is always of rank 0. 149 150 @return: The processor rank. 151 @rtype: int 152 """ 153 154 return 0
155 156
157 - def return_object(self, result):
158 159 if isinstance(result, Exception): 160 #FIXME: clear command queue 161 # and finalise mpi (or restart it if we can! 162 raise result 163 elif isinstance(result, Result_command): 164 memo = None 165 if result.memo_id != None: 166 memo = self.memo_map[result.memo_id] 167 result.run(self, memo) 168 if result.memo_id != None and result.completed: 169 del self.memo_map[result.memo_id] 170 171 elif isinstance(result, Result_string): 172 sys.stdout.write(result.string) 173 else: 174 message = 'Unexpected result type \n%s \nvalue%s' %(result.__class__.__name__, result) 175 raise Exception(message)
176 177
178 - def run_queue(self):
179 #FIXME: need a finally here to cleanup exceptions states for windows etc 180 181 last_command = len(self.command_queue)-1 182 for i, command in enumerate(self.command_queue): 183 completed = (i == last_command) 184 185 command.run(self, completed) 186 187 #self.run_command_queue() 188 #TODO: add cheques for empty queues and maps if now warn 189 del self.command_queue[:] 190 self.memo_map.clear()
191