broadcast2.c

/*****************************************************************/
/* Mise en oeuvre d'un broadcast en MPI à la main avec des       */
/* primitives de communications non bloquantes et sans rien      */
/* entre la diffusion et les calculs.                            */
/*****************************************************************/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <mpi.h>
#include <sys/time.h>

#define BUFLEN 65536

long ms_time() 
{
  struct timeval tv;

  gettimeofday(&tv, NULL);
  return(tv.tv_sec*1000 + tv.tv_usec/1000);
}

void long_computation() 
{
   long now=ms_time();
   while(ms_time()<now+4000) 
     {
     }
}

int main(int argc, char **argv) 
{
  int nb_proc;
  int my_id;
  int next;
  int buffer[BUFLEN];
  MPI_Status status;
  MPI_Request request;

  long start,end;

  MPI_Init(&argc,&argv);
  MPI_Comm_size(MPI_COMM_WORLD,&nb_proc);
  MPI_Comm_rank(MPI_COMM_WORLD,&my_id);

  if (my_id == nb_proc-1)
    next = 0;
  else
    next = my_id+1;

  if (my_id == 0) start = ms_time();

  if (my_id == 0) 
    {
      int i;
      for(i=0; i<BUFLEN; i++) buffer[i]=i;

      MPI_Isend(buffer, BUFLEN, MPI_INT, next, 666, MPI_COMM_WORLD,&request);
    } 
  else 
    {
      MPI_Recv(buffer, BUFLEN, MPI_INT, MPI_ANY_SOURCE, 666, MPI_COMM_WORLD,
	       &status);

      if (my_id != nb_proc-1)
	MPI_Isend(buffer, BUFLEN, MPI_INT, next, 666, MPI_COMM_WORLD,&request);
    }

  long_computation();

  MPI_Finalize();
  if (my_id == 0) end = ms_time();
  if (my_id == 0) printf("Ça m'a prit %ld ms\n",(end-start));

  return (0);
}

Generated by GNU enscript 1.6.3.