Download c source code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#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;
}