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
37
38#include <dune/common/version.hh>
39
40namespace Opm {
41
45template <class TypeTag>
47{
48public:
49 enum {
50#if defined(_OPENMP) || DOXYGEN
52 isFake = false
53#else
54 isFake = true
55#endif
56 };
57
61 static void registerParameters()
62 {
63 Parameters::Register<Parameters::ThreadsPerProcess>
64 ("The maximum number of threads to be instantiated per process "
65 "('-1' means 'automatic')");
66 }
67
77 static void init(bool queryCommandLineParameter = true)
78 {
79 if (queryCommandLineParameter)
80 {
81 numThreads_ = Parameters::Get<Parameters::ThreadsPerProcess>();
82
83 // some safety checks. This is pretty ugly macro-magic, but so what?
84#if !defined(_OPENMP)
85 if (numThreads_ != 1 && numThreads_ != -1)
86 throw std::invalid_argument("OpenMP is not available. The only valid values for "
87 "threads-per-process is 1 and -1 but it is "+std::to_string(numThreads_)+"!");
88 numThreads_ = 1;
89#elif !defined NDEBUG && defined DUNE_INTERFACECHECK
90 if (numThreads_ != 1)
91 throw std::invalid_argument("You explicitly enabled Barton-Nackman interface checking in Dune. "
92 "The Dune implementation of this is currently incompatible with "
93 "thread parallelism!");
94 numThreads_ = 1;
95#else
96
97 if (numThreads_ == 0)
98 throw std::invalid_argument("Zero threads per process are not possible: It must be at least 1, "
99 "(or -1 for 'automatic')!");
100#endif
101
102#ifdef _OPENMP
103 // actually limit the number of threads
104 if (numThreads_ > 0)
105 omp_set_num_threads(numThreads_);
106#endif
107 }
108
109#ifdef _OPENMP
110 // get the number of threads which are used in the end.
111 numThreads_ = omp_get_max_threads();
112#endif
113 }
114
118 static unsigned maxThreads()
119 { return static_cast<unsigned>(numThreads_); }
120
124 static unsigned threadId()
125 {
126#ifdef _OPENMP
127 return static_cast<unsigned>(omp_get_thread_num());
128#else
129 return 0;
130#endif
131 }
132
133private:
134 static int numThreads_;
135};
136
137template <class TypeTag>
138int ThreadManager<TypeTag>::numThreads_ = 1;
139} // namespace Opm
140
141#endif
Simplifies multi-threaded capabilities.
Definition: threadmanager.hh:47
@ isFake
Specify whether OpenMP is really available or not.
Definition: threadmanager.hh:52
static unsigned maxThreads()
Return the maximum number of threads of the current process.
Definition: threadmanager.hh:118
static void init(bool queryCommandLineParameter=true)
Initialize number of threads used thread manager.
Definition: threadmanager.hh:77
static unsigned threadId()
Return the index of the current OpenMP thread.
Definition: threadmanager.hh:124
static void registerParameters()
Register all run-time parameters of the thread manager.
Definition: threadmanager.hh:61
Declare the properties used by the infrastructure code of the finite volume discretizations.
Definition: blackoilboundaryratevector.hh:37
This file provides the infrastructure to retrieve run-time parameters.
The Opm property system, traits with inheritance.