[mpich-discuss] MPI I/O

Rob Latham robl at mcs.anl.gov
Fri Jun 10 08:57:40 CDT 2011


On Fri, Jun 10, 2011 at 05:05:43AM +0500, Irfan Gul wrote:
> Thanks every body for replay
> yah I have implement C program to write  integers to a (binary) file and
> also read a single value by each process. but the confusion for me is that
> how to read more than one integer values from file and store in array,
> because storing values in array we must allocate memory for the array of
> size file(values)/process. and I want read from a single file by multiple
> processes and store the values in arrays of each processes.

You'll have to restate your problem.

You have provided code where every process can read a portion of the
list of numbers.

If you want each process to read more numbers you can modify the
'count' parameter to MPI_File_read_at

If you want each process to end up with a copy of the full array, the
"read and broadcast" approach is probably your best bet.  

You could have every process read the whole file collectively, but
that's kind of overkill here.


==rob

> Example of write to Binary file is.
> #include <stdio.h>
> #include <mpi.h>
> 
> void main(int argc, char *argv[])
> {
>   int comm_size,comm_rank;
>   int size_int,amode;
> 
>   MPI_Datatype etype,filetype;
>   MPI_Info info;
>   MPI_Status status;
>   MPI_File fh;
>   MPI_Offset disp;
> 
>   MPI_Init(&argc, &argv);
>   MPI_Comm_rank(MPI_COMM_WORLD,&comm_rank);
>   MPI_Comm_size(MPI_COMM_WORLD,&comm_size);
> 
>   fname="data.dat";
>   drep="native";
> 
>   amode=(MPI_MODE_CREATE|MPI_MODE_WRONLY);
>   size_int=sizeof(size_int);
>   info=MPI_INFO_NULL;
> 
>   MPI_File_open(MPI_COMM_WORLD,"data.dat",amode,info,&fh);
> 
>   disp=comm_rank*size_int;
>   etype=MPI_INTEGER;
>   filetype=MPI_INTEGER;
> 
>     MPI_File_set_view(fh,disp,etype,filetype,"native",info);
> 
>   MPI_File_write_at(fh,disp,&comm_rank,1,MPI_INTEGER,&status);
> 
>   printf("Hello from rank %d. I wrote: %d\n",comm_rank,comm_rank);
> 
>   MPI_File_close(&fh);
>   MPI_Finalize();
> }
> 
> and Example to read values from the file
> 
> #include <stdio.h>
> #include <mpi.h>
> 
> void main(int argc, char *argv[])
> {
>   int comm_size,comm_rank;
>   int size_int,amode,itest;
> 
>   MPI_Datatype etype,filetype;
>   MPI_Info info;
>   MPI_Status status;
>   MPI_File fh;
>   MPI_Offset disp;
> 
>   MPI_Init(&argc, &argv);
>   MPI_Comm_rank(MPI_COMM_WORLD,&comm_rank);
>   MPI_Comm_size(MPI_COMM_WORLD,&comm_size);
> 
>   amode=(MPI_MODE_RDONLY);
>   size_int=sizeof(size_int);
>   info=MPI_INFO_NULL;
> 
>   MPI_File_open(MPI_COMM_WORLD,"data.dat",amode,info,&fh);
> 
>   disp=comm_rank*size_int;
>   etype=MPI_INTEGER;
>   filetype=MPI_INTEGER;
> 
> 
>   MPI_File_set_view(fh,disp,etype,filetype,"native",info);
> 
>   MPI_File_read_at(fh,disp,&itest,1,MPI_INTEGER,&status);
> 
>   printf("Hello from rank %d. I wrote: %d.n",comm_rank,itest);
> 
>   MPI_File_close(&fh);
>   MPI_Finalize();
> }
> 
> 
> 
> thanks for precious time.

> _______________________________________________
> mpich-discuss mailing list
> mpich-discuss at mcs.anl.gov
> https://lists.mcs.anl.gov/mailman/listinfo/mpich-discuss


-- 
Rob Latham
Mathematics and Computer Science Division
Argonne National Lab, IL USA


More information about the mpich-discuss mailing list