HP-MPI User's Guide (11th Edition)

Example applications
io.c
Appendix A262
io.c
In this C example, each process writes to a separate file called iodatax,
where x represents each process rank in turn. Then, the data in
iodatax is read back.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <mpi.h>
#define SIZE (65536)
#define FILENAME "iodata"
/*Each process writes to separate files and reads them back. The
file name is “iodata” and the process rank is appended to it.*/
main(argc, argv)
int argc;
char **argv;
{
int *buf, i, rank, nints, len, flag;
char *filename;
MPI_File fh;
MPI_Status status;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
buf = (int *) malloc(SIZE);
nints = SIZE/sizeof(int);
for (i=0; i<nints; i++) buf[i] = rank*100000 + i;
/* each process opens a separate file called
FILENAME.'myrank' */
filename = (char *) malloc(strlen(FILENAME) + 10);
sprintf(filename, "%s.%d", FILENAME, rank);
MPI_File_open(MPI_COMM_SELF, filename,
MPI_MODE_CREATE | MPI_MODE_RDWR,
MPI_INFO_NULL, &fh);
MPI_File_set_view(fh, (MPI_Offset)0, MPI_INT, MPI_INT,
"native",
MPI_INFO_NULL);
MPI_File_write(fh, buf, nints, MPI_INT, &status);
MPI_File_close(&fh);
/* reopen the file and read the data back */