#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
const char* compare(double num1, double num2)
{
if ( num1 < num2 )
return "less than";
if ( num1 > num2 )
return "greater than";
return "equals to";
}
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);
srand( my_rank + time(NULL) );
long my_lucky_number = rand() % 100;
long all_min = -1;
long all_sum = -1;
long all_max = -1;
MPI_Allreduce(&my_lucky_number, &all_min, /*count*/ 1, MPI_LONG, MPI_MIN, MPI_COMM_WORLD);
MPI_Allreduce(&my_lucky_number, &all_sum, /*count*/ 1, MPI_LONG, MPI_SUM, MPI_COMM_WORLD);
MPI_Allreduce(&my_lucky_number, &all_max, /*count*/ 1, MPI_LONG, MPI_MAX, MPI_COMM_WORLD);
double average = (double)all_sum / process_count;
if ( my_lucky_number == all_min )
printf("Process %d: my lucky number (%02ld) is the minimum (%02ld)\n", my_rank, my_lucky_number, all_min);
const char* const relation = compare(my_lucky_number, average);
printf("Process %d: my lucky number (%02ld) is %s the average (%.2lf)\n", my_rank, my_lucky_number, relation, average);
if ( my_lucky_number == all_max )
printf("Process %d: my lucky number (%02ld) is the maximum (%02ld)\n", my_rank, my_lucky_number, all_max);
MPI_Finalize();
return 0;
}