我有一个Fortran代码设计与默认通信器MPI_COMM_WORLD一起运行,但我打算只用几个处理器运行它.我有另一个代码,用于MPI_comm_split获取另一个通信器MyComm.这是一个整数,3当我打印它的值时我得到了.现在我在我的Fortran代码中调用一个C函数来获得对应的等级和大小MyComm.但我在这里面临几个问题.
在Fortran中,当我打印时MyComm,它的值是3,但是当我在C函数中打印它时,它就变成了17278324.我还打印了MPI_COMM_WORLD的值,它的价值大约是1140850688.我不知道这些价值观是什么意思,为什么MyComm变化的价值呢?
我的代码运行正常并创建可执行文件,但是当我执行它时,我得到了分段错误错误.我gdb以前调试我的代码,过程终止于以下行
程序以信号11,分段故障结束.
#0 0x00007fe5e8f6248c in PMPI_Comm_size (comm=0x107a574, size=0x13c4ba0) at pcomm_size.c:62
62 *size = ompi_comm_size((ompi_communicator_t*)comm);
Run Code Online (Sandbox Code Playgroud)
我注意到MPI_comm_rank给出了相应的排名MyComm,但问题只在于MPI_comm_size.没有这样的问题MPI_COMM_WORLD.所以我无法理解造成这种情况的原因.我检查了我的输入,但我没有得到任何线索.这是我的C代码,
#include <stdio.h>
#include "utils_sub_names.h"
#include <mpi.h>
#define MAX_MSGTAG 1000
int flag_msgtag=0;
MPI_Request mpi_msgtags[MAX_MSGTAG];
char *ibuff;
int ipos,nbuff;
MPI_Comm MyComm;
void par_init_fortran (MPI_Fint *MyComm_r,MPI_Fint*machnum,MPI_Fint *machsize)
{
MPI_Fint comm_in
comm_in=*MyComm_r;
MyComm=MPI_Comm_f2c(comm_in);
printf("my comm is %d \n",MyComm);
MPI_Comm_rank(MyComm,machnum);
printf("my machnum is %d \n ", machnum);
MPI_Comm_rank(MyComm,machsize);
printf("my machnum is %d \n ", machsize);
}
Run Code Online (Sandbox Code Playgroud)
编辑:
我想将MyComm声明为我的C代码中列出的所有函数的全局通信器.但我不知道为什么我的沟通者仍然无效.请注意,MPI例程仅在Fortran中初始化并最终确定,我希望我不必再次在C中初始化它们.我使用以下Fortran代码.
implicit none
include 'mpif.h'
integer :: MyColor, MyCOMM, MyError, MyKey, Nnodes
integer :: MyRank, pelast
CALL mpi_init (MyError)
CALL mpi_comm_size (MPI_COMM_WORLD, Nnodes, MyError)
CALL mpi_comm_rank (MPI_COMM_WORLD, MyRank, MyError)
MyColor=1
MyKey=0
CALL mpi_comm_split (MPI_COMM_WORLD, MyColor, MyKey, MyComm,MyError)
CALL ramcpl (MyComm)
CALL mpi_barrier (MPI_COMM_WORLD, MyError)
CALL MCTWorld_clean ()
CALL mpi_finalize (MyError)
Run Code Online (Sandbox Code Playgroud)
我的子程序ramcpl位于另一个地方
subroutine ramcpl (MyComm_r)
implicit none
integer :: MyComm_r, ierr
.
.
.
CALL par_init_fortran (MyComm_r, my_mpi_num,nmachs);
End Subroutine ramcpl
Run Code Online (Sandbox Code Playgroud)
命令行和输出是,
mpirun -np 4 ./ramcplM ramcpl.in
Model Coupling:
[localhost:31472] *** Process received signal ***
[localhost:31473] *** Process received signal ***
[localhost:31472] Signal: Segmentation fault (11)
[localhost:31472] Signal code: Address not mapped (1)
[localhost:31472] Failing at address: (nil)
[localhost:31473] Signal: Segmentation fault (11)
[localhost:31473] Signal code: Address not mapped (1)
[localhost:31473] Failing at address: (nil)
[localhost:31472] [ 0] /lib64/libpthread.so.0() [0x3120c0f7e0]
[localhost:31472] [ 1] ./ramcplM(par_init_fortran_+0x122) [0x842db2]
[localhost:31472] [ 2] ./ramcplM(__rams_MOD_rams_cpl+0x7a0) [0x8428c0]
[localhost:31472] [ 3] ./ramcplM(MAIN__+0xea6) [0x461086]
[localhost:31472] [ 4] ./ramcplM(main+0x2a) [0xc3eefa]
[localhost:31472] [ 5] /lib64/libc.so.6(__libc_start_main+0xfd) [0x312081ed1d]
[localhost:31472] [ 6] ./ramcplM() [0x45e2d9]
[localhost:31472] *** End of error message ***
[localhost:31473] [ 0] /lib64/libpthread.so.0() [0x3120c0f7e0]
[localhost:31473] [ 1] ./ramcplM(par_init_fortran_+0x122) [0x842db2]
[localhost:31473] [ 2] ./ramcplM(__rammain_MOD_ramcpl+0x7a0) [0x8428c0]
[localhost:31473] [ 3] ./ramcplM(MAIN__+0xea6) [0x461086]
[localhost:31473] [ 4] ./ramcplM(main+0x2a) [0xc3eefa]
[localhost:31473] [ 5] /lib64/libc.so.6(__libc_start_main+0xfd) [0x312081ed1d]
[localhost:31473] [ 6] ./ramcplM() [0x45e2d9]
[localhost:31473] *** End of error message ***
Run Code Online (Sandbox Code Playgroud)
Fortran和C中的句柄不兼容.使用MPI_Comm_f2c https://linux.die.net/man/3/mpi_comm_f2c 和相关的connversion函数.将它作为整数在C和Fortran之间传递,而不是MPI_Comm.