3

如果 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 程序而不打印这个巨大的错误消息?

4

1 回答 1

16

如果您的代码中有此逻辑:

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 内容是错误的,但这不是你在这里所做的,所以你很好。

于 2012-05-30T15:53:28.647 回答