Fabricio Sanches Fabricio Sanches - 1 month ago 7
C Question

Gracefully exit with MPI

I'm trying to creat um Abort for my program here part of my program
i need an gracefully exit to my code
in this part i wanna make an error of reading input, inside Rdinput() i return 1
with if an error occurs.

#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()
#define Init(x,y) MPI_Init(x,y)
#define Rank(x) MPI_Comm_rank(MPI_COMM_WORLD, x)
#define Size(x) MPI_Comm_size(MPI_COMM_WORLD, x)

int main(int argc, char **argv) /* Main routine of MGA program*/

if( rank == MASTER ){

time (&start);
printf("Initialized at %s\n", ctime (&start) );

printf("\t** Program MGA **\n");
printf("\t** (Molecular Genetic Algorithm) **\n");
printf("\t** **\n");
printf("\t** **\n");
printf("\t** Principal Author: **\n");
printf("\t** Freddy F. Guimaraes **\n");
printf("\t** email: freddy@quimica.ufg.br **\n");
printf("\t** **\n");
printf("\t** Authors: **\n");
printf("\t** Rafael Carvalho Couto, Fabrício Sanches Paranhos, **\n");
// printf("\t** Robson de Souza Vieira. **\n");
printf("\t** **\n");

printf("Reading input file: |");
error = RdInput(Infile, CHARMAX);

Bcast(&error, 1, INT);


if( error = 1 ) MPI_Abort(1);



here out...

paranhos@einstein:~/MGA/MGA-2011-mpi$ mpirun -np 2 mga-2011.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

** Program MGA **
** (Molecular Genetic Algorithm) **
** **
** **
** Principal Author: **
** Freddy F. Guimaraes **
** email: freddy@quimica.ufg.br **
** **
** Authors: **
** Rafael Carvalho Couto, Fabrício Sanches Paranhos, **
** **

Reading input file: |#|

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:

1. this process did not call "init" before exiting, but others in
the job did. This can cause a job to hang indefinitely while it waits
for all processes to call "init". By rule, if one process calls "init",
then ALL processes must call "init" prior to termination.

2. this process called "init", but exited without calling "finalize".
By rule, all processes that call "init" MUST call "finalize" prior to
exiting or it will be considered an "abnormal termination"

This may have caused other processes in the application to be
terminated by signals sent by mpirun (as reported here).
[einstein:07368] 1 more process has sent help message help-mpi-api.txt / mpi-abort
[einstein:07368] Set MCA parameter "orte_base_help_aggregate" to 0 to see all help / error messages

How can i do to get gracefully exit whith error code exemple

Program finish whith error code 10.

Answer Source

If you have this logic in your code:

Bcast(&error, 1, INT);
if( error = 1 ) MPI_Abort(1); 

then you're just about done (although you don't need any kind of wait after a broadcast). The trick, as you've discovered, is that MPI_Abort() does not do "graceful"; it basically is there to shut things down in whatever way possible when something's gone horribly wrong.

In this case, since now everyone agrees on the error code after the broadcast, just do a graceful end of your program:

   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);

It's an error to call MPI_Finalize() and keep on going with more MPI stuff, but that's not what you're doing here, so you're fine.