mpi/{hello/hello.cpp → send_recv_ord_sem/send_recv_ord_sem.cpp} RENAMED
@@ -1,28 +1,44 @@
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
 
10
  int main(int argc, char* argv[]) {
11
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
12
  int my_rank = -1;
13
  MPI_Comm_rank(MPI_COMM_WORLD, &my_rank);
14
 
15
  int process_count = -1;
16
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
17
 
18
  char hostname[MPI_MAX_PROCESSOR_NAME];
19
  int hostname_len = -1;
20
  MPI_Get_processor_name(hostname, &hostname_len);
21
 
22
+ const int32_t previous = (process_count + my_rank - 1) % process_count;
23
+ const int32_t next = (my_rank + 1) % process_count;
24
+ int32_t can_print = 1;
25
+
26
+ if (my_rank != 0) {
27
+ MPI_Recv(&can_print, /*capacity*/ 1, MPI_INT32_T, /*source*/previous
28
+ , /*tag*/ 0, MPI_COMM_WORLD, MPI_STATUS_IGNORE);
29
+ }
30
+
31
  std::cout << "Hello from main thread of process " << my_rank
32
  << " of " << process_count << " on " << hostname << std::endl;
33
+
34
+ if (my_rank < process_count - 1) {
35
+ MPI_Send(&can_print, /*count*/ 1, MPI_INT32_T, /*dest*/ next, /*tag*/ 0
36
+ , MPI_COMM_WORLD);
37
+ }
38
+
39
  MPI_Finalize();
40
  } else {
41
  std::cout << "Error: could not init MPI environment" << std::endl;
42
  }
43
  return 0;
44
  }