QMCPACK
UtilityFunctions.hpp
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source
3 // License. See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2019 QMCPACK developers.
6 //
7 // File developed by:
8 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
9 //
10 // File created by:
11 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Lab
12 ////////////////////////////////////////////////////////////////////////////////
13 
14 #ifndef QMCPLUSPLUS_UTILITYFUNCTIONS_HPP
15 #define QMCPLUSPLUS_UTILITYFUNCTIONS_HPP
16 /** @file
17  * @brief utility functions for executors
18  */
19 
20 #include "Concurrency/Info.hpp"
21 #include "Concurrency/OpenMP.h"
22 
23 namespace qmcplusplus
24 {
25 namespace Concurrency
26 {
27 /** A service class to restore active avaiable threads upon destruction as the thread count recorded during construction.
28  */
29 template<Executor TT = Executor::OPENMP>
31 {
33 };
34 
35 template<>
37 {
38 private:
40 
41 public:
42  ThreadCountProtector() : original_max_threads_(omp_get_max_threads()) {}
43 
44  ~ThreadCountProtector() { omp_set_num_threads(original_max_threads_); }
45 };
46 
47 /** A service class to override active avaiable threads upon construction.
48  * Restore active avaiable threads upon destruction as the thread count recorded during construction.
49  */
50 template<Executor TT = Executor::OPENMP>
52 {
53  OverrideMaxCapacity(int max_threads) {}
54 };
55 
56 template<>
57 class OverrideMaxCapacity<Executor::OPENMP> : private ThreadCountProtector<Executor::OPENMP>
58 {
59 public:
60  OverrideMaxCapacity(int max_threads) { omp_set_num_threads(max_threads); }
61 };
62 
63 } // namespace Concurrency
64 } // namespace qmcplusplus
65 
66 #endif
Abstraction of information on executor environments.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
void omp_set_num_threads(int num_threads)
Definition: OpenMP.h:31
A service class to restore active avaiable threads upon destruction as the thread count recorded duri...
omp_int_t omp_get_max_threads()
Definition: OpenMP.h:26
A service class to override active avaiable threads upon construction.