[mpich-discuss] Parallel MPI read from a single file

Burak ISIKLI burak.isikli at yahoo.com
Tue Dec 29 17:36:32 CST 2009


Hello, 

I have a MPI/C++ code for parallel reading from a single data file. There seems 
to be some problem which I am not able to figure out. Pls point out my mistake. 
Here is the code, data file and input: 

// example of parallel MPI read from single file, in C++ 
#include <iostream.h> 
#include <mpi.h> 

int main(int argc, char *argv[]) 
{ 
    int bufsize, buf[15], count,rank,nprocs,i; 
    MPI_File fh; 
    MPI_Status status; 

    MPI_Init(&argc, &argv); 
    MPI_Comm_rank(MPI_COMM_WORLD, &rank); 
    MPI_Comm_size(MPI_COMM_WORLD, &nprocs); 

    bufsize = sizeof(int)*30 / nprocs ; // local number to read 
    nints = bufsize/sizeof (int); 

    MPI_File_open 
(MPI_COMM_WORLD,"testfile.dat",MPI_MODE_RDONLY,MPI_INFO_NULL,&fh); 
    MPI_File_seek(fh,rank*bufsize,MPI_SEEK_SET); 
    MPI_File_read(fh,&buf,nints,MPI_INT,&status); 
    MPI_File_close(&fh); 

    if(rank == 0) for(i=0;i<15;i++) cout<<"rank = "<<rank<<" "<<" i 
= "<<i<<" "<<buf[i]<<endl; 
    free (buf); 
    MPI_Finalize(); 
    return 0; 
} 
 DATA FILE: 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 

OUTPUT: 

mpirun -np 1 MPIfilehandling testfile.dat 
rank = 0 i = 0 822751754 
rank = 0 i = 1 856306698 
rank = 0 i = 2 889861642 
rank = 0 i = 3 923416586 
rank = 0 i = 4 956969264 
rank = 0 i = 5 170995978 
rank = 0 i = 6 825362993 
rank = 0 i = 7 856305972 
rank = 0 i = 8 170997002 
rank = 0 i = 9 825625137 
rank = 0 i = 10 923414840 
rank = 0 i = 11 170998026 
rank = 0 i = 12 842009138 
rank = 0 i = 13 822751794 
rank = 0 i = 14 171062026 

The mistake is not in your application per se. MPI I/O is not intended to read in general text files - it is intended for reading 
files written by the MPI I/O interface. It definitely doesn't do the 
string to integer conversion necessary to read in your input file. 

So if i want to parallelly read a single data file which hasn't written by the MPI I/O, what can i do?

--------------------------------------------------------
Burak ISIKLI
Dumlupinar University
Electrical & Electronics - Computer Engineering
 
http://burakisikli.wordpress.com
--------------------------------------------------------



      
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/mpich-discuss/attachments/20091229/c2996e74/attachment.htm>


More information about the mpich-discuss mailing list