Template Numerical Library version\ main:8b8c8226
Loading...
Searching...
No Matches
Public Types | Public Member Functions | Static Public Member Functions | Protected Attributes | List of all members
TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator > Class Template Reference

Data structure for CSR segments format. More...

#include <TNL/Algorithms/Segments/CSR.h>

Collaboration diagram for TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >:
Collaboration graph
[legend]

Public Types

using ConstViewType = CSRView< Device, std::add_const_t< IndexType >, KernelType >
 Type of constant segments view.
 
using DeviceType = Device
 The device where the segments are operating.
 
using IndexType = std::remove_const_t< Index >
 The type used for indexing of segments elements.
 
using KernelType = Kernel
 Type of kernel used for reduction operations.
 
using OffsetsContainer = Containers::Vector< Index, DeviceType, IndexType, IndexAllocator >
 Type of container storing offsets of particular rows.
 
using SegmentViewType = SegmentView< IndexType, RowMajorOrder >
 Accessor type fro one particular segment.
 
template<typename Device_ , typename Index_ >
using ViewTemplate = CSRView< Device_, Index_, KernelType >
 Templated view type. More...
 
using ViewType = CSRView< Device, Index, KernelType >
 Type of segments view.1.
 

Public Member Functions

 CSR ()=default
 Construct with no parameters to create empty segments.
 
 CSR (const CSR &segments)=default
 Copy constructor. More...
 
template<typename SizesContainer >
 CSR (const SizesContainer &segmentsSizes)
 Construct with segments sizes. More...
 
template<typename ListIndex >
 CSR (const std::initializer_list< ListIndex > &segmentsSizes)
 Construct with segments sizes in initializer list.. More...
 
 CSR (CSR &&segments) noexcept=default
 Move constructor. More...
 
template<typename Function >
void forAllElements (Function &&function) const
 Call TNL::Algorithms::Segments::CSR::forElements for all elements of the segments. More...
 
template<typename Function >
void forAllSegments (Function &&function) const
 Call TNL::Algorithms::Segments::CSR::forSegments for all segments. More...
 
template<typename Function >
void forElements (IndexType begin, IndexType end, Function &&function) const
 Iterate over all elements of given segments in parallel and call given lambda function. More...
 
template<typename Function >
void forSegments (IndexType begin, IndexType end, Function &&function) const
 Iterate over all segments in parallel and call given lambda function. More...
 
ConstViewType getConstView () const
 Getter of a view object for constants instances. More...
 
__cuda_callable__ IndexType getGlobalIndex (Index segmentIdx, Index localIdx) const
 Computes the global index of an element managed by the segments. More...
 
KernelTypegetKernel ()
 
const KernelTypegetKernel () const
 
OffsetsContainergetOffsets ()
 Returns reference on vector with row offsets used in the CSR format. More...
 
const OffsetsContainergetOffsets () const
 Returns reference on constant vector with row offsets used in the CSR format. More...
 
__cuda_callable__ IndexType getSegmentsCount () const
 Getter of number of segments. More...
 
__cuda_callable__ IndexType getSegmentSize (IndexType segmentIdx) const
 Returns size of particular segment. More...
 
__cuda_callable__ SegmentViewType getSegmentView (IndexType segmentIdx) const
 Returns segment view (i.e. segment accessor) of segment with given index. More...
 
__cuda_callable__ IndexType getSize () const
 
__cuda_callable__ IndexType getStorageSize () const
 Returns number of elements that needs to be allocated by a container connected to this segments. More...
 
ViewType getView ()
 Getter of a view object. More...
 
void load (File &file)
 Method for loading the segments from a file in a binary form. More...
 
CSRoperator= (const CSR &source)=default
 Assignment operator. More...
 
template<typename Device_ , typename Index_ , typename Kernel_ , typename IndexAllocator_ >
CSRoperator= (const CSR< Device_, Index_, Kernel_, IndexAllocator_ > &source)
 Assignment operator with CSR segments with different template parameters. More...
 
template<typename Device_ , typename Index_ , typename Kernel_ , typename IndexAllocator_ >
CSR< Device, Index, Kernel, IndexAllocator > & operator= (const CSR< Device_, Index_, Kernel_, IndexAllocator_ > &source)
 
template<typename Fetch >
SegmentsPrinter< CSR, Fetch > print (Fetch &&fetch) const
 Return simple proxy object for insertion to output stream. More...
 
template<typename Fetch >
auto print (Fetch &&fetch) const -> SegmentsPrinter< CSR, Fetch >
 
template<typename Fetch , typename Reduce , typename Keep , typename Value >
void reduceAllSegments (Fetch &fetch, const Reduce &reduce, Keep &keep, const Value &zero) const
 Call TNL::Algorithms::Segments::CSR::reduceSegments for all segments. More...
 
template<typename Fetch , typename Reduce , typename Keep , typename Value >
void reduceSegments (IndexType begin, IndexType end, Fetch &fetch, const Reduce &reduce, Keep &keep, const Value &zero) const
 Compute reduction in each segment. More...
 
