MPI: How to guarantee no two process from different communicator doing writing at the same time
<p>I want to split the <code>MPI_comm_world</code> into smaller communicator, so I learned to use <code>MPI_Comm_split</code> from <a href="http://mpitutorial.com/tutorials/introduction-to-groups-and-communicators/" rel="nofollow noreferrer">this link</a></p>
<p>I copy the code here, it shows how to split <code>MPI_comm_world</code> into 4 color</p>
<pre><code>// Get the rank and size in the original communicator
int world_rank, world_size;
MPI_Comm_rank(MPI_COMM_WORLD, &amp;world_rank);
MPI_Comm_size(MPI_COMM_WORLD, &amp;world_size);

int color = world_rank / 4; // Determine color based on row

// Split the communicator based on the color and use the
// original rank for ordering
MPI_Comm row_comm;
MPI_Comm_split(MPI_COMM_WORLD, color, world_rank, &amp;row_comm);

int row_rank, row_size;
MPI_Comm_rank(row_comm, &amp;row_rank);
MPI_Comm_size(row_comm, &amp;row_size);

printf(&quot;WORLD RANK/SIZE: %d/%d \t ROW RANK/SIZE: %d/%d\n&quot;,
    world_rank, world_size, row_rank, row_size);

MPI_Comm_free(&amp;row_comm);
</code></pre>
<p>Now if I distribute my tasks to different color communicator, and each task paralleled in its color domain. What is more each task has a file writing operation.</p>
<p>Typically, if there is only <code>MPI_comm_world</code>, we use <code>if(world_rank==0)</code> to guaranty only one process doing writing at the same time.</p>
<p>However, now we have different color, each color doing its own job and have to write its own result to file.</p>
<p><code>if(color_rank==0)</code> is clearly not sufficient.</p>
<p><strong>So What is the best way to guaranty no two process from different communicator doing writing at the same time?</strong></p>