MPI tree structured communication

假装没事ソ 提交于 2021-02-11 14:41:30

问题


I Wrote this code in order to trace and inspect the communication in MPI model. the idea here is that, if there are for example 8 proceccors, the node-0- will communicate with itself and node-4-. also node-4- communicate with node-6- and node-2- on the other side of tree communicate with node-3-. here is the image of this scheme.

so i wrote the code below to see how nodes pass an Array of elements to each other. the line 31 to 33 doese calculate the parameters of each node. just like binary tree but alittle bit different. here is the problem: when i execute the Code each child node returns to its parent some garbage along side of Array elements. i cant figure out where is the problem . could any one give me a hand ?

#include<stdio.h>
#include<stdlib.h>
#include<mpi.h>
#include<math.h>

void print_array(int arr[], int size)
{
    for (int i = 0; i < size; i++)
        printf("%d ", arr[i]);
    printf("\n");
}
/***********************************************************************/
void copyarray(int a[] ,int start_a , int end_a, int* b, int size_b)
{
    int i = 0;
    for (i = 0; i < size_b;i++)
    {
        b[i] = a[start_a];
        start_a++;
        if (start_a > end_a)
            break;
    }
}
/***************************************************/
void tree (int arr[],int size,int level)
{
    int rank;
    int numOfcores;
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&numOfcores);
    int parent = rank & ~(1 << level);
    int next = level - 1;
    int rightChild = rank | (1 << (level - 1));

         if(level>0)
         {
                printf("\n enterig the divid process\n");
                printf("\n i am the node %d with te Rank of %d and height of %d \n",rank,rank,level);
                 int mid = (int)(size/2);///////////////////////////////////////////////////
                 print_array(arr,size);
                 tree(arr,mid,next);
                 int rightsize=size-mid;////////////////////////////////////////////////////////
                 int* rightArray =(int*)calloc(rightsize,sizeof(int));
                 if(rightArray==NULL)
                 return;
                 copyarray(arr,mid,size,rightArray,rightsize);////////////////////////////////////////
                 int massage[2];
                 massage[0]=next;
                 massage[1]=rightsize;
                 MPI_Send(massage,2,MPI_INT,rightChild,0,MPI_COMM_WORLD);
                 MPI_Send(rightArray,rightsize,MPI_INT,rightChild,1,MPI_COMM_WORLD);   

                 int* recvArray = (int*)calloc(rightsize,sizeof(int));

                 if(parent!=rank)
                 {
                  printf("\n i am the node %d withe height of  %d and i send my array to parent %d ",rank,level,parent);
                  MPI_Send(arr,size,MPI_INT,parent,2,MPI_COMM_WORLD);
                 }
                 MPI_Recv(recvArray,size,MPI_INT,rightChild,2,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
                 printf("\n i am the parent node %d with the height of %d and i recieved form %d the array :\n ",rank,level,rightChild);
                 print_array(recvArray,size);
                 free(rightArray);
                 free(recvArray);
         } 

         else 
         {
            printf("\n i am the node %d with the height of %d and i have the array of : \n ",rank,level);
            print_array(arr,size);
            if(parent!=rank)
             {
                 printf("\n i am the node %d withe height of  %d and i send my array to parent %d ",rank,level,parent);
                  MPI_Send(arr,size,MPI_INT,parent,2,MPI_COMM_WORLD);
             }
            printf("\n");
         }     
}

/*************************************/
int main()
{
    int arr[10]={1,2,3,4,5,6,7,8,9,10};
    int rank;
    int comm_size;
    MPI_Init(NULL,NULL);
    MPI_Comm_rank(MPI_COMM_WORLD,&rank);
    MPI_Comm_size(MPI_COMM_WORLD,&comm_size);

    if(rank==0)
    {
        int rootHeight=0;
        int nodeCount=1;
        while (nodeCount<comm_size)
        {
            nodeCount++;
        }
        rootHeight=(int)log2(nodeCount);
        printf("i am the root with the rank of %d and height of %d\n",rank,rootHeight);
        tree(arr,10,rootHeight);
    }
    else 
    {
        int height;
        int massage[2];
        int newSize;
        MPI_Recv(massage,2,MPI_INT,MPI_ANY_SOURCE,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        height=massage[0];
        newSize=massage[1];
        int* newArray = (int*)calloc(newSize,sizeof(int));
        MPI_Recv(newArray,newSize,MPI_INT,MPI_ANY_SOURCE,1,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        tree(newArray,newSize,height);
        free(newArray);
    }
    MPI_Finalize();
    return 0;

}

when i run the code for 4 processors the out put is :

i am the root with the rank of 0 and height of 2

 enterig the divid process

 i am the node 0 with te Rank of 0 and height of 2 

1 2 3 4 5 6 7 8 9 10 

 enterig the divid process

 i am the node 0 with te Rank of 0 and height of 1 

1 2 3 4 5 

 i am the node 0 with the height of 0 and i have the array of : 

 1 2 


 i am the node 1 with the height of 0 and i have the array of : 

 3 4 5 

 i am the node 1 withe height of  0 and i send my array to parent 0 

 i am the parent node 0 with the height of 1 and i recieved form 1 the array :

 3 4 5 0 0 

 enterig the divid process

 i am the node 2 with te Rank of 2 and height of 1 

6 7 8 9 10 

 i am the node 2 with the height of 0 and i have the array of :

 6 7 



 i am the parent node 0 with the height of 2 and i recieved form 2 the array :

 6 7 8 9 10 0 104993 0 0 0 

 i am the node 3 with the height of 0 and i have the array of : 

 8 9 10 

 i am the node 3 withe height of  0 and i send my array to parent 2 

 i am the node 2 withe height of  1 and i send my array to parent 0 

 i am the parent node 2 with the height of 1 and i recieved form 3 the array :

 8 9 10 0 0

来源:https://stackoverflow.com/questions/62063521/mpi-tree-structured-communication

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!