如何使用iso_c_binding将MPI通信器句柄从Fortran传递到C

Fed*_*ini 5 c fortran mpi fortran2003 fortran-iso-c-binding

我正在尝试将并行 MPI Fortran 程序链接到也使用 MPI 的并行 C 库。软件架构以 Fortran 为中心,所以我试图尽可能多地保留 Fortran 方面的内容。

所以我需要将 C 例程传递给 MPI 通信器的句柄。它们的形式

  int CFunction(int *something, MPI_Comm *Ccomm)
Run Code Online (Sandbox Code Playgroud)

MPI 带有将 Fortran 转换为 C 通信器句柄的接口:

MPI_Comm MPI_Comm_f2c(MPI_Fint comm)
Run Code Online (Sandbox Code Playgroud)

但是,这些例程应该从 C 调用,所以现在我必须添加 C 包装函数,我可以将 Fortran 通信器传递给:

int CFunction_FMPI(int *something, MPI_Fint *Fcomm)
{   MPI_Comm Ccomm; int status;
    Ccomm = MPI_Comm_f2c(*Fcomm); // Convert Fortran->C communicator
    status = CFunction(*something,*Ccomm); // Call original function
    return status;
}
Run Code Online (Sandbox Code Playgroud)

然后我不得不编写第二个接口——CFunction_FMPI使用 Fortran 的 C 绑定来允许它从 Fortran 调用。

我的问题是:有没有更好的方法可以做到这一点,即避免使用 Fortran->C 通信器转换的 C 包装器?我认为MPI_Comm_f2c直接从 Fortran调用并将结果存储在 atype(c_ptr)integer(c_int)变量中是最好的,但我无法做到这一点,因为MPI_Comm类型和 Fortran之间没有直接/通用的绑定。

Vla*_*r F 4

不,我认为没有更好的方法可以做到这一点。而且事情并没有那么复杂,我会担心。你可以看看我使用的类似功能

https://github.com/LadaF/PoisFFT/blob/master/src/f_mpi_comm_c2f.c

它与你的方向相反CFunction_FMPI,只是翻译通讯器。它是从C到Fortran。

// This function is callable from Fortran. MPI_Comm_c2f itself may be just a macro.

MPI_Fint f_MPI_Comm_c2f(MPI_Comm *comm) {
  return MPI_Comm_c2f(*comm);
}
Run Code Online (Sandbox Code Playgroud)

它在 Fortran 中被称为

interface
    integer function MPI_Comm_c2f(c_handle) bind(C, name="f_MPI_Comm_c2f")
      use iso_c_binding
      type(c_ptr), value :: c_handle
    end function
end interface
Run Code Online (Sandbox Code Playgroud)

重要的一点是,MPI_Comm_c2f在某些 MPI 库中是 C 宏,而不是函数,因此您不能真正从 Fortran 中调用它。我非常确定MPI_Comm_f2c也可以是宏,因此您不能从 Fortran 中调用它。


您可以做的是创建一个 Fortran 函数,该函数仅调用 C 包装器MPI_Comm_f2c,而不是使用以下bind(C)接口在 Fortran 中调用 C 函数:

status = CFunction(something, c_comm)
Run Code Online (Sandbox Code Playgroud)

从而避免为每个 C 函数创建包装器。您只需要一个 Fortran 接口块即可。

问题是你在 Fortran* 中没有MPI_Comm(实际上它是一个指针或一个 int),所以你必须使用一个不透明的指针。

MPI_Comm* f_MPI_Comm_f2c(MPI_Fint Fcomm)
{   MPI_Comm* Ccomm;
    Ccomm = malloc(sizeof(MPI_Comm));
    *Ccomm = MPI_Comm_f2c(Fcomm);
    return Ccomm;
}
Run Code Online (Sandbox Code Playgroud)

它返回一个不透明的指针type(c_ptr)。(检查潜在的 C 编码错误,我什至忘记使用分号。)

您只需将 Fortran 通信器转换为指向 C 通信器的指针即可。

 type(c_ptr) :: c_comm
 c_comm = f_MPI_Comm_f2c(comm)
Run Code Online (Sandbox Code Playgroud)

* MPI-3 中有一个派生类型type(MPI_Comm),但它包含一个整数组件,无论如何都必须通过转换例程进行转换。