# Using MPI with FEniCS to distribute computational load; not to spawn multiple instances of same code

**URL:** <https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608>\
**Category:** Uncategorized\
**Created:** [September 17, 2021, 3:25pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608 "2021-09-17T15:25:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![rkailash](https://avatars.discourse-cdn.com/v4/letter/r/848f3c/32.png) [@rkailash](https://fenicsproject.discourse.group/u/rkailash)\
**Post date:** [September 17, 2021, 3:25pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/1 "2021-09-17T15:25:13Z")

</div>

Dear Community

I was reading through the threads on this forum, and found that the simplest way to use MPI is to execute

mpirun -n np python demo.py

where “np” is the number of processors. When I use this option, my code gets executed “np” times. My motivation for using MPI is to split the computational load across several processors so that they work in parallel and the overall execution time (in real time) is reduced.

Could you please guide me to some resources which would help me achieve this? A simple example code would also be helpful.

Thank You  
Warm 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:** [September 17, 2021, 3:30pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/2 "2021-09-17T15:30:06Z")

</div>

If you can’t run the [demos](https://fenicsproject.org/olddocs/dolfin/latest/python/demos.html) in a distributed manner with the command you quote, there’s likely something wrong with your MPI setup.

---

<div class="post-metadata">

**Author:** ![rkailash](https://avatars.discourse-cdn.com/v4/letter/r/848f3c/32.png) [@rkailash](https://fenicsproject.discourse.group/u/rkailash)\
**Post date:** [September 17, 2021, 7:15pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/3 "2021-09-17T19:15:43Z")

</div>

Thank you. Could the fact that I am using “python3” and not “python” be the reason for this behavior? Because I tried running the demo programs using (e.g):

mpirun -n 2 python3 demo\_poisson.py

the contents of the folder, apart from the code, are: poisson.pvd poisson000000.pvtu poisson\_p0\_000000.vtu poisson\_p1\_000000.vtu

If you think that this is erroneous behavior, I will try to figure out if MPI is configured correctly at my end.

---

<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:** [September 17, 2021, 7:38pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/4 "2021-09-17T19:38:13Z")

</div>

Looks fine. You can also just run:

```bash
mpirun -np 2 python3 -c "from dolfin import *; print(MPI.comm_world.rank)"

```

and make sure you see

```auto
0
1

```

or

```auto
1
0

```

---

<div class="post-metadata">

**Author:** ![rkailash](https://avatars.discourse-cdn.com/v4/letter/r/848f3c/32.png) [@rkailash](https://fenicsproject.discourse.group/u/rkailash)\
**Post date:** [September 17, 2021, 8:02pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/5 "2021-09-17T20:02:08Z")

</div>

Yes, I see

```auto
1
0

```

when I run the command you have suggested.

---

<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:** [September 17, 2021, 8:11pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/6 "2021-09-17T20:11:08Z")

</div>

Then DOLFIN should be distributing problems as you expect.

---

<div class="post-metadata">

**Author:** ![conpierce8](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/conpierce8/32/2464_2.png) [@conpierce8](https://fenicsproject.discourse.group/u/conpierce8)\
**Post date:** [September 17, 2021, 8:41pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/7 "2021-09-17T20:41:23Z")

</div>

Are you sure the code is being executed `np` times? The reason you get multiple output files, e.g.

```auto
poisson_p0_000000.vtu
poisson_p1_000000.vtu
...

```

is because the pvd format doesn’t support parallel access, so the result for each distributed portion of the mesh is written to a separate file. If you replace

```auto
file = File("poisson.pvd")
file << u

```

with

```python
file = XDMFFile("poisson.xdmf")
file.write(u)

```

you should see that only one file `poisson.xdmf` is generated (with the corresponding .h5 file, of course).

---

<div class="post-metadata">

**Author:** ![rkailash](https://avatars.discourse-cdn.com/v4/letter/r/848f3c/32.png) [@rkailash](https://fenicsproject.discourse.group/u/rkailash)\
**Post date:** [September 20, 2021, 5:39pm UTC](https://fenicsproject.discourse.group/t/using-mpi-with-fenics-to-distribute-computational-load-not-to-spawn-multiple-instances-of-same-code/6608/8 "2021-09-20T17:39:47Z")

</div>

Thank you @dokken and @conpierce8 for the detailed response.

Yes, using the xdmf option in place of pvd avoids the creation of multiple output files.

Also, I have “print” commands in my FEniCS script which get executed when using MPI, and I supposed that the solver was also being called “n” times. Based on a simple timing run, I can see that using the mpirun command as shown in my question indeed distributes the computational load. I was thrown off a bit by the output from the multiple “print” statements.

Cheers and Regards
