简单MPI单边通信代码创建RMA窗口时出现段错误求助
问题
编写使用单边通信(RMA)的MPI代码,通过MPI_Win_create创建窗口,代码逻辑和输出结果均正常,但总有一个进程出现段错误。已将问题复现到如下测试代码:
#include <stdio.h> #include <mpi.h> #include <boost/mpi.hpp> #include <boost/mpi/collectives.hpp> #include <boost/serialization/serialization.hpp> #include <boost/serialization/vector.hpp> int main(int argc, char *argv[]) { boost::mpi::environment env(argc, argv); boost::mpi::communicator world; printf("init\n"); int * arr = new int[100]; for(int i=0;i<100 ; i++) { arr[i]=i+world.rank()*100; } MPI_Win win; printf("create window\n"); MPI_Win_create(arr, MPI_Aint(100*sizeof(int)), sizeof(int),MPI_INFO_NULL, MPI_COMM_WORLD, &win); MPI_Win_free(&win); printf("done\n"); delete [] arr; return 0; }
所有进程都能正确输出打印语句,但仍有进程触发段错误,错误信息如下:
[gpu023:32302:0:32318] Caught signal 11 (Segmentation fault: address not mapped to object at address 0x2b194396e760) -------------------------------------------------------------------------- Primary job terminated normally, but 1 process returned a non-zero exit code. Per user-direction, the job has been aborted. -------------------------------------------------------------------------- -------------------------------------------------------------------------- mpirun noticed that process rank 0 with PID 0 on node gpu023 exited on signal 11 (Segmentation fault). --------------------------------------------------------------------------
使用2个节点共64个进程运行代码,移除MPI_Win相关语句后代码可无错误运行,请求排查问题原因。
问题排查与解决
核心原因:原生MPI与Boost.MPI的环境管理冲突
你同时初始化了Boost.MPI的boost::mpi::environment和原生MPI调用,两者对MPI环境的底层管理逻辑存在冲突。Boost.MPI会自行完成MPI环境的初始化与终止,而原生MPI的MPI_Win_create、MPI_Win_free等调用脱离了Boost.MPI的环境管理范围,导致内存访问异常。解决方法
- 统一使用Boost.MPI的RMA接口:替换原生MPI窗口逻辑,改用Boost.MPI提供的
boost::mpi::window类,由Boost统一管理资源生命周期,避免冲突。修改后的核心代码示例:boost::mpi::window win(world, arr, 100*sizeof(int), sizeof(int)); // 无需手动调用Win_free,Boost.MPI会自动处理资源释放 - 完全使用原生MPI:如果坚持用原生MPI,移除所有Boost.MPI相关的头文件和初始化代码,直接调用
MPI_Init和MPI_Finalize管理MPI环境。
- 统一使用Boost.MPI的RMA接口:替换原生MPI窗口逻辑,改用Boost.MPI提供的
额外验证点
确认Boost.MPI编译时依赖的MPI库版本与运行时使用的MPI库版本一致,版本不匹配也可能引发内存访问错误。
内容的提问来源于stack exchange,提问作者Yash
相关产品推荐
相关产品推荐

