Broadcast

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

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

This program is a simple example of MPI_Bcast usage. The content of the vector buff of size buffsize is sent to all other processes, issuing a broadcast. Before and after the broadcast, each process calculates the sum of all elements of the vector and prints it out on the screen. The size of the vector is given as an argument to the program at run time.

In Fortran

File : bcast.f90
!-----------------------------------------------------------------
!                buff                buff
!                                
!              ########            ########
!              #      #            #      #
!          0   # ABCD #            # ABCD #
!              #      #            #      #
!              ########            ########
!     T        #      #            #      #
!          1   # 0000 #            # ABCD #
!     a        #      #            #      #
!              ########            ########
!     s        #      #            #      #
!          2   # 0000 #            # ABCD #
!     k        #      #            #      #
!              ########            ########
!     s        #      #            #      #
!          3   # 0000 #            # ABCD #
!             #      #            #      #
!              ########            ########
!              #      #            #      #
!          4   # 0000 #            # ABCD #
!              #      #            #      #
!              ########            ########
!
!               BEFORE               AFTER
!               
! Author: Carol Gauthier
!         Centre de Calcul scientifique
!         Université de Sherbrooke
!
! Last revision: Michel Béland, Calcul Québec, October 2013
!-----------------------------------------------------------------
Program Example_MPI
 
  use mpi
  integer ierr,ntasks,taskid
  integer buffsize
  character argtmp*12
  integer l
  integer, parameter :: dp=kind(1.0d0)
  real(dp) inittime,totaltime
 
  real(dp),allocatable,dimension(:) :: buff
  real(dp) buffsum
 
  !---------------------------------------------------------------
  ! 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 processes 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.
  ! We use the Fortran 2003 way of getting the arguments.
  if (command_argument_count() == 0) stop
  call get_command_argument(1,length=l)
  if (l > 12) stop
  call get_command_argument(1,value=argtmp)
  ! If you do not have access to a Fortran 2003 compiler, use the getarg subroutine,
  ! which is non standard but relatively portable :
  ! call getarg(1,argtmp)
  read(argtmp,'(I12)')buffsize
 
  !---------------------------------------------------------------
  ! Memory allocation.
  allocate( buff(buffsize) )  
 
   !---------------------------------------------------------------
  ! Printing out the description of the example.
  if ( taskid.eq.0 )then
    write(*,'(A)')
    write(*,'(A)')"##########################################################"
    write(*,'(A)')
    write(*,'(A)')" Example 3"
    write(*,'(A)')
    write(*,'(A)')" Collective Communication: MPI_Bcast"
    write(*,'(A)')
    write(*,'(A,I12)')" Vector size:",buffsize
    write(*,'(A,I5)')" Number of processes:",ntasks
    write(*,'(A)')
    write(*,'(A)')"##########################################################"
    write(*,'(A)')
    write(*,'(A)')"                --> BEFORE COMMUNICATION <--"
    write(*,'(A)')
  endif
 
   call MPI_Barrier(MPI_COMM_WORLD,ierr);
 
   !-----------------------------------------------------------------
   ! Vectors or matrices initialisation.
   if( taskid.eq.0 )then
     call random_number(harvest=buff)
   else
      buff=0.0_dp
   end if
 
   !-----------------------------------------------------------------
   ! Print out before communication.
 
   buffsum = sum(buff)
 
   write(*,'(A,I3,A,ES16.8)')"Process ",taskid,": sum of buff elements=",buffsum 
 
   !-----------------------------------------------------------------
   ! Communication
 
   inittime=MPI_Wtime()
   call MPI_Bcast(buff,buffsize,MPI_DOUBLE_PRECISION,0,MPI_COMM_WORLD,ierr)
   totaltime=MPI_Wtime()-inittime
 
   !-----------------------------------------------------------------
   ! Print out after communication.
 
   buffsum = sum(buff)
 
   call MPI_Barrier(MPI_COMM_WORLD,ierr);
 
   if ( taskid.eq.0 )then
     write(*,*)
     write(*,'(A)')"##########################################################"
     write(*,'(A)')
     write(*,'(A)')"                --> AFTER COMMUNICATION <-- "
     write(*,'(A)')
   end if
 
   call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
   write(*,'(A,I3,A,ES16.8)')"Process ",taskid,": sum of buff elements=",buffsum
 
   call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
   if(taskid.eq.0)then
     write(*,*)
     write(*,'(A)')"##########################################################"
     write(*,'(A,ES10.2,A)')" Communication time: ",totaltime," seconds"
     write(*,'(A)')"##########################################################"
     write(*,*)
   end if
 
  !-----------------------------------------------------------------
  ! Free the allocated memory
  deallocate(buff)
 
  !-----------------------------------------------------------------
  ! MPI finalisation
 
  call MPI_FINALIZE( ierr )
