MPI process synchronization

我的梦境 提交于 2020-04-30 10:25:48

问题


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 does 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.


回答1:


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.



来源:https://stackoverflow.com/questions/7004068/mpi-process-synchronization

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!