 A simple MPI example program using synchronous send                 */

/* The program consists of one sender process and one receiver         */
/* The sender process sends a message containing its identifier        */
/* to the receiver. This receives the message and sends it back        */
/* Both processes use synchronous send operations (MPI_Ssend)          */

/* Compile the program with 'mpicc send-recv3.c -o send-recv3'         */
/* Run the program with 'mpirun -machinefile hostfile -np 2 send-recv3 */

#include <stdio.h>
#include <mpi.h>

main(int argc, char* argv[]) {
  int numberOfNodes, myID, i, vectorSize;
  int tag = 42;
  MPI_Status  status;
  int *vector;

  MPI_Init(&argc, &argv);               /* Initialize MPI */
  MPI_Comm_size(MPI_COMM_WORLD, &numberOfNodes);   /* Get number of processes */
  MPI_Comm_rank(MPI_COMM_WORLD, &myID);   /* Get own identifier */
  vectorSize = argv[1];
  vector = (int *) malloc(vectorSize*sizeof(int));

//   x = me;
  if (myID == 0) {    /* Process 0 does this */
    srand(17);
    for (i=0;i<vectorSize;i++){
	vector[i]=rand()%10000;	
    }
    printf("Sending vector to processes\n");
    MPI_Bcast(&vector, ,vectorSize, MPI_INT, myID, MPI_COMM_WORLD);  /* Synchronous send */
    
  } else {         /* Process 1 does this */

    /* Since we use synchronous send, we have to do the receive-operation */
    /* first, otherwise we will get a deadlock */
    MPI_Bcast (vector, vectorSize, MPI_INT, 0, MPI_COMM_WORLD);
//     MPI_Ssend (&x, 1, MPI_INT, 0, tag, MPI_COMM_WORLD);  /* Synchronous send */
    //ler o vetor inteiro e recriar um de tamanho tam/nodos ordenado. Devolver com gatherv.
  }

  MPI_Finalize();
}

