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 に答える