title: Calculating CPU Time when using MPI
body: <p>I'm working on a parallel matrix-matrix multiplier in MPI.  I've got the calculation part working but I also want to calculate CPU time.  I'm getting stuck because it looks like some processes are reporting start and end times of 0 and for a task that should take under a second (small matrices), the program reports 1000+ second CPU times (even though I know that it runs in under a second from observation).  Here's what I'm currently doing:</p>
  
  <pre><code>#include &lt;time.h&gt;
  #include "mpi.h"
  // other includes
  int main()
  {
      int start, end, min_start, min_end;
      if (rank == 0)
      {
          // setup stuff
  
          start = clock();
          MPI_Reduce(&amp;min_start, &amp;start, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
  
          // master computation stuff
  
          end = clock();
          MPI_Reduce(&amp;max_end, &amp;end, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
  
          cout &lt;&lt; "CPU time was " 
               &lt;&lt; (double)(max_end - min_start) / CLOCKS_PER_SEC 
               &lt;&lt; " seconds" &lt;&lt; endl;
      }   
      else if (rank != 0)
      {
          // setup stuff
  
          start = clock();
          MPI_Reduce(&amp;min_start, &amp;start, 1, MPI_INT, MPI_MIN, 0, MPI_COMM_WORLD);
  
          // slave computation stuff
  
          end = clock();
          MPI_Reduce(&amp;max_end, &amp;end, 1, MPI_INT, MPI_MAX, 0, MPI_COMM_WORLD);
      }
  }
  </code></pre>
  
  <p>I'm not sure what the source of the error is.  When I added in this debugging output (after the <code>if (rank == 0)</code> and <code>else if (rank != 0)</code> statement)</p>
  
  <pre><code>MPI_Barrier(MPI_COMM_WORLD);
  for (int i=0; i&lt;size; i++)
  {
      if (rank == i)
          cout &lt;&lt; "(" &lt;&lt; i &lt;&lt; ") CPU time = " 
               &lt;&lt; end &lt;&lt; " - " &lt;&lt; start 
               &lt;&lt; " = " &lt;&lt; end - start &lt;&lt; endl;
      MPI_Barrier(MPI_COMM_WORLD);
  }
  </code></pre>
  
  <p>I get the following output</p>
  
  <pre><code>CPU time was 1627.91 seconds
  (1) CPU time = 0 - 0 = 0
  (2) CPU time = 0 - 0 = 0
  (0) CPU time = 1627938704 - 32637 = 1627906067
  (3) CPU time = 10000 - 0 = 10000
  </code></pre>