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

**URL:** <https://discourse.julialang.org/t/can-i-pass-mpi-jl-communicator-to-the-fortran-side/76547>\
**Category:** General Usage\
**Tags:** fortran, mpi\
**Created:** [February 16, 2022, 9:27am UTC](https://discourse.julialang.org/t/can-i-pass-mpi-jl-communicator-to-the-fortran-side/76547 "2022-02-16T09:27:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)\
**Post date:** [February 16, 2022, 9:27am UTC](https://discourse.julialang.org/t/can-i-pass-mpi-jl-communicator-to-the-fortran-side/76547/1 "2022-02-16T09:27:14Z")

</div>

I have a Fortran program that uses File I/O, which I want to modify to a subroutine so that I can directly call it from Julia without writing to / reading from files.

The problem is that the program depends heavily on MPI. After wrapping the main program to a subroutine, I could call it and get it running, but in this way, it could not run in parallel.

I tried using MPI.jl and initiating the communicator at the Julia side and then passing it to the Fortran side, instead of initiating at the Fortran side, but did not make any progress right now.

Are there any suggestions?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2022, 12:46pm UTC](https://discourse.julialang.org/t/can-i-pass-mpi-jl-communicator-to-the-fortran-side/76547/2 "2022-02-16T12:46:44Z")

</div>

You may need to call [`MPI_Comm_c2f`](https://www.open-mpi.org/doc/v4.0/man3/MPI_Comm_c2f.3.php) 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:

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

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [February 16, 2022, 12:53pm UTC](https://discourse.julialang.org/t/can-i-pass-mpi-jl-communicator-to-the-fortran-side/76547/3 "2022-02-16T12:53:26Z")

</div>

> [@lucifer1004](#):
>
> I tried using MPI.jl and initiating the communicator at the Julia side and then passing it to the Fortran side, instead of initiating at the Fortran side, but did not make any progress right now.

Also, make sure that your Fortran code is compiled with the _same version_ of MPI that Julia is using. See [Configuring MPI.jl](https://juliaparallel.github.io/MPI.jl/latest/configuration/).

e.g. you might want to set `ENV["JULIA_MPI_BINARY"]="system"` and re-build MPI.jl.

---

<div class="post-metadata">

**Author:** ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)\
**Post date:** [February 16, 2022, 1:23pm UTC](https://discourse.julialang.org/t/can-i-pass-mpi-jl-communicator-to-the-fortran-side/76547/4 "2022-02-16T13:23:32Z")

</div>

The part of passing `comm` as `Ref{Cint}` turned out to be the key point I missed. Thanks a lot!

---

<div class="post-metadata">

**Author:** ![lucifer1004](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lucifer1004/32/22311_2.png) [@lucifer1004](https://discourse.julialang.org/u/lucifer1004)\
**Post date:** [February 16, 2022, 1:36pm UTC](https://discourse.julialang.org/t/can-i-pass-mpi-jl-communicator-to-the-fortran-side/76547/5 "2022-02-16T13:36:43Z")

</div>

It turned out that I do not need to initiate MPI at the Julia side. I measured the run time wrongly so I thought the program was not running in parallel, but actually, it was already running in parallel.
