Jpp 21.0.0
the software that should make you happy
Loading...
Searching...
No Matches
JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t > Class Template Reference

Thread pool for event-by-event reconstruction. More...

#include <JMultiThreadedReconstruction.hh>

Classes

struct  output_type
 Output data type. More...
 
struct  queue_type_t
 Auxiliary data structure to maintain time order of events for writing. More...
 

Public Types

typedef JFit_t::input_type input_type
 
typedef JLANG::JObjectOutput< JEvt > writer_type
 

Public Member Functions

 JMultiThreadedReconstruction (const JFit_t &fit, writer_type &writer, const size_t ns, const size_t backlog=std::numeric_limits< size_t >::max())
 Constructor.
 
 ~JMultiThreadedReconstruction ()
 Destructor.
 
void enqueue (input_type &data)
 Add data in queue.
 
void flush ()
 Flush output.
 

Private Types

typedef std::priority_queue< output_type, std::vector< output_type > > queue_type
 Type definition of output queue.
 

Private Attributes

JRECONSTRUCTION::JMultiThreadedReconstruction::queue_type_t output
 
writer_type & writer
 
std::vector< std::thread > workers
 
std::queue< input_type > input
 
std::mutex in
 
std::mutex out
 
std::condition_variable cv
 
std::condition_variable cw
 
bool stop
 
size_t backlog
 

Detailed Description

template<class JFit_t>
class JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >

Thread pool for event-by-event reconstruction.


The events are output in order according KM3NETDAQ::getDAQValue.

Definition at line 33 of file JMultiThreadedReconstruction.hh.

Member Typedef Documentation

◆ input_type

template<class JFit_t >
JFit_t::input_type JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::input_type

Definition at line 36 of file JMultiThreadedReconstruction.hh.

◆ writer_type

template<class JFit_t >
JLANG::JObjectOutput<JEvt> JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::writer_type

Definition at line 37 of file JMultiThreadedReconstruction.hh.

◆ queue_type

template<class JFit_t >
std::priority_queue<output_type, std::vector<output_type> > JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::queue_type
private

Type definition of output queue.

Definition at line 226 of file JMultiThreadedReconstruction.hh.

Constructor & Destructor Documentation

◆ JMultiThreadedReconstruction()

template<class JFit_t >
JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::JMultiThreadedReconstruction ( const JFit_t & fit,
writer_type & writer,
const size_t ns,
const size_t backlog = std::numeric_limits<size_t>::max() )
inline

Constructor.

Parameters
fitfit
writerwriter
nsnumber of threads
backlogbacklog

Definition at line 48 of file JMultiThreadedReconstruction.hh.

51 :
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
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 }
JRECONSTRUCTION::JMultiThreadedReconstruction::queue_type_t output
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).

◆ ~JMultiThreadedReconstruction()

template<class JFit_t >
JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::~JMultiThreadedReconstruction ( )
inline

Destructor.

Definition at line 105 of file JMultiThreadedReconstruction.hh.

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 }
virtual bool put(const T &object)=0
Object output.

Member Function Documentation

◆ enqueue()

template<class JFit_t >
void JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::enqueue ( input_type & data)
inline

Add data in queue.

Parameters
datadata

Definition at line 137 of file JMultiThreadedReconstruction.hh.

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 }

◆ flush()

template<class JFit_t >
void JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::flush ( )
inline

Flush output.

Definition at line 162 of file JMultiThreadedReconstruction.hh.

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 }

Member Data Documentation

◆ output

◆ writer

template<class JFit_t >
writer_type& JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::writer
private

Definition at line 294 of file JMultiThreadedReconstruction.hh.

◆ workers

template<class JFit_t >
std::vector<std::thread> JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::workers
private

Definition at line 295 of file JMultiThreadedReconstruction.hh.

◆ input

template<class JFit_t >
std::queue<input_type> JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::input
private

Definition at line 296 of file JMultiThreadedReconstruction.hh.

◆ in

template<class JFit_t >
std::mutex JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::in
private

Definition at line 297 of file JMultiThreadedReconstruction.hh.

◆ out

template<class JFit_t >
std::mutex JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::out
private

Definition at line 298 of file JMultiThreadedReconstruction.hh.

◆ cv

template<class JFit_t >
std::condition_variable JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::cv
private

Definition at line 299 of file JMultiThreadedReconstruction.hh.

◆ cw

template<class JFit_t >
std::condition_variable JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::cw
private

Definition at line 300 of file JMultiThreadedReconstruction.hh.

◆ stop

template<class JFit_t >
bool JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::stop
private

Definition at line 301 of file JMultiThreadedReconstruction.hh.

◆ backlog

template<class JFit_t >
size_t JRECONSTRUCTION::JMultiThreadedReconstruction< JFit_t >::backlog
private

Definition at line 302 of file JMultiThreadedReconstruction.hh.


The documentation for this class was generated from the following file: