Collectives

File IO

C | Fortran-2008 | Fortran-90

MPI_File_open

Definition

MPI_File_open opens a file for MPI I/O. MPI_File_open is a collective operation and must therefore be called by every MPI process in the intra-communicator passed. In addition, all MPI processes must provide the same access mode and the same file name.

Copy

Feedback

int MPI_File_open(MPI_Comm comm,
                  const char* filename,
                  int access_mode,
                  MPI_Info info,
                  MPI_File* handle);

Parameters

comm

The communicator from which the collective call is issued. It must be an intra-communicator; inter-communicators are not allowed. Being a collective operation, every MPI process in this intra-communicator must therefore invoke it. An MPI process individually can open a file by passing MPI_COMM_SELF as the communicator.

filename

The name of the file to open. All filenames must point to the same file. Note that the format for specifying the file name is implementation dependent and must therefore be documented by the implementation.

access_mode

The access mode specifies how the file must be opened. Multiple access modes can be enabled simultaneously by applying the bit-wise OR operator '|' or, alternatively, by summing them. The following access modes are supported:


Restrictions:

info

The info is a means by which the user can provide information such as file access patterns and file system specifics to direct optimisations. Alternatively, MPI_INFO_NULL can be passed if no info needs to be specified.

handle

A pointer to the variable in which store the file handle on the file opened.

Return value

The error code returned from the file opening.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to open a file via MPI.
 * @details This application illustrates how to open  a temporary file for the
 * duration of the MPI execution.
 **/
int main(int argc, char* argv[])
{
    MPI_Init(&argc, &argv);

    int my_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);

    MPI_File handle;
    int access_mode = MPI_MODE_CREATE /* Create the file if it does not exist */
                    | MPI_MODE_EXCL /* The file must not exist, to avoid mistakenly erasing a file */
                    | MPI_MODE_RDWR /* With read / write access */
                    | MPI_MODE_UNIQUE_OPEN /* The file will not be opened concurrently elsewhere */
                    | MPI_MODE_DELETE_ON_CLOSE; /* Delete the file when it is closed */
    if(MPI_File_open(MPI_COMM_WORLD, "file.tmp", access_mode, MPI_INFO_NULL, &handle) != MPI_SUCCESS)
    {
        printf("[MPI process %d] Failure in opening the file.\n", my_rank);
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }

    printf("[MPI process %d] File opened successfully.\n", my_rank);

    if(MPI_File_close(&handle) != MPI_SUCCESS)
    {
        printf("[MPI process %d] Failure in closing the file.\n", my_rank);
        MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE);
    }

    printf("[MPI process %d] File closed successfully.\n", my_rank);

    MPI_Finalize();

    return EXIT_SUCCESS;
}