SMT-RAT  24.02
Toolbox for Strategic and Parallel Satisfiability-Modulo-Theories Solving
ThreadPool.h
Go to the documentation of this file.
1 /**
2  * @file ThreadPool.h
3  *
4  * @author Gereon Kremer
5  * @since 2016-03-18
6  */
7 
8 #pragma once
9 
11 
12 #include <chrono>
13 #include <condition_variable>
14 #include <future>
15 #include <stack>
16 #include <tuple>
17 #include <vector>
18 #include <queue>
19 
20 namespace smtrat {
21 
22 class Module;
23 
24 using Priority = std::vector<std::size_t>;
25 
26 class Task {
27 private:
28  std::packaged_task<Answer()> mTask;
29  const Module* mModule;
30  std::size_t mConditionalIndex;
31 public:
32  template<typename T>
33  Task(T&& task, const Module* module, std::size_t _index): mTask(std::move(task)), mModule(module), mConditionalIndex(_index) {}
34 
35  void run() {
36  mTask();
37  }
38 
39  const Module* getModule() const {
40  return mModule;
41  }
42 
43  std::size_t conditionalIndex() const {
44  return mConditionalIndex;
45  }
46 
47  std::future<Answer> getFuture() {
48  return mTask.get_future();
49  }
50 
51  bool operator<(const Task& rhs) const;
52 };
53 
55 private:
56  std::condition_variable mConditionVariable;
57  std::mutex mMutex;
58  bool mFireFlag;
59 public:
61  void wait() {
62  std::unique_lock<std::mutex> lock(mMutex);
63  mConditionVariable.wait(lock, [&](){ return mFireFlag; });
64  }
65  void notify() {
66  std::lock_guard<std::mutex> lock(mMutex);
67  mFireFlag = true;
68  mConditionVariable.notify_one();
69  }
70 };
71 
72 class ThreadPool {
73 private:
74  ///
75  const std::size_t mMaxThreads;
76  /// Initialized with 1: There is always the main thread in the beginning.
77  std::atomic<std::size_t> mCounter;
78  /// Initialized with 1: There is always the main thread in the beginning.
79  std::atomic<std::size_t> mNumberThreads;
80  ///
82  ///
83  std::vector<BackendSynchronisation*> mBackendSynchros;
84  ///
85  std::mutex mQueueMutex;
86  ///
88  ///
89  std::priority_queue<Task*> mQueue;
90 
91  bool shallBeSkipped(std::size_t index) {
92  std::lock_guard<std::mutex> bsLock(mBackendSynchrosMutex);
93  return index >= mBackendSynchros.size() || mBackendSynchros[index] == nullptr;
94  }
95  bool notify(std::size_t index) {
96  std::lock_guard<std::mutex> bsLock(mBackendSynchrosMutex);
97  if (index < mBackendSynchros.size() && mBackendSynchros[index] != nullptr) {
98  mBackendSynchros[index]->notify();
99  return true;
100  }
101  return false;
102  }
103 
104  /**
105  *
106  * @param task
107  */
108  void runTask(Task* task);
109  void deleteTask(Task* task) {
110  delete task;
111  --mNumberThreads;
112  }
113 
114  /**
115  *
116  * @param task
117  */
118  void submitBackend(Task* task);
119 public:
120  ThreadPool(std::size_t maxThreads): mMaxThreads(maxThreads), mCounter(1), mNumberThreads(1) {}
121 
123  {
124  while(mNumberThreads>1);
125  }
126 
127  /**
128  * @param _modules
129  * @param _final
130  * @param _full
131  * @param _objective
132  */
133  Answer runBackends(const std::vector<Module*>& _modules, bool _final, bool _full, carl::Variable _objective);
134 };
135 
136 }
std::condition_variable mConditionVariable
Definition: ThreadPool.h:56
A base class for all kind of theory solving methods.
Definition: Module.h:70
Task(T &&task, const Module *module, std::size_t _index)
Definition: ThreadPool.h:33
bool operator<(const Task &rhs) const
Definition: ThreadPool.cpp:13
void run()
Definition: ThreadPool.h:35
std::packaged_task< Answer()> mTask
Definition: ThreadPool.h:28
std::future< Answer > getFuture()
Definition: ThreadPool.h:47
std::size_t mConditionalIndex
Definition: ThreadPool.h:30
const Module * getModule() const
Definition: ThreadPool.h:39
const Module * mModule
Definition: ThreadPool.h:29
std::size_t conditionalIndex() const
Definition: ThreadPool.h:43
std::mutex mQueueMutex
Definition: ThreadPool.h:85
std::vector< BackendSynchronisation * > mBackendSynchros
Definition: ThreadPool.h:83
void submitBackend(Task *task)
Definition: ThreadPool.cpp:44
bool notify(std::size_t index)
Definition: ThreadPool.h:95
std::mutex mThreadCreationMutex
Definition: ThreadPool.h:87
std::atomic< std::size_t > mNumberThreads
Initialized with 1: There is always the main thread in the beginning.
Definition: ThreadPool.h:79
Answer runBackends(const std::vector< Module * > &_modules, bool _final, bool _full, carl::Variable _objective)
Definition: ThreadPool.cpp:55
const std::size_t mMaxThreads
Definition: ThreadPool.h:75
std::atomic< std::size_t > mCounter
Initialized with 1: There is always the main thread in the beginning.
Definition: ThreadPool.h:77
void runTask(Task *task)
Definition: ThreadPool.cpp:17
ThreadPool(std::size_t maxThreads)
Definition: ThreadPool.h:120
std::priority_queue< Task * > mQueue
Definition: ThreadPool.h:89
std::mutex mBackendSynchrosMutex
Definition: ThreadPool.h:81
void deleteTask(Task *task)
Definition: ThreadPool.h:109
bool shallBeSkipped(std::size_t index)
Definition: ThreadPool.h:91
Class to create the formulas for axioms.
std::vector< std::size_t > Priority
Definition: ThreadPool.h:24
Answer
An enum with the possible answers a Module can give.
Definition: types.h:57