Template Numerical Library version\ main:481315e2
Searching...
No Matches
Sorting

# Introduction

TNL offers several different parallel algorithms for sorting of arrays (or vectors) and also sorting based on user defined swapping. The later 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. See

1#include <iostream>
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
4
5using namespace TNL;
6using namespace TNL::Containers;
7using namespace TNL::Algorithms;
8
9template< typename ArrayT >
10void sort( ArrayT& array )
11{
12 const int size = 10;
13
14 /****
15 * Fill the array with random integers.
16 */
17 Array< int > aux_array( size );
18 srand( size + 2021 );
19 parallelFor< Devices::Host >( 0, size, [&]( int i ) {
20 aux_array[ i ] = std::rand() % (2*size);
21 });
22 array = aux_array;
23
24 std::cout << "Random array: " << array << std::endl;
25
26 /****
27 * Sort the array in ascending order.
28 */
29 ascendingSort( array );
30 std::cout << "Array sorted in ascending order:" << array << std::endl;
31
32 /***
33 * Sort the array in descending order.
34 */
35 descendingSort( array );
36 std::cout << "Array sorted in descending order:" << array << std::endl;
37}
38
39int main( int argc, char* argv[] )
40{
41 /***
42 * Firstly, test the sorting on CPU.
43 */
44 std::cout << "Sorting on CPU ... " << std::endl;
46 sort( host_array );
47
48#ifdef __CUDACC__
49 /***
50 * And then also on GPU.
51 */
52 std::cout << "Sorting on GPU ... " << std::endl;
54 sort( cuda_array );
55#endif
56 return EXIT_SUCCESS;
57}
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:66
T endl(T... args)
Namespace for fundamental TNL algorithms.
Definition AtomicOperations.h:12
Namespace for TNL containers.
Definition Array.h:20
The main TNL namespace.
Definition AtomicOperations.h:12
T rand(T... args)
T sort(T... args)

Here we create array with random sequence of integers (lines 17-20) and then we sort the array in ascending order (line 27) and descending order (line 33). 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:

1#include <iostream>
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
4
5using namespace TNL;
6using namespace TNL::Containers;
7using namespace TNL::Algorithms;
8
9template< typename ArrayT >
10void sort( ArrayT& array )
11{
12 const int size = 10;
13
14 /****
15 * Fill the array with random integers.
16 */
17 Array< int > aux_array( size );
18 srand( size + 2021 );
19 parallelFor< Devices::Host >( 0, size, [&]( int i ) {
20 aux_array[ i ] = std::rand() % (2*size);
21 });
22 array = aux_array;
23
24 std::cout << "Random array: " << array << std::endl;
25
26 /****
27 * Sort the array in ascending order.
28 */
29 sort( array, [] __cuda_callable__ ( int a, int b ) { return a < b; } );
30 std::cout << "Array sorted in ascending order:" << array << std::endl;
31
32 /***
33 * Sort the array in descending order.
34 */
35 sort( array, [] __cuda_callable__ ( int a, int b ) { return a > b; } );
36 std::cout << "Array sorted in descending order:" << array << std::endl;
37}
38
39int main( int argc, char* argv[] )
40{
41 /***
42 * Firstly, test the sorting on CPU.
43 */
44 std::cout << "Sorting on CPU ... " << std::endl;
46 sort( host_array );
47
48#ifdef __CUDACC__
49 /***
50 * And then also on GPU.
51 */
52 std::cout << "Sorting on GPU ... " << std::endl;
54 sort( cuda_array );
55#endif
56 return EXIT_SUCCESS;
57}
#define __cuda_callable__
Definition CudaCallable.h:22

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

1#include <iostream>
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
4
5using namespace TNL;
6using namespace TNL::Containers;
7using namespace TNL::Algorithms;
8
9template< typename ArrayT >
10void sort( ArrayT& array )
11{
12 const int size = 10;
13
14 /****
15 * Fill the array with random integers.
16 */
17 Array< int > aux_array( size );
18 srand( size + 2021 );
19 parallelFor< Devices::Host >( 0, size, [&]( int i ) {
20 aux_array[ i ] = std::rand() % (2*size);
21 });
22 array = aux_array;
23
24 /***
25 * Prepare second array holding elements positions.
26 */
27 ArrayT index( size );
28 index.forAllElements( [] __cuda_callable__ ( int idx, int& value ) { value = idx; } );
29 std::cout << "Random array: " << array << std::endl;
30 std::cout << "Index array: " << index << std::endl;
31
32 /***
33 * Sort the array array and apply the same permutation on the array identity.
34 */
35 auto array_view = array.getView();
36 auto index_view = index.getView();
37 sort< typename ArrayT::DeviceType, // device on which the sorting will be performed
38 typename ArrayT::IndexType >( // type used for indexing
39 0, size, // range of indexes
40 [=] __cuda_callable__ ( int i, int j ) -> bool { // comparison lambda function
41 return array_view[ i ] < array_view[ j ]; },
42 [=] __cuda_callable__ ( int i, int j ) mutable { // lambda function for swapping of elements
43 TNL::swap( array_view[ i ], array_view[ j ] );
44 TNL::swap( index_view[ i ], index_view[ j ] ); } );
45 std::cout << "Sorted array: " << array << std::endl;
46 std::cout << "Index: " << index << std::endl;
47}
48
49int main( int argc, char* argv[] )
50{
51 /***
52 * Firstly, test the sorting on CPU.
53 */
54 std::cout << "Sorting on CPU ... " << std::endl;
56 sort( host_array );
57
58#ifdef __CUDACC__
59 /***
60 * And then also on GPU.
61 */
62 std::cout << "Sorting on GPU ... " << std::endl;
64 sort( cuda_array );
65#endif
66 return EXIT_SUCCESS;
67}
__cuda_callable__ constexpr void swap(Type &a, Type &b)
This function swaps values of two parameters.
Definition Math.h:499

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 the same way. See the lines 34-38. Here we call function sort which does not accept any array-like data structure but only range of indexes and two lambda functions. The first one defines ordering of the elements (line 35) by comparing elements of array array. The second lambda function is responsible for elements swapping (lines 36-38 ). 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 ]