#include <mpi.h>
#include <stdio.h>
#include <string.h>
#define MESSAGE_CAPACITY 2048
int main(int argc, char *argv[])
{
MPI_Init(&argc, &argv);
int my_rank = -1;
int process_count = -1;
MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
MPI_Comm_size(MPI_COMM_WORLD, &process_count);
char hostname[MPI_MAX_PROCESSOR_NAME];
int hostname_length = -1;
MPI_Get_processor_name(hostname, &hostname_length);
char message[MESSAGE_CAPACITY];
if ( my_rank != 0 )
{
sprintf(message, "Hello from main thread of process %d of %d on %s", my_rank, process_count, hostname);
MPI_Send(message, strlen(message) + 1, MPI_CHAR, /*dest*/ 0, /*tag*/ 0, MPI_COMM_WORLD);
}
else
{
for ( int source = 1; source < process_count; ++source )
{
MPI_Recv(message, MESSAGE_CAPACITY, MPI_CHAR, MPI_ANY_SOURCE, MPI_ANY_TAG, MPI_COMM_WORLD, /*MPI_Status *status*/ MPI_STATUS_IGNORE);
printf("[%d] %s\n", source, message);
}
}
MPI_Finalize();
return 0;
}