Template Numerical Library version\ main:df396df
Loading...
Searching...
No Matches
TNL::Algorithms::Segments::CSR< Device, Index, IndexAllocator > Class Template Reference

Data structure for CSR segments format. More...

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

Inheritance diagram for TNL::Algorithms::Segments::CSR< Device, Index, IndexAllocator >:
Collaboration diagram for TNL::Algorithms::Segments::CSR< Device, Index, IndexAllocator >:

Public Types

using ConstViewType = CSRView< Device, std::add_const_t< Index > >
 Type of constant segments view.
 
using OffsetsContainer = Containers::Vector< Index, Device, typename Base::IndexType, IndexAllocator >
 Type of container storing offsets of particular rows.
 
template<typename Device_ , typename Index_ >
using ViewTemplate = CSRView< Device_, Index_ >
 Templated view type.
 
using ViewType = CSRView< Device, Index >
 Type of segments view.
 
- Public Types inherited from TNL::Algorithms::Segments::CSRBase< Device, Index >
using ConstOffsetsView = typename OffsetsView::ConstViewType
 The type for representing the constant vector view with row offsets used in the CSR format.
 
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 OffsetsView = Containers::VectorView< Index, DeviceType, IndexType >
 The type for representing the vector view with row offsets used in the CSR format.
 
using SegmentViewType = SegmentView< IndexType, RowMajorOrder >
 Accessor type fro one particular segment.
 

Public Member Functions

 CSR ()=default
 Constructor with no parameters to create empty segments.
 
 CSR (const CSR &segments)
 Copy constructor (makes deep copy).
 
template<typename SizesContainer >
 CSR (const SizesContainer &segmentsSizes)
 Construct with segments sizes.
 
template<typename ListIndex >
 CSR (const std::initializer_list< ListIndex > &segmentsSizes)
 Construct with segments sizes in initializer list..
 
 CSR (CSR &&) noexcept=default
 Move constructor.
 
ConstViewType getConstView () const
 Returns a constant view for this instance of CSR segments which can by used for example in lambda functions running in GPU kernels.
 
ViewType getView ()
 Returns a view for this instance of CSR segments which can by used for example in lambda functions running in GPU kernels.
 
void load (File &file)
 Method for loading the segments from a file in a binary form.
 
CSRoperator= (const CSR &segments)
 Copy-assignment operator (makes a deep copy).
 
template<typename Device_ , typename Index_ , typename IndexAllocator_ >
CSRoperator= (const CSR< Device_, Index_, IndexAllocator_ > &segments)
 Assignment operator with CSR segments with different template parameters.
 
template<typename Device_ , typename Index_ , typename IndexAllocator_ >
CSR< Device, Index, IndexAllocator > & operator= (const CSR< Device_, Index_, IndexAllocator_ > &segments)
 
CSRoperator= (CSR &&) noexcept(false)
 Move-assignment operator.
 
void reset ()
 Reset the segments to empty states.
 
void save (File &file) const
 Method for saving the segments to a file in a binary form.
 
template<typename SizesContainer >
void setSegmentsSizes (const SizesContainer &segmentsSizes)
 Set sizes of particular segments.
 
template<typename SizesHolder >
void setSegmentsSizes (const SizesHolder &sizes)
 
- Public Member Functions inherited from TNL::Algorithms::Segments::CSRBase< Device, Index >
__cuda_callable__ CSRBase ()=default
 Default constructor with no parameters to create empty segments view.
 
__cuda_callable__ CSRBase (const CSRBase &)=default
 Copy constructor.
 
__cuda_callable__ CSRBase (const OffsetsView &offsets)
 Binds a new CSR view to an offsets vector.
 
__cuda_callable__ CSRBase (CSRBase &&) noexcept=default
 Move constructor.
 
__cuda_callable__ CSRBase (OffsetsView &&offsets)
 Binds a new CSR view to an offsets vector.
 
template<typename Function >
void forAllElements (Function &&function) const
 Call TNL::Algorithms::Segments::CSR::forElements for all elements of the segments.
 
template<typename Function >
void forAllSegments (Function &&function) const
 Call TNL::Algorithms::Segments::CSR::forSegments for all segments.
 
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.
 
template<typename Function >
void forSegments (IndexType begin, IndexType end, Function &&function) const
 Iterate over all segments in parallel and call given lambda function.
 
__cuda_callable__ IndexType getGlobalIndex (Index segmentIdx, Index localIdx) const
 Computes the global index of an element managed by the segments.
 
__cuda_callable__ OffsetsView getOffsets ()
 Returns a modifiable vector view with row offsets used in the CSR format.
 
