Messages asynchrones bufferises avec MPI |
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 ; char processor_name[MPI_MAX_PROCESSOR_NAME], buff[SIZE], msg[SIZE] ; MPI_status status ; 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") ; if (myid==0) { strcpy(msg,"Message 1") ; size=sizeof(char)*strlen(msg) ; fprintf(stderr,"%d envoie <%s>\n",myid, msg) ; MPI_Buffer_attach( buff, SIZE) ; MPI_Bsend((void*) msg, size, MPI_CHAR, 1, 1, MPI_COMM_WORLD) ; strcpy(msg,"Message 2") ; fprintf(stderr,"%d envoie <%s>\n",myid,msg) ; MPI_Bsend(void*) msg, 15, MPI_CHAR, 2, 1, MPI_COMM_WORLD) ; MPI_Buffer_detach(buff, &size) ; } else { strcpy(msg,"") ; system("sleep 5") ; MPI_Recv(msg, 15, MPI_CHAR, 0, MPI_ANY_TAG, MPI_COMM_WORLD, &status) ; fprintf(stderr,"%d recoit <%s>\n",myid,msg) ; } endwtime = MPI_Wtime(); printf("Temps ecoule sur %d = %f\n", myid, endwtime-startwtime); MPI_Finalize(); return 0; } |
Resultat de l'execution :
Processus 0 sur la machine lil. parmi 3 processus. 0 envoie <Message 1> 0 envoie <Message 2> Temps ecoule sur 0 : 0.003934 Processus 2 sur la machine lil. parmi 3 processus. Processus 1 sur la machine lil. parmi 3 processus. 1 recoit <Message 1> Temps ecoule sur 1 : 5.060591 2 recoit <Message 2> Temps ecoule sur 2 : 5.066704 |
NB : L'implantation des procedures de messages bufferises n'est pas tres propre. L'utilisateur a besoin de definir une variable pour la zone tampon alors qu'il n'a jamais acces directement a
cette zone.
Ph. RIS 1997