Synchronisation de processus par barriere

  
Principe

Programme : 

#include "mpi.h"

#include <stdio.h>

#define SIZE 1000

int main( int argc, char *argv[];

{

    double startwtime, endwtime; int namelen, numprocs, myid, size, i ;

    char processor_name[MPI_MAX_PROCESSOR_NAME], buff[SIZE], msg[SIZE] ;

    MPI_Init(&argc,&argv);

    startwtime = MPI_Wtime();

    MPI_Comm_size(MPI_COMM_WORLD,&numprocs);

    MPI_Comm_rank(MPI_COMM_WORLD,&myid);

    MPI_Get_processor_name(processor_name,&namelen);

    fprintf(stderr,"\nProcessus %d sur la machine %s parmi %d processus.\n", myid, processor_name, numprocs);

    if (numprocs != 3) fprintf(stderr,"Il me faut 3 processus !\n") ;

    switch (myid)

    {

      case 0 : system("sleep 3") ; break ;

      case 1 : system("sleep 2") ; break ;

    }

    endwtime = MPI_Wtime();

    fprintf(stderr,"%d arrive a la barriere apres %f s.\n",myid,endwtime-startwtime) ;

    MPI_Barrier(MPI_COMM_WORLD );

    endwtime = MPI_Wtime();

    fprintf(stderr,"%d sort de la barriere apres %f s.\n",myid,endwtime-startwtime) ;

    system("sleep 1") ;

    endwtime = MPI_Wtime();

    printf("Temps ecoule sur %d = %f\n", myid, endwtime-startwtime);

    MPI_Finalize();

    return 0;

}

Resultat :

Processus 0 sur la machine lil. parmi 3 processus.

Processus 1 sur la machine lil. parmi 3 processus.

Processus 2 sur la machine lil. parmi 3 processus.

2 arrive a la barriere apres 0.000870 s.

1 arrive a la barriere apres 2.026342 s.

0 arrive a la barriere apres 3.028739 s.

0 sort de la barriere apres 3.031666 s.

1 sort de la barriere apres 3.002069 s.

2 sort de la barriere apres 2.969950 s.

Temps ecoule sur 0 = 4.078910

Temps ecoule sur 1 = 4.054713

Temps ecoule sur 2 = 4.033487


Ph. RIS 1997