c++ - 将 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;
}
解决方案
推荐阅读
- ios - 将文本字段留空 - 崩溃 - swift 4
- node.js - Mongodb聚合使用匹配与查找
- perl - 避免在 perl 循环的最后一行打印新行
- sql - SQL Server OPENJSON 返回最后一个元素为 null
- reactjs - Semantic UI React 模态的奇怪位置
- javascript - 为什么我得到 TypeError X is not a function
- c - `*(volatile T*)0x1234;` 是否保证翻译成读指令?
- php - Laravel 5 - Accessing collection values in view
- postgresql - 即使提供了所需的网络设置,也无法通过 docker 连接到 Postgresql
- r - 如何用 lmer 混合模型表示成对观察