我想要做的基本上是将一组8个处理器划分为4个子组,从而划分4个子通信器,这样每个通信器中的处理器将具有id 0和1.这是我得到的输出
rank = 0 newrank = 0
rank = 2 newrank = 0
rank = 6 newrank = 0
rank = 4 newrank = -32766
rank = 7 newrank = 1
rank = 5 newrank = -32766
rank = 3 newrank = 1
rank = 1 newrank = 1
预期输出
rank = 0 newrank = 0
rank = 2 newrank = 0
rank = 6 newrank = 0
rank = 4 newrank = 0
rank = 7 newrank = 1
rank = 5 newrank = 1
rank = 3 newrank = 1
rank = 1 newrank = 1
正如您所看到的那样,它达到了预期的结果,但我无法确定导致负值的原因。它似乎打印未初始化的变量或没有设置数据。任何帮助表示赞赏?
#include "mpi.h"
#include <stdio.h>
#include <stdlib.h>
int NPROCS = 8; //trying on 8 processors
main(int argc, char *argv[]) {
int rank, new_rank, numtasks;
MPI_Group orig_group, new_group;
MPI_Comm new_comm;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &numtasks);
/* Extract the original group handle */
MPI_Comm_group(MPI_COMM_WORLD, &orig_group);
int i;
//need two iterations to split the blocks into 8 sub blocks...
//first iteration ranks assignment 0 1 2 3 0 1 2 3
//second iteration ranks assignment 0 1 0 1 0 1 0 1
for(i=0; i<2; i++){
/* Divide tasks into two distinct groups based upon rank */
if (rank < NPROCS/2) {
int j;
int ranks[8] = {0};
//list of ranks for that sub group
for(j=0;j<NPROCS/2;j++){
ranks[j] = j;
}
MPI_Group_incl(orig_group, NPROCS/2, ranks, &new_group);
}
else {
int j;
int ranks[8] = {0};
//list of ranks for that sub group
for(j=NPROCS/2;j<NPROCS;j++){
ranks[j-NPROCS/2] = j;
}
MPI_Group_incl(orig_group, NPROCS/2, ranks, &new_group);
}
/* Create new new communicator and then perform collective communications */
MPI_Comm_create(MPI_COMM_WORLD, new_group, &new_comm);
MPI_Group_rank (new_group, &new_rank);
//here i assign the new_group identifier to orig_group so that in next iteration
//MPI_Group_incl(orig_group, NPROCS/2 ... can use the new_group as orig_group
orig_group = new_group;
NPROCS = NPROCS / 2; // divides NPROCS to half
rank = new_rank;
}
printf("rank= %d newrank= %d \n",rank,new_rank);
MPI_Finalize();
}
答案 0 :(得分:0)
在第二次迭代中,初始排名为0和1的进程属于if
的第一个分支(可以),所有其他进程进入else
分支(不是)。
因此,初始等级为0和1的进程成功获得其组;具有初始等级2,3,6,7的过程具有组等级2和3并且还在第二次迭代中得到它们的组;但是具有初始等级4和5的过程在第二次迭代组中具有等级0和1并且不包括在任何组中。因此负面排名。
如果您在循环中使用new_rank
而不是rank
(在循环之前分配new_rank = rank;
),您的代码可能会有效。
虽然我建议更直接地创建组:计算边界然后创建组(除非你真的需要一个分层组结构)。