title: MPI Distributed reading over a non-standard type
body: <p>I am trying to read a binary file containing a sequence of char and double. (For example 0 0.125 1 1.4 0 2.3 1 4.5, but written in a binary file). I created a simple struct input, and also an MPI Datatype I will call mpi_input corresponding to this struct.</p>
  
  <pre><code>typedef struct { char type; double value } input;
  </code></pre>
  
  <p>I would like to read my file in parallel (ie here using different processors) using <code>MPI_File_read_at_all</code>. I would like to use the datatype mpi_input in this function. </p>
  
  <p>The problem is, I think that this function needs a buffer it will write into until the end. I tried using an <code>input *buffer</code>, but this creates issues due to data structure alignement. Have you any ideas on how to do this ?</p>
  
  <p>Here is a minimal working example:</p>
  
  <pre><code>#include &lt;stdlib.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;mpi.h&gt;
  #include &lt;assert.h&gt;
  #include &lt;stddef.h&gt;
  
  int main(int argc, char** argv)
  {
      typedef struct
      {
          double val;
          char type;
      } input;
  
      int rank, size;
  
      MPI_Init(&amp;argc, &amp;argv);
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
      MPI_Comm_size(MPI_COMM_WORLD, &amp;size);
  
      assert(size == 4);
  
      MPI_File in;
      MPI_Offset filesize;
      MPI_File_open(MPI_COMM_WORLD, argv[1], MPI_MODE_RDONLY, MPI_INFO_NULL, &amp;in);
      MPI_File_get_size(in, &amp;filesize);
  
      int mysize = filesize/size;
      int globalstart = rank * mysize;
  
      input *chunk = malloc(sizeof(input)*2);
  
      int n = 128;
      int m = 256;
  
      int blocks[2] = {1,1};
      MPI_Datatype types[2] = {MPI_BYTE, MPI_DOUBLE};
      MPI_Aint displacements[2];
      MPI_Datatype cell_type;
      MPI_Aint charex, doublex;
      displacements[0] = offsetof(input, type);
      displacements[1] = offsetof(input, val);
      MPI_Type_create_struct(2, blocks, displacements, types, &amp;cell_type);
      MPI_Type_commit(&amp;cell_type);
  
      MPI_File_read_at_all(in, globalstart, chunk, mysize, cell_type, MPI_STATUS_IGNORE);
      if(rank == 0)
          printf("0 - Got %d %f\n", chunk-&gt;val, chunk-&gt;type);
      if(rank == 4)
          printf("Got %d %f\n", chunk-&gt;val, chunk-&gt;type);
  
      MPI_File_close(&amp;in);
      MPI_Finalize();
  }
  </code></pre>
  
  <p>And here is a code to generate a simple binary file:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main()
  {
      FILE *fp;
      char* filename = "test.dump";
      fp = fopen(filename, "wb");
  
      char bla = 8;
      for(double i = 0; i &lt; 8; i++)
      {
          fwrite(&amp;bla, sizeof(char), 1, fp);
          bla--;
          fwrite(&amp;i, sizeof(double), 1, fp);
      }
  
      fclose(fp);
  }
  </code></pre>