void reset ()
 Reset the segments to empty states. More...
 
void save (File &file) const
 Method for saving the segments to a file in a binary form. More...
 
template<typename Function >
void sequentialForAllSegments (Function &&f) const
 Call TNL::Algorithms::Segments::CSR::sequentialForSegments for all segments. More...
 
template<typename Function >
void sequentialForSegments (IndexType begin, IndexType end, Function &&function) const
 Call TNL::Algorithms::Segments::CSR::forSegments sequentially for particular segments. More...
 
template<typename SizesContainer >
void setSegmentsSizes (const SizesContainer &segmentsSizes)
 Set sizes of particular segments. More...
 
template<typename SizesHolder >
void setSegmentsSizes (const SizesHolder &sizes)
 

Static Public Member Functions

static constexpr ElementsOrganization getOrganization ()
 This functions says that CSR format is always organised in row-major order.
 
static String getSegmentsType ()
 Returns string with segments type. More...
 
static std::string getSerializationType ()
 Returns string with serialization type. More...
 
static constexpr bool havePadding ()
 This function says that CSR format does not use padding elements.
 

Protected Attributes

KernelType kernel
 
OffsetsContainer offsets
 

Detailed Description

template<typename Device, typename Index, typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
class TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >

Data structure for CSR segments format.

See TNL::Algorithms::Segments for more details about segments.

Template Parameters
Deviceis type of device where the segments will be operating.
Indexis type for indexing of the elements managed by the segments.
Kernelis type of kernel used for parallel operations with segments. It can be any of the following: CSRAdaptiveKernel, CSRHybridKernel, CSRScalarKernel, CSRVectorKernel.
IndexAllocatoris allocator for supporting index containers.

Member Typedef Documentation

◆ ViewTemplate

template<typename Device , typename Index , typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename Device_ , typename Index_ >
using TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::ViewTemplate = CSRView< Device_, Index_, KernelType >

Templated view type.

Template Parameters
Device_is alternative device type for the view.
Index_is alternative index type for the view.

Constructor & Destructor Documentation

◆ CSR() [1/4]

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename SizesContainer >
TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::CSR ( const SizesContainer &  segmentsSizes)

Construct with segments sizes.

The number of segments is given by the size of segmentsSizes. Particular elements of this container define sizes of particular segments.

Template Parameters
SizesContaineris a type of container for segments sizes. It can be TNL::Containers::Array or TNL::Containers::Vector for example.
Parameters
segmentsSizesis an instance of the container with the segments sizes.

See the following example:

1#include <iostream>
2#include <functional>
3#include <TNL/Containers/Vector.h>
4#include <TNL/Algorithms/Segments/CSR.h>
5#include <TNL/Devices/Host.h>
6#include <TNL/Devices/Cuda.h>
7
8template< typename Device >
9void SegmentsExample()
10{
11 using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
12
13 /***
14 * Create segments with given segments sizes.
15 */
16 TNL::Containers::Vector< int, Device > segmentsSizes{ 1, 2, 3, 4, 5 };
17 SegmentsType segments( segmentsSizes );
18 std::cout << "Segments sizes are: " << segments << std::endl;
19
20 /***
21 * Allocate array for the segments;
22 */
23 TNL::Containers::Array< double, Device > data( segments.getStorageSize(), 0.0 );
24
25 /***
26 * Insert data into particular segments.
27 */
28 auto data_view = data.getView();
29 segments.forAllElements( [=] __cuda_callable__ ( int segmentIdx, int localIdx, int globalIdx ) mutable {
30 if( localIdx <= segmentIdx )
31 data_view[ globalIdx ] = segmentIdx;
32 } );
33
34 /***
35 * Print the data managed by the segments.
36 */
37 auto fetch = [=] __cuda_callable__ ( int globalIdx ) -> double { return data_view[ globalIdx ]; };
38 printSegments( segments, fetch, std::cout );
39}
40
41int main( int argc, char* argv[] )
42{
43 std::cout << "Example of CSR segments on host: " << std::endl;
44 SegmentsExample< TNL::Devices::Host >();
45
46#ifdef __CUDACC__
47 std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
48 SegmentsExample< TNL::Devices::Cuda >();
49#endif
50 return EXIT_SUCCESS;
51}
#define __cuda_callable__
Definition: CudaCallable.h:22
Data structure for CSR segments format.
Definition: CSR.h:38
Array is responsible for memory management, access to array elements, and general array operations.
Definition: Array.h:67
Vector extends Array with algebraic operations.
Definition: Vector.h:40
T endl(T... args)
std::ostream & printSegments(const Segments &segments, std::ostream &str)
Print segments sizes, i.e. the segments setup.
Definition: SegmentsPrinting.h:31

The result looks as follows:

