cpp_ninja
cpp_ninja

Reputation: 347

How to read ints from a text file with MPI and C++

I'm having a bit of a problem with parallel reading

I have a text file which looks like this:

1 4 30 46 
0 2 3 29 
1 11 12 -1 
1 4 5 -1 
0 3 13 14 
3 6 7 8 
5 10 -1 -1 
13 10 -1 -1 
5 9 27 -1 

and I'm trying to read these ints 4 at a time in each process, the number of lines of the files equals the number of processes and every line contains 4 ints

int bufsize, count; 
int *buf;
MPI::Status status; 

MPI::File top = MPI::File::Open(MPI::COMM_WORLD, "top.txt", MPI::MODE_RDONLY, MPI::INFO_NULL); 

MPI::Offset filesize = top.Get_size(); 
filesize = filesize / sizeof(int);
bufsize = filesize / wasteland_size + 1;

buf = new int[bufsize * sizeof(int)]; 

top.Set_view(my_rank * bufsize * sizeof(int), MPI_INT, MPI_INT, "native", MPI::INFO_NULL); 
top.Read(buf, bufsize, MPI_INT, status); 
count = status.Get_count(MPI_INT); 

top.Close();

this is the code i'm using.

It compiles without errors or warnings, but it outputs something line :

540287025 874524723 805969974 857748000 

for every process.

Upvotes: 1

Views: 2706

Answers (1)

sdkljhdf hda
sdkljhdf hda

Reputation: 1407

The problem is that your file is a text file, but it is not interpreted as such. You are reading the integers from file in binary.

When you convert any of these numbers you get to hex you see that they consist of byes that represent digits or space in ASCII.

I would suggest changing the file format so that each number I'd represented as 4 bytes. This also allows you to split the file the way you have done.

Upvotes: 2

Related Questions