title: How can I send rows of a matrix to all the processes using MPI_Scatterv?
body: <p>I am working with the MPI interface. I want to split a matrix (by rows) and distribute the parts among every process.</p>
  
  <p>For example, I have this 7x7 square matrix M.</p>
  
  <pre><code>M = [
      0.00    1.00    2.00    3.00    4.00    5.00    6.00    
      7.00    8.00    9.00    10.00   11.00   12.00   13.00
      14.00   15.00   16.00   17.00   18.00   19.00   20.00
      21.00   22.00   23.00   24.00   25.00   26.00   27.00
      28.00   29.00   30.00   31.00   32.00   33.00   34.00
      35.00   36.00   37.00   38.00   39.00   40.00   41.00
      42.00   43.00   44.00   45.00   46.00   47.00   48.00
  ];
  </code></pre>
  
  <p>I have 3 processes, so the split could be:</p>
  
  <ul>
  <li>Process 0 gets rows 0, 1</li>
  <li>Process 1 gets rows 2, 3, 4</li>
  <li>Process 2 gets rows 5, 6</li>
  </ul>
  
  <p>After the Scatterv, it should look like this:</p>
  
  <pre><code>Process 0:
  M0 = [
      0.00    1.00    2.00    3.00    4.00    5.00    6.00    
      7.00    8.00    9.00    10.00   11.00   12.00   13.00
  ];
  
  Process 1:
  M1 = [
      14.00   15.00   16.00   17.00   18.00   19.00   20.00
      21.00   22.00   23.00   24.00   25.00   26.00   27.00
      28.00   29.00   30.00   31.00   32.00   33.00   34.00
  ];
  
  Process 2:
  M2 = [
      35.00   36.00   37.00   38.00   39.00   40.00   41.00
      42.00   43.00   44.00   45.00   46.00   47.00   48.00
  ];
  </code></pre>
  
  <p>I think I was clear enough about what I want to achieve. Feel free to ask if I didn't explain well.</p>
  
  <p>Now, I show you my code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;mpi.h&gt;
  
  #define BLOCK_LOW(id,p,n) ((id)*(n)/(p))
  #define BLOCK_HIGH(id,p,n) ((id+1)*(n)/(p) - 1)
  #define BLOCK_SIZE(id,p,n) ((id+1)*(n)/(p) - (id)*(n)/(p))
  #define BLOCK_OWNER(index,p,n) (((p)*((index)+1)-1)/(n))
  
  void **matrix_create(size_t m, size_t n, size_t size) {
     size_t i; 
     void **p= (void **) malloc(m*n*size+ m*sizeof(void *));
     char *c=  (char*) (p+m);
     for(i=0; i&lt;m; ++i)
        p[i]= (void *) c+i*n*size;
     return p;
  }
  
  void matrix_print(double **M, size_t m, size_t n, char *name) {
      size_t i,j;
      printf("%s=[",name);
      for(i=0; i&lt;m; ++i) {
          printf("\n  ");
          for(j=0; j&lt;n; ++j)
              printf("%f  ",M[i][j]);
      }
      printf("\n];\n");
  }
  
  main(int argc, char *argv[]) {
  
      int npes, myrank, root = 0, n = 7, rows, i, j, *sendcounts, *displs;
      double **m, **mParts;
  
      MPI_Status status;
      MPI_Init(&amp;argc, &amp;argv);
      MPI_Comm_size(MPI_COMM_WORLD,&amp;npes);
      MPI_Comm_rank(MPI_COMM_WORLD,&amp;myrank);
  
      // Matrix M is generated in the root process (process 0)
      if (myrank == root) {
          m = (double**)matrix_create(n, n, sizeof(double));
          for (i = 0; i &lt; n; ++i)
              for (j = 0; j &lt; n; ++j)
                  m[i][j] = (double)(n * i + j);
      }
  
      // Array containing the numbers of rows for each process
      sendcounts = malloc(n * sizeof(int));
      // Array containing the displacement for each data chunk
      displs = malloc(n * sizeof(int));
      // For each process ...
      for (j = 0; j &lt; npes; j++) {
          // Sets each number of rows
          sendcounts[j] = BLOCK_SIZE(j, npes, n);
          // Sets each displacement
          displs[j] = BLOCK_LOW(j, npes, n);
      }
      // Each process gets the number of rows that he is going to get
      rows = sendcounts[myrank];
      // Creates the empty matrixes for the parts of M
      mParts = (double**)matrix_create(rows, n, sizeof(double));
      // Scatters the matrix parts through all the processes
      MPI_Scatterv(m, sendcounts, displs, MPI_DOUBLE, mParts, rows, MPI_DOUBLE, root, MPI_COMM_WORLD);
  
      // This is where I get the Segmentation Fault
      if (myrank == 1) matrix_print(mParts, rows, n, "mParts");
  
      MPI_Finalize();
  }
  </code></pre>
  
  <p>I get a Segmentation Fault when I try to read the scattered data, suggesting that the scatter operation did not work. I already did this with one-dimensional arrays and it worked. But with two-dimensional arrays, things get a little trickier.</p>
  
  <p>Could you help me find the bug, please?</p>
  
  <p>Thanks</p>