-
Notifications
You must be signed in to change notification settings - Fork 4
/
sort512perf.cpp
971 lines (834 loc) · 38.2 KB
/
sort512perf.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
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
////////////////////////////////////////////////////////////
/// Berenger Bramas - 2016
/// MIT Licence
/// AVX 512 Sorting algorithm
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// Compilation :
/// Gcc : g++ -DNDEBUG -O3 -funroll-loops -faggressive-loop-optimizations -std=c++11 -mavx512f -mavx512pf -mavx512er -mavx512cd -fopenmp sort512perf.cpp -o sort512perf.gcc.exe
/// Intel : icpc -DNDEBUG -O3 -std=c++11 -xCOMMON-AVX512 -xMIC-AVX512 -qopenmp sort512perf.cpp -o sort512perf.intel.exe
///
///
/// SKL:
/// Gcc : g++ -DNDEBUG -O3 -funroll-loops -faggressive-loop-optimizations -std=c++11 -mavx512f -mavx512cd -mavx512vl -mavx512bw -mavx512dq -fopenmp sort512perf.cpp -o sort512perf.gcc.exe
/// Intel : icpc -DNDEBUG -O3 -std=c++11 -xCOMMON-AVX512 -xCORE-AVX512 -qopenmp sort512perf.cpp -o sort512perf.intel.exe
///
/// With Ipp:
/// Gcc : g++ -DNDEBUG -DUSE_IPP -O3 -funroll-loops -faggressive-loop-optimizations -std=c++11 -mavx512f -mavx512pf -mavx512er -mavx512cd -fopenmp sort512perf.cpp -o sort512perf.gcc.exe -I $IPPROOT/include -L $IPPROOT/lib/intel64 -lippi -lipps -lippcore -Wl,-rpath=$IPPROOT/lib/intel64
/// Intel : icpc -DNDEBUG -DUSE_IPP -O3 -std=c++11 -xCOMMON-AVX512 -xMIC-AVX512 -qopenmp sort512perf.cpp -o sort512perf.intel.exe -I $IPPROOT/include -L $IPPROOT/lib/intel64 -lippi -lipps -lippcore -Wl,-rpath,$IPPROOT/lib/intel64
///
/// SKL IPP:
/// Gcc : g++ -DNDEBUG -DUSE_IPP -O3 -funroll-loops -faggressive-loop-optimizations -std=c++11 -mavx512f -mavx512cd -mavx512vl -mavx512bw -mavx512dq -fopenmp sort512perf.cpp -o sort512perf.gcc.exe -I $IPPROOT/include -L $IPPROOT/lib/intel64 -lippi -lipps -lippcore -Wl,-rpath=$IPPROOT/lib/intel64
/// Intel : icpc -DNDEBUG -DUSE_IPP -O3 -std=c++11 -xCOMMON-AVX512 -xCORE-AVX512 -qopenmp sort512perf.cpp -o sort512perf.intel.exe -I $IPPROOT/include -L $IPPROOT/lib/intel64 -lippi -lipps -lippcore -Wl,-rpath,$IPPROOT/lib/intel64
///
/// Numa:
/// In Flat mode list with : numactl --hardware
/// Then run with : numactl --physcpubind=8 --membind=1 EXEC
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
#include <immintrin.h>
#include <omp.h>
#include <iostream>
#include <algorithm>
#include <chrono>
#include <cassert>
#include <stdexcept>
#include <climits>
#include <cfloat>
#include <cmath>
#include <cstdlib>
#include "sort512.hpp"
#include "sort512kv.hpp"
// Default alignement for the complete application by redirecting the new operator
static const int DefaultMemAlignement = 128;
namespace aligned_malloc {
template <std::size_t AlignementValue>
inline void* malloc(const std::size_t inSize){
if(inSize == 0){
return nullptr;
}
// Ensure it is a power of 2
static_assert(AlignementValue != 0 && ((AlignementValue-1)&AlignementValue) == 0, "Alignement must be a power of 2");
// We will need to store the adress of the real blocks
const std::size_t sizeForAddress = (AlignementValue < sizeof(unsigned char*)? sizeof(unsigned char*) : AlignementValue);
unsigned char* allocatedMemory = reinterpret_cast<unsigned char*>(std::malloc(inSize + AlignementValue-1 + sizeForAddress));
unsigned char* alignedMemoryAddress = reinterpret_cast<unsigned char*>((reinterpret_cast<std::size_t>(allocatedMemory) + AlignementValue-1 + sizeForAddress) & ~static_cast<std::size_t>(AlignementValue-1));
unsigned char* ptrForAddress = (alignedMemoryAddress - sizeof(unsigned char*));
// Save allocated adress
*reinterpret_cast<unsigned char**>(ptrForAddress) = allocatedMemory;
// Return aligned address
return reinterpret_cast<void*>(alignedMemoryAddress);
}
inline void free(void* ptrToFree){
if( ptrToFree ){
unsigned char** storeRealAddress = reinterpret_cast<unsigned char**>(reinterpret_cast<unsigned char*>(ptrToFree) - sizeof(unsigned char*));
std::free(*storeRealAddress);
}
}
}
// Regular scalar new
void* operator new(std::size_t n) {
void* const allocated = aligned_malloc::malloc<DefaultMemAlignement>(n);
if(allocated){
return allocated;
}
throw std::bad_alloc();
return allocated;
}
void* operator new[]( std::size_t n ) {
void* const allocated = aligned_malloc::malloc<DefaultMemAlignement>(n);
if(allocated){
return allocated;
}
throw std::bad_alloc();
return allocated;
}
void* operator new ( std::size_t n, const std::nothrow_t& tag){
void* const allocated = aligned_malloc::malloc<DefaultMemAlignement>(n);
return allocated;
}
void* operator new[] ( std::size_t n, const std::nothrow_t& tag){
void* const allocated = aligned_malloc::malloc<DefaultMemAlignement>(n);
return allocated;
}
// Regular scalar delete
void operator delete(void* p) {
aligned_malloc::free(p);
}
void operator delete[](void* p) {
aligned_malloc::free(p);
}
void operator delete ( void* p, const std::nothrow_t& /*tag*/) {
aligned_malloc::free(p);
}
void operator delete[]( void* p, const std::nothrow_t& /*tag*/) {
aligned_malloc::free(p);
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
#ifdef USE_IPP
#include <ipp.h>
#include <ipps_l.h>
#include <ippbase.h>
#include <memory>
template <class NumType>
struct GetIppDataType;
// https://software.intel.com/en-us/node/501992
template <>
struct GetIppDataType<double>{
static const IppDataType type = ipp64f;
};
template <>
struct GetIppDataType<int>{
static const IppDataType type = ipp32s;
};
template <class NumType>
class IppSort{
static void ipp_sort(Ipp64f* pSrcDst, IppSizeL len, Ipp8u* pBuffer){
IppStatus status = ippsSortRadixAscend_64f_I(pSrcDst, len, pBuffer);// TODO cannot find ippsSortRadixAscend_64f_I_L
if( status != ippStsNoErr ) {
std::cout << "ippsSortRadixAscend() Error, at line " << __LINE__ << ":\n";
std::cout << ippGetStatusString(status) << std::endl;
exit(1);
}
}
static void ipp_sort(Ipp32s* pSrcDst, IppSizeL len, Ipp8u* pBuffer){
IppStatus status = ippsSortRadixAscend_32s_I_L(pSrcDst, len, pBuffer);
if( status != ippStsNoErr ) {
std::cout << "ippsSortRadixAscend() Error, at line " << __LINE__ << ":\n";
std::cout << ippGetStatusString(status) << std::endl;
exit(1);
}
}
const size_t sizeToSort;
std::unique_ptr<Ipp8u[]> buffer;
public:
explicit IppSort(const IppSizeL inSizeToSort)
: sizeToSort(inSizeToSort){
IppSizeL lenghtBuffer = 0;
IppStatus status = ippsSortRadixGetBufferSize_L(inSizeToSort, GetIppDataType<NumType>::type, &lenghtBuffer);
if( status != ippStsNoErr ) {
std::cout << "ippsSortRadixGetBufferSize_L() Error, at line " << __LINE__ << ":\n";
std::cout << ippGetStatusString(status) << std::endl;
exit(1);
}
buffer.reset(new Ipp8u[lenghtBuffer]());
}
void sort(NumType* array){
ipp_sort(array, sizeToSort, buffer.get());
}
};
#else
#warning "IPP is disabled"
#endif
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
class dtimer {
using double_second_time = std::chrono::duration<double, std::ratio<1, 1>>;
std::chrono::high_resolution_clock::time_point
m_start; ///< m_start time (start)
std::chrono::high_resolution_clock::time_point m_end; ///< stop time (stop)
std::chrono::nanoseconds m_cumulate; ///< the m_cumulate time
public:
/// Constructor
dtimer() { start(); }
/// Copy constructor
dtimer(const dtimer& other) = delete;
/// Copies an other timer
dtimer& operator=(const dtimer& other) = delete;
/// Move constructor
dtimer(dtimer&& other) = delete;
/// Copies an other timer
dtimer& operator=(dtimer&& other) = delete;
/** Rest all the values, and apply start */
void reset() {
m_start = std::chrono::high_resolution_clock::time_point();
m_end = std::chrono::high_resolution_clock::time_point();
m_cumulate = std::chrono::nanoseconds();
start();
}
/** Start the timer */
void start() {
m_start = std::chrono::high_resolution_clock::now();
}
/** Stop the current timer */
void stop() {
m_end = std::chrono::high_resolution_clock::now();
m_cumulate += std::chrono::duration_cast<std::chrono::nanoseconds>(m_end - m_start);
}
/** Return the elapsed time between start and stop (in second) */
double getElapsed() const {
return std::chrono::duration_cast<double_second_time>(
std::chrono::duration_cast<std::chrono::nanoseconds>(m_end - m_start)).count();
}
/** Return the total counted time */
double getCumulated() const {
return std::chrono::duration_cast<double_second_time>(m_cumulate).count();
}
/** End the current counter (stop) and return the elapsed time */
double stopAndGetElapsed() {
stop();
return getElapsed();
}
};
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// Init functions
////////////////////////////////////////////////////////////
#include <iostream>
#include <memory>
#include <cstdlib>
template <class NumType>
void assertNotSorted(const NumType array[], const size_t size, const std::string log){
for(size_t idx = 1 ; idx < size ; ++idx){
if(array[idx-1] > array[idx]){
std::cout << "assertNotSorted -- Array is not sorted\n"
"assertNotSorted -- - at pos " << idx << "\n"
"assertNotSorted -- - log " << log << std::endl;
}
}
}
template <class NumType>
void assertNotPartitioned(const NumType array[], const size_t size, const NumType pivot,
const size_t limite, const std::string log){
for(size_t idx = 0 ; idx < limite ; ++idx){
if(array[idx] > pivot){
std::cout << "assertNotPartitioned -- Array is not partitioned\n"
"assertNotPartitioned -- - at pos " << idx << "\n"
"assertNotPartitioned -- - log " << log << std::endl;
}
}
for(size_t idx = limite ; idx < size ; ++idx){
if(array[idx] <= pivot){
std::cout << "assertNotPartitioned -- Array is not partitioned\n"
"assertNotPartitioned -- - at pos " << idx << "\n"
"assertNotPartitioned -- - log " << log << std::endl;
}
}
}
template <class NumType>
void assertNotEqual(const NumType array1[], const NumType array2[],
const int size, const std::string log){
for(int idx = 0 ; idx < size ; ++idx){
if(array1[idx] != array2[idx]){
std::cout << "assertNotEqual -- Array is not equal\n"
"assertNotEqual -- - at pos " << idx << "\n"
"assertNotEqual -- - array1 " << array1[idx] << "\n"
"assertNotEqual -- - array2 " << array2[idx] << "\n"
"assertNotEqual -- - log " << log << std::endl;
}
}
}
template <class NumType>
void createRandVec(NumType array[], const size_t size){
for(size_t idx = 0 ; idx < size ; ++idx){
array[idx] = NumType(drand48()*double(size));
}
}
// To ensure vec is used and to kill extra optimization
template <class NumType>
void useVec(NumType array[], const size_t size){
double all = 0;
for(size_t idx = 0 ; idx < size ; ++idx){
all += double(array[idx]) * 0.000000000001;
}
// This will never happen!
if(all == std::numeric_limits<double>::max()){
std::cout << "The impossible happens!!" << std::endl;
exit(99);
}
}
#include <cstring>
template <class NumType, class SizeType = size_t>
class Checker{
std::unique_ptr<NumType[]> cpArray;
NumType* ptrArray;
SizeType size;
public:
Checker(const NumType sourceArray[],
NumType toCheck[],
const SizeType inSinze)
: ptrArray(toCheck), size(inSinze){
cpArray.reset(new NumType[size]);
memcpy(cpArray.get(), sourceArray, size*sizeof(NumType));
}
~Checker(){
std::sort(ptrArray, ptrArray+size);
std::sort(cpArray.get(), cpArray.get()+size);
assertNotEqual(cpArray.get(), ptrArray, size, "Checker");
}
};
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
/// Timing functions
////////////////////////////////////////////////////////////
#include <fstream>
template <class NumType>
void timeAll(std::ostream& fres){
const size_t MaxSize = 1073741824;//10L*1024L*1024L*1024L;//10*1024*1024*1024;262144*8;//
const int NbLoops = 5;
std::unique_ptr<NumType[]> array(new NumType[MaxSize]);
fres << "#size\tstdsort\tsort512";
#ifdef USE_IPP
fres << "\tipp\tipplogn";
#endif
fres << "\n";
for(size_t currentSize = 64 ; currentSize <= MaxSize ; currentSize *= 8 ){
std::cout << "currentSize " << currentSize << std::endl;
double allTimes[3][3] = {{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. },
{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. },
{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. }};
#ifdef USE_IPP
IppSort<NumType> ippsort(currentSize);
#endif
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
std::cout << " idxLoop " << idxLoop << std::endl;
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
std::sort(&array[0], &array[currentSize], [&](const NumType& v1, const NumType& v2){
return v1 < v2;
});
timer.stop();
std::cout << " std::sort " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 0;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
Sort512::Sort<NumType, size_t>(array.get(), currentSize);
timer.stop();
std::cout << " Sort512 " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 1;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
#ifdef USE_IPP
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
ippsort.sort(array.get());
timer.stop();
std::cout << " IPPSORT " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 2;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
#endif
}
std::cout << currentSize << ",\"stdsort\"," << allTimes[0][0] << "," << allTimes[0][1] << "," << allTimes[0][2] << "\n";
std::cout << currentSize << ",\"sort512\"," << allTimes[1][0] << "," << allTimes[1][1] << "," << allTimes[1][2] << "\n";
std::cout << currentSize << ",\"ipp\"," << allTimes[2][0] << "," << allTimes[2][1] << "," << allTimes[2][2] << "\n";
fres << currentSize << "\t"
<< allTimes[0][2] << "\t" << allTimes[0][2]/(currentSize*std::log(currentSize)) << "\t"
<< allTimes[1][2] << "\t" << allTimes[1][2]/(currentSize*std::log(currentSize));
#ifdef USE_IPP
fres << "\t" << allTimes[2][2] << "\t" <<
allTimes[2][2]/(currentSize*std::log(currentSize));
#endif
fres << "\n";
}
}
template <class NumType>
void timeAll_pair(std::ostream& fres){
const size_t MaxSize = 1073741824;//10L*1024L*1024L*1024L;//10*1024*1024*1024;262144*8;//
const int NbLoops = 5;
std::unique_ptr<NumType[]> array(new NumType[MaxSize]);
std::unique_ptr<NumType[]> values(new NumType[MaxSize]());
std::unique_ptr<std::array<NumType,2>[]> arrayStruct(new std::array<NumType,2>[MaxSize]());
fres << "#size\tstdsort\tsort512";
fres << "\n";
for(size_t currentSize = 64 ; currentSize <= MaxSize ; currentSize *= 8 ){
std::cout << "currentSize " << currentSize << std::endl;
double allTimes[2][3] = {{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. },
{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. }};
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
std::cout << " idxLoop " << idxLoop << std::endl;
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
for(int idxItem = 0 ; idxItem < currentSize ; ++idxItem){
arrayStruct[idxItem][0] = array[idxItem];
}
dtimer timer;
std::sort(&arrayStruct[0], &arrayStruct[currentSize], [&](const std::array<NumType,2>& v1, const std::array<NumType,2>& v2){
return v1[0] < v2[0];
});
timer.stop();
std::cout << " std::sort " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 0;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
Sort512kv::Sort<NumType, size_t>(array.get(), values.get(), currentSize);
timer.stop();
std::cout << " sort512 " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 1;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
}
std::cout << currentSize << ",\"stdsort\"," << allTimes[0][0] << "," << allTimes[0][1] << "," << allTimes[0][2] << "\n";
std::cout << currentSize << ",\"sort512\"," << allTimes[1][0] << "," << allTimes[1][1] << "," << allTimes[1][2] << "\n";
fres << currentSize << "\t"
<< allTimes[0][2] << "\t" << allTimes[0][2]/(currentSize*std::log(currentSize)) << "\t"
<< allTimes[1][2] << "\t" << allTimes[1][2]/(currentSize*std::log(currentSize));
fres << "\n";
}
}
#if defined(_OPENMP)
template <class NumType>
void timeAllOmp(std::ostream& fres, const std::string prefix){
const size_t MaxSize = 1073741824;//10L*1024L*1024L*1024L;//10*1024*1024*1024;262144*8;//
const int NbLoops = 5;
std::unique_ptr<NumType[]> array(new NumType[MaxSize]);
for(size_t currentSize = 64 ; currentSize <= MaxSize ; currentSize *= 8 ){
std::cout << "currentSize " << currentSize << std::endl;
double allTimes[4][3] = {{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. },
{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. },
{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. }};
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
std::cout << " idxLoop " << idxLoop << std::endl;
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
Sort512::SortOmpPartition<NumType, size_t>(array.get(), currentSize);
timer.stop();
std::cout << " SortOmpPartition " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 0;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
Sort512::SortOmpMerge<NumType, size_t>(array.get(), currentSize);
timer.stop();
std::cout << " SortOmpMerge " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 1;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
Sort512::SortOmpMergeDeps<NumType, size_t>(array.get(), currentSize);
timer.stop();
std::cout << " SortOmpMergeDeps " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 2;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
dtimer timer;
Sort512::SortOmpParMerge<NumType, size_t>(array.get(), currentSize);
timer.stop();
std::cout << " SortOmpParMerge " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 3;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
}
fres << prefix << currentSize << ",\"SortOmpPartition\"," << allTimes[0][0] << "," << allTimes[0][1] << "," << allTimes[0][2] << "\n";
fres << prefix << currentSize << ",\"SortOmpMerge\"," << allTimes[1][0] << "," << allTimes[1][1] << "," << allTimes[1][2] << "\n";
fres << prefix << currentSize << ",\"SortOmpMergeDeps\"," << allTimes[2][0] << "," << allTimes[2][1] << "," << allTimes[2][2] << "\n";
fres << prefix << currentSize << ",\"SortOmpParMerge\"," << allTimes[3][0] << "," << allTimes[3][1] << "," << allTimes[3][2] << "\n";
fres.flush();
}
}
#endif
template <class NumType>
void timeSmall(std::ostream& fres){
const size_t MaxSizeV2 = 16*64/sizeof(NumType);
const int NbLoops = 10000;
std::unique_ptr<NumType[]> array(new NumType[MaxSizeV2*NbLoops]);
double allTimes[3] = {0};
fres << "#size\tstdsort\tstdsortlogn\tsort512\tsort512logn";
#ifdef USE_IPP
fres << "\tipp\tipplogn";
#endif
fres << "\n";
for(size_t currentSize = 1 ; currentSize <= MaxSizeV2 ; currentSize++ ){
std::cout << "currentSize " << currentSize << std::endl;
std::cout << " std::sort " << std::endl;
{
srand48((long int)(currentSize));
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
createRandVec(&array[idxLoop*currentSize], currentSize);
}
}
{
dtimer timer;
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
std::sort(&array[idxLoop*currentSize], &array[(idxLoop+1)*currentSize], [&](const NumType& v1, const NumType& v2){
return v1 < v2;
});
}
timer.stop();
std::cout << " std::sort " << timer.getElapsed() << std::endl;
const int idxType = 0;
allTimes[idxType] = timer.getElapsed()/double(NbLoops);
}
std::cout << " newqs512bitfull " << std::endl;
{
srand48((long int)(currentSize));
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
useVec(&array[idxLoop*currentSize], currentSize);
createRandVec(&array[idxLoop*currentSize], currentSize);
}
}
{
dtimer timer;
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
Sort512::SmallSort16V(&array[idxLoop*currentSize], currentSize);
}
timer.stop();
std::cout << " sort512 " << timer.getElapsed() << std::endl;
const int idxType = 1;
allTimes[idxType] = timer.getElapsed()/double(NbLoops);
}
{
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
useVec(&array[idxLoop*currentSize], currentSize);
}
}
#ifdef USE_IPP
std::cout << " ipp " << std::endl;
{
srand48((long int)(currentSize));
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
useVec(&array[idxLoop*currentSize], currentSize);
createRandVec(&array[idxLoop*currentSize], currentSize);
}
}
{
IppSort<NumType> ippsort(currentSize);
dtimer timer;
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
ippsort.sort(&array[idxLoop*currentSize]);
}
timer.stop();
std::cout << " ipp " << timer.getElapsed() << std::endl;
const int idxType = 2;
allTimes[idxType] = timer.getElapsed()/double(NbLoops);
}
{
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
useVec(&array[idxLoop*currentSize], currentSize);
}
}
#endif
fres << currentSize << "\t" << allTimes[0] << "\t" <<
allTimes[0]/(currentSize*std::log(currentSize)) << "\t" << allTimes[1] << "\t" <<
allTimes[1]/(currentSize*std::log(currentSize));
#ifdef USE_IPP
fres << "\t" << allTimes[2] << "\t" <<
allTimes[2]/(currentSize*std::log(currentSize));
#endif
fres << "\n";
}
}
template <class NumType>
void timeSmall_pair(std::ostream& fres){
const size_t MaxSizeV2 = 16*64/sizeof(NumType);
const int NbLoops = 10000;
std::unique_ptr<NumType[]> array(new NumType[MaxSizeV2*NbLoops]);
std::unique_ptr<NumType[]> indexes(new NumType[MaxSizeV2*NbLoops]());
std::unique_ptr<std::array<NumType,2>[]> arrayStruct(new std::array<NumType,2>[MaxSizeV2*NbLoops]());
double allTimes[3] = {0};
fres << "#size\tstdsort\tstdsortlogn\tsort512\tsort512logn";
fres << "\n";
for(size_t currentSize = 1 ; currentSize <= MaxSizeV2 ; currentSize++ ){
std::cout << "currentSize " << currentSize << std::endl;
std::cout << " std::sort " << std::endl;
{
srand48((long int)(currentSize));
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
createRandVec(&array[idxLoop*currentSize], currentSize);
for(int idxItem = 0 ; idxItem < currentSize ; ++idxItem){
arrayStruct[idxLoop*currentSize+idxItem][0] = array[idxLoop*currentSize+idxItem];
}
}
}
{
dtimer timer;
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
std::sort(&arrayStruct[idxLoop*currentSize], &arrayStruct[(idxLoop+1)*currentSize], [&](const std::array<NumType,2>& v1, const std::array<NumType,2>& v2){
return v1[0] < v2[0];
});
}
timer.stop();
std::cout << " std::sort " << timer.getElapsed() << std::endl;
const int idxType = 0;
allTimes[idxType] = timer.getElapsed()/double(NbLoops);
}
std::cout << " sort512 " << std::endl;
{
srand48((long int)(currentSize));
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
useVec(&array[idxLoop*currentSize], currentSize);
createRandVec(&array[idxLoop*currentSize], currentSize);
}
}
{
dtimer timer;
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
Sort512kv::SmallSort16V(&array[idxLoop*currentSize], &indexes[idxLoop*currentSize], currentSize);
}
timer.stop();
std::cout << " sort512 " << timer.getElapsed() << std::endl;
const int idxType = 1;
allTimes[idxType] = timer.getElapsed()/double(NbLoops);
}
{
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
useVec(&array[idxLoop*currentSize], currentSize);
}
}
fres << currentSize << "\t" << allTimes[0] << "\t" <<
allTimes[0]/(currentSize*std::log(currentSize)) << "\t" << allTimes[1] << "\t" <<
allTimes[1]/(currentSize*std::log(currentSize));
fres << "\n";
}
}
template <class NumType>
void timePartitionAll(std::ostream& fres){
const size_t MaxSize = 1073741824;//10L*1024L*1024L*1024L;//10*1024*1024*1024;
const int NbLoops = 20;
std::unique_ptr<NumType[]> array(new NumType[MaxSize]);
fres << "#size\tstdpart\tstdpartn\tpartition512\tpartition512n";
fres << "\n";
for(size_t currentSize = 64 ; currentSize <= MaxSize ; currentSize *= 8 ){
std::cout << "currentSize " << currentSize << std::endl;
double allTimes[2][3] = {{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. },
{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. }};
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
std::cout << " idxLoop " << idxLoop << std::endl;
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
const NumType pivot = array[(idxLoop*currentSize/NbLoops)];
dtimer timer;
std::partition(&array[0], &array[currentSize], [&](const NumType& v){
return v < pivot;
});
timer.stop();
std::cout << " std::partition " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 0;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
const NumType pivot = array[(idxLoop*currentSize/NbLoops)];
dtimer timer;
Sort512::Partition512<size_t>(array.get(), 0, currentSize-1, pivot);
timer.stop();
std::cout << " partition512 " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 1;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
}
std::cout << currentSize << ",\"stdpartion\"," << allTimes[0][0] << "," << allTimes[0][1] << "," << allTimes[0][2] << "\n";
std::cout << currentSize << ",\"partition512\"," << allTimes[1][0] << "," << allTimes[1][1] << "," << allTimes[1][2] << "\n";
fres << currentSize << "\t"
<< allTimes[0][2] << "\t" << allTimes[0][2]/(currentSize) << "\t"
<< allTimes[1][2] << "\t" << allTimes[1][2]/(currentSize) << "\n";
}
}
template <class NumType>
void timePartitionAll_pair(std::ostream& fres){
const size_t MaxSize = 1073741824;//10L*1024L*1024L*1024L;//10*1024*1024*1024;
const int NbLoops = 20;
std::unique_ptr<NumType[]> array(new NumType[MaxSize]);
std::unique_ptr<NumType[]> values(new NumType[MaxSize]());
std::unique_ptr<std::array<NumType,2>[]> arrayStruct(new std::array<NumType,2>[MaxSize]());
fres << "#size\tstdpart\tstdpartn\tpartition512\tpartition512n";
fres << "\n";
for(size_t currentSize = 64 ; currentSize <= MaxSize ; currentSize *= 8 ){
std::cout << "currentSize " << currentSize << std::endl;
double allTimes[2][3] = {{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. },
{ std::numeric_limits<double>::max(), std::numeric_limits<double>::min(), 0. }};
for(int idxLoop = 0 ; idxLoop < NbLoops ; ++idxLoop){
std::cout << " idxLoop " << idxLoop << std::endl;
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
for(int idxItem = 0 ; idxItem < currentSize ; ++idxItem){
arrayStruct[idxItem][0] = array[idxItem];
}
const NumType pivot = array[(idxLoop*currentSize/NbLoops)];
dtimer timer;
std::partition(&arrayStruct[0], &arrayStruct[currentSize], [&](const std::array<NumType,2>& v){
return v[0] < pivot;
});
timer.stop();
std::cout << " std::partition " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 0;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
{
srand48((long int)(idxLoop));
createRandVec(array.get(), currentSize);
const NumType pivot = array[(idxLoop*currentSize/NbLoops)];
dtimer timer;
Sort512kv::Partition512<size_t>(array.get(), values.get(), 0, currentSize-1, pivot);
timer.stop();
std::cout << " partition512 " << timer.getElapsed() << std::endl;
useVec(array.get(), currentSize);
const int idxType = 1;
allTimes[idxType][0] = std::min(allTimes[idxType][0], timer.getElapsed());
allTimes[idxType][1] = std::max(allTimes[idxType][1], timer.getElapsed());
allTimes[idxType][2] += timer.getElapsed()/double(NbLoops);
}
}
std::cout << currentSize << ",\"stdpartion\"," << allTimes[0][0] << "," << allTimes[0][1] << "," << allTimes[0][2] << "\n";
std::cout << currentSize << ",\"partition512V2\"," << allTimes[1][0] << "," << allTimes[1][1] << "," << allTimes[1][2] << "\n";
fres << currentSize << "\t"
<< allTimes[0][2] << "\t" << allTimes[0][2]/(currentSize) << "\t"
<< allTimes[1][2] << "\t" << allTimes[1][2]/(currentSize) << "\n";
}
}
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
int main(){
#ifdef USE_IPP
IppStatus status=ippInit();
if( status != ippStsNoErr ) {
std::cout << "IppInit() Error:\n";
std::cout << ippGetStatusString(status) << std::endl;
return -1;
}
#endif
{
std::ofstream fres("smallres-int.data");
timeSmall<int>(fres);
}
{
std::ofstream fres("smallres-double.data");
timeSmall<double>(fres);
}
{
std::ofstream fres("smallres-pair-int.data");
timeSmall_pair<int>(fres);
}
{
std::ofstream fres("partitions-int.data");
timePartitionAll<int>(fres);
}
{
std::ofstream fres("partitions-double.data");
timePartitionAll<double>(fres);
}
{
std::ofstream fres("partitions-pair-int.data");
timePartitionAll_pair<int>(fres);
}
{
std::ofstream fres("res-int.data");
timeAll<int>(fres);
}
{
std::ofstream fres("res-double.data");
timeAll<double>(fres);
}
{
std::ofstream fres("res-pair-int.data");
timeAll_pair<int>(fres);
}
#if defined(_OPENMP)
{
std::ofstream fres("res-int-openmp.data");
timeAllOmp<int>(fres, "max-threads");
}
{
std::ofstream fres("res-double-openmp.data");
timeAllOmp<double>(fres, "max-threads");
}
#endif
return 0;
}