[mpich-discuss] MPI I/O

Irfan Gul hungu.orakzai at gmail.com
Thu Jun 9 19:05:43 CDT 2011


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.
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.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/mpich-discuss/attachments/20110610/cad83e3a/attachment.htm>


More information about the mpich-discuss mailing list