如果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程序而不打印此巨大错误消息?
最佳答案
如果您的代码中包含以下逻辑:
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东西是错误的,但这不是您在这里所做的,所以您还可以。关于c - 通过MPI正常退出,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/10818740/