Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP-MPI User's Guide > Appendix A Example applications

io.c

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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 */

for (i=0; i<nints; i++) buf[i] = 0;
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_read(fh, buf, nints, MPI_INT, &status);
MPI_File_close(&fh);

/* check if the data read is correct */
flag = 0;
for (i=0; i<nints; i++)
if (buf[i] != (rank*100000 + i)) {
printf("Process %d: error, read %d, should be %d\n",
rank, buf[i], rank*100000+i);
flag = 1;
}

if (!flag) {
printf("Process %d: data read back is correct\n", rank);
MPI_File_delete(filename, MPI_INFO_NULL);
}

free(buf);
free(filename);

MPI_Finalize();
exit(0);
}

io output

The output from running the io executable is shown below. The application was run with -np = 4.

Process 0: data read back is correct
Process 1: data read back is correct
Process 2: data read back is correct
Process 3: data read back is correct
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© 1979-2007 Hewlett-Packard Development Company, L.P.