如果Rdinput返回错误,我正试图优雅地退出我的程序。
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#define MASTER 0
#define Abort(x) MPI_Abort(MPI_COMM_WORLD, x)
#define Bcast(send_data, count, type) MPI_Bcast(send_data, count, type, MASTER, GROUP) //root --> MASTER
#define Finalize() MPI_Finalize()
int main(int argc, char **argv){
//Code
if( rank == MASTER ) {
time (&start);
printf("Initialized at %s\n", ctime (&start) );
//Read file
error = RdInput();
}
Bcast(&error, 1, INT); Wait();
if( error = 1 ) MPI_Abort(1);
//Code
Finalize();
}
节目输出:
mpirun -np 2 code.x
--------------------------------------------------------------------------
MPI_ABORT was invoked on rank 0 in communicator MPI_COMM_WORLD
with errorcode 1.
NOTE: invoking MPI_ABORT causes Open MPI to kill all MPI processes.
You may or may not see output from other processes, depending on
exactly when Open MPI kills them.
--------------------------------------------------------------------------
Initialized at Wed May 30 11:34:46 2012
Error [RdInput]: The file "input.mga" is not available!
--------------------------------------------------------------------------
mpirun has exited due to process rank 0 with PID 7369 on
node einstein exiting improperly. There are two reasons this could occur:
//More error message.
如果不打印这个巨大的错误消息,我该怎样才能优雅地退出MPI程序?
答案 0 :(得分:12)
如果你的代码中有这个逻辑:
Bcast(&error, 1, INT);
if( error = 1 ) MPI_Abort(1);
然后你就完成了(尽管在广播之后你不需要任何等待)。正如您所发现的那样,诀窍是MPI_Abort()
做“优雅”;当事情发生了可怕的错误时,它基本上就是以任何可能的方式关闭事物。
在这种情况下,由于现在每个人都同意广播后的错误代码,只需优雅地结束你的程序:
MPI_Bcast(&error, 1, MPI_INT, MASTER, MPI_COMM_WORLD);
if (error != 0) {
if (rank == 0) {
fprintf(stderr, "Error: Program terminated with error code %d\n", error);
}
MPI_Finalize();
exit(error);
}
调用MPI_Finalize()
并继续使用更多MPI内容是错误的,但这不是你在这里所做的,所以你很好。