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

CSRBase serves as a base class for CSR and CSRView. More...

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

Inheritance diagram for TNL::Algorithms::Segments::CSRBase< Device, Index >:
Inheritance graph
[legend]
Collaboration diagram for TNL::Algorithms::Segments::CSRBase< Device, Index >:
Collaboration graph
[legend]

Public Types

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

__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.
 

Static Public Member Functions

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

__cuda_callable__ void bind (OffsetsView offsets)
 Re-initializes the internal attributes of the base class.
 

Protected Attributes

OffsetsView offsets
 Vector view with row offsets used in the CSR format.
 

Detailed Description

template<typename Device, typename Index>
class TNL::Algorithms::Segments::CSRBase< Device, Index >

CSRBase serves as a base class for CSR and CSRView.

Template Parameters
Deviceis type of device where the segments will be operating.
Indexis type for indexing of the elements managed by the segments.

Member Function Documentation

◆ bind()

template<typename Device , typename Index >
__cuda_callable__ void TNL::Algorithms::Segments::CSRBase< Device, Index >::bind ( OffsetsView offsets)
protected

Re-initializes the internal attributes of the base class.

Note that this function is protected to ensure that the user cannot modify the base class of segments. For the same reason, in future code development we also need to make sure that all non-const functions in the base class return by value and not by reference.

◆ forAllElements()

template<typename Device , typename Index >
template<typename Function >
void TNL::Algorithms::Segments::CSRBase< Device, Index >::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 >
template<typename Function >
void TNL::Algorithms::Segments::CSRBase< Device, Index >::forAllSegments ( Function && function) const

◆ forElements()

template<typename Device , typename Index >
template<typename Function >
void TNL::Algorithms::Segments::CSRBase< Device, Index >::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 ) {...}
#define __cuda_callable__
Definition Macros.h:49
std::remove_const_t< Index > IndexType
The type used for indexing of segments elements.
Definition CSRBase.h:29

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( std::cout, segments, fetch );
}
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;
}
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
T endl(T... args)
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 >
template<typename Function >
void TNL::Algorithms::Segments::CSRBase< Device, Index >::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:21

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( std::cout, segments, fetch );
}
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, 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 ]

◆ getGlobalIndex()

template<typename Device , typename Index >
__cuda_callable__ auto TNL::Algorithms::Segments::CSRBase< Device, Index >::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.

◆ getSegmentsType()

template<typename Device , typename Index >
std::string TNL::Algorithms::Segments::CSRBase< Device, Index >::getSegmentsType ( )
static

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
Example of CSR segments on CUDA GPU:
The segments type is: CSR

◆ getSegmentView()

template<typename Device , typename Index >
__cuda_callable__ auto TNL::Algorithms::Segments::CSRBase< Device, Index >::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:37
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 >
std::string TNL::Algorithms::Segments::CSRBase< Device, Index >::getSerializationType ( )
static

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< int >
Example of CSR segments on CUDA GPU:
The serialization type is: CSR< int >

◆ sequentialForAllSegments()

template<typename Device , typename Index >
template<typename Function >
void TNL::Algorithms::Segments::CSRBase< Device, Index >::sequentialForAllSegments ( Function && function) const

◆ sequentialForSegments()

template<typename Device , typename Index >
template<typename Function >
void TNL::Algorithms::Segments::CSRBase< Device, Index >::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

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