Example of CSR segments on host:
Segments sizes are: [ 1, 2, 3, 4, 5, ]
Seg. 0: [ 0 ]
Seg. 1: [ 1, 1 ]
Seg. 2: [ 2, 2, 2 ]
Seg. 3: [ 3, 3, 3, 3 ]
Seg. 4: [ 4, 4, 4, 4, 4 ]
Example of CSR segments on CUDA GPU:
Segments sizes are: [ 1, 2, 3, 4, 5, ]
Seg. 0: [ 0 ]
Seg. 1: [ 1, 1 ]
Seg. 2: [ 2, 2, 2 ]
Seg. 3: [ 3, 3, 3, 3 ]
Seg. 4: [ 4, 4, 4, 4, 4 ]

◆ CSR() [2/4]

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename ListIndex >
TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::CSR ( const std::initializer_list< ListIndex > &  segmentsSizes)

Construct with segments sizes in initializer list..

The number of segments is given by the size of segmentsSizes. Particular elements of this initializer list define sizes of particular segments.

Template Parameters
ListIndexis a type of indexes of the initializer list.
Parameters
segmentsSizesis an instance of the container with the segments sizes.

See the following example:

1#include <iostream>
2#include <functional>
3#include <TNL/Containers/Vector.h>
4#include <TNL/Algorithms/Segments/CSR.h>
5#include <TNL/Devices/Host.h>
6#include <TNL/Devices/Cuda.h>
7
8template< typename Device >
9void SegmentsExample()
10{
11 using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
12
13 /***
14 * Create segments with given segments sizes.
15 */
16 SegmentsType segments{ 1, 2, 3, 4, 5 };
17 std::cout << "Segments sizes are: " << segments << std::endl;
18
19 /***
20 * Allocate array for the segments;
21 */
22 TNL::Containers::Array< double, Device > data( segments.getStorageSize(), 0.0 );
23
24 /***
25 * Insert data into particular segments.
26 */
27 auto data_view = data.getView();
28 segments.forAllElements( [=] __cuda_callable__ ( int segmentIdx, int localIdx, int globalIdx ) mutable {
29 if( localIdx <= segmentIdx )
30 data_view[ globalIdx ] = segmentIdx;
31 } );
32
33 /***
34 * Print the data managed by the segments.
35 */
36 auto fetch = [=] __cuda_callable__ ( int globalIdx ) -> double { return data_view[ globalIdx ]; };
37 printSegments( segments, fetch, std::cout );
38}
39
40int main( int argc, char* argv[] )
41{
42 std::cout << "Example of CSR segments on host: " << std::endl;
43 SegmentsExample< TNL::Devices::Host >();
44
45#ifdef __CUDACC__
46 std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
47 SegmentsExample< TNL::Devices::Cuda >();
48#endif
49 return EXIT_SUCCESS;
50}

The result looks as follows:

Example of CSR segments on host:
Segments sizes are: [ 1, 2, 3, 4, 5, ]
Seg. 0: [ 0 ]
Seg. 1: [ 1, 1 ]
Seg. 2: [ 2, 2, 2 ]
Seg. 3: [ 3, 3, 3, 3 ]
Seg. 4: [ 4, 4, 4, 4, 4 ]
Example of CSR segments on CUDA GPU:
Segments sizes are: [ 1, 2, 3, 4, 5, ]
Seg. 0: [ 0 ]
Seg. 1: [ 1, 1 ]
Seg. 2: [ 2, 2, 2 ]
Seg. 3: [ 3, 3, 3, 3 ]
Seg. 4: [ 4, 4, 4, 4, 4 ]

◆ CSR() [3/4]

template<typename Device , typename Index , typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::CSR ( const CSR< Device, Index, Kernel, IndexAllocator > &  segments)
default

Copy constructor.

Parameters
segmentsare the source segments.

◆ CSR() [4/4]

template<typename Device , typename Index , typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::CSR ( CSR< Device, Index, Kernel, IndexAllocator > &&  segments)
defaultnoexcept

Move constructor.

Parameters
segmentsare the source segments.

Member Function Documentation

◆ forAllElements()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Function >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::forAllElements ( Function &&  function) const

Call TNL::Algorithms::Segments::CSR::forElements for all elements of the segments.

See TNL::Algorithms::Segments::CSR::forElements for more details.

◆ forAllSegments()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Function >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::forAllSegments ( Function &&  function) const

◆ forElements()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Function >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::forElements ( IndexType  begin,
IndexType  end,
Function &&  function 
) const

Iterate over all elements of given segments in parallel and call given lambda function.

Template Parameters
Functionis a type of the lambda function to be performed on each element.
Parameters
begindefines begining of an interval [ begin, end ) of segments on elements of which we want to apply the lambda function.
enddefines end of an interval [ begin, end ) of segments on elements of which we want to apply the lambda function.
functionis the lambda function to be applied on the elements of the segments.

Declaration of the lambda function function is supposed to be

auto f = [=] __cuda_callable__ ( IndexType segmentIdx, IndexType localIdx, IndexType globalIdx ) {...}
std::remove_const_t< Index > IndexType
The type used for indexing of segments elements.
Definition: CSR.h:48

