The output of the binary is almost right; but your calculations for your file offset and the amount of data to write is incorrect. You want your offset to be
MPI_Offset offset = sizeof(double)*Pstart;
not
MPI_Offset offset = sizeof(double)*rank;
otherwise, each rank will overwrite each other's data as (say) rank 3 of nprocs=5 starts to write in double number 3 in the file, and not (30/5) * 3 = 18.
Also, you want each bit to write NNN/nprocs doubles, not sizeof(double) doubles, which means you want
MPI_File_write(file, localArray, NNN/nprocs, MPI_DOUBLE, &status);
How to write a text file is a much more serious problem; you need to convert the data to a string inside and then print these lines, making sure you know how many characters each line requires careful formatting. This is described in this answer on this site.
source share