mpi 通信域划分不完整版

#include "mpi.h"
#include <stdio.h>
#include <unistd.h>
#define nodeNameLength 20

int main(int argc, char  *argv[])
{
    int rank,nproc;   
    MPI_Init( &argc, &argv );
    MPI_Comm_size( MPI_COMM_WORLD, &nproc);
    MPI_Comm_rank( MPI_COMM_WORLD, &rank);


    char currentNodeName[nodeNameLength];  

    if(-1==gethostname(currentNodeName,sizeof(currentNodeName)))
        {
            perror("gethostname error !");
            return;
        }
    char numStr[nodeNameLength];
    int i=-1;//************************just a count variable
    for(i=4;i<strlen(currentNodeName);i++)
        {numStr[i]=currentNodeName[i];//numStr is a string which only includes number
        }
    numStr[i]='\0';

    int num =atoi(numStr);//convert string to number



    // //root process get information of nodes. 
    // MPI_Gather(&num, 1, MPI_BYTE, allName, nproc, MPI_BYTE, 0,MPI_COMM_WORLD);

    // int rankofnode[nproc];   

    // if(0==rank)
    //  {for(i=0;i<nproc;i++)
    //       {              
    //          int noderank=0;             
    //          int j=-1;
    //          for(j=0;j<=i;j++)
    //              {
    //               if(allName[i]==allName[j])
    //                  noderank++;
    //              }
    //          rankofnode[i]=noderank;
    //       }

    //  }   
 //    MPI_Bcast(rankofnode,,MPI_BYTE,0,MPI_COMM_WORLD);


    int allName[nproc];
    MPI_Allgather(&num,1,MPI_INT,allName,nproc,MPI_INT,MPI_COMM_WORLD);
    int key=0;
    for(i=0;i<=rank;i++)
        {
         if(allName[i]==num)
            key++;

        }


    MPI_Comm myWorld,nodeWorld;
    MPI_Comm_dup(MPI_COMM_WORLD,&myWorld);
    MPI_Common_split(myWorld,num,key,&nodeWorld);


    // int members[nproc];
    // int head_num=0;
    // for(i=0;i<nproc;i++)
       //  {if(0==allName[i])
       //   {
       //   members[head_num]=i;//members store the set of head member
       //   head_num++;
       //   }
       //  }
    //need another mpiallgather

    MPI_Group group_world,head_group;
    MPI_Comm head_comm;
    MPI_COMM_group(MPI_COMM_WORLD,&group_world);
    MPI_Group_incl(group_world,head_num,members,&head_group);
    MPI_Comm_create(MPI_COMM_WORLD,head_group,&head_comm);


    //At now ,The node communication and head head communication are created.








    MPI_Finalize();
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值