title: How to find sum of the given numbers using MPI in C?
body: <p>I am trying to find the sum of all given numbers in an array. And I have to split the array in equal size and send to each process and calculate the sum. Later send back the calculated sum from each process to root process for the final answer. Actually, I know I can use <code>MPI_Scatter</code>. But my problem is what if my list is in the <em>odd number</em>. For example, I have an array with <code>13</code> elements, then I have <code>3</code> process. So by default, the <code>MPI_Scatter</code> will divide the array by <code>3</code> and left the last element. Basically, it will calculate the sum for only <code>12</code> elements. My output when I just use the <code>MPI_Scatter</code>:</p>
  
  <pre><code>myid = 0 total = 6
  myid = 1 total = 22
  myid = 2 total = 38
  results from all processors_= 66 
  size= 13 
  </code></pre>
  
  <p>So, I plan to use the <code>MPI_Scatter</code> and <code>MPI_Send</code>. So I can get the last element and send that through the <code>MPI_Send</code> and calculate it, and receive in root process. But I am getting problem.. My code:</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;string.h&gt;
  #include &lt;mpi.h&gt;
  
  /*  globals */
  int numnodes, myid, mpi_err;
  int last_core;
  int n;
  int last_elements[];
  
  #define mpi_root 0
  /* end globals  */
  
  void init_it(int  *argc, char ***argv);
  
  void init_it(int  *argc, char ***argv) {
      mpi_err = MPI_Init(argc, argv);
      mpi_err = MPI_Comm_size( MPI_COMM_WORLD, &amp;numnodes );
      mpi_err = MPI_Comm_rank(MPI_COMM_WORLD, &amp;myid);
  }
  
  int main(int argc, char *argv[]) {
      int *myray, *send_ray, *back_ray;
      int count;
      int size, mysize, i, k, j, total;
  
      MPI_Status status;
  
      init_it(&amp;argc, &amp;argv);
  
      /* each processor will get count elements from the root */
      count = 4;
      myray = (int*)malloc(count * sizeof(int));
      size = (count * numnodes) + 1;
      send_ray = (int*)malloc(size * sizeof(int));
      back_ray = (int*)malloc(numnodes * sizeof(int));
      last_core = numnodes - 1;
  
      /* create the data to be sent on the root */
      if(myid == mpi_root){
          for(i = 0; i &lt; size; i++)
          {
              send_ray[i] = i;
          }
      }
  
      /* send different data to each processor */
      mpi_err = MPI_Scatter( send_ray, count, MPI_INT,
                             myray, count, MPI_INT,
                             mpi_root, MPI_COMM_WORLD);
  
      if(myid == mpi_root) {
          n = 1;
          memcpy(last_elements, &amp;send_ray[size-n], n * sizeof(int));
  
          //Send the last numbers to the last core through send command
          MPI_Send(last_elements, n, MPI_INT, last_core, 99, MPI_COMM_WORLD);
      }
  
      /* each processor does a local sum */
      total = 0;
      for(i = 0; i &lt; count; i++)
          total = total + myray[i];
          //total = total + send_ray[size-1];
      printf("myid= %d total= %d\n", myid, total);
  
      if(myid == last_core)
      {
          printf("Last core\n");
          MPI_Recv(last_elements, n, MPI_INT, 0, 99, MPI_COMM_WORLD, &amp;status);
      }
  
      /* send the local sums back to the root */
      mpi_err = MPI_Gather(&amp;total, 1, MPI_INT,
                          back_ray, 1, MPI_INT,
                          mpi_root, MPI_COMM_WORLD);
  
      /* the root prints the global sum */
      if(myid == mpi_root){
          total=0;
          for(i = 0; i &lt; numnodes; i++)
              total = total + back_ray[i];
          printf("results from all processors_= %d \n", total);
          printf("size= %d \n ", size);
      }
  
      mpi_err = MPI_Finalize();
  }
  </code></pre>
  
  <p><strong>The output:</strong></p>
  
  <pre><code>myid = 0 total = 6
  myid = 1 total = 22
  myid = 2 total = 38
  Last core
  [ubuntu:11884] *** An error occurred in MPI_Recv
  [ubuntu:11884] *** on communicator MPI_COMM_WORLD
  [ubuntu:11884] *** MPI_ERR_TRUNCATE: message truncated
  [ubuntu:11884] *** MPI_ERRORS_ARE_FATAL: your MPI job will now abort
  --------------------------------------------------------------------------
  mpiexec has exited due to process rank 2 with PID 11884 on
  node ubuntu exiting improperly. There are two reasons this could occur:
  
  1. this process did not call "init" before exiting, but others in
  the job did. This can cause a job to hang indefinitely while it waits
  for all processes to call "init". By rule, if one process calls "init",
  then ALL processes must call "init" prior to termination.
  
  2. this process called "init", but exited without calling "finalize".
  By rule, all processes that call "init" MUST call "finalize" prior to
  exiting or it will be considered an "abnormal termination"
  
  This may have caused other processes in the application to be
  terminated by signals sent by mpiexec (as reported here).
  </code></pre>
  
  <p>I know I am doing wrong. I would appreciate if you can point me.</p>