MPI流程同步 [英] MPI process synchronization
问题描述
我仍然对使用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);
}
}
问题是,在某些情况下,我不需要所有进程,所以我不知道该如何处理空闲的进程.首先,我返回了不需要的进程,但是如果其余进程需要与Barrier()进行同步,则会产生问题.
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.
此外,我读到呼叫Barrier会降低性能,所以我宁愿不使用它.
Also, I read that calling Barrier has a performance penalty, so I would prefer not to use it.
如何实现所需的同步?
非常感谢您.
推荐答案
使用MPI_Barrier收集程序末尾的所有等级.
Use MPI_Barrier to collect all the ranks at the end of the program.
在MPI的所有合理实现中,如果还有其他工作要做,则集合中的等级将旋转或产生处理器.这看起来很像是等级消耗了100%的CPU ...但是,如果实际上有任何其他工作要做,它将被调度并允许运行.
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.
这篇关于MPI流程同步的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!