opencomm在c语言中的作用,使用OpenMPI从Fortran调用C语言
我有一個編譯時問題,我已經減少到以下測試用例.我希望從fortran調用一個C例程,讓C例程知道MPI.
請考慮以下示例代碼,
Fortran主要:
! -- main.f90
program main
implicit none
external return_three
integer return_three
write(*,*) return_three()
end program main
C子程序:
// -- subs.cpp
#include
extern "C"
{
int return_three_();
}
int return_three_()
{
return 3;
}
請注意,為了重現問題,我只需要包含mpi.h.
使用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
在我看來,像mpifort缺少一些C相關的庫.據我所知,mpifort應該用于編譯fortran主程序.針對OpenMPI 1.10.1編譯的Intel 16.0不會出現此問題.
我的問題是:
>這里發生了什么?為什么英特爾能夠處理這個示例代碼而PGI / GCC不能處理?
>有沒有一種可移植的方法在Fortran代碼中包含帶有MPI的C子程序?
>(如果可能)有一種簡單的方法可以解決我目前的問題嗎?我正在嘗試在我的機器上編譯一個包,所以如果我可以添加-lmagicfix或其他東西最好.
解決方法:
通過在最后一步添加-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
原因是openMPI包裝器編譯器mpifort和mpic鏈接到不同的MPI庫.您可以使用-showme:libs選項進行檢查:
% mpifort -showme:libs
mpi_usempif08 mpi_usempi_ignore_tkr mpi_mpifh mpi
% mpic++ -showme:libs
mpi_cxx mpi
因此,為了使用C MPI庫,您必須明確告訴mpifort鏈接到它.
標簽:openmpi,c,gcc,fortran
來源: https://codeday.me/bug/20190824/1706239.html
總結
以上是生活随笔為你收集整理的opencomm在c语言中的作用,使用OpenMPI从Fortran调用C语言的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: animation css逐渐消除,cs
- 下一篇: mysql 攻击密码_MySQL用户Ro