mpi/{hello/hello.cpp → send_recv_ord/send_recv_ord.cpp} RENAMED
@@ -1,28 +1,50 @@
1
  /*
2
  * Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
3
  * Creates a secondary thread that greets in the standard output
4
  */
5
 
6
  #include <mpi.h>
 
7
  #include <iostream>
 
 
 
 
 
8
 
9
  int main(int argc, char* argv[]) {
10
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
11
  int my_rank = -1;
12
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
13
 
14
  int process_count = -1;
15
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
16
 
17
  char hostname[MPI_MAX_PROCESSOR_NAME];
18
  int hostname_len = -1;
19
  MPI_Get_processor_name(hostname, &hostname_len);
20
 
21
- std::cout << "Hello from main thread of process " << my_rank
22
- << " of " << process_count << " on " << hostname << std::endl;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  MPI_Finalize();
24
  } else {
25
  std::cout << "Error: could not init MPI environment" << std::endl;
26
  }
27
  return 0;
28
  }
1
  /*
2
  * Copyright 2021 Jeisson Hidalgo-Cespedes - Universidad de Costa Rica
3
  * Creates a secondary thread that greets in the standard output
4
  */
5
 
6
  #include <mpi.h>
7
+ #include <cstdint>
8
  #include <iostream>
9
+ #include <string>
10
+ #include <sstream>
11
+ #include <vector>
12
+
13
+ const size_t MESSAGE_CAPACITY = 256;
14
 
15
  int main(int argc, char* argv[]) {
16
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
17
  int my_rank = -1;
18
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
19
 
20
  int process_count = -1;
21
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
22
 
23
  char hostname[MPI_MAX_PROCESSOR_NAME];
24
  int hostname_len = -1;
25
  MPI_Get_processor_name(hostname, &hostname_len);
26
 
27
+ std::ostringstream buffer;
28
+ buffer << "Hello from main thread of process " << my_rank
29
+ << " of " << process_count << " on " << hostname;
30
+
31
+ if (my_rank != 0) {
32
+ const std::string& message = buffer.str();
33
+ MPI_Send(message.data(), /*count*/ message.length() + 1, MPI_SIGNED_CHAR
34
+ , /*dest*/ 0, /*tag*/ 0, MPI_COMM_WORLD);
35
+ } else {
36
+ std::cout << 0 << " says: " << buffer.str() << std::endl;
37
+ std::vector<char> message(256, '\0');
38
+ for (int sender = 1; sender < process_count; ++sender) {
39
+ MPI_Recv(&message[0], /*capacity*/ message.size(), MPI_SIGNED_CHAR
40
+ , sender, /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
41
+ std::cout << sender << " says: " << &message[0] << std::endl;
42
+ }
43
+ }
44
+
45
  MPI_Finalize();
46
  } else {
47
  std::cout << "Error: could not init MPI environment" << std::endl;
48
  }
49
  return 0;
50
  }