ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
ParallelSort.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------------*/
2/*--------------------------- File ParallelSort.h --------------------------*/
3/*--------------------------------------------------------------------------*/
145/*--------------------------------------------------------------------------*/
146/*----------------------------- DEFINITIONS --------------------------------*/
147/*--------------------------------------------------------------------------*/
148
149#ifndef __ADS_ParallelSort
150 #define __ADS_ParallelSort
151/* self-identification: #endif constant */
152
153/*--------------------------------------------------------------------------*/
154/*------------------------------ INCLUDES ----------------------------------*/
155/*--------------------------------------------------------------------------*/
156
157#include "ADS/Contract.h"
161#include "ADS/parallel/Scratch.h"
162#include "ADS/sort/LearnedSort.h"
163#include "ADS/sort/RadixSort.h"
164#include "ADS/sort/Sort.h"
166
167#include <algorithm>
168#include <cstddef>
169#include <cstdint>
170#include <functional>
171#include <vector>
172
173/*--------------------------------------------------------------------------*/
174/*--------------------------- NAMESPACE ------------------------------------*/
175/*--------------------------------------------------------------------------*/
176
177namespace ADS
178{
179
182inline constexpr std::size_t kParallelSort = std::size_t( 1 ) << 15;
183
188inline constexpr std::size_t kParallelSortThreads = 3;
189
246template< Executor E , SortStore Store ,
247 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
248 class C = std::less<> >
249void sort( E & ex , Store && A , C less = C() );
250
252template< Executor E , class T , class C = std::less<> >
253void sort( E & ex , std::vector< T > & A , C less = C() );
254
273template< Executor E , SortStore Store ,
274 class T = std::ranges::range_value_t< std::remove_cvref_t< Store > > ,
275 class C = std::less<> >
276void stableSort( E & ex , Store && A , C less = C() );
277
279template< Executor E , class T , class C = std::less<> >
280void stableSort( E & ex , std::vector< T > & A , C less = C() );
281
301template<
302 Executor E , SortStore Store ,
303 VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > > >
304void learnedSort( E & ex , Store && A );
305
307template< Executor E , VectorKey T >
308void learnedSort( E & ex , std::vector< T > & A );
309
324template<
325 Executor E , SortStore Store ,
326 VectorKey T = std::ranges::range_value_t< std::remove_cvref_t< Store > > >
327void learnedStableSort( E & ex , Store && A );
328
330template< Executor E , VectorKey T >
331void learnedStableSort( E & ex , std::vector< T > & A );
332
333/*--------------------------------------------------------------------------*/
334/*------------------------- THE SORTS OF A RANGE ---------------------------*/
335/*--------------------------------------------------------------------------*/
336
341template< Executor E , SortRange R , class... P >
342 requires requires( E & ex ,
343 std::vector< std::ranges::range_value_t< R > > & v ,
344 P &&... p )
345{ sort( ex , v , std::forward< P >( p )... ); }
346decltype( auto ) sort( E & ex , R && A , P &&... p )
347{
348 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
349 return( sort( ex , v , std::forward< P >( p )... ) );
350 };
351 return( detail::sortRange( A , f ) );
352 }
353
358template< Executor E , SortRange R , class... P >
359 requires requires( E & ex ,
360 std::vector< std::ranges::range_value_t< R > > & v ,
361 P &&... p )
362{
363 stableSort( ex , v , std::forward< P >( p )... );
364 }
365decltype( auto ) stableSort( E & ex , R && A , P &&... p )
366{
367 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
368 return( stableSort( ex , v , std::forward< P >( p )... ) );
369 };
370 return( detail::sortRange( A , f ) );
371 }
372
377template< Executor E , SortRange R , class... P >
378 requires requires( E & ex ,
379 std::vector< std::ranges::range_value_t< R > > & v ,
380 P &&... p )
381{
382 learnedSort( ex , v , std::forward< P >( p )... );
383 }
384decltype( auto ) learnedSort( E & ex , R && A , P &&... p )
385{
386 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
387 return( learnedSort( ex , v , std::forward< P >( p )... ) );
388 };
389 return( detail::sortRange( A , f ) );
390 }
391
396template< Executor E , SortRange R , class... P >
397 requires requires( E & ex ,
398 std::vector< std::ranges::range_value_t< R > > & v ,
399 P &&... p )
400{
401 learnedStableSort( ex , v , std::forward< P >( p )... );
402 }
403decltype( auto ) learnedStableSort( E & ex , R && A , P &&... p )
404{
405 const auto f = [ & ]( auto & v ) -> decltype( auto ) {
406 return( learnedStableSort( ex , v , std::forward< P >( p )... ) );
407 };
408 return( detail::sortRange( A , f ) );
409 }
410
411/*--------------------------------------------------------------------------*/
412
413} // end( namespace ADS )
414
415/*--------------------------------------------------------------------------*/
416/*-------------------------- IMPLEMENTATION --------------------------------*/
417/*--------------------------------------------------------------------------*/
418
419#include "ADS/parallel/impl/ParallelSort.tpp"
420
421/*--------------------------------------------------------------------------*/
422
423#endif /* ParallelSort.h included */
424
425/*--------------------------------------------------------------------------*/
426/*------------------------ End File ParallelSort.h -------------------------*/
427/*--------------------------------------------------------------------------*/
The contracts of ADS: the preconditions, the postconditions and the invariants of the operations,...
The layer of execution of the parallel algorithms of ADS, beyond the book: an executor runs the calls...
An allocator for the large blocks of a structure, beyond the book: a request of at least one huge pag...
The learned sort, beyond the book: learnedSort() distributes the numbers of 32 or 64 bits into bucket...
The parallel merge of sorted ranges, beyond the book: multiwayMerge( ex , R , out ,...
The sorts by the digits of the keys, beyond the book (which has Counting Sort and Pigeonhole Sort,...
The room of the parallel algorithms, beyond the book: the buffers of items that the parallel sorts a...
The sort that chooses its algorithm, beyond the book: sort() and stableSort() look at what they sort ...
The quicksort in the lanes of the vector registers, beyond the book: vectorQuickSort() sorts integers...
the executors, whose bulk( n , f ) runs on their threads and returns when they have returned,...
Definition Executor.h:168
a range that the sorts take beside std::vector and std::span: random access, sized,...
Definition Sorted.h:153
the storage on which the sorts work in place, by reference: a std::vector or a std::span of its items...
Definition Sorted.h:178
the types that vectorQuickSort() sorts: integers and floating point numbers of 32 or 64 bits
Definition VectorQuickSort.h:178
void learnedStableSort(E &ex, Store &&A)
the vector of numbers sorted as learnedSort( ex , A ) sorts it, the equal items in their order ( an...
void sort(E &ex, Store &&A, C less=C())
the vector sorted by less on the threads of ex, not necessarily stable
constexpr std::size_t kParallelSort
the items from which the parallel sorts use more than one thread,
Definition ParallelSort.h:182
void learnedSort(E &ex, Store &&A)
the vector of numbers sorted in their natural order on the threads of ex, distributed by the model o...
void stableSort(E &ex, Store &&A, C less=C())
the vector sorted by less on the threads of ex, the equal items in their order
constexpr std::size_t kParallelSortThreads
the threads from which the parallel sorts distribute: on 2 threads the passes of the distribution cos...
Definition ParallelSort.h:188
Definition ADT.h:96