where segmentIdx is index of segment where given element belong to, localIdx is rank of the element within the segment and globalIdx is index of the element within the related container.

Example
#include <iostream>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void SegmentsExample()
{
using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
/***
* Create segments with given segments sizes.
*/
const int size( 5 );
SegmentsType segments{ 1, 2, 3, 4, 5 };
/***
* Allocate array for the segments;
*/
TNL::Containers::Array< double, Device > data( segments.getStorageSize(), 0.0 );
/***
* Insert data into particular segments.
*/
auto data_view = data.getView();
segments.forElements( 0, size, [=] __cuda_callable__ ( int segmentIdx, int localIdx, int globalIdx ) mutable {
if( localIdx <= segmentIdx )
data_view[ globalIdx ] = segmentIdx;
} );
/***
* Print the data managed by the segments.
*/
auto fetch = [=] __cuda_callable__ ( int globalIdx ) -> double { return data_view[ globalIdx ]; };
printSegments( segments, fetch, std::cout );
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
SegmentsExample< TNL::Devices::Cuda >();
#endif
return EXIT_SUCCESS;
}
Output
Example of CSR segments on host:
Seg. 0: [ 0 ]
Seg. 1: [ 1, 1 ]
Seg. 2: [ 2, 2, 2 ]
Seg. 3: [ 3, 3, 3, 3 ]
Seg. 4: [ 4, 4, 4, 4, 4 ]
Example of CSR segments on CUDA GPU:
Seg. 0: [ 0 ]
Seg. 1: [ 1, 1 ]
Seg. 2: [ 2, 2, 2 ]
Seg. 3: [ 3, 3, 3, 3 ]
Seg. 4: [ 4, 4, 4, 4, 4 ]

◆ forSegments()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Function >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::forSegments ( IndexType  begin,
IndexType  end,
Function &&  function 
) const

Iterate over all segments in parallel and call given lambda function.

Template Parameters
Functionis a type of the lambda function to be performed on each segment.
Parameters
begindefines begining of an interval [ begin, end ) of segments on elements of which we want to apply the lambda function.
enddefines end of an interval [ begin, end ) of segments on elements of which we want to apply the lambda function.
functionis the lambda function to be applied on the elements of the segments.

Declaration of the lambda function function is supposed to be

auto f = [=] __cuda_callable__ ( const SegmentView& segment ) {...}
Data structure for accessing particular segment.
Definition: SegmentView.h:26

where segment represents given segment (see TNL::Algorithms::Segments::SegmentView). Its type is given by SegmentViewType.

Example
#include <iostream>
#include <functional>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void SegmentsExample()
{
using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
using SegmentViewType = typename SegmentsType::SegmentViewType;
/***
* Create segments with given segments sizes.
*/
const int size( 5 );
SegmentsType segments{ 1, 2, 3, 4, 5 };
/***
* Allocate array for the segments;
*/
TNL::Containers::Array< double, Device > data( segments.getStorageSize(), 0.0 );
/***
* Insert data into particular segments.
*/
auto data_view = data.getView();
segments.forSegments( 0, size, [=] __cuda_callable__ ( const SegmentViewType& segment ) mutable {
for( auto element : segment )
if( element.localIndex() <= element.segmentIndex() )
data_view[ element.globalIndex() ] = element.segmentIndex() + element.localIndex();
} );
/***
* Print the data managed by the segments.
*/
auto fetch = [=] __cuda_callable__ ( int globalIdx ) -> double { return data_view[ globalIdx ]; };
printSegments( segments, fetch, std::cout );
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
SegmentsExample< TNL::Devices::Cuda >();
#endif
return EXIT_SUCCESS;
}
SegmentView< IndexType, RowMajorOrder > SegmentViewType
Accessor type fro one particular segment.
Definition: CSR.h:82
Output
Example of CSR segments on host:
Seg. 0: [ 0 ]
Seg. 1: [ 1, 2 ]
Seg. 2: [ 2, 3, 4 ]
Seg. 3: [ 3, 4, 5, 6 ]
Seg. 4: [ 4, 5, 6, 7, 8 ]
Example of CSR segments on CUDA GPU:
Seg. 0: [ 0 ]
Seg. 1: [ 1, 2 ]
Seg. 2: [ 2, 3, 4 ]
Seg. 3: [ 3, 4, 5, 6 ]
Seg. 4: [ 4, 5, 6, 7, 8 ]

◆ getConstView()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getConstView

Getter of a view object for constants instances.

Returns
View for this instance of CSR segments which can by used for example in lambda functions running in GPU kernels.

◆ getGlobalIndex()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
__cuda_callable__ auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getGlobalIndex ( Index  segmentIdx,
Index  localIdx 
) const

Computes the global index of an element managed by the segments.

The global index serves as a refernce on the element in its container.

Parameters
segmentIdxis index of a segment with the element.
localIdxis tha local index of the element within the segment.
Returns
global index of the element.

◆ getOffsets() [1/2]

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getOffsets

Returns reference on vector with row offsets used in the CSR format.

Returns
reference on vector with row offsets used in the CSR format.

