0
我仍然對使用MPI執行我的程序感到困惑。這是我的例子:MPI進程同步
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()同步,則會產生問題。
我想我可以讓流程不需要運行等待消息完成或調用Barrier,但對我來說聽起來不太合適。
此外,我讀到,調用障礙有一個性能損失,所以我寧願不使用它。
如何實現我需要的同步?
非常感謝。
你從哪裏聽說使用MPI_Barrier是一種性能損失? –