Gather

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

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

This example uses MPI_Gather to put together on a single process (in this case process 0) data that are distributed among all the processes. It is the reverse fonction of MPI_Scatter (see example 7). Each process prepares a vector of real random numbers (sendbuff) of size buffsize. Then the function MPI_Gather puts together the content of all these vectors into the matrix recvbuff on process 0.

Before and after communication the sum for each vector is printed out for verification.

In Fortran

File : gather.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 #      #      #      #      #      #
!     a    #      #      #      #      #      #
!          ####################################
!     s    #      #      #      #      #      #
!        2 #      #      #      #      #      #       AFTER
!     k    #      #      #      #      #      #
!          ####################################
!     s    #      #      #      #      #      #
!        3 #      #      #      #      #      #
!          #      #      #      #      #      #
!          ####################################
!          #      #      #      #      #      #
!        4 #      #      #      #      #      #
!          #      #      #      #      #      #
!          ####################################
!
! Author: Carol Gauthier
!         Centre de Calcul scientifique
!         Universite de Sherbrooke
!
! Last revision: August 2004
!--------------------------------------------------------
Program MPI_example
 
  include 'mpif.h'
  integer ierr,ntasks,taskid,itask,status(MPI_STATUS_SIZE)
  integer i,j,k,buffsize
  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 processess and the taskid of this process.
  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 9"
    write(6,'(A)')
    write(6,'(A)')" Collective Communication: MPI_Gather"
    write(6,'(A)')
    write(6,'(A,I12)')" Vector size:",buffsize
    write(6,'(A,I5)')" Number of processes:",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 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)')"Process",taskid,": Sum of vector = ",buffsum
 
  call MPI_Barrier(MPI_COMM_WORLD,ierr);     
 
 
  !-----------------------------------------------------------------
  ! Communication
 
  inittime = MPI_Wtime()
 
  call MPI_Gather(sendbuff,buffsize,MPI_REAL8, &
  &               recvbuff,buffsize,MPI_REAL8, &
  &               0,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,*)
    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)')"Process ",taskid,": Sum of vector received from task ",itask, &
      &                             "= ",buffsum
    end do
    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 : gather.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 #      #      #      #      #      #
     a    #      #      #      #      #      #
          ####################################
     s    #      #      #      #      #      #
        2 #      #      #      #      #      #       AFTER
     k    #      #      #      #      #      #
          ####################################
     s    #      #      #      #      #      #
        3 #      #      #      #      #      #
          #      #      #      #      #      #
          ####################################
          #      #      #      #      #      #
        4 #      #      #      #      #      #
          #      #      #      #      #      #
          ####################################
 
 Author: Carol Gauthier
         Centre de Calcul scientifique
         Universite de Sherbrooke
 
 Last revision: September 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;
   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 processes and the taskid of this process.      */
   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 9 \n\n");
     printf(" Collective Communication: MPI_Gather \n\n");
     printf(" Process 0 gathers data from the sendbuff array on\n");
     printf(" each process and places them in recvbuff. \n\n");
     printf(" Vector size: %d\n",buffsize);
     printf(" Number of processes: %d\n\n",ntasks);
     printf("##########################################################\n\n");
     printf("                --> BEFORE COMMUNICATION <--\n\n");
   }
 
   /*=============================================================*/
   /* Memory allocation.                                          */ 
   sendbuff=(double *)malloc(sizeof(double)*buffsize);
   if( taskid == 0 ){
     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;
   }
   else{
     recvbuff=(double **)malloc(sizeof(double *)*1);
     recvbuff[0]=(double *)malloc(sizeof(double)*1);
   }
 
   /*=============================================================*/
   /* Vectors 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("Process %d : Sum of vector buff= %e\n",taskid,buffsum);
 
   MPI_Barrier(MPI_COMM_WORLD);     
 
   /*===============================================================*/
   /* Communication.                                                */
 
   inittime = MPI_Wtime();
 
   ierr=MPI_Gather(sendbuff,buffsize,MPI_DOUBLE,
                   recvbuff[0],buffsize,MPI_DOUBLE,
                   0,MPI_COMM_WORLD);
 
   totaltime = MPI_Wtime() - inittime;
 
   /*===============================================================*/
   /* Print out after communication.                                */
   if ( taskid == 0 ){
     printf("\n");
     printf("##########################################################\n\n");
     printf("                --> AFTER COMMUNICATION <-- \n\n");
     for(itask=0;itask<ntasks;itask++){
       buffsum=0.0;
       for(i=0;i<buffsize;i++){
         buffsum=buffsum+recvbuff[itask][i];
       }
       printf("Process %d : Sum of vector received from %d -> %e \n",
               taskid,itask,buffsum);
 
     }       
   }       
 
   if(taskid==0){
     printf("\n");
     printf("##########################################################\n\n");
     printf(" Communication time : %f seconds\n\n",totaltime);  
     printf("##########################################################\n\n");
   }
 
   /*===============================================================*/
   /* Free the allocated memory.                                    */
   if ( taskid == 0 ){
     free(recvbuff[0]);
     free(recvbuff);
   }
   else{
     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