◆ getOffsets() [2/2]

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getOffsets

Returns reference on constant vector with row offsets used in the CSR format.

Returns
reference on constant vector with row offsets used in the CSR format.

◆ getSegmentsCount()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
__cuda_callable__ auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getSegmentsCount

Getter of number of segments.

Returns
number of segments within this object.

◆ getSegmentSize()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
__cuda_callable__ auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getSegmentSize ( IndexType  segmentIdx) const

Returns size of particular segment.

Returns
size of the segment number segmentIdx.

◆ getSegmentsType()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
String TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getSegmentsType
static

Returns string with segments type.

The string has a form CSR< KernelType >.

Returns
String with the segments type.
Example
#include <iostream>
#include <functional>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void SegmentsExample()
{
using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
/***
* Create segments and print the segments type.
*/
SegmentsType segments;
std::cout << "The segments type is: " << segments.getSegmentsType() << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
SegmentsExample< TNL::Devices::Cuda >();
#endif
return EXIT_SUCCESS;
}
Output
Example of CSR segments on host:
The segments type is: CSR< Scalar >
Example of CSR segments on CUDA GPU:
The segments type is: CSR< Scalar >

◆ getSegmentView()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
__cuda_callable__ auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getSegmentView ( IndexType  segmentIdx) const

Returns segment view (i.e. segment accessor) of segment with given index.

Parameters
segmentIdxis index of the request segment.
Returns
segment view of given segment.
Example
#include <iostream>
#include <functional>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Algorithms/SequentialFor.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void SegmentsExample()
{
using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
/***
* Create segments with given segments sizes.
*/
const int size( 5 );
SegmentsType segments{ 1, 2, 3, 4, 5 };
auto view = segments.getView();
/***
* Print the elemets mapping using segment view.
*/
std::cout << "Mapping of local indexes to global indexes:" << std::endl;
auto f = [=] __cuda_callable__ ( int segmentIdx ) {
printf( "Segment idx. %d: ", segmentIdx ); // printf works even in GPU kernels
auto segment = view.getSegmentView( segmentIdx );
for( auto element : segment )
printf( "%d -> %d \t", element.localIndex(), element.globalIndex() );
printf( "\n" );
};
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
SegmentsExample< TNL::Devices::Cuda >();
#endif
return EXIT_SUCCESS;
}
static void exec(Index start, Index end, Function f)
Static method for execution of the loop.
Definition: SequentialFor.h:40
Output
Example of CSR segments on host:
Mapping of local indexes to global indexes:
Segment idx. 0: 0 -> 0
Segment idx. 1: 0 -> 1 1 -> 2
Segment idx. 2: 0 -> 3 1 -> 4 2 -> 5
Segment idx. 3: 0 -> 6 1 -> 7 2 -> 8 3 -> 9
Segment idx. 4: 0 -> 10 1 -> 11 2 -> 12 3 -> 13 4 -> 14
Example of CSR segments on CUDA GPU:
Mapping of local indexes to global indexes:
Segment idx. 0: 0 -> 0
Segment idx. 1: 0 -> 1 1 -> 2
Segment idx. 2: 0 -> 3 1 -> 4 2 -> 5
Segment idx. 3: 0 -> 6 1 -> 7 2 -> 8 3 -> 9
Segment idx. 4: 0 -> 10 1 -> 11 2 -> 12 3 -> 13 4 -> 14

◆ getSerializationType()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
std::string TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getSerializationType
static

Returns string with serialization type.

The string has a form Algorithms::Segments::CSR< IndexType, [any_device], [any_kernel], [any_allocator] >.

Returns
String with the serialization type.
Example
#include <iostream>
#include <functional>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void SegmentsExample()
{
using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
/***
* Create segments and print the serialization type.
*/
SegmentsType segments;
std::cout << "The serialization type is: " << segments.getSerializationType() << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
SegmentsExample< TNL::Devices::Cuda >();
#endif
return EXIT_SUCCESS;
}
Output
Example of CSR segments on host:
The serialization type is: CSR< [any_device], int, TNL::Algorithms::Segments::CSRScalarKernel<int, TNL::Devices::Host> >
Example of CSR segments on CUDA GPU:
The serialization type is: CSR< [any_device], int, TNL::Algorithms::Segments::CSRScalarKernel<int, TNL::Devices::Cuda> >

◆ getStorageSize()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
__cuda_callable__ auto TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getStorageSize

Returns number of elements that needs to be allocated by a container connected to this segments.

Returns
size of container connected to this segments.

◆ getView()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
CSR< Device, Index, Kernel, IndexAllocator >::ViewType TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::getView

Getter of a view object.

Returns
View for this instance of CSR segments which can by used for example in lambda functions running in GPU kernels.

◆ load()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::load ( File file)

Method for loading the segments from a file in a binary form.

Parameters
fileis the source file.

◆ operator=() [1/2]

template<typename Device , typename Index , typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
CSR & TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::operator= ( const CSR< Device, Index, Kernel, IndexAllocator > &  source)
default

