# mpiBarrier(mpiCommWorld) not working correctly?

**URL:** <https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676>\
**Category:** General Discussion\
**Created:** [January 5, 2025, 5:17am UTC](https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676 "2025-01-05T05:17:28Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trueo](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@Trueo](https://community.freefem.org/u/Trueo)\
**Post date:** [January 5, 2025, 5:17am UTC](https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676/1 "2025-01-05T05:17:28Z")

</div>

It seems like `mpiBarrier(mpiCommWorld)` is not functioning as expected. Here’s the situation:

Given the following FreeFEM++ code:  
/////////////////////////////////////////////////////////////////////////  
verbosity = 0;  
mpiComm comm(mpiCommWorld, 0, 0);  
for (int i = 0; i \< mpisize; ++i) {  
if (i == mpirank) {  
cout \<\< "mprocess = " \<\< mpirank \<\< " among = " \<\< mpisize \<\< endl;  
}  
mpiBarrier(mpiCommWorld);  
}  
/////////////////////////////////////////////////////////////////////////////  
The output I get is:  
mprocess = 0 among = 4  
mprocess = 1 among = 4  
mprocess = 3 among = 4  
mprocess = 2 among = 4

However, I was expecting this output:  
mprocess = 0 among = 4  
mprocess = 1 among = 4  
mprocess = 2 among = 4  
mprocess = 3 among = 4

Is `mpiBarrier(mpiCommWorld)` not working correctly? It should ensure that all processes synchronize at each iteration, so only one process outputs at a time. Could you clarify if there’s an issue with my understanding or usage of `mpiBarrier`?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 5, 2025, 8:54am UTC](https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676/2 "2025-01-05T08:54:38Z")

</div>

What is not working as expected? Why would you expect the output to be:

```auto
mprocess = 0 among = 4
mprocess = 1 among = 4
mprocess = 2 among = 4
mprocess = 3 among = 4

```

I’m guessing if you want to be sure you get the expected output, you should probably flush `cout`.

---

<div class="post-metadata">

**Author:** ![Trueo](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@Trueo](https://community.freefem.org/u/Trueo)\
**Post date:** [January 5, 2025, 9:48am UTC](https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676/3 "2025-01-05T09:48:38Z")

</div>

Thank you very much for your reply.  
The reason I expected the output to be:

```auto
mprocess = 0 among = 4
mprocess = 1 among = 4
mprocess = 2 among = 4
mprocess = 3 among = 4

```

is because in my code, I use a `for` loop where each process only executes the `cout` statement when its rank matches the current iteration index `i`. For example:

- When `i == 0`, only process 0 executes `cout`.
- When `i == 1`, only process 1 executes `cout`, and so on.

After each iteration, I use `mpiBarrier(mpiCommWorld);` to synchronize all processes. My understanding was that this synchronization would ensure that the output from process 0 is completed before process 1 starts its output, and so on.

However, the actual output is out of order, such as:

```auto
mprocess = 0 among = 4
mprocess = 1 among = 4
mprocess = 3 among = 4
mprocess = 2 among = 4

```

Regarding your suggestion about flushing `cout`, I was under the impression that using `endl` in my `cout` statements already performs a flush. Could you tell me why `endl` might not be sufficient in this case? Or could the issue be related to the way `mpiBarrier` is working in this specific context?

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 5, 2025, 1:26pm UTC](https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676/4 "2025-01-05T13:26:27Z")

</div>

My bad, `endl` does actually flush the buffer. I’m not sure why you get this, what is your system? FWIW, on my Linux and macOS, I get your expected output.

---

<div class="post-metadata">

**Author:** ![Trueo](https://avatars.discourse-cdn.com/v4/letter/t/f6c823/32.png) [@Trueo](https://community.freefem.org/u/Trueo)\
**Post date:** [January 7, 2025, 1:15pm UTC](https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676/5 "2025-01-07T13:15:53Z")

</div>

Sorry for late reply.  
I’m using Windows 11, Intel Core i5 8265.

---

<div class="post-metadata">

**Author:** ![prj](https://avatars.discourse-cdn.com/v4/letter/p/ecae2f/32.png) [@prj](https://community.freefem.org/u/prj)\
**Post date:** [January 8, 2025, 7:33am UTC](https://community.freefem.org/t/mpibarrier-mpicommworld-not-working-correctly/3676/6 "2025-01-08T07:33:58Z")

</div>

OK, not a big surprise then.
