Download c source code

#include <mpi.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

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;
	printf("Process %d: my lucky number is %ld\n", my_rank, my_lucky_number);

	long all_min = -1;
	long all_sum = -1;
	long all_max = -1;

	MPI_Reduce(&my_lucky_number, &all_min, /*count*/ 1, MPI_LONG, MPI_MIN, /*root*/ 0, MPI_COMM_WORLD);
	MPI_Reduce(&my_lucky_number, &all_sum, /*count*/ 1, MPI_LONG, MPI_SUM, /*root*/ 0, MPI_COMM_WORLD);
	MPI_Reduce(&my_lucky_number, &all_max, /*count*/ 1, MPI_LONG, MPI_MAX, /*root*/ 0, MPI_COMM_WORLD);

	if ( my_rank == 0 )
	{
		printf("Process %d: all minimum: %02ld\n", my_rank, all_min);
		printf("Process %d: all average: %.2lf\n", my_rank, (double)all_sum / process_count);
		printf("Process %d: all maximum: %02ld\n", my_rank, all_max);
	}

	MPI_Finalize();
	return 0;
}