Helios++
Helios software for LiDAR simulations
PulseThreadPool.h
1 #pragma once
2 
3 #include <ResThreadPool.h>
4 #include <scanner/detector/PulseThreadPoolInterface.h>
5 #include <noise/RandomnessGenerator.h>
6 #include <noise/UniformNoiseSource.h>
7 #include <TimeWatcher.h>
8 
15  public ResThreadPool<
16  std::vector<std::vector<double>>&,
17  RandomnessGenerator<double>&,
18  RandomnessGenerator<double>&,
19  NoiseSource<double>&
20  >,
22 {
23 #ifdef DATA_ANALYTICS
24 public:
25 #else
26 protected:
27 #endif
28  // *** ATTRIBUTES *** //
29  // ******************** //
34  std::vector<std::vector<double>> *apMatrices;
35 
44  RandomnessGenerator<double> *randGens2; // To substitute BoxMuller
49 
50 public:
68  bool dynamic;
69 
70 public:
71  // *** CONSTRUCTION / DESTRUCTION *** //
72  // ************************************ //
79  explicit PulseThreadPool(
80  std::size_t const _pool_size,
81  double const deviceAccuracy,
82  bool const dynamic
83  ) :
85  std::vector<std::vector<double>>&,
86  RandomnessGenerator<double>&,
87  RandomnessGenerator<double>&,
88  NoiseSource<double>&
89  >(_pool_size),
91  {
92  // Allocate
93  apMatrices = new std::vector<std::vector<double>>[this->pool_size];
94  randGens = new RandomnessGenerator<double>[this->pool_size];
95  randGens2 = new RandomnessGenerator<double>[this->pool_size];
96  intersectionHandlingNoiseSources =
98 
99  // Initialize
100  for (std::size_t i = 0; i < this->pool_size; ++i){
101  randGens[i] = *DEFAULT_RG;
103  randGens[i].computeNormalDistribution(0.0, deviceAccuracy);
104  randGens2[i] = *DEFAULT_RG;
107  *DEFAULT_RG, 0.0, 1.0
108  );
109  }
110  }
111 
112  virtual ~PulseThreadPool(){
113  // Release memory
114  delete[] apMatrices;
115  delete[] randGens;
116  delete[] randGens2;
118  }
119 
120  // *** PULSE THREAD POOL INTERFACE *** //
121  // ************************************ //
125  inline void run_pulse_task(
126  TaskDropper<
127  PulseTask,
129  std::vector<std::vector<double>>&,
133  > &dropper
134  ) override {
135  run_res_task(dropper);
136  }
140  inline bool try_run_pulse_task(
141  TaskDropper<
142  PulseTask,
144  std::vector<std::vector<double>>&,
148  > &dropper
149  ) override {
150  return try_run_res_task(dropper);
151  }
155  inline void join() override{
157  std::vector<std::vector<double>>&,
161  >::join();
162  }
163 
164 protected:
165  // *** M E T H O D S *** //
166  // *********************** //
172  inline void do_res_task(
173  boost::function<void(
174  std::vector<std::vector<double>>&,
178  )> &task,
179  int const resourceIdx
180  ) override{
181  task(
182  apMatrices[resourceIdx],
183  randGens[resourceIdx],
184  randGens2[resourceIdx],
186  );
187  }
194  boost::function<void(
195  std::vector<std::vector<double>>&,
199  )> &task,
200  int const resourceIdx
201  ) override {
202  // Run the user supplied task.
203  try{
204  do_res_task(task, resourceIdx);
205  }
206  // Suppress all exceptions.
207  catch (const std::exception &e) {
208  std::stringstream ss;
209  ss << "PulseThreadPool::wrap_res_task EXCEPTION: " << e.what();
210  logging::WARN(ss.str());
211  }
212 
213  // Task has finished, so increment count of available threads.
214  boost::unique_lock<boost::mutex> lock(this->mutex_);
215  ++(this->available_);
216  resourceSetAvailable[resourceIdx] = true;
217  idleTimer.startIfNull(); // Start time at first idle thread
218  lock.unlock();
219  this->cond_.notify_one();
220  }
221 
222 public:
228  virtual inline bool isDynamic() const {return dynamic;}
229 };
Class to handle a noise source.
Definition: NoiseSource.h:17
Pulse task interface.
Definition: PulseTask.h:13
Interface for thread pools supporting pulse tasks.
Definition: PulseThreadPoolInterface.h:18
Class implementing a thread pool to deal with pulse tasks.
Definition: PulseThreadPool.h:22
TimeWatcher idleTimer
Time watcher to count the amount of idle time.
Definition: PulseThreadPool.h:63
void wrap_res_task(boost::function< void(std::vector< std::vector< double >> &, RandomnessGenerator< double > &, RandomnessGenerator< double > &, NoiseSource< double > &)> &task, int const resourceIdx) override
Override task wrapping so when the full thread group is used the time at which first occupied thread ...
Definition: PulseThreadPool.h:193
std::vector< std::vector< double > > * apMatrices
Definition: PulseThreadPool.h:34
bool dynamic
Specify whether the pulse thread pool uses a dynamic chunk size strategy (true) or a static one (fals...
Definition: PulseThreadPool.h:68
void run_pulse_task(TaskDropper< PulseTask, PulseThreadPoolInterface, std::vector< std::vector< double >> &, RandomnessGenerator< double > &, RandomnessGenerator< double > &, NoiseSource< double > & > &dropper) override
Definition: PulseThreadPool.h:125
PulseThreadPool(std::size_t const _pool_size, double const deviceAccuracy, bool const dynamic)
Pulse thread pool constructor.
Definition: PulseThreadPool.h:79
virtual bool isDynamic() const
Check if the pulse thread pool is operating on dynamic mode (true) or not (false)
Definition: PulseThreadPool.h:228
void do_res_task(boost::function< void(std::vector< std::vector< double >> &, RandomnessGenerator< double > &, RandomnessGenerator< double > &, NoiseSource< double > &)> &task, int const resourceIdx) override
Do a pulse task.
Definition: PulseThreadPool.h:172
UniformNoiseSource< double > * intersectionHandlingNoiseSources
Intersection handling noise sources, one per thread.
Definition: PulseThreadPool.h:48
RandomnessGenerator< double > * randGens
First randomness generators (general purpose), one per thread.
Definition: PulseThreadPool.h:39
bool try_run_pulse_task(TaskDropper< PulseTask, PulseThreadPoolInterface, std::vector< std::vector< double >> &, RandomnessGenerator< double > &, RandomnessGenerator< double > &, NoiseSource< double > & > &dropper) override
Definition: PulseThreadPool.h:140
RandomnessGenerator< double > * randGens2
Second randomness generators (to substitute old box muller), one per thread.
Definition: PulseThreadPool.h:44
void join() override
Definition: PulseThreadPool.h:155
void computeUniformRealDistribution(RealType lowerBound, RealType upperBound)
Compute a uniform real distribution using the specified real data type.
Definition: RandomnessGenerator.h:354
void computeNormalDistribution(RealType mean, RealType stdev)
Compute a normal distribution using the specified real data type.
Definition: RandomnessGenerator.h:400
Abstract class extending basic thread pool implementation to provide a basis layer to handle thread a...
Definition: ResThreadPool.h:13
void run_res_task(Task task)
Run a task with associated resources when there is an available thread for it.
Definition: ResThreadPool.h:67
bool try_run_res_task(Task task)
Run a task with associated resources. If there is not even a single available thread,...
Definition: ResThreadPool.h:105
bool * resourceSetAvailable
Array of flags specifying availability of resource sets.
Definition: ResThreadPool.h:21
std::size_t pool_size
Size of thread pool (number of threads)
Definition: ThreadPool.h:47
boost::mutex mutex_
Mutex to handle concurrent tasks.
Definition: SimpleThreadPool.h:26
boost::condition_variable cond_
Condition variable to handle tasks dispatching depending on available threads.
Definition: SimpleThreadPool.h:31
std::size_t available_
Number of available threads, those which are not currently performing a task.
Definition: SimpleThreadPool.h:22
Class which handles tasks dropping. It is, executing and then removing each task when dropping.
Definition: TaskDropper.h:22
A time watcher can be used to perform and report time measures.
Definition: TimeWatcher.h:13
void startIfNull()
Start the time watcher but only if it has not been started before. Previous start might come either f...
Definition: TimeWatcher.cpp:22