aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
OMPThreads.h
Go to the documentation of this file.
1 /**
2  *
3  * Copyright (c) 2005-2021 by Pierre-Henri WUILLEMIN(@LIP6) & Christophe GONZALES(@AMU)
4  * info_at_agrum_dot_org
5  *
6  * This library is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with this library. If not, see <http://www.gnu.org/licenses/>.
18  *
19  */
20 
21 
22 /**
23  * @file
24  * @brief Wrappers for OpenMP.
25  * @author Matthieu HOURBRACQ and Pierre-Henri WUILLEMIN(@LIP6)
26  */
27 
28 #ifndef GUM_OPEM_MP_THREADS_H
29 #define GUM_OPEM_MP_THREADS_H
30 
31 #ifdef _OPENMP
32 # include <omp.h>
33 #endif
34 
35 namespace gum {
36 
37  /**
38  * @brief Is OMP active ?
39  * @ingroup basicstruct_group
40  * @return \c True if OMP has been set at compilation, \c False otherwise.
41  */
42  bool isOMP();
43 
44  /**
45  * @brief Set the number of threads to be used.
46  * @ingroup basicstruct_group
47  *
48  * To avoid spare cycles (less then 100% CPU occupied), use more threads than
49  * logical processors (x2 is a good all-around value).
50  * @param number The number of threads to be used.
51  */
52  void setNumberOfThreads(unsigned int number);
53 
54  /**
55  * @brief Returns the maximum number of threads at any time.
56  * @ingroup basicstruct_group
57  *
58  * Call this from anywhere (parallel region or not). By default, it is the
59  * number of threads launched in any parallel region.
60  *
61  * It should return the number of logical processors by default, i.e.
62  * omp_get_num_procs(). If setNumberOfThreads(number) was called, it will
63  * return the choosen number.
64  *
65  * @return Returns the maximum number of threads at any time.
66  */
67  unsigned int getMaxNumberOfThreads();
68 
69  /**
70  * @brief Get the calling thread id.
71  * @ingroup basicstruct_group
72  *
73  * Call this from a parallel region.
74  * @return The calling thread id.
75  */
76  unsigned int getThreadNumber();
77 
78  /**
79  * @brief Get the current number of running threads.
80  * @ingroup basicstruct_group
81  *
82  * Call this from a parallel region.
83  * @return The current number of running threads.
84  */
85  unsigned int getNumberOfRunningThreads();
86 
87  /**
88  * @brief Get the number of logical processors.
89  * @ingroup basicstruct_group
90  * @return The number of logical processors.
91  */
92  unsigned int getNumberOfLogicalProcessors();
93 
94  /**
95  * @brief Set nested parallelism (false bu default).
96  * @ingroup basicstruct_group
97  *
98  * Nested parallelism, i.e. parallel activity within another parallel
99  * activity : threads creating more threads.
100  *
101  * Off by default.
102  *
103  * @param value \c True if nested parallelism should be activated, \c False
104  * otherwise.
105  */
106  void setNestedParallelism(bool value);
107 
108  /**
109  * @brief Get nested parallelism status.
110  * @ingroup basicstruct_group
111  * @return \c True if nested parallelism is enabled, \c False otherwise.
112  */
113  bool getNestedParallelism();
114 
115  /**
116  * @brief Set the dynamic threads number (false by default).
117  * @ingroup basicstruct_group
118  *
119  * Automatically adjust the number of running threads within a parallel
120  * region.
121  *
122  * Desactivated by default.
123  *
124  * @param value \c True if dynamic thread number should be used, \c False
125  * otherwise.
126  */
127  void setDynamicThreadsNumber(bool value);
128 
129  /**
130  * @brief Get the dynamic thread number adjustment status.
131  * @ingroup basicstruct_group
132  * @ingroup basicstruct_group
133  * @return \c True if dynamic adjustment is enabled, \c False otherwise.
134  */
136 } // namespace gum
137 
138 // include the inlined functions if necessary
139 #ifndef GUM_NO_INLINE
140 # include <agrum/tools/core/OMPThreads_inl.h>
141 #endif /* GUM_NO_INLINE */
142 
143 #endif // GUM_OPEM_MP_THREADS_H
bool isOMP()
Is OMP active ?
unsigned int getNumberOfLogicalProcessors()
Get the number of logical processors.
unsigned int getNumberOfRunningThreads()
Get the current number of running threads.
unsigned int getThreadNumber()
Get the calling thread id.
INLINE void emplace(Args &&... args)
Definition: set_tpl.h:643
bool getDynamicThreadsNumber()
Get the dynamic thread number adjustment status.
void setNestedParallelism(bool value)
Set nested parallelism (false bu default).
unsigned int getMaxNumberOfThreads()
Returns the maximum number of threads at any time.
bool getNestedParallelism()
Get nested parallelism status.
void setDynamicThreadsNumber(bool value)
Set the dynamic threads number (false by default).
void setNumberOfThreads(unsigned int number)
Set the number of threads to be used.