Barrier

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

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

This example shows how to use the call MPI_Barrier that allows to synchronize processes. When a process encounters an MPI_Barrier call, it waits for all the other processes of the given communicator to reach the same point. The example given is just an illustration, and not very parallel as it is like a group of workers that paint a house with only one paint brush. Nevertheless, the function MPI_Barrier is essential in many other situations.

In Fortran

File : hello2.f
! --------------------------------------------------------
!   Note: The default communicator used here (MPI_COMM_WORLD)
!   includes all the processes of the run.
!
!   This program must be run with 4 MPI processes.
!
! Author: Carol Gauthier
!         Centre de Calcul scientifique
!         Université de Sherbrooke
!
! Last revision: August 2004
! --------------------------------------------------------
Program HelloWorld2
 
  include 'mpif.h'
 
  integer ntasks,taskid
  integer ierr,i,j,itask
  integer buffsize
 
  Call MPI_Init( ierr )
 
  Call MPI_Comm_Size( MPI_COMM_WORLD,ntasks,ierr )
  Call MPI_Comm_Rank( MPI_COMM_WORLD,taskid,ierr )
 
  write(6,*)
  write(6,*)
 
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
  if ( taskid.eq.0 )write(6,'(a,$)')'Hel'
  if ( taskid.eq.1 )write(6,'(a,$)')'lo '
  if ( taskid.eq.2 )write(6,'(a,$)')'Wor'
  if ( taskid.eq.3 )write(6,'(a,$)')'ld!'
 
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
  if ( taskid.eq.0 )write(6,*)" (Unordered)"
 
  if ( taskid.eq.0 )write(6,'(a,$)')"Hel"
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
  if ( taskid.eq.1 )write(6,'(a,$)')"lo "
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
  if ( taskid.eq.2 )write(6,'(a,$)')"Wor"
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
  if ( taskid.eq.3 )write(6,'(a,$)')"ld!"
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
  if ( taskid.eq.0 ) write(6,*)" (Ordered)"
 
  call MPI_Barrier(MPI_COMM_WORLD,ierr)
 
  write(6,*)
  write(6,*)
 
  Call MPI_Finalize( ierr )
 
End Program


In C

File : hello2.c
/* --------------------------------------------------------
   Note: The default communicator used here (MPI_COMM_WORLD)
   includes all the processes of the run.  
 
   This program must be run with 4 MPI processes.
 
 Author: Carol Gauthier
         Centre de Calcul scientifique
         Université de Sherbrooke
 
 Last revision: August 2004
-------------------------------------------------------- */
#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;
 
   MPI_Init(&argc, &argv);
 
   MPI_Comm_rank(MPI_COMM_WORLD,&taskid);
   MPI_Comm_size(MPI_COMM_WORLD,&ntasks);
 
   if ( taskid == 0 ){
     printf("\n\n\n\n\n");
   } 
 
   ierr=MPI_Barrier(MPI_COMM_WORLD);
 
   if ( taskid == 0 )printf("Hel");
   if ( taskid == 1 )printf("lo ");
   if ( taskid == 2 )printf("Wor");
   if ( taskid == 3 )printf("ld!");
 
   ierr=MPI_Barrier(MPI_COMM_WORLD);
 
   if ( taskid == 0 )printf(" (Unordered)\n");
 
   if ( taskid == 0){
       printf("Hel");
       fflush(stdout);
   }
   ierr=MPI_Barrier(MPI_COMM_WORLD);
   if ( taskid == 1){
       printf("lo ");
       fflush(stdout);
   }
   ierr=MPI_Barrier(MPI_COMM_WORLD);
   if ( taskid == 2){
        printf("Wor");
        fflush(stdout);
   }
   ierr=MPI_Barrier(MPI_COMM_WORLD);
   if ( taskid == 3){
        printf("ld!");
        fflush(stdout);
   }
   ierr=MPI_Barrier(MPI_COMM_WORLD);
 
   if ( taskid == 0 ){
     printf(" (Ordered)\n\n\n\n\n");
   }
 
   MPI_Finalize();
}


Outils personnels
Espaces de noms

Variantes
Actions
Navigation
Ressources de Calcul Québec
Outils
Partager