如何使用MPI_File_open替换MPI中的现有文件

时间:2014-08-25 08:14:10

标签: file-io operating-system filesystems mpi hpc

我正在阅读"使用MPI-2"并尝试自己执行代码。我为MPI_File_open指定了MPI_MODE_CREATE,但它实际上没有创建新文件,而是覆盖了具有相同名称的先前文件。我碰巧在第一次运行更多进程时找到了这个,然后用更少的进程运行。我的MPI版本是openmpi 1.8.1。

这是代码

#include <stdio.h>
#include <mpi.h>
#define BUFSIZE 4
int main(int argc, char **argv)
{
    int map[BUFSIZE], i, rank , size;
    char buf[BUFSIZE];
    MPI_File fh;
    MPI_Datatype filetype;
    MPI_Status status;
    MPI_Offset disp=0;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    for(i=0;i<BUFSIZE;i++)
    {
//      buf[i]=size*i+rank+'0';
        buf[i]=size*i+rank+'A';
        map[i]=size*i+rank;
    }
    MPI_File_open(MPI_COMM_WORLD, "datafile4", MPI_MODE_CREATE|MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
    MPI_Type_create_indexed_block(BUFSIZE, 1, map, MPI_CHAR, &filetype);
    MPI_Type_commit(&filetype);
    MPI_File_set_view(fh, disp, MPI_CHAR, filetype, "native", MPI_INFO_NULL);
    MPI_File_write_all(fh, buf, BUFSIZE, MPI_CHAR, &status);
    MPI_File_close(&fh);
    MPI_Type_free(&filetype);
    MPI_Finalize();
    return 0;
}

以下是使用6个进程运行时的文件内容

ABCDEFGHIJKLMNOPQRSTUVWX

然后我修改了一下。替代

buf[i]=size*i+rank+'A';

buf[i]=size*i+rank+'0';

并运行2个进程,文件如下

01234567IJKLMNOPQRSTUVWX

1 个答案:

答案 0 :(得分:6)

这是正确的行为 - MPI_MODE_CREATE&#34; creates the file if it does not exist&#34;,如果有,则无效;正如文档所说,完全像对应的POSIX O_CREAT

无论出于何种原因,MPI_MODE_ *都不等同于O_TRUNC,如果文件已经存在,它将破坏该文件;为此你必须用MPI_MODE_CREATE|MPI_MODE_EXCL打开,检查打开是否失败,如果是,删除文件并重复(或者用MPI_MODE_DELETE_ON_CLOSE打开它,关闭它,然后重新打开)它)

以两种方式编码:

#include <stdio.h>
#include <mpi.h>
#define BUFSIZE 4
int main(int argc, char **argv)
{
    int map[BUFSIZE], i, rank , size;
    char buf[BUFSIZE];
    MPI_File fh;
    MPI_Datatype filetype;
    MPI_Status status;
    MPI_Offset disp=0;

    if (argc != 3) {
        fprintf(stderr,"Usage: %s letters|numbers test|delete|none\n", argv[0]);
        exit(-1);
    }

    int letters=0;
    if (!strcasecmp(argv[1],"letters"))
        letters = 1;

    int test=0;
    if (!strcasecmp(argv[2],"test"))
        test = 1;
    int delete = 0;
    if (!strcasecmp(argv[2],"delete"))
        delete = 1;

    MPI_Init(&argc, &argv);
    MPI_Comm_size(MPI_COMM_WORLD, &size);
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);
    for(i=0;i<BUFSIZE;i++)
    {
        if (letters)
            buf[i]=size*i+rank+'A';
        else
            buf[i]=size*i+rank+'0';

        map[i]=size*i+rank;
    }

    if (test) {
        int err = MPI_File_open(MPI_COMM_WORLD, "datafile4", MPI_MODE_CREATE|MPI_MODE_EXCL|MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
        if (err != MPI_SUCCESS)  {
            if (rank == 0)
                MPI_File_delete("datafile4",MPI_INFO_NULL);
            MPI_File_open(MPI_COMM_WORLD, "datafile4", MPI_MODE_CREATE|MPI_MODE_EXCL|MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
        }
    } else if (delete) {
        MPI_File_open(MPI_COMM_WORLD, "datafile4", MPI_MODE_CREATE|MPI_MODE_DELETE_ON_CLOSE|MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
        MPI_File_close(&fh);
        MPI_File_open(MPI_COMM_WORLD, "datafile4", MPI_MODE_CREATE|MPI_MODE_EXCL|MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
    } else {
        MPI_File_open(MPI_COMM_WORLD, "datafile4", MPI_MODE_CREATE|MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
    }

    MPI_Type_create_indexed_block(BUFSIZE, 1, map, MPI_CHAR, &filetype);
    MPI_Type_commit(&filetype);
    MPI_File_set_view(fh, disp, MPI_CHAR, filetype, "native", MPI_INFO_NULL);
    MPI_File_write_all(fh, buf, BUFSIZE, MPI_CHAR, &status);
    MPI_File_close(&fh);
    MPI_Type_free(&filetype);
    MPI_Finalize();
    return 0;
}

并且运行给出:

# just open CREATE|WRONLY, reproduce result above
$ mpirun -np 6 ./clobber letters none
$ cat datafile4
ABCDEFGHIJKLMNOPQRSTUVWX$
$ mpirun -np 2 ./clobber numbers none
$ cat datafile4
01234567IJKLMNOPQRSTUVWX$ 

# test for failed EXCL open, and if so, remove file:
$ mpirun -np 6 ./clobber letters test
$ cat datafile4
ABCDEFGHIJKLMNOPQRSTUVWX$
$ mpirun -np 2 ./clobber numbers test
$ cat datafile4
01234567$

# or always open delete-on-close, close, then re-open              
$ mpirun -np 6 ./clobber letters delete
$ cat datafile4
ABCDEFGHIJKLMNOPQRSTUVWX$
$ mpirun -np 2 ./clobber numbers delete
$ cat datafile4
01234567$