How does MPI_Barrier() work?
<p>I have this code:</p>

<pre><code>#include &lt;cstdint&gt;
#include &lt;mpi.h&gt;
#include &lt;iostream&gt;
using namespace std;

int main(int argc, char **argv)
{
    MPI_Init(&amp;argc, &amp;argv);
    int rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &amp;rank);
    if (rank == 0)
        MPI_Barrier(MPI_COMM_WORLD);
    cout &lt;&lt; "Some output\n";
    if (rank == 1)
        MPI_Barrier(MPI_COMM_WORLD);
    MPI_Barrier(MPI_COMM_WORLD);
    cout &lt;&lt; "end\n";
    MPI_Finalize();
    return 0;
}
</code></pre>

<p>When I run as </p>

<blockquote>
  <p>mpiexec -n 2 MPI.exe</p>
</blockquote>

<p>the program works; the output is:</p>

<pre><code>Some output
End
Some output
End
</code></pre>

<p>However, when I run as </p>

<blockquote>
  <p>mpiexec -n 3 MPI.exe</p>
</blockquote>

<p>the program does <em>not</em> work properly. I expected for output like this:</p>

<pre><code>rank 3 - Some_output
rank 2 - Some output
rank 3 - End
rank 0 - Some output
</code></pre>

<p>At this step, I expect the program to stop.</p>