-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdriver.cpp
650 lines (556 loc) · 17.8 KB
/
driver.cpp
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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
/******************************************************************************
* driver.cpp
*
* Driver to benchmark sorting algorithms. Comparison and move measurements do NOT work for all algorithms.
* Modified version of sort-driver.c++ from http://www.diku.dk/~jyrki/Myris/Kat2014S.html
******************************************************************************
* Modified (more test cases etc.) by
* Copyright (C) 2016 Stefan Edelkamp <edelkamp@tzi.de>
* Copyright (C) 2016 Armin Weiß <armin.weiss@fmi.uni-stuttgart.de>
******************************************************************************
*
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*******************************************************************************
*
* Copyright (C) 2014 Amr Elmasry
* Copyright (C) 2014 Jyrki Katajainen
* Copyright (C) 2014 Max Stenmark
*
*****************************************************************************/
#if ! defined(MAXSIZE)
#define MAXSIZE (1024 * 1024 * 1024)
#endif
//Maximal number of bytes to sort
#ifndef BYTESTOSORT
#define BYTESTOSORT (128 * 1024 * 1024)
#endif
#include <algorithm>
#include <functional>
#include <iostream>
#include <iterator>
#include <string>
#include <cmath>
#include <random>
#include <string>
#include <chrono>
#include <typeinfo>
//to print types
#define QUOTE(name) #name
#define STR(macro) QUOTE(macro)
#define ALG_NAME STR(NAME)
#ifndef TYPE
#define TYPE int32_t
#endif
#if not __APPLE__
extern int ilogb(double) throw();
#endif
//for counting numbers of calls to std::partial_sort
#define PARTIAL_SORT_COUNT
long long volatile partial_sort_count = 0;
#ifndef IS_THRESH
#define IS_THRESH (1<<4)
#endif // !1IS_THRESH
#include "algorithm.h++"
//measure comparisons does NOT work for all algorithms
#ifdef MEASURE_COMPARISONS
long long volatile comparisons = 0;
template<typename T>
class counting_comparator {
public:
using first_argument_type = T;
using second_argument_type = T;
using result_type = bool;
bool operator()(T const& a, T const& b) const {
++comparisons;
return a < b;
}
};
template<typename T>
class vector_comparator {
public:
using first_argument_type = T;
using second_argument_type = T;
using result_type = bool;
bool operator()(std::vector<T> const& a, std::vector<T> const& b) const {
if(a.size() < b.size())
return true;
if(b.size() < a.size())
return false;
if(a.size() == 0) // also b.size() == 0
return false;
return a[0] < b[0];
}
};
#endif
class Rational {
private:
int numerator;
unsigned int denominator;
public:
Rational(int v) : numerator(v), denominator(1) {};
Rational() : numerator(0), denominator(1) {};
double value(void) { return (double) numerator / (double) denominator; }
bool operator<(const Rational &b) const { return numerator*(long long int) b.denominator < b.numerator*(long long int)denominator; }
bool operator>(const Rational &b) const { return numerator*(long long int) b.denominator > b.numerator*(long long int)denominator; }
bool operator<=(const Rational &b) const { return numerator*(long long int) b.denominator <= b.numerator*(long long int)denominator; }
bool operator!=(const Rational &b) const { return numerator*(long long int) b.denominator != b.numerator*(long long int)denominator; }
bool operator==(const Rational &b) const { return numerator*(long long int) b.denominator == b.numerator*(long long int)denominator; }
};
class Record {
private:
int key;
int blub[20];
public:
Record(int v) : key(v) { blub[v % 10] = v;};
Record() : key(0) {};
int value(void) { return key; }
bool operator<(const Record &b) const { return key < b.key; }
bool operator>(const Record &b) const { return key > b.key; }
bool operator<=(const Record &b) const { return key <= b.key; }
bool operator!=(const Record &b) const { return key != b.key; }
bool operator==(const Record &b) const { return key == b.key; }
};
#ifndef VECTORSIZE
#define VECTORSIZE 10
#endif
class Vector {
private:
double components[VECTORSIZE];
public:
Vector(int v) { for (int j = 0 ; j < VECTORSIZE ; j++) components[j] = v;};
Vector() {};
double value(void) {
double abs_this = 0;
for (int j = 0 ; j < VECTORSIZE ; j++){
abs_this += components[j]*components[j];
}
return abs_this;
}
//compare Euclidean norm
bool operator<(const Vector &b) const {
double abs_this = 0, abs_b = 0;
for (int j = 0 ; j < VECTORSIZE ; j++){
abs_this += components[j]*components[j];
abs_b += b.components[j]*b.components[j];
}
return abs_this < abs_b;
}
bool operator>(const Vector &b) const {
double abs_this = 0, abs_b = 0;
for (int j = 0 ; j < VECTORSIZE ; j++){
abs_this += components[j]*components[j];
abs_b += b.components[j]*b.components[j];
}
return abs_this > abs_b;
}
bool operator<=(const Vector &b) const {
double abs_this = 0, abs_b = 0;
for (int j = 0 ; j < VECTORSIZE ; j++){
abs_this += components[j]*components[j];
abs_b += b.components[j]*b.components[j];
}
return abs_this <= abs_b;
}
bool operator!=(const Vector &b) const {
double abs_this = 0, abs_b = 0;
for (int j = 0 ; j < VECTORSIZE ; j++){
abs_this += components[j]*components[j];
abs_b += b.components[j]*b.components[j];
}
return abs_this != abs_b;
}
bool operator==(const Vector &b) const {
double abs_this = 0, abs_b = 0;
for (int j = 0 ; j < VECTORSIZE ; j++){
abs_this += components[j]*components[j];
abs_b += b.components[j]*b.components[j];
}
return abs_this == abs_b;
}
};
//measure moves does NOT work for all algorithms
#ifdef MEASURE_MOVES
long long volatile moves = 0;
template<typename T>
class move_counter {
private:
T datum;
move_counter(move_counter const&) = delete;
move_counter& operator=(move_counter const&) = delete;
public:
explicit move_counter()
: datum(0) {
moves += 1;
}
template<typename number>
explicit move_counter(number x = 0)
: datum(x) {
moves += 1;
}
move_counter(move_counter&& other) {
datum = std::move(other.datum);
moves += 1;
}
move_counter& operator=(move_counter&& other) {
datum = std::move(other.datum);
moves += 1;
return *this;
}
operator T() const {
return datum;
}
template<typename U>
friend bool operator<(move_counter<U> const&, move_counter<U> const&);
template<typename U>
friend bool operator==(move_counter<U> const&, move_counter<U> const&);
};
template<typename T>
bool operator<(move_counter<T> const& x, move_counter<T> const& y) {
return x.datum < y.datum;
}
template<typename T>
bool operator==(move_counter<T> const& x, move_counter<T> const& y) {
return x.datum == y.datum;
}
#endif
void usage(int argc, char **argv) {
std::cerr << "Usage: " << argv[0]
<< " <N> <b c d i m n o p q r s t u v w z> <seed> "
<< std::endl;
exit(1);
}
int main(int argc, char** argv) {
#ifdef MEASURE_MOVES
using element = move_counter<int>;
using C = std::less<element>;
#else
#ifdef TYPE
using element = TYPE;
#else
using element = int;
#endif
#ifdef MEASURE_COMPARISONS
using C = counting_comparator<element>;
#else
using C = std::less<element>;
#endif
#endif
unsigned int N = 15;
unsigned int inversions = 0;
unsigned long long int modulo = 2;
unsigned long long int offset = 0;
char method = 'i';
std::string method_string = std::string("random");
std::string param_string = std::string("0");
if (argc == 2) {
N = atoi(argv[1]);
} else if (argc != 3 && argc != 4) {
usage(argc, argv);
} else {
N = atoi(argv[1]);
method = *argv[2];
}
if(argc == 5)
{
inversions = atoi(argv[3]);
modulo = inversions;
offset = inversions;
// param_string = std::to_string(atoi(argv[3]));
}
else
{
inversions = N;
modulo = (unsigned int)sqrt(N);
offset = N / 2;
if (method=='s' || method =='m'|| method =='o'|| method =='v') {
param_string = "sqrt(N)";
modulo = (unsigned int)sqrt(N);
inversions = (unsigned int)sqrt(N);
}
if (method=='p'){
param_string = "N";
inversions = N;
}
if (method=='q'){
param_string = "NlogN";
inversions = (int)((double)N*log(N));
}
if (method=='n'){
param_string = "N/2";
modulo = N/2;
}
}
std::random_device rd;
unsigned long long int seed = 0;
if(argc == 5)
{
seed = atoi(argv[4]);
}
else if(argc == 4)
{
seed = atoi(argv[3]);
}
else
{
seed = rd();
}
if (N < 1 || N > MAXSIZE) {
std::cerr << "N out of bounds [1.."
<< MAXSIZE
<< "]"
<< std::endl;
usage(argc, argv);
};
switch (method) {
case 'b':
method_string = "boolean ";
break;
case 'c':
method_string = "constant ";
break;
case 'd':
method_string = "decreasing ";
break;
case 'm':
method_string = "modulo ";
break;
case 'n':
method_string = "modulo ";
break;
case 'i':
method_string = "increasing ";
break;
case 'o':
case 'p':
case 'q':
method_string = "inversions ";
break;
case 'r':
method_string = "random ";
break;
case 's':
method_string = "sawtooth ";
break;
case 't':
method_string = "transposition of identity";
break;
case 'u':
method_string = "step ";
break;
case 'v':
method_string = "stairs ";
break;
case 'w':
method_string = "eigth_power_mod_power_of_2";
break;
case 'z':
method_string = "squares_mod_power_of_2 ";
break;
default:
std::cerr << "Method not in ['d','i','r','b','m', 's', 'q', 'z', 'c', 't', 'w']" << std::endl;
usage(argc, argv);
}
if(N*sizeof(element) > ((unsigned long long int)1)<<31)
return 0;
#if defined(MEASURE_MOVES)
moves = 0;
#elif defined(MEASURE_COMPARISONS)
comparisons = 0;
#endif
#if defined(REPETITIONS)
int const repetitions = REPETITIONS;
#else
//for every measurement sort at least almost BYTESTOSORT bytes -- to ensure that the elapsed time is someting around 1 second (this is not always the case, but often)
unsigned int const repetitions = std::max((unsigned int)(BYTESTOSORT / ((unsigned long long int)N*sizeof(element))), (unsigned int)1);
#endif
unsigned long long int temp;
int elements = N;
unsigned long long int modulo_power_2 = 1 << ((unsigned int)ilogb(elements));
unsigned long long int offset_zw = modulo_power_2 / 2;
typedef std::vector<element> tv;
std::vector<tv> test_data;
std::vector<tv> test_data_sorted;
std::mt19937 random_generator(seed);
//generate test data
// std::cout << "Generating " << repeats << " times " << elements << " elements of test data..." << std::endl;
test_data.clear();
test_data.reserve(repetitions);
for (unsigned int j = 0; j < repetitions; j++) {
test_data.push_back(tv());
test_data_sorted.push_back(tv());
test_data[j].reserve(elements);
test_data_sorted[j].reserve(elements);
switch (method) {
case 'd':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(elements-1-i));
test_data_sorted[j].push_back(element(elements-1-i));
}
break;
case 'c':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(1));
test_data_sorted[j].push_back(element(1));
}
break;
case 'i':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(i));
test_data_sorted[j].push_back(element(i));
}
break;
case 'b':
for (int i = 0; i < elements; i++) {
int el = random_generator() % 2;
test_data[j].push_back(element(el));
test_data_sorted[j].push_back(element(el));
}
break;
case 'r':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(i));
test_data_sorted[j].push_back(element(i));
}
std::shuffle(test_data[j].begin(), test_data[j].end(),random_generator);
break;
case 'm':
case 'n':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(i % modulo));
test_data_sorted[j].push_back(element(i % modulo));
}
std::shuffle(test_data[j].begin(), test_data[j].end(),random_generator);
break;
case 's':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(i % modulo));
test_data_sorted[j].push_back(element(i % modulo));
}
break;
case 'o':
case 'p':
case 'q':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(i));
test_data_sorted[j].push_back(element(i));
}
for (unsigned int i = 0; i < inversions; i++) {
int pos = random_generator() % (elements - 1);
std::swap(test_data[j][pos],test_data[j][pos + 1]);
std::swap(test_data_sorted[j][pos],test_data_sorted[j][pos + 1]);
}
break;
case 't':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element((i + offset) % elements));
test_data_sorted[j].push_back(element((i + offset) % elements));
}
break;
case 'u':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(i / (elements/2)));
test_data_sorted[j].push_back(element(i / (elements/2)));
}
break;
case 'v':
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element(i / modulo));
test_data_sorted[j].push_back(element(i / modulo));
}
break;
case 'w':
for (int i = 0; i < elements; i++) {
temp = ((long long int)i*(long long int)i) % modulo_power_2;
temp = ((long long int)temp*(long long int)temp) % modulo_power_2;
test_data[j].push_back(element((offset_zw + (long long int)temp*(long long int)temp) % modulo_power_2));
test_data_sorted[j].push_back(element((offset_zw + (long long int)temp*(long long int)temp) % modulo_power_2));
}
break;
case 'z':
unsigned long long int offset_zw = modulo_power_2 / 2;
for (int i = 0; i < elements; i++) {
test_data[j].push_back(element((offset_zw + (long long int)i*(long long int)i) % modulo_power_2));
test_data_sorted[j].push_back(element((offset_zw + (long long int)i*(long long int)i) % modulo_power_2));
}
break;
}
}
#if ! defined(MEASURE_COMPARISONS) && ! defined(MEASURE_MOVES)
auto begin = std::chrono::high_resolution_clock::now();
#endif
for (unsigned int j = 0; j < repetitions; ++j) {
NAME::sort(test_data[j].begin(), test_data[j].end(), C());
}
#if ! defined(MEASURE_COMPARISONS) && ! defined(MEASURE_MOVES)
auto end = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count();
#endif
#if ! defined(NDEBUG)
std::cout << "Sorting test data..." << std::endl;
for (unsigned int j = 0; j < repetitions; j++) {
std::sort(test_data_sorted[j].begin(), test_data_sorted[j].end(), C());
}
for (unsigned int j = 0; j < repetitions; ++j) {
for (int i = 0; i < elements; i++) {
if (test_data[j][i] != test_data_sorted[j][i])
{
std::cout << "Not sorted correctly:" << i << std::endl;
break;
}
}
}
#endif
double T = double(repetitions) * double(N);
std::string type_string;
if (typeid(element) == typeid(uint32_t) || typeid(element) == typeid(int))
type_string = std::string("int");
else if (typeid(element) == typeid(Record))
type_string = std::string("Record");
else if (typeid(element) == typeid(double))
type_string = std::string("double");
else if (typeid(element) == typeid(Vector))
type_string = std::string("Vector");
else if (typeid(element) == typeid(Rational))
type_string = std::string("Rational");
else if (typeid(element) == typeid(std::string))
type_string = std::string("string");
#if defined(MEASURE_COMPARISONS)
//prints N, #comparisons/N , #comparisons/(NlogN ) , (#comparisons-NlogN)/N
double avg_comp = double(comparisons) / double(repetitions);
double c = ((double) avg_comp/(N * log2((double)N)));
double d = ((double) (avg_comp - (N * log2((double)N))) / (double)N);
std::cout.precision(5);
std::cout << N << '\t' << double(comparisons) / T;
std::cout.precision(3);
std::cout << '\t' << c;
std::cout << '\t' << d << std::endl;
#elif defined(MEASURE_MOVES)
//prints N, #moves/N , #moves/(NlogN )
std::cout.precision(4);
double avg_move = double(moves) / double(repetitions);
double c = ((double) avg_move/(N * log2((double)N)));
std::cout << N << '\t' << double(moves) / T;
std::cout << '\t' << c<< std::endl;
#else
double ns = double(duration) ;
std::cout.precision(4);
std::cout.width(10);
#ifdef BLOCKSIZETEST
std::cout << type_string << '\t' << BLOCKSIZE <<'\t' << N << '\t' << ns / T <<'\t' << partial_sort_count <<'\t' << repetitions << std::endl;
#else
std::cout<<ALG_NAME<<'\t'<< method_string << '\t'<< param_string << '\t' << type_string << '\t'<< N << '\t' << ns / T <<'\t' << partial_sort_count<<'\t' << repetitions << std::endl;
#endif
#endif
return 0;
}