예제 #1
0
    def parallelRun(self, verbose=True, **kwargs):
        '''
        parallelRun: Run the worker in parallel status and send the result message back
        @param verbose: verbose mode
        @type verbose: boolean
        '''

        from pytom.parallel.messages import StatusMessage, MessageError
        from pytom.basic.exceptions import ParameterError
        from pytom.basic.structures import PyTomClassError

        end = False

        while not end:
            # get the message string
            mpi_msgString = getMsgStr()

            try:
                msg = self.getJobMsg(mpi_msgString)
                self.jobFromMsg(msg)

                if verbose == True:
                    print(self.name + ': running...')
                [resV, orientV, sumV, sqrV] = self.run(verbose, moreInfo=True)

                # write the result volume back to the disk
                resFilename = self.name + '_job' + str(self.jobID) + '_res.em'
                orientFilename = self.name + '_job' + str(
                    self.jobID) + '_orient.em'
                resV.write(resFilename)
                orientV.write(orientFilename)

                if sumV and sqrV:
                    sumFilename = self.name + '_job' + str(
                        self.jobID) + '_sum.em'
                    sqrFilename = self.name + '_job' + str(
                        self.jobID) + '_sqr.em'
                    sumV.write(sumFilename)
                    sqrV.write(sqrFilename)

                from pytom.localization.structures import Volume, Orientation
                res = Volume(resFilename, self.volume.subregion)
                orient = Orientation(orientFilename)

                # construct the result
                from pytom.localization.peak_job import PeakResult
                result = PeakResult(res, orient, self.jobID)

                # sent the result back
                if verbose == True:
                    print(self.name + ': sending back result')
                result.send(self.mpi_id, self.backTo)

            except (MessageError, PyTomClassError, ParameterError):
                try:
                    # get the message as StatusMessage and finish
                    msg = StatusMessage('', '')
                    msg.fromStr(mpi_msgString)
                    if msg.getStatus() == 'End':
                        end = True
                    if verbose == True:
                        print(self.name + ': ending...')
                except (MessageError, PyTomClassError, ParameterError):
                    print(
                        'Error parsing message. Message either unknown or invalid.'
                    )
                    assert False
예제 #2
0
    def parallelRun(self,
                    job,
                    splitX=0,
                    splitY=0,
                    splitZ=0,
                    verbose=True,
                    gpuID=-1):
        """
        parallelRun: Parallel run the job on the computer cluster.
        @param job: job
        @type job: L{pytom.localization.peak_job.PeakJob}
        @param splitX: split part along the x dimension
        @type splitX: integer
        @param splitY: split part along the y dimension
        @type splitY: integer
        @param splitZ: split part along the z dimension
        @type splitZ: integer
        """
        import pytom_mpi
        if self.mpi_id == 0:  # send the first message
            #            if not pytom_mpi.isInitialised():
            #                pytom_mpi.init()
            job.members = pytom_mpi.size()
            print('job members', job.members)

            job.send(0, 0)
            print("\n")

        self.gpuID = gpuID
        end = False
        while not end:
            # get the message string
            mpi_msgString = getMsgStr()

            msgType = self.getMsgType(mpi_msgString)

            if msgType == 2:  # Job msg
                msg = self.getJobMsg(mpi_msgString)
                job = self.jobFromMsg(msg)  # set members

                if self.mpi_id == 0:
                    self.distributeJobs(job, splitX, splitY, splitZ)
                else:
                    self.distributeJobs(job)

                result = self.run(verbose, gpuID=gpuID)
                self.summarize(result, self.jobID)

            elif msgType == 1:  # Result msg
                msg = self.getResMsg(mpi_msgString)
                res = self.resFromMsg(msg)

                if verbose == True:
                    print(self.name + ": processing result from worker " +
                          msg.getSender())

                resV = res.result.getVolume()
                resO = res.orient.getVolume()
                jobID = res.jobID

                self.summarize([resV, resO], jobID)

            elif msgType == 0:  # Status msg
                # get the message as StatusMessage and finish
                from pytom.parallel.messages import StatusMessage
                msg = StatusMessage('', '')
                msg.fromStr(mpi_msgString)
                if msg.getStatus() == 'End':
                    end = True
                    if verbose == True:
                        print(self.name + ': end')
            else:  # Error
                raise RuntimeError("False message type!")

        if self.mpi_id == 0:
            # delete the temporary files on the disk
            import os
            files = os.listdir(self.dstDir)
            for name in files:
                if 'job' in name and '.em' in name and not 'sum' in name and not 'sqr' in name:
                    os.remove(self.dstDir + '/' + name)

            if self.gpuID:
                gpuflag = ''
            else:
                gpuflag = ''
            # rename the result files name
            os.rename(
                self.dstDir + '/' + 'node_0_res.em', self.dstDir + '/' +
                'scores_{}{}.em'.format(self.suffix, gpuflag))
            os.rename(
                self.dstDir + '/' + 'node_0_orient.em', self.dstDir + '/' +
                'angles_{}{}.em'.format(self.suffix, gpuflag))

        self.clean()  # clean itself
        pytom_mpi.finalise()