Repository navigation
Expand file tree
/
Copy pathparallel.cpp
More file actions
3236 lines (3120 loc) · 107 KB
/
Copy pathparallel.cpp
File metadata and controls
3236 lines (3120 loc) · 107 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
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
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*--------------------------------------------------------------------------*/
/*--------------------------- File parallel.cpp ----------------------------*/
/*--------------------------------------------------------------------------*/
/** @file
* The benchmark of the parallel algorithms on t threads (-t, 1 by
* default): each algorithm of ADS on its own pool of threads (ThreadPool,
* "pool") and, when they are compiled in, on the executors of FastFlow,
* OpenMP and oneTBB, against the parallel versions of the libraries:
* - the sorts: tbb::parallel_sort, std::sort and std::stable_sort with
* std::execution::par (libstdc++ on oneTBB), __gnu_parallel::sort and
* stable_sort (the parallel mode of libstdc++, on OpenMP), the parallel
* IPS4o and IPS2Ra, parlay::sort_inplace, stable_sort_inplace and
* integer_sort_inplace of ParlayLib, on 64-bit integers, doubles and
* strings in the patterns of bench/sort.cpp, the sequential ADS::sort and
* ADS::stableSort as well when t = 1, and the learned sorts of ADS on
* the numbers, on the pool;
* - the patterns: the sum of 64-bit integers (parallelReduce against
* tbb::parallel_reduce, std::reduce with par, an OpenMP reduction,
* parlay::reduce and the ParallelForReduce of FastFlow), the inclusive
* prefix sums (parallelInclusiveScan against tbb::parallel_scan,
* std::inclusive_scan with par, parlay::scan_inclusive and a scan of
* OpenMP in two passes) and a loop that maps each item (parallelFor
* against tbb::parallel_for, std::for_each with par, an OpenMP loop,
* parlay::parallel_for and the ParallelFor of FastFlow);
* - the stable merge of 4 and 64 sorted runs (multiwayMerge against
* __gnu_parallel::stable_multiway_merge and the parallel merge of tlx);
* - the products of sparse matrices of 10^5 and 10^6 rows by a vector,
* 8 nonzeros per row or rows of lengths from a power law (CsrMatrix
* and SellMatrix against loops on the rows of oneTBB, OpenMP and
* ParlayLib and the product of Eigen on OpenMP), per nonzero;
* - the products of dense matrices of 1024 and 2048 (gemm against
* OpenBLAS and Eigen on OpenMP), per multiplication;
* - the visit in breadth from the node of largest degree and the connected
* components of graphs of 2^16 and 2^19 nodes and 16 edges per node, at
* random or of Kronecker (bfsDistances and connectedComponents against
* the direction-optimizing visit, Afforest and Shiloach-Vishkin of the
* GAP benchmark suite, and the visit of the examples of ParlayLib), per
* arc;
* - the shortest paths from the node of largest degree of the same graphs
* and of a grid of 2^16 and 2^20 nodes (a proxy of a road network), with
* integer costs at random in [ 1 , 255 ] (deltaStepping, Delta = 32 and
* 256 on the grid, and rhoStepping, rho = 2^20 and 2^12 on the grid,
* each on the two layouts of the arcs of StaticGraph, with the row A/A
* of the first, against the Delta-stepping of the GAP benchmark suite,
* the bucketed algorithm of Dijkstra of the examples of ParlayLib and,
* when t = 1, johnson(), the algorithm of Dijkstra of ShortestPaths.h on
* a binary heap), per arc;
* - the builds of the succinct structures: the directories of rank and
* select of 64 n random bits (RankSelect and CompactRankSelect against
* rank_support_v5 with select_support_mcl of sdsl-lite, sequential), per
* bit, the wavelet matrix of n random integers of 8, 16 and 32 bits,
* given as integers of 64 bits and as integers of their width
* ("narrow"), against wm_int of sdsl-lite, and the wavelet tree of
* Huffman of n symbols of 8 bits of a geometric law and of 16 bits at
* random, against wt_huff_int of sdsl-lite, per item, the sequential
* builds of ADS as well when t = 1 (pwm runs on the sequences that
* ADS_WAVELET_DUMP writes);
* - the LCP array of texts of 10^6 and 4 10^6 symbols, of 4 letters or of
* words of a Zipf law, from a suffix array of 32 bits (lcpArrayPhi
* against the one of the examples of ParlayLib), per symbol;
* - the concurrent hash tables, all driven by the same threads of the pool
* (ConcurrentFlatTable, as a map and as a set, and ConcurrentCuckooTable
* against tbb::concurrent_hash_map, tbb::concurrent_unordered_map, the
* cuckoohash_map of libcuckoo, the hashtable of ParlayLib and the
* uaGrow and usGrow tables of growt), per
* operation, for 10^6 and 4 10^6 keys (the nodes of 10^7 keys of the
* tables of oneTBB take more than a GB): the insertion of distinct keys
* into a table made for them and into an empty one that grows, the
* lookups of present and of absent keys, the find-or-insert of a CLOSED
* set of a search (each key twice) and a mix of 90% lookups and 10%
* insertions; and the find-or-insert and the lookups of states of 32
* bytes, the CLOSED set of a search;
* - the concurrent priority queues (the MultiQueue of ADS, relaxed and exact,
* against tbb::concurrent_priority_queue, the reference MultiQueue of
* Williams, Sanders and Dementiev, the k-LSM of Wimmer et al. and the
* SprayList of Alistarh et al.), per pair, for 10^6 and 4 10^6 pairs: their
* push and then their pops, and the hold model of a search, each pop
* followed by the push of a greater key; and the mean and the maximum rank
* error of the relaxed ones in the hold model of 10^6 pairs (-b rank), from
* the times at which the operations return, replayed in their order;
* - the branch-and-bound by best first of the complete binary trees of
* 2^18 - 1 and 2^21 - 1 nodes (-b bnb), the bound of a child that of its
* parent plus 1 to 1024 (bnb), or minus 1 or 2 (bnb-dive, a search that
* dives, its frontier small), with no evaluation of a node and with 256
* rounds of mixing (about a microsecond), per node: the stealing frontier
* of ADS, a queue per worker of which a thief takes about half (also with a
* bound per node, the key in bnb and at random in bnb-dive, and its least
* bound asked after each pop, "bounds"), against a std::priority_queue
* under a lock per worker, of which a thief takes the back half of the heap
* under the lock, and against tbb::concurrent_priority_queue and the
* MultiQueue of ADS shared by all, which stop at the count of the nodes
* known in advance.
* Each structure runs only if ADS_BENCH_WHO names it, or with no such
* variable, and ADS_BENCH_LIST lists them [see Bench.h], so that run.sh
* runs each in a process of its own. Each cell is the median of r runs,
* in nanoseconds per item, of the work on a fresh copy of the input, the
* copy excluded, after a first run not timed that starts the threads of
* the runtime; a result different from
* the sequential one gets -1. The benchmark of a cell is "parallel
* <algorithm> t<t>", so that the cells of each number of threads make a
* table of their own; its first structure is run twice, the second time as
* "A/A".
*
* parallel [ -r runs ] [ -s seed ] [ -t threads ] [ -w family ]
* [ -b sort|patterns|merge|spmv|gemm|graphs|sssp|succinct|lcp|
* hash|closed|pq|rank|bnb ]
* n1 n2 ...
*
* \author Donato Meoli \n
* Dipartimento di Informatica \n
* Università di Pisa \n
*
* \copyright © by Donato Meoli
*/
/*--------------------------------------------------------------------------*/
/*------------------------------ INCLUDES ----------------------------------*/
/*--------------------------------------------------------------------------*/
#ifndef ADS_CONTRACTS
#define ADS_CONTRACTS 0
#endif
#include "Bench.h"
#include "ADS/parallel/Executor.h"
#include "ADS/parallel/FastFlowExecutor.h"
#include "ADS/concurrent/ConcurrentCuckooTable.h"
#include "ADS/concurrent/ConcurrentFlatTable.h"
#include "ADS/concurrent/MultiQueue.h"
#include "ADS/concurrent/StealablePriorityQueue.h"
#include "ADS/graph/ShortestPaths.h"
#include "ADS/graph/StaticGraph.h"
#include "ADS/numeric/CsrMatrix.h"
#include "ADS/numeric/DenseMatrix.h"
#include "ADS/numeric/SellMatrix.h"
#include "ADS/parallel/OpenMPExecutor.h"
#include "ADS/parallel/ParallelGraph.h"
#include "ADS/parallel/ParallelMerge.h"
#include "ADS/parallel/ParallelNumeric.h"
#include "ADS/parallel/ParallelShortestPaths.h"
#include "ADS/parallel/ParallelSort.h"
#include "ADS/parallel/ParallelText.h"
#include "ADS/priority/MultiwayMerge.h"
#include "ADS/string/SuffixArray.h"
#include "ADS/string/SuffixSorting.h"
#include "ADS/succinct/CompactRankSelect.h"
#include "ADS/succinct/HuffmanWaveletTree.h"
#include "ADS/succinct/RankSelect.h"
#include "ADS/succinct/WaveletMatrix.h"
#include "ADS/parallel/TBBExecutor.h"
#include "ADS/parallel/ThreadPool.h"
#include "ADS/sort/Sort.h"
#if defined( ADS_WITH_FASTFLOW )
#include <ff/dc.hpp>
#endif
#if defined( ADS_HAS_TBB )
#include <tbb/concurrent_hash_map.h>
#include <tbb/concurrent_priority_queue.h>
#include <tbb/concurrent_unordered_map.h>
#include <tbb/global_control.h>
#include <tbb/parallel_for.h>
#include <tbb/parallel_reduce.h>
#include <tbb/parallel_scan.h>
#include <tbb/parallel_sort.h>
#include <execution>
#endif
#if defined( _OPENMP )
#include <omp.h>
#include <parallel/algorithm>
#endif
#if __has_include( <ips4o.hpp> ) && defined( _REENTRANT )
#include <ips4o.hpp>
#define ADS_HAS_IPS4O 1
#endif
#if __has_include( <ips2ra.hpp> ) && defined( _REENTRANT )
#include <ips2ra.hpp>
#define ADS_HAS_IPS2RA 1
#endif
#if __has_include( <parlay/primitives.h> )
#include <parlay/hash_table.h>
#include <parlay/parallel.h>
#include <parlay/primitives.h>
#define ADS_HAS_PARLAY 1
#if __has_include( <examples/BFS_ligra.h> )
#include <examples/BFS_ligra.h>
#include <examples/bucketed_dijkstra.h>
#include <examples/longest_common_prefix.h>
#define ADS_HAS_PARLAY_EXAMPLES 1
#endif
#endif
#if __has_include( <tlx/algorithm/parallel_multiway_merge.hpp> ) && \
defined( _OPENMP )
#include <tlx/algorithm/parallel_multiway_merge.hpp>
#define ADS_HAS_TLX 1
#endif
#if __has_include( <libcuckoo/cuckoohash_map.hh> )
#include <libcuckoo/cuckoohash_map.hh>
#define ADS_HAS_LIBCUCKOO 1
#endif
#if __has_include( <multiqueue/multiqueue.hpp> )
#include <multiqueue/modes/random.hpp>
#include <multiqueue/modes/stick_random.hpp>
#include <multiqueue/multiqueue.hpp>
#define ADS_HAS_MULTIQUEUE 1
#endif
// the k-LSM needs its source of the threads compiled in (KLSM=<dir> of the
// makefile), which defines ADS_HAS_KLSM
#if defined( ADS_HAS_KLSM ) && __has_include( <k_lsm/k_lsm.h> )
#include <k_lsm/k_lsm.h>
#undef V // a macro of its counters
#else
#undef ADS_HAS_KLSM
#endif
// the SprayList of Alistarh et al., through the wrapper of the k-LSM
// (SPRAYLIST=<lib> of the makefile, which defines ADS_HAS_SPRAYLIST); its
// header needs <cstdint> before it
#if defined( ADS_HAS_SPRAYLIST ) && __has_include( <spraylist.h> )
#include <cstdint>
#include <spraylist.h>
#else
#undef ADS_HAS_SPRAYLIST
#endif
#if __has_include( <data-structures/table_config.hpp> )
#include <allocator/alignedallocator.hpp>
#include <data-structures/hash_table_mods.hpp>
#include <utils/hash/murmur2_hash.hpp>
#include <data-structures/table_config.hpp>
#define ADS_HAS_GROWT 1
#endif
#if defined( ADS_HAS_SDSL ) && __has_include( <sdsl/wavelet_trees.hpp> )
#include <sdsl/bit_vectors.hpp>
#include <sdsl/rank_support.hpp>
#include <sdsl/select_support.hpp>
#include <sdsl/wavelet_trees.hpp>
#else
#undef ADS_HAS_SDSL
#endif
#if __has_include( <Eigen/Sparse> ) && defined( _OPENMP )
#include <Eigen/Dense>
#include <Eigen/Sparse>
#define ADS_HAS_EIGEN 1
#endif
#if defined( ADS_HAS_OPENBLAS )
#include <cblas.h>
extern "C" void openblas_set_num_threads( int );
#endif
// the kernels of the GAP benchmark suite, each in a namespace of its own
// (their files define main(), and helpers of the same names)
#if __has_include( <benchmark.h> ) && __has_include( <bfs.cc> ) && \
defined( _OPENMP )
#include <cinttypes>
#include <iostream>
#include <unordered_map>
#include <benchmark.h>
#include <bitmap.h>
#include <builder.h>
#include <command_line.h>
#include <graph.h>
#include <platform_atomics.h>
#include <pvector.h>
#include <sliding_queue.h>
#include <timer.h>
#define main gapbsMain
namespace gapbsBfs
{
#include <bfs.cc>
}
namespace gapbsCc
{
#include <cc.cc>
}
namespace gapbsSv
{
#include <cc_sv.cc>
}
namespace gapbsSssp
{
#include <sssp.cc>
}
#undef main
#define ADS_HAS_GAPBS 1
#endif
#include <algorithm>
#include <array>
#include <atomic>
#include <bit>
#include <chrono>
#include <cmath>
#include <cstdint>
#include <cstdlib>
#include <functional>
#include <memory>
#include <mutex>
#include <numeric>
#include <random>
#include <span>
#include <string>
#include <tuple>
#include <utility>
#include <variant>
#include <vector>
/*--------------------------------------------------------------------------*/
/*------------------------------- INPUTS -----------------------------------*/
/*--------------------------------------------------------------------------*/
namespace
{
int threads = 1; ///< -t
const char * patterns[] = { "random" , "sorted" , "reversed" ,
"few" , "organ" , "runs16" };
/// the key of rank i of n in the pattern p, from the generator g, as in
/// bench/sort.cpp
std::uint64_t rank( int p , std::size_t i , std::size_t n ,
std::mt19937_64 & g )
{
switch( p ) {
case( 0 ):
return( g() );
case( 1 ):
return( i );
case( 2 ):
return( n - i );
case( 3 ):
return( g() % 16 );
case( 4 ):
return( std::min( i , n - 1 - i ) );
default:
return( ( i % ( n / 16 + 1 ) ) * 16 + i / ( n / 16 + 1 ) );
}
}
template< class T >
T make( std::uint64_t k );
template<>
std::uint64_t make( std::uint64_t k )
{
return( k );
}
template<>
double make( std::uint64_t k )
{
return( double( std::int64_t( k ) ) / 1e3 );
}
template<>
std::string make( std::uint64_t k )
{
std::string s = "item";
for( int d = 0 ; d < 12 ; ++d , k /= 26 )
s += char( 'a' + k % 26 );
return( s );
}
/// the name of a benchmark on the threads of the run
std::string bench( const std::string & what )
{
return( "parallel " + what + " t" + std::to_string( threads ) );
}
/// the median of r times, in nanoseconds per item, of work on copies of A,
/// or -1 if ok( copy ) is false after a run (checked untimed); a first run,
/// not timed, starts the threads of the runtime
template< class V , class W , class K >
double timeIt( const V & A , W && work , int r , K && ok )
{
{
V B = A;
work( B );
}
std::vector< double > t;
for( int i = 0 ; i < r ; ++i ) {
V B = A;
const auto a = std::chrono::steady_clock::now();
work( B );
const auto b = std::chrono::steady_clock::now();
if( ! ok( B ) )
return( -1 );
t.push_back( std::chrono::duration< double , std::nano >( b - a ).count() /
double( std::max< std::size_t >( A.size() , 1 ) ) );
}
std::nth_element( t.begin() , t.begin() + t.size() / 2 , t.end() );
return( t[ t.size() / 2 ] );
}
/// -w: the family of the structures run, "" for all: pool, fastflow (the
/// executor of FastFlow), ff (the loops of FastFlow itself), openmp (with
/// the parallel mode of libstdc++), tbb (with the policies of
/// std::execution), parlay, ips (IPS4o and IPS2Ra) or seq; the runtimes
/// keep their threads between the calls, spinning for a while, so that
/// two families in one process slow each other down
std::string family;
/// whether the family f runs
bool runs( const char * f )
{
return( family.empty() || ( family == f ) );
}
#if defined( ADS_WITH_FASTFLOW )
/// the loops of FastFlow itself, the family ff: one object, since its
/// workers spin between the calls; t - 1 workers, the caller waits
ff::ParallelForReduce< std::uint64_t > & ffNative( void )
{
static ff::ParallelForReduce< std::uint64_t > f( std::max( threads - 1 , 1 ) ,
true );
return( f );
}
#endif
#if defined( ADS_WITH_FASTFLOW )
/// the merge sort of A by the pattern ff_DC of FastFlow on t - 1 workers,
/// the leaves of at most n / 4t items sorted by ADS::stableSort, the two
/// sorted halves of a part merged through a buffer by std::merge; stable.
/// The pattern is built for each sort, as FastFlow uses it (its operand is
/// bound at the construction, and a pattern frozen and run again on a new
/// operand fails), so that the time includes the start of its farm
template< class T , class C >
void ffMergeSort( std::vector< T > & A , C less )
{
struct Part
{
T * a; // the items
T * b; // room for as many
std::size_t n;
};
const std::size_t leaf =
std::max< std::size_t >( A.size() / ( 4 * std::size_t( threads ) ) , 1024 );
std::vector< T > room( A.size() );
Part all{ A.data() , room.data() , A.size() } , out{};
ff::ff_DC< Part , Part > dc(
[]( const Part & p , std::vector< Part > & sub ) {
const std::size_t h = p.n / 2;
sub.push_back( Part{ p.a , p.b , h } );
sub.push_back( Part{ p.a + h , p.b + h , p.n - h } );
} ,
[ & ]( std::vector< Part > & r , Part & m ) {
std::merge( r[ 0 ].a , r[ 0 ].a + r[ 0 ].n , r[ 1 ].a ,
r[ 1 ].a + r[ 1 ].n ,
r[ 0 ].b , less );
std::copy( r[ 0 ].b , r[ 0 ].b + r[ 0 ].n + r[ 1 ].n , r[ 0 ].a );
m = Part{ r[ 0 ].a , r[ 0 ].b , r[ 0 ].n + r[ 1 ].n };
} ,
[ & ]( const Part & p , Part & m ) {
std::vector< T > v( p.a , p.a + p.n );
ADS::stableSort( v , less );
std::copy( v.begin() , v.end() , p.a );
m = p;
} ,
[ & ]( const Part & p ) { return( p.n <= leaf ); } , all , out ,
std::max( threads - 1 , 1 ) );
dc.run_and_wait_end();
}
#endif
/// the executors of ADS on the threads of the run, each built at its first
/// use
struct Executors
{
ADS::ThreadPool & pool( void ) {
if( ! f_pool )
f_pool = std::make_unique< ADS::ThreadPool >( threads );
return( *f_pool );
}
#if defined( ADS_WITH_FASTFLOW )
// the caller of FastFlow waits spinning: t - 1 workers on t cores
ADS::FastFlowExecutor & ff( void ) {
if( ! f_ff )
f_ff =
std::make_unique< ADS::FastFlowExecutor >( std::max( threads - 1 , 1 ) );
return( *f_ff );
}
#endif
#if defined( _OPENMP )
ADS::OpenMPExecutor & omp( void ) {
if( ! f_omp )
f_omp = std::make_unique< ADS::OpenMPExecutor >( threads );
return( *f_omp );
}
#endif
#if defined( ADS_WITH_TBB )
ADS::TBBExecutor & tbb( void ) {
if( ! f_tbb )
f_tbb = std::make_unique< ADS::TBBExecutor >( threads );
return( *f_tbb );
}
#endif
private:
std::unique_ptr< ADS::ThreadPool > f_pool;
#if defined( ADS_WITH_FASTFLOW )
std::unique_ptr< ADS::FastFlowExecutor > f_ff;
#endif
#if defined( _OPENMP )
std::unique_ptr< ADS::OpenMPExecutor > f_omp;
#endif
#if defined( ADS_WITH_TBB )
std::unique_ptr< ADS::TBBExecutor > f_tbb;
#endif
};
/// a structure of a battery and its work on a copy of the input
template< class V >
using Battery =
std::vector< std::pair< std::string , std::function< void( V & ) > > >;
/// adds the structure of the family f to the battery, if f runs
template< class V , class W >
void add( Battery< V > & S , const char * f , const std::string & name ,
W && work )
{
if( runs( f ) )
S.emplace_back( name , std::forward< W >( work ) );
}
/// the work of an algorithm of ADS on each executor, named "<name> pool",
/// "<name> FastFlow", "<name> OpenMP" and "<name> TBB"
template< class V , class F >
void onExecutors( Battery< V > & S , Executors & X , const std::string & name ,
F && f )
{
add( S , "pool" , name + " pool" ,
[ &X , f ]( V & A ) { f( X.pool() , A ); } );
#if defined( ADS_WITH_FASTFLOW )
add( S , "fastflow" , name + " FastFlow" ,
[ &X , f ]( V & A ) { f( X.ff() , A ); } );
#endif
#if defined( _OPENMP )
add( S , "openmp" , name + " OpenMP" ,
[ &X , f ]( V & A ) { f( X.omp() , A ); } );
#endif
#if defined( ADS_WITH_TBB )
add( S , "tbb" , name + " TBB" , [ &X , f ]( V & A ) { f( X.tbb() , A ); } );
#endif
}
/// runs a battery on A: its first structure twice, the second as "A/A"
template< class V , class K >
void runBattery( const std::string & b , const std::string & c , const V & A ,
Battery< V > & S , int r , K && ok )
{
if( S.empty() )
return;
for( std::size_t i = 0 ; i < S.size() ; ++i ) {
if( ! ADS::bench::skip( S[ i ].first ) )
ADS::bench::print( b.c_str() , c , A.size() , S[ i ].first ,
timeIt( A , S[ i ].second , r , ok ) );
if( ( i == 0 ) && ! ADS::bench::skip( S[ i ].first + " A/A" ) )
ADS::bench::print( b.c_str() , c , A.size() , S[ i ].first + " A/A" ,
timeIt( A , S[ i ].second , r , ok ) );
}
}
/*--------------------------------------------------------------------------*/
/*-------------------------------- SORTS -----------------------------------*/
/*--------------------------------------------------------------------------*/
/// the sorts of T, the stable ones apart
template< class T >
void sorts( const char * type , const std::vector< std::size_t > & sizes ,
int r ,
unsigned seed , Executors & X )
{
using V = std::vector< T >;
std::less<> less;
auto sorted = [ & ]( const V & B ) {
return( std::is_sorted( B.begin() , B.end() , less ) );
};
for( const bool stable : { false , true } ) {
Battery< V > S;
#if defined( ADS_HAS_TBB )
if( ! stable )
add( S , "tbb" , "tbb::parallel_sort" ,
[ & ]( V & A ) { tbb::parallel_sort( A.begin() , A.end() , less ); } );
else
add( S , "tbb" , "std::stable_sort(par)" , [ & ]( V & A ) {
std::stable_sort( std::execution::par , A.begin() , A.end() , less );
} );
#endif
if( ! stable )
onExecutors( S , X , "ADS::sort" ,
[ less ]( auto & ex ,
V & A ) { ADS::sort( ex , A , less ); } );
else
onExecutors( S , X , "ADS::stableSort" , [ less ]( auto & ex , V & A ) {
ADS::stableSort( ex , A , less );
} );
if constexpr( ADS::VectorKey< T > ) {
if( ! stable )
add( S , "pool" , "ADS::learnedSort pool" ,
[ &X ]( V & A ) { ADS::learnedSort( X.pool() , A ); } );
else
add( S , "pool" , "ADS::learnedStableSort pool" ,
[ &X ]( V & A ) { ADS::learnedStableSort( X.pool() , A ); } );
}
if( threads == 1 )
add( S , "seq" ,
stable ? "ADS::stableSort (sequential)" : "ADS::sort (sequential)" ,
[ & , stable ]( V & A ) {
if( stable )
ADS::stableSort( A , less );
else
ADS::sort( A , less );
} );
#if defined( ADS_HAS_TBB )
if( ! stable )
add( S , "tbb" , "std::sort(par)" , [ & ]( V & A ) {
std::sort( std::execution::par , A.begin() , A.end() , less );
} );
#endif
#if defined( _OPENMP )
if( ! stable )
add( S , "openmp" , "__gnu_parallel::sort" ,
[ & ]( V & A ) { __gnu_parallel::sort( A.begin() , A.end() ,
less ); } );
else
add( S , "openmp" , "__gnu_parallel::stable_sort" , [ & ]( V & A ) {
__gnu_parallel::stable_sort( A.begin() , A.end() , less );
} );
#endif
#if defined( ADS_HAS_IPS4O )
if( ! stable )
add( S , "ips" , "ips4o::parallel::sort" , [ & ]( V & A ) {
static ips4o::DefaultThreadPool pool( threads );
ips4o::parallel::sort( A.begin() , A.end() , less , pool );
} );
#endif
#if defined( ADS_HAS_IPS2RA )
if constexpr( std::is_integral_v< T > )
if( ! stable )
add( S , "ips" , "ips2ra::parallel::sort" , [ & ]( V & A ) {
ips2ra::parallel::sort(
A.begin() , A.end() , []( const T & x ) { return( x ); } , threads );
} );
#endif
#if defined( ADS_WITH_FASTFLOW )
// the divide and conquer of FastFlow: a merge sort whose halves go to
// its workers down to n / 4t items, sorted by ADS::stableSort, and whose
// merges (std::merge, on one thread each) climb back
add( S , "ff" , "ff_DC mergesort (ADS leaves)" ,
[ & ]( V & A ) { ffMergeSort( A , less ); } );
#endif
#if defined( ADS_HAS_PARLAY )
if( ! stable )
add( S , "parlay" , "parlay::sort_inplace" ,
[ & ]( V & A ) { parlay::sort_inplace( A , less ); } );
else
add( S , "parlay" , "parlay::stable_sort_inplace" ,
[ & ]( V & A ) { parlay::stable_sort_inplace( A , less ); } );
if constexpr( std::is_integral_v< T > )
add( S , "parlay" , "parlay::integer_sort_inplace" ,
[ & ]( V & A ) { parlay::integer_sort_inplace( A ); } );
#endif
for( std::size_t n : sizes )
for( int p = 0 ; p < 6 ; ++p ) {
std::mt19937_64 g( seed );
V A( n );
for( std::size_t i = 0 ; i < n ; ++i )
A[ i ] = make< T >( rank( p , i , n , g ) );
runBattery(
bench( std::string( stable ? "stableSort " : "sort " ) + type ) ,
patterns[ p ] , A , S , r , sorted );
}
}
}
/*--------------------------------------------------------------------------*/
/*------------------------------- PATTERNS ---------------------------------*/
/*--------------------------------------------------------------------------*/
/// the reduction, the prefix sums and the loop on 64-bit integers
void patternsOn( const std::vector< std::size_t > & sizes , int r ,
unsigned seed , Executors & X )
{
using V = std::vector< std::uint64_t >;
for( std::size_t n : sizes ) {
std::mt19937_64 g( seed );
V A( n );
for( auto & x : A )
x = g() >> 20;
// the sum, written into the first item so that the copy carries it
const std::uint64_t sum =
std::accumulate( A.begin() , A.end() , std::uint64_t( 0 ) );
{
Battery< V > S;
auto body = []( const V & B , std::size_t lo , std::size_t hi ) {
std::uint64_t s = 0;
for( std::size_t i = lo ; i < hi ; ++i )
s += B[ i ];
return( s );
};
#if defined( ADS_HAS_TBB )
add( S , "tbb" , "tbb::parallel_reduce" , [ & ]( V & B ) {
B[ 0 ] = tbb::parallel_reduce(
tbb::blocked_range< std::size_t >( 0 , B.size() ) , std::uint64_t( 0 ) ,
[ & ]( const tbb::blocked_range< std::size_t > & q , std::uint64_t s ) {
return( s + body( B , q.begin() , q.end() ) );
} ,
std::plus<>() );
} );
#endif
onExecutors( S , X , "ADS::parallelReduce" , [ body ]( auto & ex , V & B ) {
B[ 0 ] = ADS::parallelReduce(
ex , 0 , B.size() , std::uint64_t( 0 ) ,
[ & ]( std::size_t lo , std::size_t hi ) {
return( body( B , lo , hi ) );
} ,
std::plus<>() );
} );
#if defined( ADS_HAS_TBB )
add( S , "tbb" , "std::reduce(par)" , [ & ]( V & B ) {
B[ 0 ] = std::reduce( std::execution::par , B.begin() , B.end() ,
std::uint64_t( 0 ) );
} );
#endif
#if defined( _OPENMP )
add( S , "openmp" , "omp reduction" , [ & ]( V & B ) {
std::uint64_t s = 0;
const long m = long( B.size() );
#pragma omp parallel for reduction( + : s ) num_threads( threads )
for( long i = 0 ; i < m ; ++i )
s += B[ std::size_t( i ) ];
B[ 0 ] = s;
} );
#endif
#if defined( ADS_HAS_PARLAY )
add( S , "parlay" , "parlay::reduce" ,
[ & ]( V & B ) { B[ 0 ] = parlay::reduce( B ); } );
#endif
#if defined( ADS_WITH_FASTFLOW )
add( S , "ff" , "ff::ParallelForReduce" , [ & ]( V & B ) {
std::uint64_t s = 0;
ffNative().parallel_reduce_static(
s , std::uint64_t( 0 ) , 0 , long( B.size() ) , 1 , 0 ,
[ & ]( const long i ,
std::uint64_t & t ) { t += B[ std::size_t( i ) ]; } ,
[]( std::uint64_t & t , const std::uint64_t e ) { t += e; } ,
std::max( threads - 1 , 1 ) );
B[ 0 ] = s;
} );
#endif
runBattery( bench( "reduce" ) , "uint64" , A , S , r ,
[ & ]( const V & B ) { return( B[ 0 ] == sum ); } );
}
// the prefix sums, in place
V P( n );
std::inclusive_scan( A.begin() , A.end() , P.begin() );
{
Battery< V > S;
#if defined( ADS_HAS_TBB )
add( S , "tbb" , "tbb::parallel_scan" , [ & ]( V & B ) {
tbb::parallel_scan(
tbb::blocked_range< std::size_t >( 0 , B.size() ) , std::uint64_t( 0 ) ,
[ & ]( const tbb::blocked_range< std::size_t > & q , std::uint64_t s ,
bool final ) {
for( std::size_t i = q.begin() ; i < q.end() ; ++i ) {
s += B[ i ];
if( final )
B[ i ] = s;
}
return( s );
} ,
std::plus<>() );
} );
#endif
onExecutors( S , X , "ADS::parallelInclusiveScan" ,
[]( auto & ex , V & B ) {
ADS::parallelInclusiveScan( ex , B.begin() , B.size() , B.begin() ,
std::uint64_t( 0 ) );
} );
#if defined( ADS_HAS_TBB )
add( S , "tbb" , "std::inclusive_scan(par)" , [ & ]( V & B ) {
std::inclusive_scan( std::execution::par , B.begin() , B.end() ,
B.begin() );
} );
#endif
#if defined( _OPENMP )
add( S , "openmp" , "omp scan" , [ & ]( V & B ) {
// the sums of the blocks of the threads, then each block again
std::vector< std::uint64_t > part( std::size_t( threads ) + 1 , 0 );
const std::size_t m = B.size();
#pragma omp parallel num_threads( threads )
{
const std::size_t t = std::size_t( omp_get_thread_num() ) ,
q = std::size_t( omp_get_num_threads() ) ,
lo = m * t / q , hi = m * ( t + 1 ) / q;
std::uint64_t s = 0;
for( std::size_t i = lo ; i < hi ; ++i )
s += B[ i ];
part[ t + 1 ] = s;
#pragma omp barrier
#pragma omp single
for( std::size_t i = 1 ; i <= q ; ++i )
part[ i ] += part[ i - 1 ];
s = part[ t ];
for( std::size_t i = lo ; i < hi ; ++i )
B[ i ] = s += B[ i ];
}
} );
#endif
#if defined( ADS_HAS_PARLAY )
add( S , "parlay" , "parlay::scan_inclusive_inplace" ,
[ & ]( V & B ) { parlay::scan_inclusive_inplace( B ); } );
#endif
runBattery( bench( "scan" ) , "uint64" , A , S , r ,
[ & ]( const V & B ) { return( B == P ); } );
}
// a map: x -> a hash of x, in place
auto mix = []( std::uint64_t x ) {
x ^= x >> 33;
x *= 0xFF51AFD7ED558CCDull;
x ^= x >> 33;
return( x );
};
V M( n );
std::transform( A.begin() , A.end() , M.begin() , mix );
{
Battery< V > S;
#if defined( ADS_HAS_TBB )
add( S , "tbb" , "tbb::parallel_for" , [ & ]( V & B ) {
tbb::parallel_for( tbb::blocked_range< std::size_t >( 0 , B.size() ) ,
[ & ]( const tbb::blocked_range< std::size_t > & q ) {
for( std::size_t i = q.begin() ; i < q.end() ; ++i )
B[ i ] = mix( B[ i ] );
} );
} );
#endif
onExecutors( S , X , "ADS::parallelForBlocks" ,
[ mix ]( auto & ex , V & B ) {
ADS::parallelForBlocks( ex , 0 , B.size() ,
[ & ]( std::size_t lo , std::size_t hi ) {
for( std::size_t i = lo ; i < hi ; ++i )
B[ i ] = mix( B[ i ] );
} );
} );
#if defined( ADS_HAS_TBB )
add( S , "tbb" , "std::for_each(par)" , [ & ]( V & B ) {
std::for_each( std::execution::par , B.begin() , B.end() ,
[ & ]( std::uint64_t & x ) { x = mix( x ); } );
} );
#endif
#if defined( _OPENMP )
add( S , "openmp" , "omp for" , [ & ]( V & B ) {
const long m = long( B.size() );
#pragma omp parallel for num_threads( threads )
for( long i = 0 ; i < m ; ++i )
B[ std::size_t( i ) ] = mix( B[ std::size_t( i ) ] );
} );
#endif
#if defined( ADS_HAS_PARLAY )
add( S , "parlay" , "parlay::parallel_for" , [ & ]( V & B ) {
parlay::parallel_for( 0 , B.size() ,
[ & ]( std::size_t i ) { B[ i ] = mix( B[ i ] ); } );
} );
#endif
#if defined( ADS_WITH_FASTFLOW )
add( S , "ff" , "ff::ParallelFor" , [ & ]( V & B ) {
ffNative().parallel_for_static(
0 , long( B.size() ) , 1 , 0 ,
[ & ]( const long i ) {
B[ std::size_t( i ) ] = mix( B[ std::size_t( i ) ] );
} ,
std::max( threads - 1 , 1 ) );
} );
#endif
runBattery( bench( "map" ) , "uint64" , A , S , r ,
[ & ]( const V & B ) { return( B == M ); } );
}
}
}
/*--------------------------------------------------------------------------*/
/*------------------------------ NO COPIES ---------------------------------*/
/*--------------------------------------------------------------------------*/
/// the median of r runs of work(), in nanoseconds per one of n items, each
/// after an untimed prepare(), or -1 if ok() is false after a run; a first
/// run, not timed, starts the threads of the runtime
template< class P , class W , class K >
double timeRuns( std::size_t n , P && prepare , W && work , int r , K && ok )
{
prepare();
work();
std::vector< double > t;
for( int i = 0 ; i < r ; ++i ) {
prepare();
const auto a = std::chrono::steady_clock::now();
work();
const auto b = std::chrono::steady_clock::now();
if( ! ok() )
return( -1 );
t.push_back( std::chrono::duration< double , std::nano >( b - a ).count() /
double( std::max< std::size_t >( n , 1 ) ) );
}
std::nth_element( t.begin() , t.begin() + t.size() / 2 , t.end() );
return( t[ t.size() / 2 ] );
}
/// a battery of works on inputs that need no copy
using Works =
std::vector< std::pair< std::string , std::function< void( void ) > > >;
void addW( Works & S , const char * f , const std::string & name ,
std::function< void( void ) > work )
{
if( runs( f ) )
S.emplace_back( name , std::move( work ) );
}
/// the work of an algorithm of ADS on each executor, f( ex )
template< class F >
void onExecutorsW( Works & S , Executors & X , const std::string & name ,
F && f )
{
addW( S , "pool" , name + " pool" , [ &X , f ] { f( X.pool() ); } );
#if defined( ADS_WITH_FASTFLOW )
addW( S , "fastflow" , name + " FastFlow" , [ &X , f ] { f( X.ff() ); } );
#endif
#if defined( _OPENMP )
addW( S , "openmp" , name + " OpenMP" , [ &X , f ] { f( X.omp() ); } );
#endif
#if defined( ADS_WITH_TBB )
addW( S , "tbb" , name + " TBB" , [ &X , f ] { f( X.tbb() ); } );
#endif
}
/// runs a battery: its first structure twice, the second as "A/A"
template< class P , class K >
void runWorks( const std::string & b , const std::string & c , std::size_t n ,
Works & S , int r , P && prepare , K && ok )
{
for( std::size_t i = 0 ; i < S.size() ; ++i ) {
if( ! ADS::bench::skip( S[ i ].first ) )
ADS::bench::print( b.c_str() , c , n , S[ i ].first ,
timeRuns( n , prepare , S[ i ].second , r , ok ) );
if( ( i == 0 ) && ! ADS::bench::skip( S[ i ].first + " A/A" ) )
ADS::bench::print( b.c_str() , c , n , S[ i ].first + " A/A" ,
timeRuns( n , prepare , S[ i ].second , r , ok ) );
}
}
/*--------------------------------------------------------------------------*/
/*-------------------------------- MERGES ----------------------------------*/
/*--------------------------------------------------------------------------*/
/// the stable merge of k sorted runs of 64-bit integers, k = 4 and 64
void merges( const std::vector< std::size_t > & sizes , int r , unsigned seed ,
Executors & X )
{
using U = std::uint64_t;
using Range = std::pair< const U * , const U * >;
for( std::size_t n : sizes )
for( std::size_t k : { 4 , 64 } ) {
std::mt19937_64 g( seed );
std::vector< std::vector< U > > R( k );
std::vector< Range > Q;
// the merges of libstdc++ and tlx want iterators that can write
std::vector< std::pair< U * , U * > > Qw;
for( std::size_t i = 0 ; i < k ; ++i ) {
R[ i ].resize( n / k + ( i < n % k ) );
for( auto & x : R[ i ] )
x = g() % ( n / 2 + 1 ); // with ties
std::sort( R[ i ].begin() , R[ i ].end() );
Q.emplace_back( R[ i ].data() , R[ i ].data() + R[ i ].size() );
Qw.emplace_back( R[ i ].data() , R[ i ].data() + R[ i ].size() );
}
std::vector< U > ref( n ) , out( n , 0 );
ADS::multiwayMerge( Q , ref.begin() );
Works S;
#if defined( _OPENMP )
addW( S , "openmp" , "__gnu_parallel::stable_multiway_merge" , [ & ] {
auto C = Qw;
__gnu_parallel::stable_multiway_merge( C.begin() , C.end() , out.begin() ,
n ,
std::less<>() ,
__gnu_parallel::exact_tag() );
} );
#endif
onExecutorsW( S , X , "ADS::multiwayMerge" , [ & ]( auto & ex ) {
ADS::multiwayMerge( ex , Q , out.begin() );
} );