# Fail to run the demo code in parallel with MPI

**URL:** <https://fenicsproject.discourse.group/t/fail-to-run-the-demo-code-in-parallel-with-mpi/7486>\
**Category:** Uncategorized\
**Created:** [January 16, 2022, 3:07pm UTC](https://fenicsproject.discourse.group/t/fail-to-run-the-demo-code-in-parallel-with-mpi/7486 "2022-01-16T15:07:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![bearsan](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bearsan/32/3365_2.png) [@bearsan](https://fenicsproject.discourse.group/u/bearsan)\
**Post date:** [January 16, 2022, 3:07pm UTC](https://fenicsproject.discourse.group/t/fail-to-run-the-demo-code-in-parallel-with-mpi/7486/1 "2022-01-16T15:07:49Z")

</div>

Dear community,

I am trying to run the demo code of incompressible Navier-Stokes equations from [Bitbucket](https://bitbucket.org/fenics-project/dolfin/src/master/python/demo/documented/navier-stokes/demo_navier-stokes.py) in parallel with MPI.

However, according to the timing summary, there is no improvement in computational time with increasing number of processors. It seems like that the code is executed several times individually. The timing summary is listed in the following figure

 ![image](https://global.discourse-cdn.com/free1/uploads/fenicsproject1/original/2X/9/932223c0d5f17098cf1b2c44d3047568dfabf929.png)

The FEniCS is installed on Docker, and it is the latest stable version. The total number of cores in my computer is 8. Did I misuse the MPI command? Or should I add some extra codes to parallelize the demo?

Could you please guide me to some resources or tips which would help me solve this problem?

Thank you,  
Best regards

---

<div class="post-metadata">

**Author:** ![nate](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/nate/32/17_2.png) [@nate](https://fenicsproject.discourse.group/u/nate)\
**Post date:** [January 16, 2022, 4:11pm UTC](https://fenicsproject.discourse.group/t/fail-to-run-the-demo-code-in-parallel-with-mpi/7486/2 "2022-01-16T16:11:54Z")

</div>

Try running the following:

```auto
mpirun -np 2 python3 -c "from mpi4py import MPI; print(MPI.COMM_WORLD.rank)"

```

You should see

```auto
0
1

```

---

<div class="post-metadata">

**Author:** ![bearsan](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bearsan/32/3365_2.png) [@bearsan](https://fenicsproject.discourse.group/u/bearsan)\
**Post date:** [January 17, 2022, 12:40am UTC](https://fenicsproject.discourse.group/t/fail-to-run-the-demo-code-in-parallel-with-mpi/7486/4 "2022-01-17T00:40:41Z")

</div>

Thank you so much for your reply. I tried this command and got the same output as yours

```auto
mpirun -np 2 python3 -c "from mpi4py import MPI; print(MPI.COMM_WORLD.rank)"

```

```auto
0
1

```

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [January 17, 2022, 8:41am UTC](https://fenicsproject.discourse.group/t/fail-to-run-the-demo-code-in-parallel-with-mpi/7486/5 "2022-01-17T08:41:59Z")

</div>

The reason for the code not speeding up is that the problem is very small (1000 DOFS in velocity space and 100 in the pressure space). Running code in parallell is useful for large problems, as the mesh is partitioned and distributed over more processes. For small problems such as this, the communication will take as much time as the speed-up of the partitioning.  
This can be illustrated by refining the mesh:

```python
for i in range(2):
    mesh = refine(mesh)

```

which will yield the following output:

```bash
fenics@3d1c51f37d8c:/root/shared/navier-stokes$ time sudo mpirun -n 1 python3 demo_navier-stokes.py 
15170 1937

real 0m21.718s
user 1m20.987s
sys 4m13.590s
fenics@3d1c51f37d8c:/root/shared/navier-stokes$ time sudo mpirun -n 2 python3 demo_navier-stokes.py 
real 0m10.502s
user 0m20.699s
sys 0m1.968s
fenics@3d1c51f37d8c:/root/shared/navier-stokes$ time sudo mpirun -n 4 python3 demo_navier-stokes.py 
real 0m7.383s
user 0m28.660s
sys 0m2.509s

```

As you can observe here, going from one to two processes gives you a significant speedup. However, as we go from 2 to 4 processes, we see that the runtime is decreased, but not halfed, as the number of dofs on each process decreases.

---

<div class="post-metadata">

**Author:** ![bearsan](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/bearsan/32/3365_2.png) [@bearsan](https://fenicsproject.discourse.group/u/bearsan)\
**Post date:** [January 17, 2022, 9:15am UTC](https://fenicsproject.discourse.group/t/fail-to-run-the-demo-code-in-parallel-with-mpi/7486/6 "2022-01-17T09:15:43Z")

</div>

Thank you very much for your help! Based on your refinement suggestion, I can observe the speedup in parallel now.
