title: How to sum a 2D array in C using MPI
body: <p>This is the program I am using to sum all values in a 1D array, and it works correctly. But how do I modify it to work on 2D array? Imagine variable <code>a</code> is something like <code>a = { {1,2}, {3,4}, {5,6} };</code>.</p>
  <p>I tried few solutions but they are not working, so can someone explain few important changes to make to make it compatible with 2D array also.</p>
  <pre><code>#include &lt;mpi.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  
  // size of array
  #define n 10
  
  int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
  
  // Temporary array for slave process
  int a2[1000];
  
  int main(int argc, char* argv[])
  {
  
      int pid, np,
          elements_per_process,
          n_elements_recieved;
      // np -&gt; no. of processes
      // pid -&gt; process id
  
      MPI_Status status;
  
      // Creation of parallel processes
      MPI_Init(&amp;argc, &amp;argv);
  
      // find out process ID,
      // and how many processes were started
      MPI_Comm_rank(MPI_COMM_WORLD, &amp;pid);
      MPI_Comm_size(MPI_COMM_WORLD, &amp;np);
  
      // master process
      if (pid == 0) {
  
          int index, i;
          elements_per_process = n / np;
  
          // check if more than 1 processes are run
          if (np &gt; 1) {
              // distributes the portion of array
              // to child processes to calculate
              // their partial sums
              for (i = 1; i &lt; np - 1; i++) {
                  index = i * elements_per_process;
  
                  MPI_Send(&amp;elements_per_process,
                      1, MPI_INT, i, 0,
                      MPI_COMM_WORLD);
                  MPI_Send(&amp;a[index],
                      elements_per_process,
                      MPI_INT, i, 0,
                      MPI_COMM_WORLD);
              }
              // last process adds remaining elements
              index = i * elements_per_process;
              int elements_left = n - index;
  
              MPI_Send(&amp;elements_left,
                  1, MPI_INT,
                  i, 0,
                  MPI_COMM_WORLD);
              MPI_Send(&amp;a[index],
                  elements_left,
                  MPI_INT, i, 0,
                  MPI_COMM_WORLD);
          }
  
          // master process add its own sub array
          int sum = 0;
          for (i = 0; i &lt; elements_per_process; i++) 
              sum += a[i];
  
          // collects partial sums from other processes
          int tmp;
          for (i = 1; i &lt; np; i++) {
              MPI_Recv(&amp;tmp, 1, MPI_INT,
                  MPI_ANY_SOURCE, 0,
                  MPI_COMM_WORLD,
                  &amp;status);
              int sender = status.MPI_SOURCE;
              sum += tmp;
          }
  
          // prints the final sum of array
          printf(&quot;Sum of array is : %d\n&quot;, sum);
  
  
  
      }
      // slave processes
      else {
          MPI_Recv(&amp;n_elements_recieved,
              1, MPI_INT, 0, 0,
              MPI_COMM_WORLD,
              &amp;status);
  
          // stores the received array segment
          // in local array a2
          MPI_Recv(&amp;a2, n_elements_recieved,
              MPI_INT, 0, 0,
              MPI_COMM_WORLD,
              &amp;status);
  
          // calculates its partial sum
          int partial_sum = 0;
          for (int i = 0; i &lt; n_elements_recieved; i++)
              partial_sum += a2[i];
  
          // sends the partial sum to the root process
          MPI_Send(&amp;partial_sum, 1, MPI_INT,
              0, 0, MPI_COMM_WORLD);
      }
  
      // cleans up all MPI state before exit of process
      MPI_Finalize();
  
  
  
  
      return 0;
  }
  </code></pre>