如何使用和解释MPI-IO错误代码?

时间:2014-04-04 09:47:25

标签: mpi mpi-io

#include <stdio.h>
#include <iostream>
#include <Windows.h>
#include <C:\Program Files\Microsoft MPI\Inc\mpi.h>

using namespace std;


#define BUFSIZE 128


int main (int argc,  char *argv[])
{

    int err;
    int rank;
    int size;
    double start_time = 0.0;
    double end_time;
    MPI_Comm comm = MPI_COMM_WORLD;
    MPI_File file;

    char cbuf[BUFSIZE];

    for(int i = 0; i < BUFSIZE; i++)
    {
        cbuf[i] = 'a' + i;
    }

    if(err = MPI_Init(&argc, &argv))
    {
        printf("%s \n", "Error! MPI is halted!");
        MPI_Abort(comm, err);
    }

    MPI_Comm_size(comm, &size);
    MPI_Comm_rank(comm, &rank);


    if(rank == 0)
    {
        start_time = MPI_Wtime();
    }

    err = MPI_File_open(comm, "testfile", MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &file);

    if(err != MPI_SUCCESS)
    {
        printf("Error %d! Can't open the file!\n", err);
        MPI_Abort(comm, err);
        return EXIT_FAILURE;
    }

    err = MPI_File_set_view(file, (MPI_Offset) (rank * BUFSIZE * sizeof(char)), MPI_CHAR, MPI_CHAR, "native", MPI_INFO_NULL);

    if(err != MPI_SUCCESS)
    {
        printf("%s \n", "Error! Can't set the view!");
        MPI_Abort(comm, err);
        return EXIT_FAILURE;
    }

    err = MPI_File_write(file, cbuf, BUFSIZE, MPI_CHAR, MPI_STATUSES_IGNORE);

    if(err != MPI_SUCCESS)
    {
        printf("%s \n", "Error! Problems with writing!");
        MPI_Abort(comm, err);
        return EXIT_FAILURE;
    }


    MPI_File_close(&file);

    if(rank == 0)
    {
        end_time = MPI_Wtime();
        printf("Time elapsed : %f seconds", (end_time - start_time) * 1000);
    }

    MPI_Finalize();

    return EXIT_SUCCESS;
}

我试图用MPI将一些符号写入文件。当我这样做时,我得到一个288的错误代码,并且文件无法打开。我使用命令行:mpiexec -n 10 myapp.exe。我正在搜索错误代码,但根本找不到任何东西。

1 个答案:

答案 0 :(得分:2)

更进一步。您的错误代码本身并不意味着任何事情。但是,您可以将该代码提供给MPI_Error_string并获得更具人性化的内容。我在我写的每个MPI-IO代码中都有这个功能:

static void handle_error(int errcode, char *str)
{       
    char msg[MPI_MAX_ERROR_STRING];
    int resultlen;
    MPI_Error_string(errcode, msg, &resultlen);
    fprintf(stderr, "%s: %s\n", str, msg);
    MPI_Abort(MPI_COMM_WORLD, 1);
}

然后定义这个宏:

#define MPI_CHECK(fn) { int errcode; errcode = (fn);\
     if (errcode != MPI_SUCCESS) handle_error  (errcode, #fn ); }

所以我可以调用这样的例程:

CHECK(MPI_File_open(comm, "testfile", 
    MPI_MODE_CREATE | MPI_MODE_RDWR, MPI_INFO_NULL, &file) );