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
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
9template<
typename ArrayT >
10void sort( ArrayT& array )
19 parallelFor< Devices::Host >( 0, size, [&](
int i ) {
29 ascendingSort( array );
35 descendingSort( array );
39int main(
int argc,
char* argv[] )
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:66
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
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:
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
9template<
typename ArrayT >
10void sort( ArrayT& array )
19 parallelFor< Devices::Host >( 0, size, [&](
int i ) {
39int main(
int argc,
char* argv[] )
#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
2#include <TNL/Containers/Array.h>
3#include <TNL/Algorithms/sort.h>
9template<
typename ArrayT >
10void sort( ArrayT& array )
19 parallelFor< Devices::Host >( 0, size, [&](
int i ) {
28 index.forAllElements( []
__cuda_callable__ (
int idx,
int& value ) { value = idx; } );
35 auto array_view = array.getView();
36 auto index_view = index.getView();
37 sort<
typename ArrayT::DeviceType,
38 typename ArrayT::IndexType >(
41 return array_view[ i ] < array_view[ j ]; },
43 TNL::swap( array_view[ i ], array_view[ j ] );
44 TNL::swap( index_view[ i ], index_view[ j ] ); } );
49int main(
int argc,
char* argv[] )
__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 ]