def parallelRun(self,verbose=False): import pytom_mpi from pytom.parallel.messages import StatusMessage,MessageError from pytom.basic.exceptions import ParameterError from pytom.basic.structures import PyTomClassError end = False if not pytom_mpi.isInitialised(): pytom_mpi.init() mpi_id = pytom_mpi.rank() while not end: #listen for messages mpi_msgString = pytom_mpi.receive() if verbose: print(mpi_msgString) try: #wait for job and start processing msg = ReconstructionMessage() msg.fromStr(mpi_msgString) self.setJob(msg) self.run() resultMsg = StatusMessage(mpi_id,'0') resultMsg.setStatus('Finished') pytom_mpi.send(str(resultMsg),0) except (MessageError,PyTomClassError,ParameterError): try: #as StatusMessage and finish msg = StatusMessage('','') msg.fromStr(mpi_msgString) if msg.getStatus() == 'End': end = True except (MessageError,PyTomClassError,ParameterError): #print mpi_msgString #raise MessageError('Message unknown!') print('Error parsing message. Message either unknown or invalid.') assert False except: print('wild except')
def parallelEnd(self, verbose=True): """ parallelEnd: End the parallel running of the program. """ import pytom_mpi from pytom.parallel.messages import StatusMessage if verbose == True: print(self.name + ': sending end messages to all') for i in range(pytom_mpi.size()): msg = StatusMessage(str(self.mpi_id), str(i)) msg.setStatus("End") pytom_mpi.send(str(msg), i)
def end(self, verbose=False): if verbose == True: print(self.node_name + ': sending end messages to others') from pytom.parallel.messages import StatusMessage mpi_numberNodes = pytom_mpi.size() mpi_myid = pytom_mpi.rank() for i in range(1, mpi_numberNodes): msg = StatusMessage(str(mpi_myid),str(i)) msg.setStatus("End") pytom_mpi.send(str(msg),i) pytom_mpi.finalise()
def parallelEnd(self): """ parallelEnd : Sends status message = end to all workers. All workers will terminate upon receiving this message. @author: Thomas Hrabe """ import pytom_mpi from pytom.parallel.messages import StatusMessage if not pytom_mpi.isInitialised(): pytom_mpi.init() mpi_myid = pytom_mpi.rank() mpi_numberNodes = pytom_mpi.size() for i in range(1, mpi_numberNodes): msg = StatusMessage(mpi_myid.__str__(), i.__str__()) msg.setStatus("End") pytom_mpi.send(msg.__str__(), i)
def parallelEnd(self, verbose=True): """ parallelEnd : Sends status message = end to all workers. @param verbose: verbose mode @type verbose: boolean """ if verbose == True: print('Manager: sending end messages to workers') import pytom_mpi from pytom.parallel.messages import StatusMessage mpi_numberNodes = pytom_mpi.size() mpi_myid = pytom_mpi.rank() for i in range(1, mpi_numberNodes): msg = StatusMessage(str(mpi_myid), str(i)) msg.setStatus("End") pytom_mpi.send(str(msg), i)
def parallelWork(self, verbose=False, doFinalize=True): """ parallelWork: Distribute joblist to workers. Leave as it is. @param verbose: @param doFinalize: """ import pytom_mpi from pytom.parallel.messages import Message, StatusMessage, MessageError from pytom.basic.exceptions import ParameterError from pytom.basic.structures import PyTomClassError if not pytom_mpi.isInitialised(): pytom_mpi.init() if self._mpi_id == 0: #if current node == 0, be the master node numberJobs = len(self._jobList) if self._numberWorkers <= numberJobs: numberJobsToSend = self._numberWorkers else: numberJobsToSend = numberJobs for i in range(0, numberJobsToSend): #send out all first numberJobsToSend jobs pytom_mpi.send(str(self._jobList[i]), i + 1) numberFinishedJobs = 0 numberSentJobs = numberJobsToSend finished = numberSentJobs == numberJobs and numberFinishedJobs == numberJobs while not finished: #distribute remaining jobs to finished workers mpi_msgString = pytom_mpi.receive() msg = Message('1', '0') msg.fromStr(mpi_msgString) numberFinishedJobs += 1 if numberSentJobs < numberJobs: pytom_mpi.send(str(self._jobList[numberSentJobs]), int(msg.getSender())) numberSentJobs += 1 finished = numberSentJobs == numberJobs and numberFinishedJobs == numberJobs if doFinalize: for i in range(0, self._numberWorkers): msg = StatusMessage('0', i + 1) msg.setStatus('End') pytom_mpi.send(str(msg), i + 1) print('Sending end msg to:', i + 1) else: #if any other node id, be a worker node end = False while not end: #listen for messages mpi_msgString = pytom_mpi.receive() if verbose: print(mpi_msgString) try: #wait for job and start processing msg = self.getMsgObject(mpi_msgString) self.setJob(msg) self.run() resultMsg = StatusMessage(self._mpi_id, '0') resultMsg.setStatus('Finished') pytom_mpi.send(str(resultMsg), 0) except (MessageError, PyTomClassError, ParameterError): try: #message is a StatusMessage #if message status is End, finish this worker. #You can also add other statuses msg = StatusMessage('', '') msg.fromStr(mpi_msgString) if msg.getStatus() == 'End': end = True except (MessageError, PyTomClassError, ParameterError): #print mpi_msgString #raise MessageError('Message unknown!') raise RuntimeError( 'Error parsing message. Message either unknown or invalid.' ) except: raise RuntimeError( 'Something went terribly wrong. Aborting.') if doFinalize: pytom_mpi.finalise()