问题
I am trying to make multiple processes write an integer buffer into a file at the same time using MPI parallel io. To achieve this goal I searched various websites:
- MPI and Parallel IO
- Google book search
- Parallel IO with MPI
And I tried to learn their teachings. To test them i created the following simple code in c++:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#define BUFSIZE 100
using namespace std;
int main(int argc, char *argv[])
{
int myrank, buf[BUFSIZE], rcode;
MPI_File thefile;
MPI_Init(&argc, &argv);
MPI_Comm_rank(MPI_COMM_WORLD, &myrank);
for (int i=0; i<BUFSIZE; i++)
buf[i] = myrank * BUFSIZE + i;
rcode = MPI_File_open(MPI_COMM_WORLD, "testfile", MPI_MODE_CREATE | MPI_MODE_RDWR,
MPI_INFO_NULL, &thefile);
if(rcode != MPI_SUCCESS){
cerr << "Did not open file" << endl;
return EXIT_FAILURE;
}
rcode = MPI_File_set_view(thefile, myrank * (MPI_Offset)BUFSIZE * sizeof(int), MPI_INT, MPI_INT,
"native", MPI_INFO_NULL);
if(rcode != MPI_SUCCESS){
cerr << "Problem setting process view" << endl;
return EXIT_FAILURE;
}
MPI_File_write(thefile, buf, BUFSIZE, MPI_INT, MPI_STATUS_IGNORE);
if(rcode != MPI_SUCCESS){
cerr << "Problem writting file" << endl;
return EXIT_FAILURE;
}
MPI_File_close(&thefile);
MPI_Finalize();
return EXIT_SUCCESS;
}
However, when I try to read the file with Kate, I get random garbage: a bunch of squares, rectangles, !$"%&!!/ symbols and no integers at all.
What I am doing wrong?
回答1:
You seem to have a text/binary data misconception.
The int
s written by MPI_File_write(..., MPI_INT,...)
will be written as binary data, not ASCII or Unicode text. "A bunch of squares, rectangles, @#$@#! symbols" reads to me as if you are opening the file with a text editor and looking for ASCII numbers.
If you want formatted output, you have to format it yourself. If you want a text editor to read your file, you need to write text, not int
s.
来源:https://stackoverflow.com/questions/15602521/simple-example-mpi-parallel-io-writing-garbage