Jpp
Classes | Namespaces | Functions
JMergeSort.cc File Reference
#include <iostream>
#include <iomanip>
#include <functional>
#include <limits>
#include <vector>
#include <algorithm>
#include "TRandom3.h"
#include "JTools/JMergeSort.hh"
#include "Jeep/JTimer.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Classes

struct  JTOOLS::JGetElement< T >
 Auxiliary class to convert value to element. More...
 

Namespaces

 JTOOLS
 Auxiliary classes and methods for multi-dimensional interpolations and histograms.
 

Functions

template<class T >
void JTOOLS::inplace_merge (T __begin, const size_t N, const size_t *delimiter)
 Merge multiple sorted ranges. More...
 
template<class T >
bool JTOOLS::is_sorted (T __begin, T __end)
 Test whether range is sorted. More...
 
int main (int argc, char **argv)
 

Detailed Description

Example program to test performance of JTOOLS::JMergeSort.

Author
mdejong

Definition in file JMergeSort.cc.

Function Documentation

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 126 of file JMergeSort.cc.

127 {
128  using namespace std;
129 
130  double rate_Hz;
131  bool do_test;
132  int debug;
133 
134  try {
135 
136  JParser<> zap("Example program to test performance of merge sort.");
137 
138  zap['B'] = make_field(rate_Hz);
139  zap['D'] = make_field(do_test);
140  zap['d'] = make_field(debug) = 0;
141 
142  zap(argc, argv);
143  }
144  catch(const exception &error) {
145  FATAL(error.what() << endl);
146  }
147 
148 
149  using namespace JPP;
150 
151  if (rate_Hz <= 0.0) {
152  FATAL("Rate " << rate_Hz << " < 0 Hz." << endl);
153  }
154 
155  const double period = 1.0e9 / rate_Hz; // [ns]
156 
157  const double Tmin = 0.0; // [ns]
158  const double Tmax = 1.0e8; // [ns]
159 
160  const int NUMBER_OF_PMTS = 31;
161  const int NUMBER_OF_MODULES = 115 * 18;
162 
163 
164  //typedef int hit_type;
165  typedef double hit_type;
166  typedef JGetElement<hit_type> JGetElement_t;
167  const JGetElement_t getElement;
168  typedef std::vector<hit_type> JBuffer1D;
169  typedef std::vector<JBuffer1D> JBuffer2D;
170 
171 
172  // generate random data
173 
174  JBuffer2D input(NUMBER_OF_PMTS);
175 
176  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
177 
178  for (double t1 = Tmin + gRandom->Exp(period); t1 < Tmax; t1 += gRandom->Exp(period)) {
179  input[i].push_back(getElement(t1));
180  }
181 
182  NOTICE("PMT[" << setw(2) << i << "] " << input[i].size() << endl);
183 
184  input[i].push_back(JGetElement_t::getEndMarker());
185  }
186 
187 
188  if (true) {
189 
190  JTimer timer("std::sort");
191 
192  JBuffer1D buffer;
193 
194  timer.start();
195 
196  for (int i = 0; i != NUMBER_OF_MODULES; ++i) {
197 
198  buffer.clear();
199 
200  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
201  copy(input[i].begin(), input[i].end() - 1, back_inserter(buffer));
202  }
203 
204  std::sort(buffer.begin(), buffer.end());
205 
206  if (do_test && !JTOOLS::is_sorted(buffer.begin(), buffer.end())) {
207  FATAL("std::sort failed." << endl);
208  }
209  }
210 
211  timer.stop();
212  timer.print(cout, false);
213  }
214 
215  if (true) {
216 
217  JTimer timer("std::inplace_merge");
218 
219  JBuffer1D buffer;
220 
221  vector<size_t> delimiter(NUMBER_OF_PMTS + 1);
222 
223  timer.start();
224 
225  for (int i = 0; i != NUMBER_OF_MODULES; ++i) {
226 
227  size_t n = 0;
228 
229  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
230  n += input[i].size() - 1;
231  }
232 
233  buffer.resize(n);
234 
235  JBuffer1D::iterator out = buffer.begin();
236 
237  delimiter[0] = 0;
238 
239  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
240 
241  out = copy(input[i].begin(), input[i].end() - 1, out);
242 
243  delimiter[i+1] = distance(buffer.begin(), out);
244  }
245 
246  inplace_merge(buffer.begin(), delimiter.size(), delimiter.data());
247 
248  if (do_test && !JTOOLS::is_sorted(buffer.begin(), buffer.end())) {
249  FATAL("std::inplace_merge failed." << endl);
250  }
251  }
252 
253  timer.stop();
254  timer.print(cout, false);
255  }
256 
257  if (true) {
258 
259  JTimer timer("JMergeSort");
260 
261  JBuffer1D buffer;
262  JMergeSort<hit_type> merge;
263 
264  timer.start();
265 
266  for (int i = 0; i != NUMBER_OF_MODULES; ++i) {
267 
268  buffer.clear();
269 
270  merge(input, buffer);
271 
272  if (do_test && !JTOOLS::is_sorted(buffer.begin(), buffer.end())) {
273  FATAL("JMergeSort failed." << endl);
274  }
275  }
276 
277  timer.stop();
278  timer.print(cout, false);
279  }
280 }
JTOOLS::n
const int n
Definition: JPolint.hh:628
std::vector
Definition: JSTDTypes.hh:12
KM3NETDAQ::NUMBER_OF_PMTS
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
JTOOLS::inplace_merge
void inplace_merge(T __begin, const size_t N, const size_t *delimiter)
Merge multiple sorted ranges.
Definition: JMergeSort.cc:28
JPARSER::JParser
Utility class to parse command line options.
Definition: JParser.hh:1493
distance
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
Definition: PhysicsEvent.hh:434
NOTICE
#define NOTICE(A)
Definition: JMessage.hh:64
JAANET::copy
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:152
JPP
This name space includes all other name spaces (except KM3NETDAQ, KM3NET and ANTARES).
Definition: JAAnetToolkit.hh:37
debug
int debug
debug level
Definition: JSirene.cc:59
make_field
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1954
std
Definition: jaanetDictionary.h:36
JTOOLS::is_sorted
bool is_sorted(T __begin, T __end)
Test whether range is sorted.
Definition: JMergeSort.cc:105
FATAL
#define FATAL(A)
Definition: JMessage.hh:67