Consider:
import dolfinx
from mpi4py.MPI import COMM_WORLD
mesh=dolfinx.UnitSquareMesh(COMM_WORLD,2,2)
import ufl
FE=ufl.FiniteElement("Lagrange",mesh.ufl_cell(),1)
V=dolfinx.FunctionSpace(mesh,FE)
num_dofs_local = (V.dofmap.index_map.size_local) * V.dofmap.index_map_bs
num_dofs_global = V.dofmap.index_map.size_global * V.dofmap.index_map_bs
print(f"Number of dofs (owned) by rank {COMM_WORLD.rank}: {num_dofs_local}")
if COMM_WORLD.rank ==0:
print(f"Number of dofs global: {num_dofs_global}")
Yielding:
root@81aa206a058d:~/shared# mpirun -n 1 python3 num_dofs.py
Number of dofs (owned) by rank 0: 9
Number of dofs global: 9
root@81aa206a058d:~/shared# mpirun -n 2 python3 num_dofs.py
Number of dofs (owned) by rank 0: 6
Number of dofs global: 9
Number of dofs (owned) by rank 1: 3