I am reading "Using MPI-2" and try to execute the code myself. I specified MPI_MODE_CREATE for MPI_File_open, but it actually does not create a new file, instead, it overwrites the previous file with the same name. I happen to find this out when first running with more processes, and then with fewer processes. My MPI version is openmpi 1.8.1.
Here is the code
#include <stdio.h>
#include <mpi.h>
#define BUFSIZE 4
int main(int argc, char **argv)
{
int map[BUFSIZE], i, rank , size;
char buf[BUFSIZE];
MPI_File fh;
MPI_Datatype filetype;
MPI_Status status;
MPI_Offset disp=0;
MPI_Init(&argc, &argv);
MPI_Comm_size(MPI_COMM_WORLD, &size);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
for(i=0;i<BUFSIZE;i++)
{
// buf[i]=size*i+rank+'0';
buf[i]=size*i+rank+'A';
map[i]=size*i+rank;
}
MPI_File_open(MPI_COMM_WORLD, "datafile4", MPI_MODE_CREATE|MPI_MODE_WRONLY, MPI_INFO_NULL, &fh);
MPI_Type_create_indexed_block(BUFSIZE, 1, map, MPI_CHAR, &filetype);
MPI_Type_commit(&filetype);
MPI_File_set_view(fh, disp, MPI_CHAR, filetype, "native", MPI_INFO_NULL);
MPI_File_write_all(fh, buf, BUFSIZE, MPI_CHAR, &status);
MPI_File_close(&fh);
MPI_Type_free(&filetype);
MPI_Finalize();
return 0;
}
Here is the file content when I run with 6 processes
ABCDEFGHIJKLMNOPQRSTUVWX
Then I modified it a little bit. Substitute
buf[i]=size*i+rank+'A';
with
buf[i]=size*i+rank+'0';
and run with 2 processes, the file is as follows
01234567IJKLMNOPQRSTUVWX