Assignment operator.

It makes a deep copy of the source segments.

Parameters
sourceare the CSR segments to be assigned.
Returns
reference to this instance.

◆ operator=() [2/2]

template<typename Device , typename Index , typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename Device_ , typename Index_ , typename Kernel_ , typename IndexAllocator_ >
CSR & TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::operator= ( const CSR< Device_, Index_, Kernel_, IndexAllocator_ > &  source)

Assignment operator with CSR segments with different template parameters.

It makes a deep copy of the source segments.

Template Parameters
Device_is device type of the source segments.
Index_is the index type of the source segments.
Kernel_is the kernel type of the source segments.
IndexAllocator_is the index allocator of the source segments.
Parameters
sourceis the source segments object.
Returns
reference to this instance.

◆ print()

template<typename Device , typename Index , typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename Fetch >
SegmentsPrinter< CSR, Fetch > TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::print ( Fetch &&  fetch) const

Return simple proxy object for insertion to output stream.

The proxy object serves for wrapping segments with lambda function mediating access to data managed by the segments.

Template Parameters
Fetchis type of lambda function for data access.
Parameters
fetchis an instance of lambda function for data access. It is supposed to be defined as
auto fetch = [=] __cuda_callable__ ( IndexType globalIdx ) -> ValueType { return data_view[ globalIdx ]; };
Returns
Proxy object for insertion to output stream.
Example
#include <iostream>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Algorithms/Segments/Ellpack.h>
#include <TNL/Algorithms/Segments/ChunkedEllpack.h>
#include <TNL/Algorithms/Segments/BiEllpack.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Segments >
void SegmentsExample()
{
using Device = typename Segments::DeviceType;
/***
* Create segments with given segments sizes.
*/
Segments segments( sizes );
std::cout << "Segments sizes are: " << segments << std::endl;
/***
* Allocate array for the segments;
*/
TNL::Containers::Array< double, Device > data( segments.getStorageSize(), 0.0 );
data.forAllElements( [=] __cuda_callable__ ( int idx, double& value ) {
value = idx;
} );
/***
* Print the data managed by the segments.
*/
auto data_view = data.getView();
auto fetch = [=] __cuda_callable__ ( int globalIdx ) -> double { return data_view[ globalIdx ]; };
printSegments( segments, fetch, std::cout ) << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Host, int > >();
std::cout << "Example of Ellpack segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Host, int > >();
std::cout << "Example of ChunkedEllpack segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::ChunkedEllpack< TNL::Devices::Host, int > >();
std::cout << "Example of BiEllpack segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::BiEllpack< TNL::Devices::Host, int > >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Cuda, int > >();
std::cout << "Example of Ellpack segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Cuda, int > >();
std::cout << "Example of ChunkedEllpack segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::ChunkedEllpack< TNL::Devices::Cuda, int > >();
std::cout << "Example of BiEllpack segments on host: " << std::endl;
SegmentsExample< TNL::Algorithms::Segments::BiEllpack< TNL::Devices::Cuda, int > >();
#endif
return EXIT_SUCCESS;
}
Output
Example of CSR segments on host:
Segments sizes are: [ 1, 2, 3, 4, 5, ]
Seg. 0: [ 0 ]
Seg. 1: [ 1, 2 ]
Seg. 2: [ 3, 4, 5 ]
Seg. 3: [ 6, 7, 8, 9 ]
Seg. 4: [ 10, 11, 12, 13, 14 ]
Example of Ellpack segments on host:
Segments sizes are: [ 5, 5, 5, 5, 5, ]
Seg. 0: [ 0, 1, 2, 3, 4 ]
Seg. 1: [ 5, 6, 7, 8, 9 ]
Seg. 2: [ 10, 11, 12, 13, 14 ]
Seg. 3: [ 15, 16, 17, 18, 19 ]
Seg. 4: [ 20, 21, 22, 23, 24 ]
Example of ChunkedEllpack segments on host:
Segments sizes are: [ 17, 34, 51, 67, 87, ]
Seg. 0: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
Seg. 1: [ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 ]
Seg. 2: [ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101 ]
Seg. 3: [ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168 ]
Seg. 4: [ 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 ]
Example of BiEllpack segments on host:
Segments sizes are: [ 2, 4, 4, 5, 5, ]
Seg. 0: [ 8, 9 ]
Seg. 1: [ 6, 7, 22, 23 ]
Seg. 2: [ 4, 5, 20, 21 ]
Seg. 3: [ 2, 3, 18, 19, 25 ]
Seg. 4: [ 0, 1, 16, 17, 24 ]
Example of CSR segments on host:
Segments sizes are: [ 1, 2, 3, 4, 5, ]
Seg. 0: [ 0 ]
Seg. 1: [ 1, 2 ]
Seg. 2: [ 3, 4, 5 ]
Seg. 3: [ 6, 7, 8, 9 ]
Seg. 4: [ 10, 11, 12, 13, 14 ]
Example of Ellpack segments on host:
Segments sizes are: [ 5, 5, 5, 5, 5, ]
Seg. 0: [ 0, 32, 64, 96, 128 ]
Seg. 1: [ 1, 33, 65, 97, 129 ]
Seg. 2: [ 2, 34, 66, 98, 130 ]
Seg. 3: [ 3, 35, 67, 99, 131 ]
Seg. 4: [ 4, 36, 68, 100, 132 ]
Example of ChunkedEllpack segments on host:
Segments sizes are: [ 17, 34, 51, 67, 87, ]
Seg. 0: [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16 ]
Seg. 1: [ 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 ]
Seg. 2: [ 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101 ]
Seg. 3: [ 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168 ]
Seg. 4: [ 169, 170, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 191, 192, 193, 194, 195, 196, 197, 198, 199, 200, 201, 202, 203, 204, 205, 206, 207, 208, 209, 210, 211, 212, 213, 214, 215, 216, 217, 218, 219, 220, 221, 222, 223, 224, 225, 226, 227, 228, 229, 230, 231, 232, 233, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 249, 250, 251, 252, 253, 254, 255 ]
Example of BiEllpack segments on host:
Segments sizes are: [ 2, 4, 4, 5, 5, ]
Seg. 0: [ 4, 12 ]
Seg. 1: [ 3, 11, 19, 23 ]
Seg. 2: [ 2, 10, 18, 22 ]
Seg. 3: [ 1, 9, 17, 21, 25 ]
Seg. 4: [ 0, 8, 16, 20, 24 ]