__cuda_callable__ ConstOffsetsView getOffsets () const
 Returns a constant vector view with row offsets used in the CSR format.
 
__cuda_callable__ IndexType getSegmentsCount () const
 Returns the number of segments.
 
__cuda_callable__ IndexType getSegmentSize (IndexType segmentIdx) const
 Returns the size of a particular segment denoted by segmentIdx.
 
__cuda_callable__ SegmentViewType getSegmentView (IndexType segmentIdx) const
 Returns segment view (i.e. segment accessor) of segment with given index.
 
__cuda_callable__ IndexType getSize () const
 Returns the number of elements managed by all segments.
 
__cuda_callable__ IndexType getStorageSize () const
 Returns number of elements that needs to be allocated by a container connected to this segments.
 
CSRBaseoperator= (const CSRBase &)=delete
 Copy-assignment operator.
 
CSRBaseoperator= (CSRBase &&)=delete
 Move-assignment operator.
 
template<typename Function >
void sequentialForAllSegments (Function &&function) const
 Call TNL::Algorithms::Segments::CSR::sequentialForSegments for all segments.
 
template<typename Function >
void sequentialForSegments (IndexType begin, IndexType end, Function &&function) const
 Call TNL::Algorithms::Segments::CSR::forSegments sequentially for particular segments.
 

Protected Attributes

OffsetsContainer offsets
 
- Protected Attributes inherited from TNL::Algorithms::Segments::CSRBase< Device, Index >
OffsetsView offsets
 Vector view with row offsets used in the CSR format.
 

Additional Inherited Members

- Static Public Member Functions inherited from TNL::Algorithms::Segments::CSRBase< Device, Index >
static constexpr ElementsOrganization getOrganization ()
 Returns the data layout for the CSR format (it is always row-major order).
 
static std::string getSegmentsType ()
 Returns string with the segments type.
 
static std::string getSerializationType ()
 Returns string with the serialization type.
 
static constexpr bool havePadding ()
 This function denotes that the CSR format does not use padding elements.
 
- Protected Member Functions inherited from TNL::Algorithms::Segments::CSRBase< Device, Index >
__cuda_callable__ void bind (OffsetsView offsets)
 Re-initializes the internal attributes of the base class.
 

Detailed Description

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
class TNL::Algorithms::Segments::CSR< Device, Index, 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.
IndexAllocatoris allocator for supporting index containers.

Member Typedef Documentation

◆ ViewTemplate

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

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/2]

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

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/2]

template<typename Device , typename Index , typename IndexAllocator >
template<typename ListIndex >
TNL::Algorithms::Segments::CSR< Device, Index, 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
10SegmentsExample()
11{
12 using SegmentsType = typename TNL::Algorithms::Segments::CSR< Device, int >;
13
14 /***
15 * Create segments with given segments sizes.
16 */
17 SegmentsType segments{ 1, 2, 3, 4, 5 };
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(
30 [ = ] __cuda_callable__( int segmentIdx, int localIdx, int globalIdx ) mutable
31 {
32 if( localIdx <= segmentIdx )
33 data_view[ globalIdx ] = segmentIdx;
34 } );
35
36 /***
37 * Print the data managed by the segments.
38 */
39 auto fetch = [ = ] __cuda_callable__( int globalIdx ) -> double
40 {
41 return data_view[ globalIdx ];
42 };
43 printSegments( std::cout, segments, fetch );
44}
45
46int
47main( int argc, char* argv[] )
48{
49 std::cout << "Example of CSR segments on host: " << std::endl;
50 SegmentsExample< TNL::Devices::Host >();
51
52#ifdef __CUDACC__
53 std::cout << "Example of CSR segments on CUDA GPU: " << std::endl;
54 SegmentsExample< TNL::Devices::Cuda >();
55#endif
56 return EXIT_SUCCESS;
57}

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 ]

Member Function Documentation

◆ load()

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

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

Parameters
fileis the source file.

◆ operator=()

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

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.
IndexAllocator_is the index allocator of the source segments.
Parameters
segmentsis the source segments object.
Returns
reference to this instance.

◆ reset()

template<typename Device , typename Index , typename IndexAllocator >
void TNL::Algorithms::Segments::CSR< Device, Index, 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 IndexAllocator >
void TNL::Algorithms::Segments::CSR< Device, Index, IndexAllocator >::save ( File & file) const

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

Parameters
fileis the target file.

◆ setSegmentsSizes()

template<typename Device , typename Index , typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >>
template<typename SizesContainer >
void TNL::Algorithms::Segments::CSR< Device, Index, 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: