Allgather

De Wiki de Calcul Québec
Aller à : Navigation, rechercher
Cette page est une traduction de la page Allgather et la traduction est complétée à 100 % et à jour.

Autres langues :anglais 100% • ‎français 100%

This example shows how to use the MPI_Allgather function, which gathers the date from multiple tasks and distributes the collected data to all tasks. Hence, MPI_Allgather efficiently combines an MPI_Gather and an MPI_Bcast in a single call.

Each task prepares a vector of real random numbers (sendbuff) of size buffsize. Then the function MPI_Allgather puts together the content of all these vectors into the matrix recvbuff on all tasks. Before and after communication the sum for each vector is printed out for verification.

In Fortran

File : allgather.f
!--------------------------------------------------------
!          sendbuff
!
!          ########
!          #      #
!        0 #  AA  #
!          #      #
!          ########
!     T    #      #
!        1 #  BB  #
!     a    #      #
!          ########
!     s    #      #
!        2 #  CC  #                                   BEFORE
!     k    #      #
!          ########
!     s    #      #
!        3 #  DD  #
!          #      #
!          ########
!          #      #
!        4 #  EE  #
!          #      #
!          ########
!          
!            <---------- recvbuff ---------->
!
!          ####################################
!          #      #      #      #      #      #
!        0 #  AA  #  BB  #  CC  #  DD  #  EE  #
!          #      #      #      #      #      #
!          ####################################
!     T    #      #      #      #      #      #
!        1 #  AA  #  BB  #  CC  #  DD  #  EE  #
!     a    #      #      #      #      #      #
!          ####################################
!     s    #      #      #      #      #      #
!        2 #  AA  #  BB  #  CC  #  DD  #  EE  #       AFTER
!     k    #      #      #      #      #      #
!          ####################################
!     s    #      #      #      #      #      #
!        3 #  AA  #  BB  #  CC  #  DD  #  EE  #
!          #      #      #      #      #      #
!          ####################################
!          #      #      #      #      #      #
!        4 #  AA  #  BB  #  CC  #  DD  #  EE  #
!          #      #      #      #      #      #
!          ####################################
!
! Author: Carol Gauthier
!         Centre de Calcul scientifique
!         Universite de Sherbrooke
!
! Last revision: 2004/08/25
!
!--------------------------------------------------------
Program Example_MPI
 
  include 'mpif.h'
  integer ierr,ntasks,taskid,itask,status(MPI_STATUS_SIZE)
  integer i,j,k,buffsize,jtask
  character argtmp*12
  real(8) inittime,recvtime,totaltime,rand,buffsum
 
  real(8),allocatable,dimension(:) :: sendbuff
  real(8),allocatable,dimension(:,:) :: recvbuff
 
  !---------------------------------------------------------------
  ! MPI Initialisation. It's important to put this call at the
  ! beginning of the program, after variable declarations.   
  call MPI_INIT( ierr )
 
  !---------------------------------------------------------------
  ! Get the number of MPI tasks and the taskid of this task.
  call MPI_COMM_SIZE(MPI_COMM_WORLD,ntasks,ierr)
  call MPI_COMM_RANK(MPI_COMM_WORLD,taskid,ierr)
 
  !---------------------------------------------------------------
   ! Get buffsize value from program arguments.
  call getarg(1,argtmp)
  read(argtmp,'(I12)')buffsize
 
  !---------------------------------------------------------------
  ! Printing out the description of the example.
  if ( taskid.eq.0 )then
    write(6,'(A)')
    write(6,'(A)')"##########################################################"
    write(6,'(A)')
    write(6,'(A)')" Example 10"
    write(6,'(A)')
    write(6,'(A)')" Collective Communication : MPI_Allgather"
    write(6,'(A)')
    write(6,'(A,I12)')" Vector size:",buffsize
    write(6,'(A,I5)')" Number of tasks:",ntasks
    write(6,'(A)')
    write(6,'(A)')"##########################################################"
    write(6,'(A)')
    write(6,'(A)')"                --> BEFORE COMMUNICATION <--"
    write(6,'(A)')
  endif
 
  !---------------------------------------------------------------
  ! Memory allocation. 
  allocate( sendbuff(0:buffsize-1) )
  allocate( recvbuff(0:buffsize-1,0:ntasks-1) )
 
  !-----------------------------------------------------------------
  ! Vectors and/or matrices initialisation.
  call srand(taskid*10)
  do i=0,buffsize-1
    sendbuff(i)=rand()
  end do
 
  !-----------------------------------------------------------------
  ! Print out before communication.
 
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
  buffsum=0.0
  do i=0,buffsize-1
    buffsum=buffsum+sendbuff(i)
  end do
 
  write(6,'(A,I3,A,E14.8)')"Task",taskid,": Sum of vector = ",buffsum
 
  call MPI_Barrier(MPI_COMM_WORLD,ierr)   
 
  !-----------------------------------------------------------------
  ! Communication
 
  inittime = MPI_Wtime()
 
  call MPI_Allgather(sendbuff,buffsize,MPI_REAL8, &
  &                  recvbuff,buffsize,MPI_REAL8, &
  &                  MPI_COMM_WORLD,ierr)
 
  totaltime = MPI_Wtime()
 
  !-----------------------------------------------------------------
  ! Print out after communication.
  if(taskid.eq.0)then
    write(6,*)
    write(6,'(A)')"##########################################################"
    write(6,*)
    write(6,'(A)')"                --> AFTER COMMUNICATION <--"
    write(6,*)
  end if
  do jtask=0,ntasks-1
    call MPI_Barrier(MPI_COMM_WORLD,ierr)
    if(taskid.eq.jtask)then
      write(6,*)
      do itask=0,ntasks-1
        buffsum=0.0
        do i=0,buffsize-1
          buffsum=buffsum+recvbuff(i,itask)
        end do
        write(6,'(A,I3,A,I3,A,E14.8)')"Task ",taskid,": Sum of vector received from task ",itask, &
        &                             "= ",buffsum
      end do
    end if
  end do
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
  if(taskid.eq.0)then
    write(6,*)
    write(6,'(A)')"##########################################################"
    write(6,'(A,F5.2,A)')" Total communication time : ",totaltime," seconds"
    write(6,'(A)')"##########################################################"
    write(6,*)
  end if
 
  !-----------------------------------------------------------------
  ! Free the allocated memory
  deallocate(sendbuff)
  deallocate(recvbuff)
 
  !-----------------------------------------------------------------
  ! MPI finalisation
 
  call MPI_FINALIZE( ierr )
