Metall  v0.29
A persistent memory allocator for data-centric analytics
open_mp.hpp
Go to the documentation of this file.
1 // Copyright 2020 Lawrence Livermore National Security, LLC and other Metall
2 // Project Developers. See the top-level COPYRIGHT file for details.
3 //
4 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
5 
6 #ifndef METALL_UTILITY_OPEN_MP_HPP
7 #define METALL_UTILITY_OPEN_MP_HPP
8 
9 #include <cstdint>
10 #include <string>
11 
12 #ifdef _OPENMP
13 #include <omp.h>
14 #endif
15 
16 #ifdef OPEN_MP_PRAGMA_OPERATOR
17 #error "OPEN_MP_PRAGMA_OPERATOR is already defined"
18 #endif
19 
20 #ifdef OMP_DIRECTIVE
21 #error "OMP_DIRECTIVE is already defined"
22 #endif
23 
24 #define OPEN_MP_PRAGMA_OPERATOR(x) _Pragma(#x)
25 #ifdef _OPENMP
26 #define OMP_DIRECTIVE(directive) OPEN_MP_PRAGMA_OPERATOR(omp directive)
27 #else
28 #define OMP_DIRECTIVE(directive)
29 #endif
30 
31 namespace metall::utility {
32 
35 namespace omp {
36 
38 #ifdef _OPENMP
39  omp_sched_t;
40 #else
41  int;
42 #endif
43 
45  [[maybe_unused]] const omp_sched_type kind) {
46  std::string name;
47 
48 #ifdef _OPENMP
49 #if _OPENMP >= 201811 // OpenMP 5.0
50  if (kind == omp_sched_static ||
51  kind == (omp_sched_static | omp_sched_monotonic)) {
52  name = "omp_sched_static";
53  } else if (kind == omp_sched_dynamic ||
54  kind == (omp_sched_dynamic | omp_sched_monotonic)) {
55  name = "omp_sched_dynamic";
56  } else if (kind == omp_sched_guided ||
57  kind == (omp_sched_guided | omp_sched_monotonic)) {
58  name = "omp_sched_guided";
59  } else if (kind == omp_sched_auto ||
60  kind == (omp_sched_auto | omp_sched_monotonic)) {
61  name = "omp_sched_auto";
62  } else {
63  name = "Unknown kind (" + std::to_string((uint64_t)kind) + ")";
64  }
65  if (kind & omp_sched_monotonic) {
66  name += " with omp_sched_monotonic";
67  }
68 #else
69  if (kind == omp_sched_static) {
70  name = "omp_sched_static";
71  } else if (kind == omp_sched_dynamic) {
72  name = "omp_sched_dynamic";
73  } else if (kind == omp_sched_guided) {
74  name = "omp_sched_guided";
75  } else if (kind == omp_sched_auto) {
76  name = "omp_sched_auto";
77  } else {
78  name = "Unknown kind (" + std::to_string((uint64_t)kind) + ")";
79  }
80 #endif
81 #else
82  name = "OpenMP is not supported";
83 #endif
84 
85  return name;
86 }
87 
88 inline std::pair<omp_sched_type, int> get_schedule() {
89 #ifdef _OPENMP
90  omp_sched_t kind;
91  int chunk_size;
92  ::omp_get_schedule(&kind, &chunk_size);
93 
94  return std::make_pair(kind, chunk_size);
95 #else
96  return std::make_pair(0, 0);
97 #endif
98 }
99 
100 inline int get_num_threads() noexcept {
101 #ifdef _OPENMP
102  return ::omp_get_num_threads();
103 #else
104  return 1;
105 #endif
106 }
107 
108 inline int get_thread_num() noexcept {
109 #ifdef _OPENMP
110  return ::omp_get_thread_num();
111 #else
112  return 0;
113 #endif
114 }
115 
116 inline void set_num_threads([[maybe_unused]] const int n) noexcept {
117 #ifdef _OPENMP
118  ::omp_set_num_threads(n);
119 #endif
120 }
121 
122 } // namespace omp
123 } // namespace metall::utility
124 
125 #endif // METALL_UTILITY_OPEN_MP_HPP
basic_string< char > string
A string container that uses char as its character type and Metall as its default allocator.
Definition: string.hpp:23
void set_num_threads([[maybe_unused]] const int n) noexcept
Definition: open_mp.hpp:116
std::string schedule_kind_name([[maybe_unused]] const omp_sched_type kind)
Definition: open_mp.hpp:44
std::pair< omp_sched_type, int > get_schedule()
Definition: open_mp.hpp:88
int get_thread_num() noexcept
Definition: open_mp.hpp:108
int omp_sched_type
Definition: open_mp.hpp:41
int get_num_threads() noexcept
Definition: open_mp.hpp:100
Namespace for utility items.