如何为openMPI创建清除错误函数

时间:2020-06-25 08:47:42

标签: c++ mpi

我正在尝试为我的代码创建一个错误函数,该函数与openMPI 4.0.3一起运行

有人知道在这种情况下如何正确地退出mpi代码吗? 我试图避免收到来自mpi的其他错误消息。我尝试使用MPI_Abort添加障碍,以广播错误值,但是,没有任何结果。

更新29 / 06-2020:正如@Hristo Iliev所提到的,mpirun的“ -q”标志确实确实从MPI中删除了注释。我仍然想知道这是否是预期的方法。

当前代码:

#include <iostream>
#include <mpi.h>
#include <string>

using namespace std;

// Error function used to properly print error messages
void error(string file_name, int line_number, string message) {

    int mpi_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &mpi_rank);
    if (mpi_rank == 0) {
        fprintf(stderr, "Error: %s:%d\n\t%s\n",
                file_name.c_str(), line_number, message.c_str());
    }
    MPI_Finalize();
    exit(EXIT_FAILURE);
}

int main(int argc, char **argv){
    MPI_Init(&argc, &argv);
    error(__FILE__, __LINE__, "Error message here");
    MPI_Finalize();
    return EXIT_SUCCESS;
}

程序的STDERR输出:

Error: main.cpp:22
        Error message here.
--------------------------------------------------------------------------
Primary job  terminated normally, but 1 process returned
a non-zero exit code. Per user-direction, the job has been aborted.
--------------------------------------------------------------------------
--------------------------------------------------------------------------
mpirun detected that one or more processes exited with non-zero status, 
thus causing the job to be terminated. The first process to do so was:

  Process name: [[29891,1],3]
  Exit code:    1
--------------------------------------------------------------------------

0 个答案:

没有答案