Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JMultiThreadedReconstruction.hh
Go to the documentation of this file.
1#ifndef __JRECONSTRUCTION__JMULTITHREADEDRECONSTRUCTION__
2#define __JRECONSTRUCTION__JMULTITHREADEDRECONSTRUCTION__
3
4#include <mutex>
5#include <thread>
6#include <condition_variable>
7#include <vector>
8#include <queue>
9#include <map>
10
12
13#include "JDAQ/JDAQEvaluator.hh"
15
17
18
19/**
20 * \author mdejong
21 */
22
23namespace JRECONSTRUCTION {}
24namespace JPP { using namespace JRECONSTRUCTION; }
25
26namespace JRECONSTRUCTION {
27
28 /**
29 * Thread pool for event-by-event reconstruction.\n
30 * The events are output in order according KM3NETDAQ::getDAQValue.
31 */
32 template<class JFit_t>
34 {
35 public:
36 typedef typename JFit_t::input_type input_type;
38
39
40 /**
41 * Constructor.
42 *
43 * \param fit fit
44 * \param writer writer
45 * \param ns number of threads
46 * \param backlog backlog
47 */
50 const size_t ns,
51 const size_t backlog = std::numeric_limits<size_t>::max()) :
53 stop(false),
55 {
56 using namespace std;
57 using namespace JPP;
58
59 for (size_t id = 0; id < ns; ++id) {
60 output[id] = 0;
61 }
62
63 for (size_t id = 0; id < ns; ++id) {
64
65 thread worker([this, &fit, id]() {
66
67 input_type data;
68
69 for (JFit_t f1(fit); ; ) {
70
71 {
72 unique_lock<mutex> lock(in);
73
74 cv.wait(lock, [this]() { return stop || !input.empty(); });
75
76 if (stop && input.empty()) {
77 return;
78 }
79
80 swap(data, input.front());
81
82 input.pop();
83 }
84
85 cw.notify_one();
86
87 output_type evt(id, data.getDAQEventHeader(), f1(data));
88
89 {
90 unique_lock<mutex> lock(out);
91
92 output.push(evt);
93 }
94 }
95 });
96
97 workers.emplace_back(std::move(worker));
98 }
99 }
100
101
102 /**
103 * Destructor.
104 */
106 {
107 using namespace std;
108
109 {
110 unique_lock<mutex> lock(in);
111
112 stop = true;
113 }
114
115 cv.notify_all();
116
117 for (auto& worker : workers) {
118 worker.join();
119 }
120
121 // write remaining output
122
123 while (!output.empty()) {
124
125 writer.put(output.top());
126
127 output.pop();
128 }
129 }
130
131
132 /**
133 * Add data in queue.
134 *
135 * \param data data
136 */
137 void enqueue(input_type& data)
138 {
139 using namespace std;
140
141 {
142 unique_lock<mutex> lock(in);
143
144 cw.wait(lock, [this]() { return stop || input.size() < backlog; });
145
146 if (stop) {
147 throw runtime_error("The thread pool has been stopped.");
148 }
149
150 input.emplace(std::move(data));
151 }
152
153 cv.notify_one();
154
155 flush();
156 }
157
158
159 /**
160 * Flush output.
161 */
162 void flush()
163 {
164 using namespace std;
165
166 unique_lock<mutex> lock(out);
167
168 while (output.is_ready()) {
169
170 writer.put(output.top());
171
172 output.pop();
173 }
174 }
175
176 private:
177 /**
178 * Output data type.
179 */
180 struct output_type :
181 public JDAQEventHeader,
182 public JEvt
183 {
184 /**
185 * Default constructor.
186 */
188 {}
189
190
191 /**
192 * Constructor.
193 *
194 * \param id thread identifier
195 * \param header header
196 * \param out result values
197 */
198 output_type(const size_t id,
199 const JDAQEventHeader& header,
200 const JEvt& out) :
201 JDAQEventHeader(header),
202 JEvt(out),
203 id(id)
204 {}
205
206
207 /**
208 * Less-than operator for priority queue.
209 *
210 * \param first first event
211 * \param second second event
212 * \return true if first event later then second event
213 */
214 friend inline bool operator<(const output_type& first, const output_type& second)
215 {
217 }
218
219 size_t id;
220 };
221
222
223 /**
224 * Type definition of output queue.
225 */
226 typedef std::priority_queue<output_type, std::vector<output_type> > queue_type;
227
228
229 /**
230 * Auxiliary data structure to maintain time order of events for writing.
231 */
233 public queue_type
234 {
235 /**
236 * Get queue counter.
237 *
238 * \param id thread identifier
239 * \return counter
240 */
241 size_t& operator[](const size_t id)
242 {
243 return queue[id];
244 }
245
246
247 /**
248 * Push object in queue.
249 *
250 * \param object object
251 */
252 void push(output_type& object)
253 {
254 queue[object.id] += 1;
255
256 static_cast<queue_type&>(*this).emplace(std::move(object));
257 }
258
259
260 /**
261 * Pop first element from queue.
262 */
263 void pop()
264 {
265 const output_type& object = this->top();
266
267 queue[object.id] -= 1;
268
269 static_cast<queue_type&>(*this).pop();
270 }
271
272
273 /**
274 * Check readiness.
275 *
276 * \return true if number of entries in queue from each thread not equal to zero; else false
277 */
278 bool is_ready() const
279 {
280 for (const auto& i : queue) {
281 if (i.second == 0) {
282 return false;
283 }
284 }
285
286 return !queue.empty();
287 }
288
289 private:
290 std::map<size_t, size_t> queue; //!< number of entries in queue per thread
291
293
296 std::queue <input_type> input;
297 std::mutex in;
298 std::mutex out;
299 std::condition_variable cv;
300 std::condition_variable cw;
301 bool stop;
302 size_t backlog;
303 };
304}
305
306#endif
307
Data structure for set of track fit results.
Template interface of object output for single data type.
virtual bool put(const T &object)=0
Object output.
Thread pool for event-by-event reconstruction.
JMultiThreadedReconstruction(const JFit_t &fit, writer_type &writer, const size_t ns, const size_t backlog=std::numeric_limits< size_t >::max())
Constructor.
JRECONSTRUCTION::JMultiThreadedReconstruction::queue_type_t output
std::priority_queue< output_type, std::vector< output_type > > queue_type
Type definition of output queue.
const JDAQEventHeader & getDAQEventHeader() const
Get DAQ event header.
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
static const JDAQEvaluator getDAQValue
Function object for evaluation of DAQ objects.
output_type(const size_t id, const JDAQEventHeader &header, const JEvt &out)
Constructor.
friend bool operator<(const output_type &first, const output_type &second)
Less-than operator for priority queue.
Auxiliary data structure to maintain time order of events for writing.
std::map< size_t, size_t > queue
number of entries in queue per thread