comm_split奇怪的行为?

时间:2014-03-25 05:31:55

标签: c parallel-processing multiprocessing mpi

我花了几个小时试图解决这个问题,所以我终于来到这里寻求帮助。我正在学习mpi,问题是因为我是新手,我不知道为什么会出现一些奇怪的行为。我想要做的是我有8个数字分布在8个处理器上,如下所示。然后我拆分通信器,然后我从子通信器交换元素如下:

0 1 2 3 4 5 6 7

拆分

0 1 2 3 | 4 5 6 7

交换

2 3 0 1 | 6 7 4 5

输出为4 5 6 7 0 1 2 3但输出应该是2 3 0 1 6 7 4 5.我可以得到一些帮助,说明为什么它会给我这个奇怪的结果吗?

#include <stdio.h>
#include <stdlib.h>
#include <mpi.h>
#include <math.h>
int main(argc,argv)
int argc;
char *argv[];

{
    int myid, numprocs;
    int color,Zero_one,new_id,new_nodes;
    MPI_Comm NEW_COMM; 
    MPI_Init(&argc,&argv);
    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
    MPI_Comm_rank(MPI_COMM_WORLD,&myid);


    int my_num, my_received;

       int old_id;


    switch(myid){

        case 0:
            my_num = 0;
            old_id = 0;

        break;


        case 1:
            my_num = 1;
            old_id = 1;

        break;

        case 2:
            my_num = 2;
            old_id = 2;

        break;

        case 3:
            my_num = 3;
            old_id = 3;

        break;

        case 4:
            my_num = 4;
            old_id = 4;

        break;

        case 5:
            my_num = 5;
            old_id = 5;

        break;

        case 6:
            my_num = 6;
            old_id = 6;

        break;

        case 7:
            my_num = 7;
            old_id = 7;

        break;


    }

//here i split

    color=myid % 2;
    MPI_Comm_split(MPI_COMM_WORLD,color,myid,&NEW_COMM);
    MPI_Comm_rank( NEW_COMM, &new_id);
    MPI_Comm_rank( NEW_COMM, &new_nodes);



  //here i exchange in the sub list or communicator

        if(new_id < 2){

            MPI_Send(&my_num, 1, MPI_INT,  2 + new_id, 0, NEW_COMM);
            MPI_Recv(&my_received, 1, MPI_INT, 2 + new_id, 0, NEW_COMM, MPI_STATUS_IGNORE);         

        }
        else
        {
            MPI_Recv(&my_received, 1, MPI_INT, new_id - 2, 0, NEW_COMM, MPI_STATUS_IGNORE);         
            MPI_Send(&my_num, 1, MPI_INT, new_id - 2 , 0, NEW_COMM);


        }



    printf("old_id= %d received num= %d\n", old_id, my_received);


    MPI_Finalize();

}

1 个答案:

答案 0 :(得分:0)

运行你的程序我得到了:

old_id= 4 received num= 0
old_id= 5 received num= 1
old_id= 1 received num= 5
old_id= 3 received num= 7
old_id= 6 received num= 2
old_id= 7 received num= 3
old_id= 0 received num= 4
old_id= 2 received num= 6

根据你的分歧,所有偶数等级都收集在一个传播者中,所有偶数都收集在另一个传播者中:

old_id= 0 received num= 4
old_id= 2 received num= 6
old_id= 4 received num= 0
old_id= 6 received num= 2

old_id= 1 received num= 5
old_id= 3 received num= 7
old_id= 5 received num= 1
old_id= 7 received num= 3

这看起来像你要求的东西。要么我不能重现你的问题,误解它或什么都没有错?