Exemple #1
0
    def main(self):
        from pyina import mpi

        nsegs = self.inventory.nseg
        world = mpi.world
        logging.info("I am rank %d of %d" % (world.rank, world.size))
        if world.rank == 0:
            for peer in range(1, world.size):
                status = mpi.Status()
                message = world.recv(source=MPI_ANY_SOURCE, tag=17)
                print "[%d/%d]: received {%s}" % (world.rank, world.size,
                                                  message)
        else:
            s = "My message is this: I am node %d " % world.rank
            logging.debug("%s" % s)
            world.send(s, 0, 17)

        return
Exemple #2
0
    def main(self):
        from pyina import mpi
        import random
        from mystic.models import mogi; forward_mogi = mogi.evaluate

        world = mpi.world
        size = world.size

        logging.info("I am rank %d of %d" % (world.rank, size))
        master = 0
        MPI_ANY_SOURCE = mpi.ANY_SOURCE
        MPI_ANY_TAG = mpi.ANY_TAG

        EXITTAG = 999
        if world.rank == master:
            # let's say I have a set of NJOBS points to test
            NJOBS = 100
            assert (EXITTAG > NJOBS)

            # all the "evaluation points" are the same, so broadcast to slaves
            eval_at = array([[1,2],[2,3]])
            recv = world.bcast(eval_at, master)
            # the universe of params
            params = [[random.random() for _ in range(4)] for _ in range(NJOBS)]
            # now farm out to the slaves
            numsent = 0
            for slave in range(1, size):
                logging.info("MASTER : First Send: Sending job %d to slave %d" % (numsent, slave))
                world.send(params[numsent], slave, numsent)
                numsent = numsent+1
            # start receiving
            for i in range(NJOBS):  
                logging.info("MASTER : Top of loop")
                status = mpi.Status()
                message = world.recv(source=MPI_ANY_SOURCE, tag=MPI_ANY_TAG, status=status)
                sender = status.source
                anstag = status.tag
                logging.info("MASTER : Received job %d from slave %d" % (anstag, sender))
                #sleep(3)
                if (numsent < NJOBS):
                    # send next job
                    logging.info("MASTER : Sending job %d to slave %d" % (numsent, sender))
                    world.send(params[numsent], sender, numsent)
                    numsent = numsent + 1
                else:
                    # done
                    logging.info("MASTER : Sending DONE signal to slave %d" % (sender))
                    world.send("", sender, EXITTAG)
        else:
            eval_at = world.bcast("", master)
            #logging.info("Rank %d has message %s" % (world.rank, eval_at))
            for iter in range(99999):
                # receive job
                logging.info("   SLAVE %d, iteration %d." % (world.rank, iter))
                status = mpi.Status()
                param = world.recv(source=master, tag=MPI_ANY_TAG, status=status)
                tag = status.tag
                if tag == EXITTAG:
                    logging.info("   SLAVE %d: is done." % world.rank)
                    return
                logging.info("   SLAVE %d receiving job %d ... running" % (world.rank, tag))
                res = forward_mogi(param, eval_at)
                # send result back to master
                logging.info("   SLAVE %d done running job %d, send results back to master" % (world.rank, tag))
                #sleep(2)
                world.send(res, master, tag)

        return