File IO

C | Fortran-2008 | Fortran-90

MPI_File_close

Definition

MPI_File_close closes the file associated with the file handle passed, deallocates the file handle and sets it to MPI_FILE_NULL. Before closing, MPI_File_close synchronises the file state (equivalent to performing an MPI_File_sync). Also, MPI_File_close deletes the file once closed if MPI_MODE_DELETE_ON_CLOSE was passed to the corresponding MPI_File_open. MPI_File_close is a collective operation, it must be called by all MPI processes in the communicator concerned.

Copy

Feedback

int MPI_File_close(MPI_File* handle);

Parameters

handle

The handle on the file to close.

Return value

The error code returned from the file closure.

Example

Copy

Feedback

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

/**
 * @brief Illustrates how to close a file via MPI.
 * @details This application illustrates how to close a file that was opened for
 * temporary use.
 **/
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;
}