end


In C

File : allgather.c
/*--------------------------------------------------------
          sendbuff
 
          ########
          #      #
        0 #  AA  #
          #      #
          ########
     T    #      #
        1 #  BB  #
     a    #      #
          ########
     s    #      #
        2 #  CC  #                                   BEFORE
     k    #      #
          ########
     s    #      #
        3 #  DD  #
          #      #
          ########
          #      #
        4 #  EE  #
          #      #
          ########
 
            <---------- recvbuff ---------->
 
          ####################################
          #      #      #      #      #      #
        0 #  AA  #  BB  #  CC  #  DD  #  EE  #
          #      #      #      #      #      #
          ####################################
     T    #      #      #      #      #      #
        1 #  AA  #  BB  #  CC  #  DD  #  EE  #
     a    #      #      #      #      #      #
          ####################################
     s    #      #      #      #      #      #
        2 #  AA  #  BB  #  CC  #  DD  #  EE  #       AFTER
     k    #      #      #      #      #      #
          ####################################
     s    #      #      #      #      #      #
        3 #  AA  #  BB  #  CC  #  DD  #  EE  #
          #      #      #      #      #      #
          ####################################
          #      #      #      #      #      #
        4 #  AA  #  BB  #  CC  #  DD  #  EE  #
          #      #      #      #      #      #
          #################################### 
 
 Author: Carol Gauthier
         Centre de Calcul scientifique
         Universite de Sherbrooke
 
 Derniere revision: Septembre 2005
--------------------------------------------------------*/
 
#include <malloc.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "math.h"
#include "mpi.h"
 
