tasklets.hpp
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 OPM_TASKLETS_HPP
28#define OPM_TASKLETS_HPP
29
30#include <atomic>
31#include <thread>
32#include <queue>
33#include <mutex>
34#include <condition_variable>
35
36namespace Opm {
37
44{
45public:
46 TaskletInterface(int refCount = 1)
47 : referenceCount_(refCount)
48 {}
49 virtual ~TaskletInterface() {}
50 virtual void run() = 0;
51 virtual bool isEndMarker () const { return false; }
52
54 { -- referenceCount_; }
55
56 int referenceCount() const
57 { return referenceCount_; }
58
59private:
60 int referenceCount_;
61};
62
67template <class Fn>
69{
70public:
72 FunctionRunnerTasklet(int numInvocations, const Fn& fn)
73 : TaskletInterface(numInvocations)
74 , fn_(fn)
75 {}
76 void run() override
77 { fn_(); }
78
79private:
80 const Fn& fn_;
81};
82
90{
92 class BarrierTasklet : public TaskletInterface
93 {
94 public:
95 BarrierTasklet(unsigned numWorkers);
96
97 void run();
98
99 void wait();
100
101 private:
102 unsigned numWorkers_;
103 unsigned numWaiting_;
104
105 std::condition_variable barrierCondition_;
106 std::mutex barrierMutex_;
107 };
108
111 class TerminateThreadTasklet : public TaskletInterface
112 {
113 public:
114 void run()
115 { }
116
117 bool isEndMarker() const
118 { return true; }
119 };
120
121public:
122 // prohibit copying of tasklet runners
123 TaskletRunner(const TaskletRunner&) = delete;
124
131 TaskletRunner(unsigned numWorkers);
132
141
142 bool failure() const;
143
149 int workerThreadIndex() const;
150
155 { return threads_.size(); }
156
162 void dispatch(std::shared_ptr<TaskletInterface> tasklet);
163
167 template <class Fn>
168 std::shared_ptr<FunctionRunnerTasklet<Fn> > dispatchFunction(Fn &fn, int numInvocations=1)
169 {
170 using Tasklet = FunctionRunnerTasklet<Fn>;
171 auto tasklet = std::make_shared<Tasklet>(numInvocations, fn);
172 this->dispatch(tasklet);
173 return tasklet;
174 }
175
179 void barrier();
180
181private:
182 // Atomic flag that is set to failure if any of the tasklets run by the TaskletRunner fails.
183 // This flag is checked before new tasklets run or get dispatched and in case it is true, the
184 // thread execution will be stopped / no new tasklets will be started and the program will abort.
185 // To set the flag and load the flag, we use std::memory_order_relaxed.
186 // Atomic operations tagged memory_order_relaxed are not synchronization operations; they do not
187 // impose an order among concurrent memory accesses. They guarantee atomicity and modification order
188 // consistency. This is the right choice for the setting here, since it is enough to broadcast failure
189 // before new run or get dispatched.
190 std::atomic<bool> failureFlag_ = false;
191
192protected:
193 // main function of the worker thread
194 static void startWorkerThread_(TaskletRunner* taskletRunner, int workerThreadIndex);
195
197 void run_();
198
199 std::vector<std::unique_ptr<std::thread> > threads_;
200 std::queue<std::shared_ptr<TaskletInterface> > taskletQueue_;
202 std::condition_variable workAvailableCondition_;
203
204 static thread_local TaskletRunner* taskletRunner_;
205 static thread_local int workerThreadIndex_;
206};
207
208} // end namespace Opm
209
210#endif // OPM_TASKLETS_HPP
A simple tasklet that runs a function that returns void and does not take any arguments a given numbe...
Definition: tasklets.hpp:69
FunctionRunnerTasklet(int numInvocations, const Fn &fn)
Definition: tasklets.hpp:72
FunctionRunnerTasklet(const FunctionRunnerTasklet &)=default
void run() override
Definition: tasklets.hpp:76
The base class for tasklets.
Definition: tasklets.hpp:44
virtual bool isEndMarker() const
Definition: tasklets.hpp:51
TaskletInterface(int refCount=1)
Definition: tasklets.hpp:46
int referenceCount() const
Definition: tasklets.hpp:56
void dereference()
Definition: tasklets.hpp:53
virtual ~TaskletInterface()
Definition: tasklets.hpp:49
virtual void run()=0
Handles where a given tasklet is run.
Definition: tasklets.hpp:90
int workerThreadIndex() const
Returns the index of the current worker thread.
bool failure() const
static thread_local int workerThreadIndex_
Definition: tasklets.hpp:205
int numWorkerThreads() const
Returns the number of worker threads for the tasklet runner.
Definition: tasklets.hpp:154
void run_()
do the work until the queue received an end tasklet
std::mutex taskletQueueMutex_
Definition: tasklets.hpp:201
~TaskletRunner()
Destructor.
static thread_local TaskletRunner * taskletRunner_
Definition: tasklets.hpp:204
std::condition_variable workAvailableCondition_
Definition: tasklets.hpp:202
void barrier()
Make sure that all tasklets have been completed after this method has been called.
std::shared_ptr< FunctionRunnerTasklet< Fn > > dispatchFunction(Fn &fn, int numInvocations=1)
Convenience method to construct a new function runner tasklet and dispatch it immediately.
Definition: tasklets.hpp:168
void dispatch(std::shared_ptr< TaskletInterface > tasklet)
Add a new tasklet.
TaskletRunner(unsigned numWorkers)
Creates a tasklet runner with numWorkers underling threads for doing work.
static void startWorkerThread_(TaskletRunner *taskletRunner, int workerThreadIndex)
std::vector< std::unique_ptr< std::thread > > threads_
Definition: tasklets.hpp:199
TaskletRunner(const TaskletRunner &)=delete
std::queue< std::shared_ptr< TaskletInterface > > taskletQueue_
Definition: tasklets.hpp:200
Definition: blackoilboundaryratevector.hh:37