Doldinx / Petsc using more cores than allocated via "mpirun -n XX"

I have installed dolfinx via conda and run it from terminal doing:
mpirun -n 20 python3 filename.py
Inside the script, I set:
import os
os.environ['MKL_NUM_THREADS'] = '1'
os.environ['NUMEXPR_NUM_THREADS'] = '1'
os.environ['OMP_NUM_THREADS'] = '1'
My problem is that the computation ends up always using more than the number of threads I assigned and uses all available cores.
Thanks for your help.

It’s hard to imagine anyone will be able to help, considering that there are (almost) no details on how you installed dolfinx, it’s impossible to access your machine to debug, and it’s unclear how is it that you measure the number of actually used cores.

To fix this, make sure the following two lines of code shows up in the begining of your main function script (here, your filename.py):

import os
os.environ['OMP_NUM_THREADS'] = '1'



# Then you can add other things hereafter, for example:
os.environ['MKL_NUM_THREADS'] = '1'
os.environ['NUMEXPR_NUM_THREADS'] = '1'
1 Like

Hi Francesco,
As I said, I installed dolfinx 0.7 via conda, conda install conda-forge::fenics-dolfinx.
I check the number of cores being used with htop on a Ubuntu 23.10 system.
The problem being solved is a simple Laplace equation taken from Dolfinx documentation.
I suspect Petsc using more than the allocated number of threads.
The lines where I solve the linear system are:

problem = LinearProblem(a, L, bcs=bcs, petsc_options=petsc_options)
self.u = problem.solve()

Thanks for your time. I really appreciate it.