int main(int argc,char** argv)
{
   /*===============================================================*/
   /* Declaration of variables                                      */
   int          taskid, ntasks;
   MPI_Status   status;
   int          ierr,i,j,itask,jtask;
   int	        buffsize;
   double       *sendbuff,**recvbuff,buffsum;
   double       inittime,totaltime;
 
   /*===============================================================*/
   /* MPI Initialisation. It is important to put this call at the   */
   /* beginning of the program, after variable declarations.        */
   MPI_Init(&argc, &argv);
 
   /*===============================================================*/
   /* Get the number of MPI tasks and the taskid of this task.      */
   MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
   MPI_Comm_size(MPI_COMM_WORLD,&ntasks);
 
   /*===============================================================*/
   /* Get buffsize value from program arguments.                    */
   buffsize=atoi(argv[1]);
 
   /*===============================================================*/
   /* Printing out the description of the example.                  */
   if ( taskid == 0 ){
     printf("\n\n\n");
     printf("##########################################################\n\n");
     printf(" Example 10 \n\n");
     printf(" Collective Communication : MPI_Allgather \n\n");
     printf(" Vector size: %d\n",buffsize);
     printf(" Number of tasks: %d\n\n",ntasks);
     printf("##########################################################\n\n");
     printf("                --> BEFORE COMMUNICATION <--\n\n");
   }
 
   /*=============================================================*/
   /* Memory allocation.                                          */ 
   sendbuff=(double *)malloc(sizeof(double)*buffsize);
   recvbuff=(double **)malloc(sizeof(double *)*ntasks);
   recvbuff[0]=(double *)malloc(sizeof(double)*ntasks*buffsize);
   for(i=1;i<ntasks;i++)recvbuff[i]=recvbuff[i-1]+buffsize;
 
   /*=============================================================*/
   /* Vectors and/or matrices initialisation.                      */
   srand((unsigned)time( NULL ) + taskid);
   for(i=0;i<buffsize;i++){
       sendbuff[i]=(double)rand()/RAND_MAX;
   }
 
   /*==============================================================*/
   /* Print out before communication.                              */
 
   MPI_Barrier(MPI_COMM_WORLD);
 
   buffsum=0.0;
   for(i=0;i<buffsize;i++){
     buffsum=buffsum+sendbuff[i];
   }
   printf("Task %d : Sum of vector = %e \n",taskid,buffsum);
 
   /*===============================================================*/
   /* Communication.                                                */
 
   inittime = MPI_Wtime();
 
   ierr=MPI_Allgather(sendbuff,buffsize,MPI_DOUBLE,
                      recvbuff[0],buffsize,MPI_DOUBLE,
                      MPI_COMM_WORLD);
 
   totaltime = MPI_Wtime() - inittime;
 
   /*===============================================================*/
   /* Print out after communication.                                */
 
   if ( taskid == 0 ){
     printf("\n");
     printf("##########################################################\n\n");
     printf("                --> AFTER COMMUNICATION <-- \n\n");
   }
 
   /*===============================================================*/
   /* Compute the sum of all gathered vectors,                      */
   /* and display these sums ordered by task                        */
   for(jtask=0;jtask<ntasks;jtask++){
     MPI_Barrier(MPI_COMM_WORLD);
     if ( taskid == jtask ){
       printf("\n");
       for(itask=0;itask<ntasks;itask++){
         buffsum=0.0;
         for(i=0;i<buffsize;i++){
           buffsum=buffsum+recvbuff[itask][i];
         }
         printf("Task %d : Sum of vector received from %d -> %e \n",
               taskid,itask,buffsum); 
       }              
     }
   }
 
   MPI_Barrier(MPI_COMM_WORLD);
 
   if(taskid==0){
     printf("\n");
     printf("##########################################################\n\n");
     printf(" Communication time : %f seconds\n\n",totaltime);
     printf("##########################################################\n\n");
   }
 
   /*===============================================================*/
   /* Free the allocated memory.                                    */
     free(recvbuff[0]);
     free(recvbuff);
     free(sendbuff);
 
   /*===============================================================*/
   /* MPI finalisation.                                             */
   MPI_Finalize();
}


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager