Jpp  18.3.1
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JFremantle_t.hh
Go to the documentation of this file.
1 #ifndef __JACOUSTICS__JFREMANTLE_T__
2 #define __JACOUSTICS__JFREMANTLE_T__
3 
4 #include <string>
5 #include <type_traits>
6 #include <functional>
7 #include <future>
8 #include <mutex>
9 #include <thread>
10 #include <vector>
11 #include <queue>
12 #include <memory>
13 #include <limits>
14 
15 #include "JLang/JObjectOutput.hh"
16 
17 #include "JAcoustics/JHit.hh"
18 #include "JAcoustics/JGlobalfit.hh"
19 #include "JAcoustics/JSuperEvt.hh"
21 
22 
23 namespace JACOUSTICS {}
24 namespace JPP { using namespace JACOUSTICS; }
25 
26 namespace JACOUSTICS {
27 
28  /**
29  * Thread pool for global fits.
30  */
31  class JFremantle {
32  public:
33 
36 
37 
38  /**
39  * Constructor.
40  *
41  * \param geometry detector geometry
42  * \param velocity sound velocity
43  * \param parameters parameters
44  * \param ns number of threads
45  * \param backlog backlog
46  */
47  JFremantle(const JGeometry& geometry,
48  const JSoundVelocity& velocity,
50  const size_t ns,
51  const size_t backlog = std::numeric_limits<size_t>::max()) :
52  stop(false),
54  {
55  using namespace std;
56  using namespace JPP;
57 
58  Q.reset();
59 
60  for (size_t i = 0; i < ns; ++i) {
61 
62  thread worker([this, geometry, velocity, parameters]() {
63 
65 
66  for (JGlobalfit katoomba(geometry, velocity, parameters); ; ) {
67 
68  {
69  unique_lock<mutex> lock(in);
70 
71  cv.wait(lock, [this]() { return stop || !input.empty(); });
72 
73  if (stop && input.empty()) {
74  return;
75  }
76 
77  data.swap(input.front());
78 
79  input.pop();
80  }
81 
82  cw.notify_one();
83 
84  const auto result = katoomba(data.begin(), data.end());
85 
86  if (result.chi2 / result.ndf <= katoomba.parameters.chi2perNDF) {
87 
88  {
89  unique_lock<mutex> lock(out);
90 
91  Q.put(result.chi2 / result.ndf);
92 
93  if (JFremantle::output != NULL) {
95  result.getTimeRange(),
96  data .size(),
97  result.size(),
98  result.value.getN(),
99  result.ndf,
100  result.chi2),
101  result.value,
102  result.begin,
103  JFremantle::squash ? result.begin : result.end));
104  }
105  }
106  }
107  }
108  });
109 
110  workers.emplace_back(std::move(worker));
111  }
112  }
113 
114 
115  /**
116  * Destructor.
117  */
119  {
120  using namespace std;
121 
122  {
123  unique_lock<mutex> lock(in);
124 
125  stop = true;
126  }
127 
128  cv.notify_all();
129 
130  for (auto& worker : workers) {
131  worker.join();
132  }
133  }
134 
135 
136  /**
137  * Queue data.
138  *
139  * \param data data
140  */
142  {
143  using namespace std;
144 
145  {
146  unique_lock<mutex> lock(in);
147 
148  cw.wait(lock, [this]() { return stop || input.size() <= backlog; });
149 
150  if (stop) {
151  throw runtime_error("The thread pool has been stopped.");
152  }
153 
154  input.emplace(std::move(data));
155  }
156 
157  cv.notify_one();
158  }
159 
160  static std::string oid; //!< detector identifier
161  static JQuantile Q; //!< chi2/NDF
162  static bool squash; //!< squash transmissions in output
163  static output_type* output; //!< optional output
164 
165  private:
167  std::queue <input_type> input;
168  std::mutex in;
169  std::mutex out;
170  std::condition_variable cv;
171  std::condition_variable cw;
172  bool stop;
173  size_t backlog;
174  };
175 }
176 
177 #endif
Fit function of acoustic model.
Acoustic hit.
~JFremantle()
Destructor.
void reset()
Reset.
Definition: JKatoomba_t.hh:89
JLANG::JObjectOutput< JSuperEvt > output_type
Definition: JFremantle_t.hh:35
std::vector< size_t > ns
*fatal Wrong number of arguments esac JCookie sh typeset Z DETECTOR typeset Z SOURCE_RUN typeset Z TARGET_RUN set_variable PARAMETERS_FILE $WORKDIR parameters
Definition: diff-Tuna.sh:38
std::condition_variable cv
void enqueue(input_type &data)
Queue data.
Global fit of prameterised detector geometry to acoustics data.
Definition: JGlobalfit.hh:29
Acoustic super event fit toolkit.
static std::string oid
detector identifier
JFremantle(const JGeometry &geometry, const JSoundVelocity &velocity, const JFitParameters &parameters, const size_t ns, const size_t backlog=std::numeric_limits< size_t >::max())
Constructor.
Definition: JFremantle_t.hh:47
Acoustic event header.
Acoustic event fit.
then awk string
std::queue< input_type > input
Implementation for depth dependend velocity of sound.
static JQuantile Q
chi2/NDF
Auxiliary data structure for average.
Definition: JKatoomba_t.hh:76
std::vector< JHit > input_type
Definition: JFremantle_t.hh:34
static output_type * output
optional output
void put(const double x)
Put value.
Definition: JKatoomba_t.hh:101
Thread pool for global fits.
Definition: JFremantle_t.hh:31
Template interface of object output for single data type.
virtual bool put(const T &object)=0
Object output.
static bool squash
squash transmissions in output
Auxiliary data structure to convert model to super event.
std::vector< std::thread > workers
std::condition_variable cw