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

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

If you can’t run the demos in a distributed manner with the command you quote, there’s likely something wrong with your MPI setup.

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.

1 Like

Looks fine. You can also just run:

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

and make sure you see

0
1

or

1
0

Yes, I see

1
0

when I run the command you have suggested.

Then DOLFIN should be distributing problems as you expect.

1 Like

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

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

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

with

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).

1 Like

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