/***********************************************************************
*
* Dupays Isabelle (CNRS/IDRIS) 
*
* Nom        : colonne.c
*
* But        : Utilisation d'un type derive (type_colonne).
*              Le processus 0 envoie au processus 1 la premiere colonne
*              de sa matrice, le processus 1 la recoit dans sa derniere
*              colonne.
*
* Historique :
*  Créé le 12 May 1998 par Dupays Isabelle  
*
***********************************************************************/

#include <mpi.h> 
#include <stdio.h>

#define NB_LIGNES      5
#define NB_COLONNES    6

int main(int argc,char *argv[])
{
  
  int          rang, i, j;
  const int    ETIQUETTE=1000;
  float        a[NB_LIGNES][NB_COLONNES];
  MPI_Datatype type_colonne;
  MPI_Status   statut;

  /* Initialisation de MPI */
  MPI_Init(&argc,&argv);

  /* Savoir quel processeur je suis */
  MPI_Comm_rank(MPI_COMM_WORLD,&rang);
  
  /* Initialisation de la matrice a sur chaque processeur */
  for (i=0; i<NB_LIGNES ; i++)
    for (j=0; j<NB_COLONNES ; j++)
      a[i][j] = (float) rang;
      
  /* Definition du type type_colonne */
  MPI_Type_vector(NB_LIGNES,1,NB_COLONNES,MPI_FLOAT,&type_colonne);
  MPI_Type_commit(&type_colonne);

  /* Envoi de la premiere colonne */
  if (rang == 0)
    MPI_Send(a,1,type_colonne,1,ETIQUETTE,MPI_COMM_WORLD);

  /* Reception dans la derniere colonne */
  else    {
    MPI_Recv(&a[0][NB_COLONNES-1],1,type_colonne,0,ETIQUETTE,MPI_COMM_WORLD,&statut);
    for (i=0; i<NB_LIGNES ; i++) {
      for (j=0; j<NB_COLONNES ; j++) 
	printf ("%f ",a[i][j]);
      printf ("\n");
    }
  }
    
  /* Libere le type */
  MPI_Type_free(&type_colonne);
     
  /* Desactivation de MPI */
  MPI_Finalize();

  return(0);

}
  
