/*
* mpicode.c
* Simple send and Receive
* Author: Kostas Diamantaras
*/
#include <stdio.h>
#include "mpi.h"
#include <math.h>
#include <string.h>
int main( int argc, char *argv[] ) {
int rank, size, i;
int count;
int tag = 50;
char message[100];
MPI_Status status;
MPI_Init( &argc, &argv );
MPI_Comm_size( MPI_COMM_WORLD, &size );
MPI_Comm_rank( MPI_COMM_WORLD, &rank );
if (rank != 0) { /* Send message to process 0 */
printf("Process %d: sending message to process 0\n", rank);
fflush(stdout);
sprintf(message, "Hello from process %d of %d", rank, size);
MPI_Send(message, strlen(message), MPI_CHAR, 0, tag, MPI_COMM_WORLD);
}
if (rank == 0) { /* Receive messages from other processes and print them out */
for (i = 1; i < size; i++ ) {
MPI_Recv(message, 100, MPI_CHAR, i, tag, MPI_COMM_WORLD, &status);
MPI_Get_count( &status, MPI_CHAR, &count );
message[count]=0; /* append a 0-byte to terminate string */
printf("Process %d: received this --> \"%s\"\n", rank, message);
fflush(stdout);
}
}
MPI_Finalize();
return 0;
}