开发者

MPI process synchronization

开发者 https://www.devze.com 2023-03-27 12:24 出处:网络
I\'m still confused about the implementation of my program using MPI. This is my example: import mpi.*;

I'm still confused about the implementation of my program using MPI. This is my example:

 import mpi.*;
 public class HelloWorld {
     static int me;
     static Object [] o = new Object[1];
     public static void main(String args[]) throws Exception {
       //10 processes were started: -np 10
       MPI.Init(args);
       me = MPI.COMM_WORLD.Rank();
       if(me == 0) {
            o[0] = generateRandBoolean(0.5);
            for(int i=1; i<10;i++) 
                MPI.COMM_WORLD.Isend(o, 0, 1, MPI.OBJECT, i,0);
            if((Boolean)o[0])
                MPI.COMM_WORLD.Barrier();
        } else {

            (new HelloWorld()).work();
        }
        MPI.Finalize();
    }

    public void work() {
        //do some calculation
            //for some reason, the 10th process
        //will not be needed
            if(me == 9) 
            return;

        //some times, the rest of the
        //processes have to be synchronized
        Request rreq = MPI.COMM_WORLD.Irecv(o, 0, 1, MPI.OBJECT, MPI.ANY_SOURCE, 0);
        rreq.Wait();
        if((Boolean)o[0])
            MPI.COMM_WORLD.Barrier();
    }

    public static boolean generateRandBoolean(double p) {
        return (Math.random() < p);
    }
}

The problem is that in some cases, I will not need all the processes, so I don't know what to do with the idle ones. At first, I was returning the not needed processes, but it generates problem if the rest of the processes need to be synchronized with Barrier().

I thought I could let the processes I don't need running waiting for a message to finish or to call Barrier, but it doe开发者_高级运维s not sound good to me.

Also, I read that calling Barrier has a performance penalty, so I would prefer not to use it.

How can I achieve the synchronization I need?

Thank you very much.


Use MPI_Barrier to collect all the ranks at the end of the program.

In all reasonable implementations of MPI, the ranks in a collective will spin or yield the processor if there are any other processes that have work to do. This may look a lot like the rank is consuming 100% of the CPU...but if any other process actually has work to do it will be scheduled and allowed to run.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号