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  Copyright (C) 2014 by Andreas Lauser
5 
6  This file is part of the Open Porous Media project (OPM).
7 
8  OPM is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 2 of the License, or
11  (at your option) any later version.
12 
13  OPM is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with OPM. If not, see <http://www.gnu.org/licenses/>.
20 */
25 #ifndef EWOMS_THREAD_MANAGER_HH
26 #define EWOMS_THREAD_MANAGER_HH
27 
28 #ifdef _OPENMP
29 #include <omp.h>
30 #endif
31 
32 #include <ewoms/parallel/locks.hh>
34 
36 
37 #include <dune/common/version.hh>
38 
39 namespace Ewoms {
40 namespace Properties {
41 NEW_PROP_TAG(ThreadsPerProcess);
42 }
43 
47 template <class TypeTag>
49 {
50 public:
51  enum {
52 #if defined(_OPENMP) || DOXYGEN
53  isFake = false
55 #else
56  isFake = true
57 #endif
58  };
59 
63  static void registerParameters()
64  {
65  EWOMS_REGISTER_PARAM(TypeTag, int, ThreadsPerProcess,
66  "The maximum number of threads to be instantiated per process "
67  "('-1' means 'automatic')");
68  }
69 
70  static void init()
71  {
72  numThreads_ = EWOMS_GET_PARAM(TypeTag, int, ThreadsPerProcess);
73 
74  // some safety checks. This is pretty ugly macro-magic, but so what?
75 #if !defined(_OPENMP)
76  if (numThreads_ != 1 && numThreads_ != -1)
77  OPM_THROW(std::invalid_argument,
78  "OpenMP is not available. The only valid values for "
79  "threads-per-process is 1 and -1 but it is " << numThreads_ << "!");
80  numThreads_ = 1;
81 #elif !DUNE_VERSION_NEWER(DUNE_COMMON, 2,4) && !defined NDEBUG
82  if (numThreads_ != 1)
83  OPM_THROW(std::invalid_argument,
84  "You seem to be using Dune "
85  <<DUNE_COMMON_VERSION_MAJOR<<"."<<DUNE_COMMON_VERSION_MINOR
86  << " in debug mode and with the number of OpenMP threads larger than 1. "
87  "This Dune version does not support thread parallelism in debug mode!");
88  numThreads_ = 1;
89 
90 #elif DUNE_VERSION_NEWER(DUNE_COMMON, 2,4) && !defined NDEBUG && defined DUNE_INTERFACECHECK
91  if (numThreads_ != 1)
92  OPM_THROW(std::invalid_argument,
93  "You explicitly enabled Barton-Nackman interface checking in Dune. "
94  "The Dune implementation of this is currently incompatible with "
95  "thread parallelism!");
96  numThreads_ = 1;
97 #else
98  if (numThreads_ == 0)
99  OPM_THROW(std::invalid_argument,
100  "Zero threads per process are not possible: It must be at least 1, "
101  "(or -1 for 'automatic')!");
102 #endif
103 
104 #ifdef _OPENMP
105  // actually limit the number of threads and get the number of threads which are
106  // used in the end.
107  if (numThreads_ > 0)
108  omp_set_num_threads(numThreads_);
109 
110  numThreads_ = omp_get_max_threads();
111 #endif
112  }
113 
117  static int maxThreads()
118  { return numThreads_; }
119 
123  static int threadId()
124  {
125 #ifdef _OPENMP
126  return omp_get_thread_num();
127 #else
128  return 0;
129 #endif
130  }
131 
132 private:
133  static int numThreads_;
134 };
135 
136 template <class TypeTag>
137 int ThreadManager<TypeTag>::numThreads_ = 1;
138 } // namespace Ewoms
139 
140 #endif
This file implements various objects which provide mutual exclusion capabilities for multi-threaded a...
static void registerParameters()
Register all run-time parameters of the thread manager.
Definition: threadmanager.hh:63
static void init()
Definition: threadmanager.hh:70
Simplifies multi-threaded capabilities.
Definition: threadmanager.hh:48
Specify whether OpenMP is really available or not.
Definition: threadmanager.hh:54
NEW_PROP_TAG(Grid)
The type of the DUNE grid.
This file provides the infrastructure to retrieve run-time parameters.
Definition: baseauxiliarymodule.hh:35
static int maxThreads()
Return the maximum number of threads of the current process.
Definition: threadmanager.hh:117
#define EWOMS_REGISTER_PARAM(TypeTag, ParamType, ParamName, Description)
Register a run-time parameter.
Definition: parametersystem.hh:64
static int threadId()
Return the index of the current OpenMP thread.
Definition: threadmanager.hh:123
Provides the magic behind the eWoms property system.
#define EWOMS_GET_PARAM(TypeTag, ParamType, ParamName)
Retrieve a runtime parameter.
Definition: parametersystem.hh:95