mpi/{hello/hello.cpp → send_recv_ord/send_recv_ord.cpp} RENAMED
@@ -1,22 +1,44 @@
1
  #include <mpi.h>
2
  #include <iostream>
 
 
 
 
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
- std::cout << "Hello from main thread of process " << my_rank
19
- << " of " << process_count << " on " << hostname << "\n"; //std::endl;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  MPI_Finalize();
22
  }
1
  #include <mpi.h>
2
  #include <iostream>
3
+ #include <cstdio>
4
+ #include <cstring>
5
+
6
+ #define MESSAGE_CAPACITY 1024
7
 
8
  int main(int argc, char* argv[])
9
  {
10
  MPI_Init(&argc, &argv);
11
 
12
  int my_rank = -1;
13
  int process_count = -1;
14
 
15
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
16
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
17
 
18
  char hostname[MPI_MAX_PROCESSOR_NAME];
19
  int hostname_length = -1;
20
  MPI_Get_processor_name(hostname, &hostname_length);
21
 
22
+ char message[MESSAGE_CAPACITY];
23
+ sprintf(message, "Hello from main thread of process %d of %d on %s\n"
24
+ , my_rank, process_count, hostname);
25
+ // message << "Hello from main thread of process " << my_rank
26
+ // << " of " << process_count << " on " << hostname << "\n"; //std::endl;
27
+
28
+ if ( my_rank == 0 )
29
+ {
30
+ std::cout << message;
31
+ for ( int sender = 1; sender < process_count; ++sender )
32
+ {
33
+ MPI_Recv(message, /*capacity*/ MESSAGE_CAPACITY, MPI_CHAR, /*source*/ sender, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
34
+ std::cout << message;
35
+ }
36
+ }
37
+ else
38
+ {
39
+ MPI_Send(message, /*count*/ strlen(message) + 1, MPI_CHAR, /*dest*/ 0, /*tag*/ 0, MPI_COMM_WORLD);
40
+ }
41
+
42
 
43
  MPI_Finalize();
44
  }