Jpp  15.0.0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Namespaces | Functions
JMergeSort.cc File Reference

Example program to test performance of JTOOLS::JMergeSort. More...

#include <iostream>
#include <iomanip>
#include <functional>
#include <limits>
#include <vector>
#include <algorithm>
#include "TRandom3.h"
#include "JTools/JMergeSort.hh"
#include "JLang/JSTDToolkit.hh"
#include "Jeep/JTimer.hh"
#include "Jeep/JParser.hh"
#include "Jeep/JMessage.hh"

Go to the source code of this file.

Classes

struct  JTOOLS::JElement< 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...
 
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

int main ( int  argc,
char **  argv 
)

Definition at line 105 of file JMergeSort.cc.

106 {
107  using namespace std;
108 
109  double rate_Hz;
110  int debug;
111 
112  try {
113 
114  JParser<> zap("Example program to test performance of merge sort.");
115 
116  zap['B'] = make_field(rate_Hz);
117  zap['d'] = make_field(debug) = 0;
118 
119  zap(argc, argv);
120  }
121  catch(const exception &error) {
122  FATAL(error.what() << endl);
123  }
124 
125 
126  using namespace JPP;
127 
128  if (rate_Hz <= 0.0) {
129  FATAL("Rate " << rate_Hz << " < 0 Hz." << endl);
130  }
131 
132  const double period = 1.0e9 / rate_Hz; // [ns]
133 
134  const double Tmin = 0.0; // [ns]
135  const double Tmax = 1.0e8; // [ns]
136 
137  const int NUMBER_OF_PMTS = 31;
138  const int NUMBER_OF_MODULES = 115 * 18;
139 
140 
141  //typedef int hit_type;
142  typedef double hit_type;
143  typedef JElement<hit_type> JElement_t;
144  const JElement_t getElement;
145  typedef std::vector<hit_type> JBuffer1D;
146  typedef std::vector<JBuffer1D> JBuffer2D;
147 
148 
149  // generate random data
150 
151  JBuffer2D input(NUMBER_OF_PMTS);
152 
153  size_t number_of_hits = 0;
154 
155  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
156 
157  for (double t1 = Tmin + gRandom->Exp(period); t1 < Tmax; t1 += gRandom->Exp(period), ++number_of_hits) {
158  input[i].push_back(getElement(t1));
159  }
160 
161  NOTICE("PMT[" << setw(2) << i << "] " << input[i].size() << endl);
162 
163  putEndMarker(input[i], JElement_t::getEndMarker());
164  }
165 
166 
167  JTimer timer[] = {
168  JTimer("std::sort"),
169  JTimer("std::inplace_merge"),
170  JTimer("JMergeSort")
171  };
172 
173 
174  {
175  JBuffer1D buffer;
176 
177  for (int i = 0; i != NUMBER_OF_MODULES; ++i) {
178 
179  timer[0].start();
180 
181  buffer.clear();
182 
183  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
184  copy(input[i].begin(), input[i].end(), back_inserter(buffer));
185  }
186 
187  std::sort(buffer.begin(), buffer.end());
188 
189  timer[0].stop();
190 
191  ASSERT(buffer.size() == number_of_hits, "Test std::sort.");
192  ASSERT(is_sorted(buffer.begin(), buffer.end()), "Test std::sort.");
193  }
194  }
195 
196  {
197  JBuffer1D buffer;
198 
199  vector<size_t> delimiter(NUMBER_OF_PMTS + 1);
200 
201  for (int i = 0; i != NUMBER_OF_MODULES; ++i) {
202 
203  timer[1].start();
204 
205  size_t n = 0;
206 
207  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
208  n += input[i].size();
209  }
210 
211  buffer.resize(n);
212 
213  JBuffer1D::iterator out = buffer.begin();
214 
215  delimiter[0] = 0;
216 
217  for (int i = 0; i != NUMBER_OF_PMTS; ++i) {
218 
219  out = copy(input[i].begin(), input[i].end(), out);
220 
221  delimiter[i+1] = distance(buffer.begin(), out);
222  }
223 
224  inplace_merge(buffer.begin(), delimiter.size(), delimiter.data());
225 
226  timer[1].stop();
227 
228  ASSERT(buffer.size() == number_of_hits, "Test std::inplace_merge.");
229  ASSERT(is_sorted(buffer.begin(), buffer.end()), "Test std::inplace_merge.");
230  }
231  }
232 
233  {
234  JBuffer1D buffer;
235  JMergeSort<hit_type> merge;
236 
237  for (int i = 0; i != NUMBER_OF_MODULES; ++i) {
238 
239  timer[2].start();
240 
241  buffer.clear();
242 
243  merge(input, buffer);
244 
245  timer[2].stop();
246 
247  ASSERT(buffer.size() == number_of_hits, "Test JMergeSort.");
248  ASSERT(is_sorted(buffer.begin(), buffer.end()), "Test JMergeSort.");
249  }
250  }
251 
252 
253  for (int i = 0; i != sizeof(timer)/sizeof(timer[0]); ++i) {
254  timer[i].print(cout, false);
255  }
256 }
Utility class to parse command line options.
Definition: JParser.hh:1500
std::vector< T >::difference_type distance(typename std::vector< T >::const_iterator first, typename PhysicsEvent::const_iterator< T > second)
Specialisation of STL distance.
void inplace_merge(T __begin, const size_t N, const size_t *delimiter)
Merge multiple sorted ranges.
Definition: JMergeSort.cc:29
const int n
Definition: JPolint.hh:660
#define ASSERT(A,...)
Assert macro.
Definition: JMessage.hh:90
#define make_field(A,...)
macro to convert parameter to JParserTemplateElement object
Definition: JParser.hh:1961
#define NOTICE(A)
Definition: JMessage.hh:64
int debug
debug level
Definition: JSirene.cc:63
#define FATAL(A)
Definition: JMessage.hh:67
void copy(const Head &from, JHead &to)
Copy header from from to to.
Definition: JHead.cc:139
static const int NUMBER_OF_PMTS
Total number of PMTs in module.
Definition: JDAQ.hh:26
void putEndMarker(std::vector< JElement_t, JAllocator_t > &buffer, const JElement_t &value)
Put end marker.
Definition: JSTDToolkit.hh:47