简体   繁体   English

使用OpenMPI从Fortran调用C ++

[英]Calling C++ from Fortran with OpenMPI

I am having a compile-time issue which I have reduced to the following test case. 我有一个编译时问题,我已经减少到以下测试用例。 I wish to call a C++ routine from fortran and have the C++ routine be MPI aware. 我想从fortran调用一个C ++例程,让C ++例程知道MPI。

Consider the following sample code, 请考虑以下示例代码,

Fortran main: Fortran主要:

! -- main.f90
program main
   implicit none
   external return_three
   integer return_three

   write(*,*) return_three()
end program main

C++ subroutine: C ++子程序:

// -- subs.cpp
#include <mpi.h>

extern "C"
{
   int return_three_();
}

int return_three_()
{
   return 3;
}

Note that, for the problem to reproduce, I only need to include mpi.h . 注意,为了重现问题,我只需要包含mpi.h

Compiling with GCC 5.3 and OpenMPI 1.10.1 (I checked GCC 4.8 and PGI 15.10 too) gives the following problem during linking: 使用GCC 5.3和OpenMPI 1.10.1进行编译(我也检查了GCC 4.8和PGI 15.10)在链接期间出现以下问题:

% mpic++ -c subs.cpp
% mpifort -c main.f90
% mpifort -o main subs.o main.o -lstdc++ -lgcc_s
subs.o: In function `MPI::Intracomm::Intracomm()':
subs.cpp:(.text._ZN3MPI9IntracommC2Ev[_ZN3MPI9IntracommC5Ev]+0x14): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Intracomm::Intracomm(ompi_communicator_t*)':
subs.cpp:(.text._ZN3MPI9IntracommC2EP19ompi_communicator_t[_ZN3MPI9IntracommC5EP19ompi_communicator_t]+0x19): undefined reference to `MPI::Comm::Comm()'
subs.o: In function `MPI::Op::Init(void (*)(void const*, void*, int, MPI::Datatype const&), bool)':
subs.cpp:(.text._ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb[_ZN3MPI2Op4InitEPFvPKvPviRKNS_8DatatypeEEb]+0x24): undefined reference to `ompi_mpi_cxx_op_intercept'
subs.o:(.rodata._ZTVN3MPI3WinE[_ZTVN3MPI3WinE]+0x48): undefined reference to `MPI::Win::Free()'
subs.o:(.rodata._ZTVN3MPI8DatatypeE[_ZTVN3MPI8DatatypeE]+0x78): undefined reference to `MPI::Datatype::Free()'
collect2: error: ld returned 1 exit status

It seems to me like mpifort is missing some C++-related libraries. 在我看来,像mpifort缺少一些与C ++相关的库。 It's my understanding that mpifort should be used to compile a fortran main program, though. 据我所知,mpifort应该用于编译fortran主程序。 The problem doesn't occur with Intel 16.0 compiled against OpenMPI 1.10.1. 针对OpenMPI 1.10.1编译的Intel 16.0不会出现此问题。

My questions are: 我的问题是:

  • What's going on here? 这里发生了什么? Why is Intel able to handle this sample code and PGI/GCC is not? 为什么英特尔能够处理这个示例代码而PGI / GCC不能处理?
  • Is there a portable way to include C++ subroutines with MPI in a fortran code? 有一种可移植的方法在Fortran代码中包含带有MPI的C ++子例程吗?
  • (if possible) Is there an easy way to fix my current problem? (如果可能的话)有没有简单的方法来解决我当前的问题? I'm trying to compile a package on my machine, so it'd be best if I could just add -lmagicfix or something. 我正在尝试在我的机器上编译一个包,所以如果我可以添加-lmagicfix或其他东西最好。

I was able to compile your code with GCC 5.3.0 and openMPI 1.10.2 by adding -lmpi_cxx in the final step: 通过在最后一步添加-lmpi_cxx,我能够使用GCC 5.3.0和openMPI 1.10.2编译代码:

% mpic++ -c subs.cpp
% mpifort -c main.f90
% mpifort -o main main.o subs.o -lstdc++ -lmpi_cxx

The reason is that the openMPI wrapper compilers mpifort and mpic++ link to different MPI libraries. 原因是openMPI包装器编译器mpifort和mpic ++链接到不同的MPI库。 You can check this with the -showme:libs option: 您可以使用-showme:libs选项进行检查:

% mpifort -showme:libs
mpi_usempif08 mpi_usempi_ignore_tkr mpi_mpifh mpi
% mpic++ -showme:libs
mpi_cxx mpi

So in order to use the C++ MPI library, you have to tell mpifort explicitly to link to it. 因此,为了使用C ++ MPI库,您必须明确告诉mpifort链接到它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM