c - 使用 MPI-IO 写入多个共享文件

标签 c mpi hpc mpi-io

我正在运行具有数千个 MPI 进程的模拟,并且需要将输出数据写入一小组文件。例如,尽管我可能有 10,000 个进程,但我只想写出 10 个文件,每个文件写入 1,000 个(以某个适当的偏移量)。据我所知,执行此操作的正确方法是为将写入相同文件的进程组创建一个新的通信器,使用 MPI_File_open() 打开该通信器的共享文件,然后写入它与 MPI_File_write_at_all()。那是对的吗?以下代码是我编写的玩具示例:

#include <mpi.h>
#include <math.h>
#include <stdio.h>

const int MAX_NUM_FILES = 4;

int main(){
    MPI_Init(NULL, NULL);

    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int numProcs;
    MPI_Comm_size(MPI_COMM_WORLD, &numProcs);

    int numProcsPerFile = ceil(((double) numProcs) / MAX_NUM_FILES);
    int targetFile = rank / numProcsPerFile;

    MPI_Comm fileComm;
    MPI_Comm_split(MPI_COMM_WORLD, targetFile, rank, &fileComm);

    int targetFileRank;
    MPI_Comm_rank(fileComm, &targetFileRank);

    char filename[20]; // Sufficient for testing purposes
    snprintf(filename, 20, "out_%d.dat", targetFile);
    printf(
        "Proc %d: writing to file %s with rank %d\n", rank, filename,
        targetFileRank);

    MPI_File outFile;
    MPI_File_open(
        fileComm, filename, MPI_MODE_CREATE | MPI_MODE_WRONLY,
        MPI_INFO_NULL, &outFile);

    char bufToWrite[4];
    snprintf(bufToWrite, 4, "%3d", rank);

    MPI_File_write_at_all(
        outFile, targetFileRank * 3,
        bufToWrite, 3, MPI_CHAR, MPI_STATUS_IGNORE);

    MPI_File_close(&outFile);
    MPI_Finalize();
}

我可以使用 mpicc file.c -lm 进行编译,然后使用 mpirun -np 20 a.out 运行 20 个进程,我得到了预期的输出 (四个文件,每个文件有五个条目),但我不确定这是否是技术上正确/最佳的实现方式。有什么我应该做的不同的吗?

最佳答案

您的做法是正确的。为了澄清,我们需要重新审视标准和定义。来自 MPI: A Message-Passing Interface Standard Version 2.2 的 MPI_File_Open API (第 391 页)

int MPI_File_open(MPI_Comm comm, char *filename, int amode, MPI_Info info, MPI_File *fh)

描述:

MPI_FILE_OPEN opens the file identified by the file name filename on all processes in the comm communicator group. MPI_FILE_OPEN is a collective routine: all processes must provide the same value for amode, and all processes must provide filenames that reference the same file. (Values for info may vary.) comm must be an intracommunicator; it is erroneous to pass an intercommunicator to MPI_FILE_OPEN.

内部沟通者与内部沟通者(第 134 页):

For the purposes of this chapter, it is sufficient to know that there are two types of communicators: intra-communicators and inter-communicators. An intracommunicator can be thought of as an identifier for a single group of processes linked with a context. An intercommunicator identifies two distinct groups of processes linked with a context.

将内部通信器传递给 MPI_File_open() 的目的是指定一组将对文件执行操作的进程。 MPI 运行时需要此信息,因此它可以在发生集体 I/O 操作时强制执行适当的同步。理解应用程序的逻辑并创建/选择正确的内部通信器是程序员的责任。

MPI_Comm_Split() 在一个强大的 API 中,允许将通信组拆分为不相交的子组以用于不同的用例,包括 MPI I/O。

关于c - 使用 MPI-IO 写入多个共享文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42431064/

相关文章:

c++ - 这在 c int a :16;? 中意味着什么

c - 将指向函数的指针作为参数传递给具有多个参数的其他函数

linux - 网络上所有机器的主机名发现

python - mpi4py | comm.bcast 不起作用

c# - BCP 任务在执行时挂起

创建有错误的文件

c - 将可执行文件命名为c

compiler-errors - 我如何在MPI中使用ULFM。要实现这些功能需要做什么

c - MPI_Scatter 和 Gather 用于 MPI 中的二维矩阵,使用 C

parallel-processing - 根据标志对 mpi 进程进行分组