◆ reduceAllSegments()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Fetch , typename Reduce , typename Keep , typename Value >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::reduceAllSegments ( Fetch &  fetch,
const Reduce &  reduce,
Keep &  keep,
const Value &  zero 
) const

◆ reduceSegments()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Fetch , typename Reduce , typename Keep , typename Value >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::reduceSegments ( IndexType  begin,
IndexType  end,
Fetch &  fetch,
const Reduce &  reduce,
Keep &  keep,
const Value &  zero 
) const

Compute reduction in each segment.

Template Parameters
Fetchis type of lambda function for data fetching.
Reduceis a reduction operation.
Keepis lambda function for storing results from particular segments.
Parameters
begindefines begining of an interval [ begin, end ) of segments in which we want to perform the reduction.
enddefines and of an interval [ begin, end ) of segments in which we want to perform the reduction.
fetchis a lambda function for fetching of data. It is suppos have one of the following forms:
  1. Full form
    auto fetch = [=] __cuda_callable__ ( IndexType segmentIdx, IndexType localIdx, IndexType globalIdx, bool& compute ) { ...
    }
  2. Brief form
    auto fetch = [=] __cuda_callable__ ( IndexType globalIdx, bool& compute ) { ... }
    where for both variants segmentIdx is segment index, localIdx is a rank of element in the segment, globalIdx is index of the element in related container and compute is a boolean variable which serves for stopping the reduction if it is set to false. It is however, only a hint and the real behaviour depends on type of kernel used ofr the redcution. Some kernels are optimized so that they can be significantly faster with the brief variant of the fetch lambda function.
reduceis a lambda function representing the reduction opeartion. It is supposed to be defined as:
auto reduce = [=] __cuda_callable__ ( const Value& a, const Value& b ) -> Value { ... }
Result reduce(Index begin, Index end, Fetch &&fetch, Reduction &&reduction, const Result &identity)
reduce implements (parallel) reduction for vectors and arrays.
Definition: reduce.h:71

where a and b are values to be reduced and the lambda function returns result of the reduction.

Parameters
keepis a lambda function for saving results from particular segments. It is supposed to be defined as:
auto keep = [=] __cuda_callable__ ( IndexType segmentIdx, const Value& value ) { ... }

where segmentIdx is an index of the segment and value is the result of the reduction in given segment to be stored.

Parameters
zerois the initial value for the reduction operation.
Example
#include <iostream>
#include <functional>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void SegmentsExample()
{
using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
/***
* Create segments with given segments sizes.
*/
const int size( 5 );
SegmentsType segments{ 1, 2, 3, 4, 5 };
/***
* Allocate array for the segments;
*/
TNL::Containers::Array< double, Device > data( segments.getStorageSize(), 0.0 );
/***
* Insert data into particular segments.
*/
auto data_view = data.getView();
segments.forElements( 0, size, [=] __cuda_callable__ ( int segmentIdx, int localIdx, int globalIdx ) mutable {
if( localIdx <= segmentIdx )
data_view[ globalIdx ] = segmentIdx;
} );
/***
* Compute sums of elements in each segment.
*/
auto sums_view = sums.getView();
auto fetch_full = [=] __cuda_callable__ ( int segmentIdx, int localIdx, int globalIdx, bool& compute ) -> double {
if( localIdx <= segmentIdx )
return data_view[ globalIdx ];
else
{
compute = false;
return 0.0;
}
};
auto fetch_brief = [=] __cuda_callable__ ( int globalIdx, bool& compute ) -> double {
return data_view[ globalIdx ];
};
auto keep = [=] __cuda_callable__ ( int globalIdx, const double& value ) mutable {
sums_view[ globalIdx ] = value; };
segments.reduceAllSegments( fetch_full, std::plus<>{}, keep, 0.0 );
std::cout << "The sums with full fetch form are: " << sums << std::endl;
segments.reduceAllSegments( fetch_brief, std::plus<>{}, keep, 0.0 );
std::cout << "The sums with brief fetch form are: " << sums << std::endl;
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
SegmentsExample< TNL::Devices::Cuda >();
#endif
return EXIT_SUCCESS;
}
Output
Example of CSR segments on host:
The sums with full fetch form are: [ 0, 2, 6, 12, 20 ]
The sums with brief fetch form are: [ 0, 2, 6, 12, 20 ]
Example of CSR segments on CUDA GPU:
The sums with full fetch form are: [ 0, 2, 6, 12, 20 ]
The sums with brief fetch form are: [ 0, 2, 6, 12, 20 ]

