将MPI通信器从Fortran传递到C / C ++之后,并检查大小通信器后,导致以下错误消息:
MPI_COMM_WORLD上的MPI_Comm_size发生错误 ... MPI_ERR_COMM:无效的沟通者
尽管C / C ++端的通信器不是MPI_COMM_NULL
,但看起来也不是有效的。下面是产生错误消息的代码。
C ++
extern "C"
{
MPI_Comm* f_MPI_Comm_f2c(MPI_Fint f_handle)
{
MPI_Comm* comm;
comm = (MPI_Comm*)malloc(sizeof(MPI_Comm));
*comm = MPI_Comm_f2c(f_handle);
assert(*comm != MPI_COMM_NULL);
int size;
MPI_Comm_size(*comm, &size);
std::cout << "size: " << size << std::endl;
return comm;
}
}
Fortran模块
module mymodule
use mpi
use, intrinsic :: ISO_C_Binding, only: c_ptr, c_null_ptr
implicit none
private
interface
function f_MPI_Comm_f2c(comm) result(optr) bind(C, name="f_MPI_Comm_f2c")
import c_ptr
implicit none
integer, intent(in) :: comm
type(c_ptr) :: optr
end function f_MPI_Comm_f2c
end interface
type(c_ptr), save :: ccomm = c_null_ptr
public :: CreateCcomm
CONTAINS
subroutine CreateCcomm(com)
!type(c_ptr) :: ccomm
integer, intent(in) :: com
ccomm = f_MPI_Comm_f2c(com)
end subroutine CreateCcomm
end module mymodule
Fortran驱动程序
program main
use mpi
use mymodule, only : CreateCcomm
IMPLICIT NONE
integer error
call MPI_Init(error)
call CreateCcomm(MPI_COMM_WORLD)
call MPI_Finalize (error)
end