Template Numerical Library version\ main:94209208
Loading...
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 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:

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
11sort( ArrayT& array )
12{
13 const int size = 10;
14
15 /****
16 * Fill the array with random integers.
17 */
18 Array< int > aux_array( size );
19 srand( size + 2021 );
20 parallelFor< Devices::Host >( 0,
21 size,
22 [ & ]( int i )
23 {
24 aux_array[ i ] = std::rand() % ( 2 * size );
25 } );
26 array = aux_array;
27
28 std::cout << "Random array: " << array << std::endl;
29
30 /****
31 * Sort the array in ascending order.
32 */
33 ascendingSort( array );
34 std::cout << "Array sorted in ascending order:" << array << std::endl;
35
36 /***
37 * Sort the array in descending order.
38 */
39 descendingSort( array );
40 std::cout << "Array sorted in descending order:" << array << std::endl;
41}
42
43int
44main( int argc, char* argv[] )
45{
46 /***
47 * Firstly, test the sorting on CPU.
48 */
49 std::cout << "Sorting on CPU ... " << std::endl;
51 sort( host_array );
52
53#ifdef __CUDACC__
54 /***
55 * And then also on GPU.
56 */
57 std::cout << "Sorting on GPU ... " << std::endl;
59 sort( cuda_array );
60#endif
61 return EXIT_SUCCESS;
62}
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:64
T endl(T... args)
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
T rand(T... args)
T sort(T... args)

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:

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
11sort( ArrayT& array )
12{
13 const int size = 10;
14
15 /****
16 * Fill the array with random integers.
17 */
18 Array< int > aux_array( size );
19 srand( size + 2021 );
20 parallelFor< Devices::Host >( 0,
21 size,
22 [ & ]( int i )
23 {
24 aux_array[ i ] = std::rand() % ( 2 * size );
25 } );
26 array = aux_array;
27
28 std::cout << "Random array: " << array << std::endl;
29
30 /****
31 * Sort the array in ascending order.
32 */
33 sort( array,
34 [] __cuda_callable__( int a, int b )
35 {
36 return a < b;
37 } );
38 std::cout << "Array sorted in ascending order:" << array << std::endl;
39
40 /***
41 * Sort the array in descending order.
42 */
43 sort( array,
44 [] __cuda_callable__( int a, int b )
45 {
46 return a > b;
47 } );
48 std::cout << "Array sorted in descending order:" << array << std::endl;
49}
50
51int
52main( int argc, char* argv[] )
53{
54 /***
55 * Firstly, test the sorting on CPU.
56 */
57 std::cout << "Sorting on CPU ... " << std::endl;
59 sort( host_array );
60
61#ifdef __CUDACC__
62 /***
63 * And then also on GPU.
64 */
65 std::cout << "Sorting on GPU ... " << std::endl;
67 sort( cuda_array );
68#endif
69 return EXIT_SUCCESS;
70}
#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

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
11sort( ArrayT& array )
12{
13 const int size = 10;
14
15 /****
16 * Fill the array with random integers.
17 */
18 Array< int > aux_array( size );
19 srand( size + 2021 );
20 parallelFor< Devices::Host >( 0,
21 size,
22 [ & ]( int i )
23 {
24 aux_array[ i ] = std::rand() % ( 2 * size );
25 } );
26 array = aux_array;
27
28 /***
29 * Prepare second array holding elements positions.
30 */
31 ArrayT index( size );
32 index.forAllElements(
33 [] __cuda_callable__( int idx, int& value )
34 {
35 value = idx;
36 } );
37 std::cout << "Random array: " << array << std::endl;
38 std::cout << "Index array: " << index << std::endl;
39
40 /***
41 * Sort the array `array` and apply the same permutation on the array `identity`.
42 */
43 auto array_view = array.getView();
44 auto index_view = index.getView();
45 sort< typename ArrayT::DeviceType, // device on which the sorting will be performed
46 typename ArrayT::IndexType >( // type used for indexing
47 0,
48 size, // range of indexes
49 [ = ] __cuda_callable__( int i, int j ) -> bool { // comparison lambda function
50 return array_view[ i ] < array_view[ j ];
51 },
52 [ = ] __cuda_callable__( int i, int j ) mutable { // lambda function for swapping of elements
53 TNL::swap( array_view[ i ], array_view[ j ] );
54 TNL::swap( index_view[ i ], index_view[ j ] );
55 } );
56 std::cout << "Sorted array: " << array << std::endl;
57 std::cout << "Index: " << index << std::endl;
58}
59
60int
61main( int argc, char* argv[] )
62{
63 /***
64 * Firstly, test the sorting on CPU.
65 */
66 std::cout << "Sorting on CPU ... " << std::endl;
68 sort( host_array );
69
70#ifdef __CUDACC__
71 /***
72 * And then also on GPU.
73 */
74 std::cout << "Sorting on GPU ... " << std::endl;
76 sort( cuda_array );
77#endif
78 return EXIT_SUCCESS;
79}
__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 ]