首页 > 解决方案 > 将 MPI_Comm 从 Fortran 传递到 C/C++ 会导致分段错误

问题描述

我正在尝试将 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;
}

标签: c++fortranmpiinterop

解决方案


推荐阅读