Can I pass MPI.jl communicator to the Fortran side?

You may need to call MPI_Comm_c2f to convert the C-style communicator (used in MPI.jl) to a Fortran-style one. MPI.jl doesn’t provide a high-level interface to this, but you can always wrap it with ccall.

Maybe something like:

import MPI
@static if MPI.MPI_Comm === Cint
    # some MPI libraries don't define MPI_Comm_c2f … they use
    # a Fortran-like integer communicator even in the C api
    comm2f(comm::MPI.Comm) = comm.val
else
    comm2f(comm::MPI.Comm) =
        ccall((:MPI_Comm_c2f, MPI.libmpi), Cint, (MPI.MPI_Comm,), comm)
end

Note also that you pass arguments to Fortran as pointers, i.e. declare the comm argument as Ref{Cint} when passing to Fortran.

(The MPI.MPI_Comm === Cint probably isn’t quite right; you should really check whether MPI_Comm_c2f is defined in MPI.libmpi.)

2 Likes