SendReceive.c
SendReceive.c
—
C source code,
1 kB (1701 bytes)
Contenido del Archivo
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "mpi.h"
main(int argc, char* argv[])
{
int my_rank; // Rank del proceso
int p; // Numero de procesos
int source; // Rank del que envia
int dest; // Rank del que recibe
int tag = 0; // Tag del mensaje
char message[100]; // Mensaje
MPI_Status status;
char hostname[10];
double start,stop;
/* Inicio */
MPI_Init(&argc, &argv);
/* Averiguando el Rank del proceso */
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
/* Averiguando el numero de procesos que participan */
MPI_Comm_size(MPI_COMM_WORLD, &p);
/* Hasta que todos los procesos no lleguen hasta aqui ninguno continua */
MPI_Barrier(MPI_COMM_WORLD);
start= MPI_Wtime();
if (my_rank == 0) { // Si soy el proceso 0 entonces me ocupo de enviar un mensaje a todos los demas
sprintf(message, "Este es un mensaje para todos, soy el proceso %d!",my_rank);
source = 0;
for (dest = 1; dest < p; dest++){
MPI_Send(message, strlen(message)+1, MPI_CHAR, dest, tag, MPI_COMM_WORLD);
}
}else{
MPI_Recv(message, 100, MPI_CHAR, source, tag, MPI_COMM_WORLD, &status);
gethostname(hostname, 10);
printf("Esta recibiendo el proceso %d cuyo hostname es %s. \n", my_rank, hostname);
printf("%s\n", message);
}
MPI_Barrier(MPI_COMM_WORLD); // Espero a que todos los procesos terminen para calcular el tiempo de finalizacion.
stop= MPI_Wtime();
if (my_rank == 0)
printf("Tiempo empleado: %g\n",stop-start);
MPI_Finalize();
}





