title: MPI gatherv multiple vectors
body: <p>I'm trying to gather some rows of different matrices from nodes in an MPI configuration. So far I've got the program to receive one row to another process with the code I have below, i.e. the code will change the matrix <code>recv</code> to the numbers <code>1..7</code> but ideally what I'd like it to do is change the first two rows,  numbers <code>1..7</code> on the first row and <code>8..14</code> on the second, but this doesn't happen when I change the send/receive count on line 55/57. The blocks should be laid out contiguously in memory so I'm not sure where I'm going wrong currently, any help would be appreciated.</p>
  
  <p>Code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;mpi.h&gt;
  #include &lt;math.h&gt;
  #include &lt;string.h&gt;
  #include &lt;unistd.h&gt;
  double **allocMatrix(int dim) {
      int i;
      double **matrix;
      matrix = (double **)malloc(dim*sizeof(double *));
      for(i=0; i &lt; dim; i++) {
          matrix[i] = (double *)malloc(dim*sizeof(double));
      }
      return matrix;
  }
  void printMatrix(double **values, int size) {
      int i, j;
  
      for (i = 0; i &lt; size; i++) {
          for (j = 0; j &lt; size; j++) {
              printf("%10lf ", values[i][j]);
          }
          printf("\n");
      }
  }
  int main(int argc, char* argv[]) {
      MPI_Init(&amp;argc, &amp;argv);
      int size, rank, i, j;
      int dimensions = 7;
      MPI_Comm_size(MPI_COMM_WORLD, &amp;size);//number of processes
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);//rank for each process
  
      double **send = allocMatrix(dimensions);
      double **recv = allocMatrix(dimensions);
      int count = 0;
      for (i=0; i&lt;dimensions; i++) {
          for (j=0; j&lt;dimensions; j++) {
              if (rank == 0) {
                  recv[i][j] = 0;
              } else {
                  send[i][j] = ++count;
              }
          }
      }
  
      MPI_Datatype arrType;
      MPI_Type_vector(1, dimensions, 0, MPI_DOUBLE, &amp;arrType);
      MPI_Type_commit(&amp;arrType);
      int recvCounts[size];
      int displs[size];
      recvCounts[0] = 0;
      displs[0] = 0;
      recvCounts[1] = 1;
      displs[1] = 0;
      MPI_Gatherv(&amp;(send[0][0]), 1, arrType,
           &amp;(recv[0][0]), recvCounts, displs, arrType,
           0, MPI_COMM_WORLD);
  
      if (rank == 0) {
          printMatrix(recv, dimensions);
      }
  
  
      MPI_Finalize();
      return 0;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>make gatherv
  mpicc -Wall -o gatherv gatherv.c &amp;&amp; ./gather
    1.000000   2.000000   3.000000   4.000000   5.000000   6.000000   7.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
  </code></pre>
  
  <p>Desired output:</p>
  
  <pre><code>  1.000000   2.000000   3.000000   4.000000   5.000000   6.000000   7.000000 
    8.000000   9.000000   10.00000   11.00000   12.00000   13.00000   14.00000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
    0.000000   0.000000   0.000000   0.000000   0.000000   0.000000   0.000000 
  </code></pre>