◆ reset()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::reset

Reset the segments to empty states.

It means that there is no segment in the CSR segments.

◆ save()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::save ( File file) const

Method for saving the segments to a file in a binary form.

Parameters
fileis the target file.

◆ sequentialForAllSegments()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Function >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::sequentialForAllSegments ( Function &&  f) const

◆ sequentialForSegments()

template<typename Device , typename Index , typename Kernel , typename IndexAllocator >
template<typename Function >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::sequentialForSegments ( IndexType  begin,
IndexType  end,
Function &&  function 
) const

Call TNL::Algorithms::Segments::CSR::forSegments sequentially for particular segments.

With this method, the given segments are processed sequentially one-by-one. This is usefull for example for printing of segments based data structures or for debugging reasons.

Parameters
begindefines begining of an interval [ begin, end ) of segments on elements of which we want to apply the lambda function.
enddefines end of an interval [ begin, end ) of segments on elements of which we want to apply the lambda function.
functionis the lambda function to be applied on the elements of the segments.

See TNL::Algorithms::Segments::CSR::forSegments for more details.

Example
#include <iostream>
#include <functional>
#include <TNL/Containers/Vector.h>
#include <TNL/Algorithms/Segments/CSR.h>
#include <TNL/Algorithms/SequentialFor.h>
#include <TNL/Devices/Host.h>
#include <TNL/Devices/Cuda.h>
template< typename Device >
void SegmentsExample()
{
using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
using SegmentView = typename SegmentsType::SegmentViewType;
/***
* Create segments with given segments sizes.
*/
const int size( 5 );
SegmentsType segments{ 1, 2, 3, 4, 5 };
/***
* Print the elemets mapping using segment view.
*/
std::cout << "Mapping of local indexes to global indexes:" << std::endl;
auto f = [=] __cuda_callable__ ( const SegmentView& segment ) {
printf( "Segment idx. %d: ", segment.getSegmentIndex() ); // printf works even in GPU kernels
for( auto element : segment )
printf( "%d -> %d \t", element.localIndex(), element.globalIndex() );
printf( "\n" );
};
segments.sequentialForSegments( 0, size, f );
}
int main( int argc, char* argv[] )
{
std::cout << "Example of CSR segments on host: " << std::endl;
SegmentsExample< TNL::Devices::Host >();
#ifdef __CUDACC__
std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
SegmentsExample< TNL::Devices::Cuda >();
#endif
return EXIT_SUCCESS;
}
Output
Example of CSR segments on host:
Mapping of local indexes to global indexes:
Segment idx. 0: 0 -> 0
Segment idx. 1: 0 -> 1 1 -> 2
Segment idx. 2: 0 -> 3 1 -> 4 2 -> 5
Segment idx. 3: 0 -> 6 1 -> 7 2 -> 8 3 -> 9
Segment idx. 4: 0 -> 10 1 -> 11 2 -> 12 3 -> 13 4 -> 14
Example of CSR segments on CUDA GPU:
Mapping of local indexes to global indexes:
Segment idx. 0: 0 -> 0
Segment idx. 1: 0 -> 1 1 -> 2
Segment idx. 2: 0 -> 3 1 -> 4 2 -> 5
Segment idx. 3: 0 -> 6 1 -> 7 2 -> 8 3 -> 9
Segment idx. 4: 0 -> 10 1 -> 11 2 -> 12 3 -> 13 4 -> 14

◆ setSegmentsSizes()

template<typename Device , typename Index , typename Kernel = CSRScalarKernel< Index, Device >, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename SizesContainer >
void TNL::Algorithms::Segments::CSR< Device, Index, Kernel, IndexAllocator >::setSegmentsSizes ( const SizesContainer &  segmentsSizes)

Set sizes of particular segments.

Template Parameters
SizesContaineris a container with segments sizes. It can be TNL::Containers::Array or TNL::Containers::Vector for example.
Parameters
segmentsSizesis an instance of the container with segments sizes.

The documentation for this class was generated from the following files: