4-mpi/{hello/hello.c → send_recv_ord/send_recv_ord.c} RENAMED
@@ -1,24 +1,39 @@
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 <string.h>
4
+
5
+ #define MESSAGE_CAPACITY 2048
6
 
7
  int main(int argc, char *argv[])
8
  {
9
  MPI_Init(&argc, &argv);
10
 
11
  int my_rank = -1;
12
  int process_count = -1;
13
 
14
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
15
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
16
 
17
  char hostname[MPI_MAX_PROCESSOR_NAME];
18
  int hostname_length = -1;
19
  MPI_Get_processor_name(hostname, &hostname_length);
20
 
21
+ char message[MESSAGE_CAPACITY];
22
+ if ( my_rank != 0 )
23
+ {
24
+ sprintf(message, "Hello from main thread of process %d of %d on %s", my_rank, process_count, hostname);
25
+ MPI_Send(message, strlen(message) + 1, MPI_CHAR, /*dest*/ 0, /*tag*/ 0, MPI_COMM_WORLD);
26
+ }
27
+ else
28
+ {
29
+ for ( int source = 1; source < process_count; ++source )
30
+ {
31
+ MPI_Recv(message, MESSAGE_CAPACITY, MPI_CHAR, source, /*tag*/ 0, MPI_COMM_WORLD, /*MPI_Status *status*/ MPI_STATUS_IGNORE);
32
+ printf("[%d] %s\n", source, message);
33
+ }
34
+ }
35
 
36
  MPI_Finalize();
37
  return 0;
38
  }
39