Jpp 19.3.0-rc.2
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< JEvtwriter_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.
 

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_typewriter
 
std::vector< std::thread > workers
 
std::queue< input_typeinput
 
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.

Definition at line 32 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 35 of file JMultiThreadedReconstruction.hh.

◆ writer_type

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

Definition at line 36 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 216 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 47 of file JMultiThreadedReconstruction.hh.

50 :
52 stop(false),
54 {
55 using namespace std;
56 using namespace JPP;
57
58 for (size_t id = 0; id < ns; ++id) {
59 output[id] = 0;
60 }
61
62 for (size_t id = 0; id < ns; ++id) {
63
64 thread worker([this, fit, id]() {
65
67
68 for (JFit_t f1(fit); ; ) {
69
70 {
71 unique_lock<mutex> lock(in);
72
73 cv.wait(lock, [this]() { return stop || !input.empty(); });
74
75 if (stop && input.empty()) {
76 return;
77 }
78
79 swap(data, input.front());
80
81 input.pop();
82 }
83
84 cw.notify_one();
85
86 output_type evt(id, data.getDAQEventHeader(), f1(data));
87
88 {
89 unique_lock<mutex> lock(out);
90
91 output.push(evt);
92 }
93 }
94 });
95
96 workers.emplace_back(std::move(worker));
97 }
98 }
JRECONSTRUCTION::JMultiThreadedReconstruction::queue_type_t output
const JPolynome f1(1.0, 2.0, 3.0)
Function.
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 104 of file JMultiThreadedReconstruction.hh.

105 {
106 using namespace std;
107
108 {
109 unique_lock<mutex> lock(in);
110
111 stop = true;
112 }
113
114 cv.notify_all();
115
116 for (auto& worker : workers) {
117 worker.join();
118 }
119
120 // write remaining output
121
122 while (!output.empty()) {
123
124 writer.put(output.top());
125
126 output.pop();
127 }
128 }
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 136 of file JMultiThreadedReconstruction.hh.

137 {
138 using namespace std;
139
140 {
141 unique_lock<mutex> lock(in);
142
143 cw.wait(lock, [this]() { return stop || input.size() < backlog; });
144
145 if (stop) {
146 throw runtime_error("The thread pool has been stopped.");
147 }
148
149 input.emplace(std::move(data));
150 }
151
152 cv.notify_one();
153
154 {
155 unique_lock<mutex> lock(out);
156
157 while (output.is_ready()) {
158
159 writer.put(output.top());
160
161 output.pop();
162 }
163 }
164 }

Member Data Documentation

◆ output

◆ writer

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

Definition at line 284 of file JMultiThreadedReconstruction.hh.

◆ workers

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

Definition at line 285 of file JMultiThreadedReconstruction.hh.

◆ input

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

Definition at line 286 of file JMultiThreadedReconstruction.hh.

◆ in

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

Definition at line 287 of file JMultiThreadedReconstruction.hh.

◆ out

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

Definition at line 288 of file JMultiThreadedReconstruction.hh.

◆ cv

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

Definition at line 289 of file JMultiThreadedReconstruction.hh.

◆ cw

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

Definition at line 290 of file JMultiThreadedReconstruction.hh.

◆ stop

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

Definition at line 291 of file JMultiThreadedReconstruction.hh.

◆ backlog

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

Definition at line 292 of file JMultiThreadedReconstruction.hh.


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