mpi/{hello/src/hello.cpp → hello_hybrid/src/hello_hybrid.cpp} RENAMED
@@ -1,26 +1,41 @@
1
  // Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
2
 
3
  #include <mpi.h>
 
4
  #include <iostream>
5
 
6
  int main(int argc, char* argv[]) {
7
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
8
  int process_number = -1;
9
  MPI_Comm_rank(MPI_COMM_WORLD, &process_number);
10
 
11
  int process_count = -1;
12
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
13
 
14
  char process_hostname[MPI_MAX_PROCESSOR_NAME] = { '\0' };
15
  int hostname_length = -1;
16
  MPI_Get_processor_name(process_hostname, &hostname_length);
17
 
18
  std::cout << "Hello from main thread of process " << process_number
19
  << " of " << process_count << " on " << process_hostname << std::endl;
20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
  MPI_Finalize();
22
  } else {
23
  std::cerr << "error: could not init MPI" << std::endl;
24
  }
25
  return 0;
26
  }
1
  // Copyright 2021 Jeisson Hidalgo <jeisson.hidalgo@ucr.ac.cr> CC-BY 4.0
2
 
3
  #include <mpi.h>
4
+ #include <omp.h>
5
  #include <iostream>
6
 
7
  int main(int argc, char* argv[]) {
8
  if (MPI_Init(&argc, &argv) == MPI_SUCCESS) {
9
  int process_number = -1;
10
  MPI_Comm_rank(MPI_COMM_WORLD, &process_number);
11
 
12
  int process_count = -1;
13
  MPI_Comm_size(MPI_COMM_WORLD, &process_count);
14
 
15
  char process_hostname[MPI_MAX_PROCESSOR_NAME] = { '\0' };
16
  int hostname_length = -1;
17
  MPI_Get_processor_name(process_hostname, &hostname_length);
18
 
19
  std::cout << "Hello from main thread of process " << process_number
20
  << " of " << process_count << " on " << process_hostname << std::endl;
21
 
22
+ int thread_count = omp_get_max_threads();
23
+ if (argc == 2) {
24
+ thread_count = atoi(argv[1]);
25
+ }
26
+
27
+ #pragma omp parallel num_threads(thread_count) default(none) \
28
+ shared(std::cout, process_number, process_count, process_hostname)
29
+ { // NOLINT(whitespace/braces)
30
+ #pragma omp critical(stdout)
31
+ std::cout << "\tHello from thread " << omp_get_thread_num() << " of "
32
+ << omp_get_num_threads() << " of process " << process_number
33
+ << " of " << process_count << " on " << process_hostname << std::endl;
34
+ }
35
+
36
  MPI_Finalize();
37
  } else {
38
  std::cerr << "error: could not init MPI" << std::endl;
39
  }
40
  return 0;
41
  }