Introduction
TNL offers several different parallel algorithms for sorting of arrays (or vectors) and also sorting based on user defined swapping. The latter is more general but also less efficient.
Sorting of arrays and vectors
The sorting of arrays and vectors is accessible via the following functions:
The following example demonstrates the use of ascending and descending sort:
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
9template<
typename ArrayT >
20 parallelFor< Devices::Host >( 0,
24 aux_array[ i ] =
std::rand() % ( 2 * size );
33 ascendingSort( array );
39 descendingSort( array );
44main(
int argc,
char* argv[] )
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:64
Namespace for fundamental TNL algorithms.
Definition AtomicOperations.h:9
Namespace for TNL containers.
Definition Array.h:17
The main TNL namespace.
Definition AtomicOperations.h:9
Here we create an array with random sequence of integers using the parallelFor function and then we sort the array in ascending order using ascendingSort and descending order using the descendingSort.
The result looks as follows:
Sorting on CPU ...
Random array: [ 5, 1, 15, 5, 0, 11, 2, 14, 14, 8 ]
Array sorted in ascending order:[ 0, 1, 2, 5, 5, 8, 11, 14, 14, 15 ]
Array sorted in descending order:[ 15, 14, 14, 11, 8, 5, 5, 2, 1, 0 ]
Sorting on GPU ...
Random array: [ 5, 1, 15, 5, 0, 11, 2, 14, 14, 8 ]
Array sorted in ascending order:[ 0, 1, 2, 5, 5, 8, 11, 14, 14, 15 ]
Array sorted in descending order:[ 15, 14, 14, 11, 8, 5, 5, 2, 1, 0 ]
How to achieve the same result with user defined ordering is demonstrated by the following example:
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
9template<
typename ArrayT >
20 parallelFor< Devices::Host >( 0,
24 aux_array[ i ] =
std::rand() % ( 2 * size );
52main(
int argc,
char* argv[] )
#define __cuda_callable__
Definition Macros.h:49
The result looks as follows:
Sorting on CPU ...
Random array: [ 5, 1, 15, 5, 0, 11, 2, 14, 14, 8 ]
Array sorted in ascending order:[ 0, 1, 2, 5, 5, 8, 11, 14, 14, 15 ]
Array sorted in descending order:[ 15, 14, 14, 11, 8, 5, 5, 2, 1, 0 ]
Sorting on GPU ...
Random array: [ 5, 1, 15, 5, 0, 11, 2, 14, 14, 8 ]
Array sorted in ascending order:[ 0, 1, 2, 5, 5, 8, 11, 14, 14, 15 ]
Array sorted in descending order:[ 15, 14, 14, 11, 8, 5, 5, 2, 1, 0 ]
The same way, one can sort also TNL::Containers::ArrayView, TNL::Containers::Vector and TNL::Containers::VectorView.
Sorting with user-defined swapping
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
9template<
typename ArrayT >
20 parallelFor< Devices::Host >( 0,
24 aux_array[ i ] =
std::rand() % ( 2 * size );
43 auto array_view = array.getView();
44 auto index_view = index.getView();
45 sort<
typename ArrayT::DeviceType,
46 typename ArrayT::IndexType >(
50 return array_view[ i ] < array_view[ j ];
53 TNL::swap( array_view[ i ], array_view[ j ] );
54 TNL::swap( index_view[ i ], index_view[ j ] );
61main(
int argc,
char* argv[] )
__cuda_callable__ constexpr void swap(Type &a, Type &b)
This function swaps values of two parameters.
Definition Math.h:496
In this example, we fill array array
with random numbers and array index
with numbers equal to position of an element in the array. We want to sort the array array
and permute the index
array correspondingly. This is achieved by calling a variant of the sort
function, which does not accept an array-like data structure, but only range of indexes and two lambda functions. The first lambda function defines the ordering of the elements by comparing elements of array array
. The second lambda function is responsible for swapping elements. Note that we do not swap only elements of array array
, but also index
array.
The result looks as follows:
Sorting on CPU ...
Random array: [ 5, 1, 15, 5, 0, 11, 2, 14, 14, 8 ]
Index array: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Sorted array: [ 0, 1, 2, 5, 5, 8, 11, 14, 14, 15 ]
Index: [ 4, 1, 6, 3, 0, 9, 5, 8, 7, 2 ]
Sorting on GPU ...
Random array: [ 5, 1, 15, 5, 0, 11, 2, 14, 14, 8 ]
Index array: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]
Sorted array: [ 0, 1, 2, 5, 5, 8, 11, 14, 14, 15 ]
Index: [ 4, 1, 6, 0, 3, 9, 5, 7, 8, 2 ]