为何MPI_Comm_split函数无法将进程拆分为不同子通信器?
MPI_Comm_split拆分通信器后所有进程显示相同句柄值的原因分析
问题场景
在Windows系统中使用MSMPI运行一段MPI代码,意图通过MPI_Comm_split将4个进程按奇偶分为两个子通信器(进程0、2为一组,进程1、3为一组),但输出显示所有进程所属子通信器的句柄值均为84000000,误以为未成功拆分,求原因。
原代码
#include <stdio.h> #include <stdlib.h> #include <mpi.h> //Please run this program with 4 processes int main(int argc, char* argv[]) { MPI_Init(&argc, &argv); // Check that 4 MPI processes are used int comm_size; MPI_Comm_size(MPI_COMM_WORLD, &comm_size); if (comm_size != 4) { printf("This application is meant to be run with 4 MPI processes, not %d.\n", comm_size); MPI_Abort(MPI_COMM_WORLD, EXIT_FAILURE); } // Get my rank in the global communicator int my_rank; MPI_Comm_rank(MPI_COMM_WORLD, &my_rank); // Determine the colour and key based on whether my rank is even. int colour; int key; if (my_rank % 2 == 0) { colour = 0; key = my_rank; } else { colour = 1; key = comm_size - my_rank; } // Split the global communicator MPI_Comm new_comm; MPI_Comm_split(MPI_COMM_WORLD, colour, key, &new_comm); // Get my rank in the new communicator int my_new_comm_rank; MPI_Comm_rank(new_comm, &my_new_comm_rank); // Print my new rank and new communicator printf("I am process %d and I belong to %x\n", my_rank, new_comm); MPI_Finalize(); return EXIT_SUCCESS; }
程序输出
I am process 3 and I belong to 84000000 I am process 1 and I belong to 84000000 I am process 2 and I belong to 84000000 I am process 0 and I belong to 84000000
原因分析
你误解了MPI_Comm类型变量的意义:
MPI_Comm是MPI库定义的抽象句柄,不是内存地址。在MSMPI实现中,不同进程里对应同一逻辑子通信器的句柄值完全可能相同,这是正常的实现行为,不代表通信器没有拆分成功。- 你的代码逻辑本身没有问题,
MPI_Comm_split已经成功将进程按奇偶分成了两个子通信器,只是你用来验证的方式不对。
验证拆分成功的方法
要确认通信器是否拆分成功,应该打印每个进程在子通信器内的秩和子通信器的大小,而不是句柄值。修改代码的打印部分:
// 获取子通信器的大小 int new_comm_size; MPI_Comm_size(new_comm, &new_comm_size); // 打印验证信息 printf("全局进程%d | 子通信器句柄%x | 子通信器内秩%d | 子通信器大小%d\n", my_rank, new_comm, my_new_comm_rank, new_comm_size);
修改后的输出会类似:
全局进程3 | 子通信器句柄84000000 | 子通信器内秩0 | 子通信器大小2 全局进程1 | 子通信器句柄84000000 | 子通信器内秩1 | 子通信器大小2 全局进程2 | 子通信器句柄84000000 | 子通信器内秩1 | 子通信器大小2 全局进程0 | 子通信器句柄84000000 | 子通信器内秩0 | 子通信器大小2
从输出可以看到:
- 进程0、2的子通信器大小为2,内部秩分别是0和1,属于同一组;
- 进程1、3的子通信器大小为2,内部秩分别是1和0,属于另一组;
- 这说明
MPI_Comm_split已经成功完成了拆分。
内容的提问来源于stack exchange,提问作者user19470144
相关产品推荐
相关产品推荐

