#include <omp.h>
#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>


int main(int argc, char **argv)
{
  int rang, nbproc;
  char machine[100];

  gethostname(machine, 100);
  // ATTENTION : chaque processus a sa variable t, indépendant des autres.
  int t[10]={0};
  srand(time(0)+getpid());
  t[5] = rand();

  if(MPI_Init(&argc, &argv))
  {
    fprintf(stderr, "Erreur init MPI!\n");
    return(1);
  }
  MPI_Comm_rank(MPI_COMM_WORLD, &rang);
  MPI_Comm_size(MPI_COMM_WORLD, &nbproc);

 if(nbproc>=2)
 {
  if(rang==0)
  {
    printf("il y a %d processus\n(sur 0, t[5] = %d)\n", nbproc, t[5]);
    MPI_Ssend(t, 10, MPI_INT, 1, 42, MPI_COMM_WORLD);
  }
  else if (rang==1)
  {
    MPI_Status s;
    printf("t avant receive vaut : ");
    for(int i=0; i<10; i++)
	    printf("%d ", t[i]);
    printf("\n");
    MPI_Recv(t, 10, MPI_INT, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, &s);
    printf("J'ai reçu un message du processus %d avec le tag %d.\nt après receive vaut : ",
		    s.MPI_SOURCE, s.MPI_TAG);
    t[0] = 12;
    for(int i=0; i<10; i++)
	    printf("%d ", t[i]);
    printf("\n");
  }
 }
  printf("hello. Je suis le numéro %d:%d (sur %s).\n", rang, omp_get_thread_num(), machine);

  MPI_Finalize();
  return(0);
}
