5
\$\begingroup\$

This toy code uses MPI I/O to read from a binary file.

It goes through reading:

  1. an ASCII header
  2. the first 10 doubles from an array of 100 doubles
  3. another ASCII header
  4. another 10 doubles from another array

It works, but probably isn't good for more than one process, so I will work on that. What I am wondering about is positioning the file pointer at the beginning of each new read. I am making two calls with each read - MPI_File_get_position and MPI_File_get_byte_offset - just to get an offset to put into the MPI_File_set_view call. I've marked these with: \*"DO I REALLY HAVE TO DO ALL THIS"\*.

It seems pretty inefficient and clunky, but it's all I've been able to come up with. Is there a better way?

#include <stdio.h>
#include "mpi.h"


int main(int argc, char **argv) {

   int myid;
   double a[10];
   int n, fulldim = 100, slctdim = 10, offset=0;
   char header[7];

   MPI_Datatype selector;
   MPI_File infile;
   MPI_Status statmpi;
   MPI_Offset foffset, current_file_pos;

   MPI_Init(&argc, &argv);
   MPI_Comm_rank(MPI_COMM_WORLD, &myid);

   offset = slctdim*myid;

   /* creat datatype for access to file */
   MPI_Type_create_subarray(1, &fulldim, &slctdim, &offset,
                            MPI_ORDER_C, MPI_DOUBLE, &selector);
   MPI_Type_commit(&selector);

   MPI_File_open(MPI_COMM_SELF, "bindatahdr.dat", MPI_MODE_RDONLY,
                 MPI_INFO_NULL, &infile);
   MPI_File_set_view(infile, 0, MPI_CHAR, MPI_CHAR, "native", MPI_INFO_NULL);
   MPI_File_read(infile, header, 6, MPI_CHAR, &statmpi);
   printf("%s\n",header);
   MPI_File_set_view(infile, 6, MPI_DOUBLE, selector, "native", MPI_INFO_NULL);
   MPI_File_read(infile, a, 10, MPI_DOUBLE, &statmpi);

   for (n=0;n<10;n++) {
      printf("%d   %lf\n", myid, a[n]);
   }

/* DO I REALLY HAVE TO DO ALL THIS: */
   MPI_File_get_position(infile, &foffset);
   MPI_File_get_byte_offset(infile, foffset, &current_file_pos);
   MPI_File_set_view(infile, current_file_pos, MPI_CHAR, MPI_CHAR, "native", MPI_INFO_NULL);
   MPI_File_read(infile, header, 6, MPI_CHAR, &statmpi);
   printf("%s\n",header);

/* DO I REALLY HAVE TO DO ALL THIS: */
   MPI_File_get_position(infile, &foffset);
   MPI_File_get_byte_offset(infile, foffset, &current_file_pos);
   MPI_File_set_view(infile, current_file_pos, MPI_DOUBLE, selector, "native", MPI_INFO_NULL);
   MPI_File_read(infile, a, 10, MPI_DOUBLE, &statmpi);

   for (n=0;n<10;n++) {
      printf("%d   %lf\n", myid, a[n]);
   }

   MPI_File_close(&infile);

   MPI_Finalize();

}
\$\endgroup\$
  • 2
    \$\begingroup\$ Why not use a function for the repetitive code, isn't that what they are for? \$\endgroup\$ – pacmaninbw Jul 8 '16 at 18:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.