将MPI_Comm从Fortran传递到C / C ++会导致分段错误

时间:2019-07-04 12:29:37

标签: c++ fortran mpi interop

我正在尝试将MPI通信器从Fortran传递到C / C ++。我不确定要传递哪种类型的句柄:type(MPI_Comm)MPI_Fint,或者仅仅是整数。我尝试了上述选项的几种组合,但无法编译或出现分段错误。以下代码会产生错误消息:

  

程序接收到的信号SIGSEGV:分段错误-无效的存储器   参考。

界面

module Tailor_module

use mpi
use, intrinsic :: ISO_C_Binding, only: C_int, C_double, C_char, c_associated
use, intrinsic :: ISO_C_Binding, only: c_ptr, C_NULL_ptr

implicit none
private

interface

    function Tailor__getobject(comm) result(optr) bind(C, name="Tailor__getobject")
        import c_ptr
        implicit none
        integer, intent(in), value :: comm
        type(c_ptr) :: optr
    end function Tailor__getobject

end interface

type(c_ptr), save :: obj = c_null_ptr

public :: Create

CONTAINS

    subroutine Create(com)
        integer, intent(in) :: com
        obj = Tailor__getobject(comm=com)
        return
    end subroutine Create

end module Tailor_module

Fortran驱动程序

program main

use mpi
use Tailor_module, only : Create
IMPLICIT NONE

integer error

call MPI_Init(error)
call Create(MPI_COMM_WORLD);
call MPI_Finalize (error)

end

C ++

typedef void * OpaqueObject;

extern "C"
{
    OpaqueObject Tailor__getobject(MPI_Fint *f_handle);
}

OpaqueObject Tailor__getobject(MPI_Fint *f_handle)
{
    MPI_Comm comm;
    comm = MPI_Comm_f2c(*f_handle); 
    OpaqueObject s;
    return s;
}

0 个答案:

没有答案