【发布时间】:2026-02-09 12:55:01
【问题描述】:
我正在尝试查看是否可以将 c++ 代码中的数组内容发送到 fortran 90 代码。我正在使用使用 intel 11.1.072 编译器构建的 openmpi 1.4.3。它们安装在 Linux 版本 2.6.18-108chaos (mockbuild@chaos4builder1) (gcc 版本 4.1.2 20080704 (Red Hat 4.1.2-48)) 上。
这里是c++端:
# include <cstdlib>
# include <iostream>
# include <mpi.h>
using namespace std;
void printarray (float arg[], int length) {
for (int n=0; n<length; n++)
cout << arg[n] << " ";
cout << "\n";
}
int main(int argc, char *argv[] ){
float a[10];
int myrank,i;
MPI::Init ( argc, argv );
myrank=MPI::COMM_WORLD.Get_rank();
cout << "rank "<<myrank<<" is c++ rank."<<std::endl;
for (i=0;i<10;i++){
a[i]=10.0;
}
printarray(a,10);
MPI::COMM_WORLD.Send(&a[0],1,MPI::DOUBLE_PRECISION,1,100);
MPI::Finalize();
}
这是 f90 的一面:
program main
implicit none
include "mpif.h"
integer:: ierr,stat(MPI_STATUS_SIZE)
real(8):: a(10)
call mpi_init(ierr)
a=0
print*,a
call mpi_recv(a(1),10,MPI_DOUBLE_PRECISION,0,100,MPI_COMM_WORLD,stat,ierr)
print*,a
call mpi_finalize(ierr)
end program
编译完这两个代码后,我运行
$mpirun -n 1 *c_executable* : -n 1 *fortran_executable* > output
我在 fortran 方面得到的数字不是 10.0。
【问题讨论】:
-
不要使用 MPI C++ 绑定!它们在 MPI 3.0 规范的第 2 版草案中被删除,这种更改很可能使其成为最终的 3.0 标准。请改用 C 绑定 - 它们与 Fortran 的绑定非常相似。还要使用 Fortran 中的 MPI 模块接口:
USE mpi而不是include "mpif.h"。