All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
threadmanager.hh
Go to the documentation of this file.
1 // -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 // vi: set et ts=4 sw=4 sts=4:
3 /*
4  This file is part of the Open Porous Media project (OPM).
5 
6  OPM is free software: you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation, either version 2 of the License, or
9  (at your option) any later version.
10 
11  OPM 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 General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with OPM. If not, see <http://www.gnu.org/licenses/>.
18 
19  Consult the COPYING file in the top-level source directory of this
20  module for the precise wording of the license and the list of
21  copyright holders.
22 */
27 #ifndef EWOMS_THREAD_MANAGER_HH
28 #define EWOMS_THREAD_MANAGER_HH
29 
30 #ifdef _OPENMP
31 #include <omp.h>
32 #endif
33 
34 #include <ewoms/parallel/locks.hh>
37 
38 #include <opm/common/ErrorMacros.hpp>
39 #include <opm/common/Exceptions.hpp>
40 
41 #include <dune/common/version.hh>
42 
43 namespace Ewoms {
44 namespace Properties {
45 NEW_PROP_TAG(ThreadsPerProcess);
46 }
47 
51 template <class TypeTag>
53 {
54 public:
55  enum {
56 #if defined(_OPENMP) || DOXYGEN
57  isFake = false
59 #else
60  isFake = true
61 #endif
62  };
63 
67  static void registerParameters()
68  {
69  EWOMS_REGISTER_PARAM(TypeTag, int, ThreadsPerProcess,
70  "The maximum number of threads to be instantiated per process "
71  "('-1' means 'automatic')");
72  }
73 
74  static void init()
75  {
76  numThreads_ = EWOMS_GET_PARAM(TypeTag, int, ThreadsPerProcess);
77 
78  // some safety checks. This is pretty ugly macro-magic, but so what?
79 #if !defined(_OPENMP)
80  if (numThreads_ != 1 && numThreads_ != -1)
81  OPM_THROW(std::invalid_argument,
82  "OpenMP is not available. The only valid values for "
83  "threads-per-process is 1 and -1 but it is " << numThreads_ << "!");
84  numThreads_ = 1;
85 #elif !defined NDEBUG && defined DUNE_INTERFACECHECK
86  if (numThreads_ != 1)
87  OPM_THROW(std::invalid_argument,
88  "You explicitly enabled Barton-Nackman interface checking in Dune. "
89  "The Dune implementation of this is currently incompatible with "
90  "thread parallelism!");
91  numThreads_ = 1;
92 #else
93 
94  if (numThreads_ == 0)
95  OPM_THROW(std::invalid_argument,
96  "Zero threads per process are not possible: It must be at least 1, "
97  "(or -1 for 'automatic')!");
98 #endif
99 
100 #ifdef _OPENMP
101  // actually limit the number of threads and get the number of threads which are
102  // used in the end.
103  if (numThreads_ > 0)
104  omp_set_num_threads(numThreads_);
105 
106  numThreads_ = omp_get_max_threads();
107 #endif
108  }
109 
113  static unsigned maxThreads()
114  { return static_cast<unsigned>(numThreads_); }
115 
119  static unsigned threadId()
120  {
121 #ifdef _OPENMP
122  return static_cast<unsigned>(omp_get_thread_num());
123 #else
124  return 0;
125 #endif
126  }
127 
128 private:
129  static int numThreads_;
130 };
131 
132 template <class TypeTag>
133 int ThreadManager<TypeTag>::numThreads_ = 1;
134 } // namespace Ewoms
135 
136 #endif
This file implements various objects which provide mutual exclusion capabilities for multi-threaded a...
Simplifies multi-threaded capabilities.
Definition: threadmanager.hh:52
static unsigned threadId()
Return the index of the current OpenMP thread.
Definition: threadmanager.hh:119
#define EWOMS_REGISTER_PARAM(TypeTag, ParamType, ParamName, Description)
Register a run-time parameter.
Definition: parametersystem.hh:68
This file provides the infrastructure to retrieve run-time parameters.
static unsigned maxThreads()
Return the maximum number of threads of the current process.
Definition: threadmanager.hh:113
#define EWOMS_GET_PARAM(TypeTag, ParamType, ParamName)
Retrieve a runtime parameter.
Definition: parametersystem.hh:99
Provides the magic behind the eWoms property system.
#define NEW_PROP_TAG(PTagName)
Define a property tag.
Definition: propertysystem.hh:247
static void registerParameters()
Register all run-time parameters of the thread manager.
Definition: threadmanager.hh:67