Jpp 19.3.0-rc.5
the software that should make you happy
Loading...
Searching...
No Matches
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

namespace  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.
 
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 105 of file JMergeSort.cc.

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