QMCPACK
check-affinity.cpp
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 //
9 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 #ifdef __linux__
13 #include <sched.h>
14 #endif
15 #include <iostream>
16 #include <sstream>
17 #include "config.h"
18 #ifdef HAVE_MPI
19 #include <mpi.h>
20 #endif
21 #include "Concurrency/OpenMP.h"
22 
23 namespace
24 {
25 /*=======================================*/
26 /* routine to return the core ID */
27 /*=======================================*/
28 int get_core()
29 {
30 #ifdef __linux__
31  int cpuid = sched_getcpu();
32  return cpuid;
33 #else
34  return -1;
35 #endif
36 }
37 
38 /*=======================================*/
39 /* routine to return the HW thread ID */
40 /*=======================================*/
41 int get_hwthread() { return -1; }
42 
43 } // namespace
44 
45 int main()
46 {
47  int rank = 0, world_size = 1;
48 #ifdef HAVE_MPI
49  int provided;
50  MPI_Init_thread(NULL, NULL, MPI_THREAD_FUNNELED, &provided);
51  MPI_Comm_rank(MPI_COMM_WORLD, &rank);
52  MPI_Comm_size(MPI_COMM_WORLD, &world_size);
53 #endif
54 
55  bool hwthread_id_supported = (get_hwthread() != -1);
56 
57  if (rank == 0)
58  std::cout << "OpenMP OMP_MAX_ACTIVE_LEVELS = " << omp_get_max_active_levels() << std::endl;
59 
60  for (int l_rank = 0; l_rank < world_size; l_rank++)
61  {
62  if (l_rank == rank)
63  {
64 #pragma omp parallel
65  {
66  int L1_thread_id = omp_get_thread_num();
67  int L1_num_thread = omp_get_num_threads();
68 #pragma omp parallel
69  {
70 #pragma omp master
71  if (L1_thread_id == 0 and l_rank == 0)
72  std::cout << "OpenMP enables " << L1_num_thread << " 1st level threads, "
73  << "and " << omp_get_num_threads() << " 2nd level threads." << std::endl
74  << std::endl;
75  }
76 #pragma omp barrier
77 
78  for (int i = 0; i < L1_num_thread; i++)
79  {
80  if (L1_thread_id == i)
81  {
82 #pragma omp parallel
83  {
84  int L2_thread_id = omp_get_thread_num();
85  int L2_num_thread = omp_get_num_threads();
86  for (int j = 0; j < L2_num_thread; j++)
87  {
88  if (L2_thread_id == j)
89  {
90  std::ostringstream o;
91  o << "MPI rank " << rank << " L1 tid " << L1_thread_id << " L2 tid " << L2_thread_id
92  << " is placed on Core ID " << get_core();
93  if (hwthread_id_supported)
94  o << " HW thread ID " << get_hwthread();
95  o << std::endl;
96  std::cout << o.str();
97  }
98 #pragma omp barrier
99  }
100  }
101  }
102 #pragma omp barrier
103  }
104  }
105  }
106 #ifdef HAVE_MPI
107  MPI_Barrier(MPI_COMM_WORLD);
108 #endif
109  }
110 #ifdef HAVE_MPI
111  MPI_Finalize();
112 #endif
113  return 0;
114 }
omp_int_t omp_get_thread_num()
Definition: OpenMP.h:25
int main()
omp_int_t omp_get_max_active_levels()
Definition: OpenMP.h:30
omp_int_t omp_get_num_threads()
Definition: OpenMP.h:27