Skip to content

Fix Fortran mpi inheritance issue

Hello there.

This is a fix for a problem that was previously explained in !2257 (closed), but this time it was made simpler.

What is the problem

The initial problem I faced was that it is impossible to compile a Fortran code using PETSc and the new Fortran interface mpi_f08. This happens due to an inheritance issue in the Fortran modules of PETSc.

Currently the PETSc code is written with everything in Fortran modules as public, therefore the modules are cumulative and the usage of a single one lead to the inheritance of everything else. This can make things easy to use but is dangerous as well.

When a user tries to compile a code with something like:

use petscvec
use mpi_f08

The compile will show an error of ambiguous interface because Fortran MPI interfaces (use mpi_f08 and use mpi) change the basic MPI structures and constants, therefore it's not possible to have both in the same scope. And petscvec has use mpi inherited inside it.

How it was solved

There is nothing wrong with keep using the use mpi old interface inside PETSc.

So the approach I took was to keeping that use mpi inside PETSc with proper private statement. For this to happen, I had to remove the custom MPI definitions of PETSc from src/sys/f90-mod/petscsys.h and put them in a new module named petscmpi. This module contains the use mpi statement but it's also private and the only variables accessed upon using it are MPIU_REAL, MPIU_SUM, MPIU_SCALAR and MPIU_INTEGER.

An unwanted consequence was that CHKERRA definition used MPI_COMM_SELF in it's MPIU_Abort call. And therefore it was impossible to use CHKERRA in a Fortran code without including mpi. To overcome this I changed that MPI_COMM_SELF to PETSC_COMM_SELF. This fixed this unwanted dependecy.

Consequences

By denying the inheritance of use mpi to the user's code, if the user wants to use any MPI constant (such as MPI_SUM, MPI_MAX, MPI_COMM_WORLD ...) they need to include MPI on their own.

Several examples where fixed (added use mpi to them), but some of the examples could be changed to not need use mpi like substituting in src/sys/tutorials/ex1f.F90 the MPI_COMM_WORLD for PETSC_COMM_WORLD.

Risks

This might be a inconvenience for the users initially, since the code will break if mpi was not used explicitly. But I think that things get clearer this way, and the user is free to use whatever mpi interface it wants.

Merge request reports