-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sort.cpp
More file actions
277 lines (209 loc) · 6.67 KB
/
test_sort.cpp
File metadata and controls
277 lines (209 loc) · 6.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <emscripten/bind.h>
#endif
#include <sstream>
#include "test_case_with_time_measurements.h"
#include "test_pattern_generation.h"
#include <boost/sort/spreadsort/spreadsort.hpp>
#include <boost/sort/sort.hpp>
namespace Sort {
template<class T>
class TestCaseRadixSort : public TestCaseWithTimeMeasurements {
protected:
const size_t m_num_elements;
T* m_array;
bool m_calculation_correct;
public:
TestCaseRadixSort( const string& case_name, const size_t num_elements )
:TestCaseWithTimeMeasurements { case_name }
,m_num_elements { num_elements }
,m_calculation_correct { false }
{
;
}
virtual ~TestCaseRadixSort()
{
;
}
void checkResult( const T* truth )
{
T* out = getArray();
setTrueFalse( true );
for ( size_t i = 0; i < m_num_elements; i++ ) {
if ( out[i] != truth[i] ) {
setTrueFalse( false );
}
}
}
virtual void setArray( T* const a ) { m_array = a; }
virtual T* getArray() { return m_array; }
virtual void run() = 0;
};
template<class T>
class TestCaseRadixSort_baseline : public TestCaseRadixSort<T> {
public:
TestCaseRadixSort_baseline( const string& case_name, const size_t num_elements )
:TestCaseRadixSort<T>{ case_name, num_elements }
{
;
}
virtual ~TestCaseRadixSort_baseline()
{
;
}
void run()
{
std::sort( this->m_array, this->m_array + this->m_num_elements );
}
};
template<class T>
class TestCaseRadixSort_boost_spread_sort : public TestCaseRadixSort<T> {
public:
TestCaseRadixSort_boost_spread_sort( const string& case_name, const size_t num_elements )
:TestCaseRadixSort<T>{ case_name, num_elements }
{
;
}
virtual ~TestCaseRadixSort_boost_spread_sort()
{
;
}
void run()
{
if constexpr ( is_same<float, T>::value || is_same<double, T>::value ) {
boost::sort::spreadsort::float_sort ( this->m_array, this->m_array + this->m_num_elements );
}
else {
boost::sort::spreadsort::integer_sort( this->m_array, this->m_array + this->m_num_elements );
}
}
};
template<class T>
class TestCaseRadixSort_boost_sample_sort : public TestCaseRadixSort<T> {
private:
size_t m_num_threads;
public:
TestCaseRadixSort_boost_sample_sort( const string& case_name, const size_t num_element, const size_t num_threads )
:TestCaseRadixSort<T>{ case_name, num_element }
,m_num_threads { num_threads }
{
;
}
virtual ~TestCaseRadixSort_boost_sample_sort()
{
;
}
void run()
{
boost::sort::block_indirect_sort( this->m_array, this->m_array + this->m_num_elements , m_num_threads );
}
};
template <class T>
class TestExecutorRadixSort : public TestExecutor {
protected:
const int m_num_elements;
const bool m_repeatable;
default_random_engine m_e;
T* m_array_original;
T* m_array_sorted_baseline;
T* m_array_sorted;
public:
TestExecutorRadixSort(
TestResults& results,
const int num_elements,
const int num_trials,
const bool repeatable,
const T min_val,
const T max_val
)
:TestExecutor { results, num_elements, num_trials }
,m_num_elements { num_elements }
,m_repeatable { repeatable }
,m_e { static_cast<unsigned int>( repeatable ? 0 : chrono::system_clock::now().time_since_epoch().count() ) }
,m_array_original { new T[ num_elements ] }
,m_array_sorted_baseline { new T[ num_elements ] }
,m_array_sorted { new T[ num_elements ] }
{
fillArrayWithRandomValues( m_e, m_array_original, m_num_elements, min_val, max_val );
}
virtual ~TestExecutorRadixSort()
{
delete[] m_array_original;
delete[] m_array_sorted_baseline;
delete[] m_array_sorted;
}
void cleanupAfterBatchRuns ( const int test_case )
{
auto t = dynamic_pointer_cast< TestCaseRadixSort <T> >( this->m_test_cases[ test_case ] );
if ( test_case == 0 ) {
memcpy( m_array_sorted_baseline , t->getArray(), sizeof(T)*m_num_elements );
}
t->checkResult( m_array_sorted_baseline );
}
void prepareForRun ( const int test_case, const int num )
{
auto t = dynamic_pointer_cast< TestCaseRadixSort<T> >( this->m_test_cases[ test_case ] );
memcpy( m_array_sorted, m_array_original, sizeof(T) * this->m_num_elements );
t->setArray( m_array_sorted );
}
};
static const size_t NUM_TRIALS = 10;
static size_t nums_elements[]{ 32, 128, 512, 2*1024, 8*1024, 32*1024, 128*1024 };
template<class T>
string testSuitePerType ( const bool print_diag )
{
vector< string > case_names {
"std::sort()",
"boost::sort::spreadsort()",
"boost::sort::block_indirect_sort()"
};
vector< string > header_line {
"number of elements",
"32",
"128",
"512",
"2K",
"8K",
"32K",
"128K"
};
TestResults results{ case_names, header_line };
for( auto num_elements : nums_elements ) {
TestExecutorRadixSort<T> e( results, num_elements, NUM_TRIALS, false, static_cast<T>(INT_MIN), static_cast<T>(INT_MAX) );
e.addTestCase( make_shared< TestCaseRadixSort_baseline <T> > ( case_names[0], num_elements ) );
e.addTestCase( make_shared< TestCaseRadixSort_boost_spread_sort <T> > ( case_names[1], num_elements ) );
e.addTestCase( make_shared< TestCaseRadixSort_boost_sample_sort <T> > ( case_names[2], num_elements, 1 ) );
e.execute( print_diag );
}
std::stringstream ss;
results.printHTML( ss );
return ss.str();
}
} // namespace Sort
#ifdef __EMSCRIPTEN__
string testSortInt()
{
return Sort::testSuitePerType<int> ( true );
}
string testSortFloat()
{
return Sort::testSuitePerType<float> (true );
}
EMSCRIPTEN_BINDINGS( sort_module ) {
emscripten::function( "testSortInt", &testSortInt );
emscripten::function( "testSortFloat", &testSortFloat );
}
#else
int main( int argc, char* argv[] )
{
const bool print_diag = (argc == 2);
cout << "sort (int)\n\n";
cout << Sort::testSuitePerType<int> ( print_diag );
cout << "\n\n";
cout << "sort (float)\n\n";
cout << Sort::testSuitePerType<float> ( print_diag );
cout << "\n\n";
return 0;
}
#endif