首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用MPI优雅退出

使用MPI优雅退出
EN

Stack Overflow用户
提问于 2012-05-30 22:49:49
回答 1查看 17.5K关注 0票数 3

如果Rdinput返回错误,我会尝试优雅地退出我的程序。

代码语言:javascript
复制
#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();
}

程序输出:

代码语言:javascript
复制
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程序?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-30 23:53:29

如果你的代码中有这样的逻辑:

代码语言:javascript
复制
Bcast(&error, 1, INT);
if( error = 1 ) MPI_Abort(1); 

然后你就快完成了(尽管在广播之后你不需要任何形式的等待)。正如您已经发现的,诀窍在于MPI_Abort()并不“优雅”;它基本上是在发生严重错误时以任何可能的方式关闭程序。

在这种情况下,现在每个人都同意广播后的错误代码,所以只需优雅地结束您的程序:

代码语言:javascript
复制
   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内容是错误的,但这不是您在这里要做的,所以您很好。

票数 16
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10818740

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档