4-mpi/{hello/hello.c → lucky_number_reduce/lucky_number_reduce.c} RENAMED
@@ -1,24 +1,41 @@
1
  #include <mpi.h>
2
  #include <stdio.h>
 
 
3
 
4
  int main(int argc, char *argv[])
5
  {
6
  MPI_Init(&argc, &argv);
7
 
8
  int my_rank = -1;
9
  int process_count = -1;
10
 
11
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
12
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
13
 
14
  char hostname[MPI_MAX_PROCESSOR_NAME];
15
  int hostname_length = -1;
16
  MPI_Get_processor_name(hostname, &hostname_length);
17
 
18
- printf("Hello from main thread of process %d of %d on %s\n"
19
- , my_rank, process_count, hostname);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  MPI_Finalize();
22
  return 0;
23
  }
24
-
1
  #include <mpi.h>
2
  #include <stdio.h>
3
+ #include <stdlib.h>
4
+ #include <time.h>
5
 
6
  int main(int argc, char *argv[])
7
  {
8
  MPI_Init(&argc, &argv);
9
 
10
  int my_rank = -1;
11
  int process_count = -1;
12
 
13
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
14
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
15
 
16
  char hostname[MPI_MAX_PROCESSOR_NAME];
17
  int hostname_length = -1;
18
  MPI_Get_processor_name(hostname, &hostname_length);
19
 
20
+ srand( my_rank + time(NULL) );
21
+ long my_lucky_number = rand() % 100;
22
+ printf("Process %d: my lucky number is %ld\n", my_rank, my_lucky_number);
23
+
24
+ long all_min = -1;
25
+ long all_sum = -1;
26
+ long all_max = -1;
27
+
28
+ MPI_Reduce(&my_lucky_number, &all_min, /*count*/ 1, MPI_LONG, MPI_MIN, /*root*/ 0, MPI_COMM_WORLD);
29
+ MPI_Reduce(&my_lucky_number, &all_sum, /*count*/ 1, MPI_LONG, MPI_SUM, /*root*/ 0, MPI_COMM_WORLD);
30
+ MPI_Reduce(&my_lucky_number, &all_max, /*count*/ 1, MPI_LONG, MPI_MAX, /*root*/ 0, MPI_COMM_WORLD);
31
+
32
+ if ( my_rank == 0 )
33
+ {
34
+ printf("Process %d: all minimum: %02ld\n", my_rank, all_min);
35
+ printf("Process %d: all average: %.2lf\n", my_rank, (double)all_sum / process_count);
36
+ printf("Process %d: all maximum: %02ld\n", my_rank, all_max);
37
+ }
38
 
39
  MPI_Finalize();
40
  return 0;
41
  }