end


In C

File : bcast.c
/* -----------------------------------------------------------------   
                buff                buff
 
              ########            ########
              #      #            #      #
          0   # ABCD #            # ABCD #
              #      #            #      #
              ########            ########
     T        #      #            #      #
          1   # 0000 #            # ABCD #
     a        #      #            #      #
              ########            ########
     s        #      #            #      #
          2   # 0000 #            # ABCD #
     k        #      #            #      #
              ########            ########
     s        #      #            #      #
          3   # 0000 #            # ABCD #
              #      #            #      #
              ########            ########
              #      #            #      #
          4   # 0000 #            # ABCD #
              #      #            #      #
              ########            ########
BEFORE               AFTER        
 
 Author: Carol Gauthier
         Centre de Calcul scientifique
         Université 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){
 
   int          taskid, ntasks;
   int          ierr,i,j,itask;
   int	        buffsize;
   double       *buff,buffsum;
   double       inittime,totaltime;
 
   /*---------------------------------------------------------------*/
   /* MPI Initialisation. It's 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]);
 
   /*---------------------------------------------------------------*/
   /* Memory allocation.                                            */ 
   buff=(double *)malloc(sizeof(double)*buffsize);
 
   /*---------------------------------------------------------------*/
   /* Printing out the description of the example.                  */
   if ( taskid == 0 ){
     printf("\n\n\n");
     printf("##########################################################\n\n");
     printf(" Example 3 \n\n");
     printf(" Collective Communication : MPI_Bcast \n\n");
     printf(" Vector size: %d\n",buffsize);
     printf(" Number of processes: %d\n\n",ntasks);
     printf("##########################################################\n\n");
     printf("                --> BEFORE COMMUNICATION <--\n\n");
   }
 
   ierr=MPI_Barrier(MPI_COMM_WORLD);
 
   /*---------------------------------------------------------------*/
   /* Vectors or matrices initialisation.                      */
   if( taskid == 0 ){
     srand((unsigned)time( NULL ) + taskid);
     for(i=0;i<buffsize;i++)buff[i]=(double)rand()/RAND_MAX;
   }
   else{
     for(i=0;i<buffsize;i++)buff[i]=0.0;
   }
 
   /*==============================================================*/
   /* Print out before communication.                              */
 
   buffsum=0.0;
   for(i=0;i<buffsize;i++){
     buffsum += buff[i];
   }
 
   printf("Process %d : Sum of vector buff= %e\n",taskid,buffsum);  
 
   /*---------------------------------------------------------------*/
   /* Communication.                                                */
 
   inittime = MPI_Wtime();
 
   ierr=MPI_Bcast(buff,buffsize,MPI_DOUBLE,0,MPI_COMM_WORLD);
 
   totaltime = MPI_Wtime() - inittime;
 
   /*---------------------------------------------------------------*/
   /* Print out after communication.                                */
 
   buffsum=0.0;
   for(i=0;i<buffsize;i++){
     buffsum += buff[i];
   } 
 
 
   if ( taskid == 0 ){
     printf("##########################################################\n\n");
     printf("                --> AFTER COMMUNICATION <-- \n\n");
   }
   ierr=MPI_Barrier(MPI_COMM_WORLD);
 
   printf("Process %d : Sum of vector buff= %e\n",taskid,buffsum);
 
   ierr=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(buff);
 
   /*===============================================================*/
   /* MPI finalisation.                                             */
   MPI_Finalize();
}


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager