Template Numerical Library version\ main:4904c12
Loading...
Searching...
No Matches
TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization > Class Template Reference

Data structure for Chunked Ellpack segments. More...

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

Inheritance diagram for TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >:
[legend]
Collaboration diagram for TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >:
[legend]

Public Types

using ConstViewType = typename ViewType::ConstViewType
 Type of constant segments view.
using IndexAllocatorType = IndexAllocator
using OffsetsContainer = Containers::Vector< Index, Device, typename Base::IndexType, IndexAllocator >
 Type of container storing offsets of particular segments.
template<typename Device_ = Device, typename Index_ = Index, typename IndexAllocator_ = typename Allocators::Default< Device_ >::template Allocator< Index_ >, ElementsOrganization Organization_ = Organization>
using Self = ChunkedEllpack< Device_, Index_, IndexAllocator_, Organization_ >
 Templated type for creating ChunkedEllpack segments with different template parameters.
using SliceInfoAllocator = typename Allocators::Default< Device >::template Allocator< typename Base::SliceInfoType >
using SliceInfoContainer
template<typename Device_, typename Index_>
using ViewTemplate = ChunkedEllpackView< Device_, Index_, Organization >
 Templated view type.
using ViewType = ChunkedEllpackView< Device, Index, Organization >
 Type of segments view.
Public Types inherited from TNL::Algorithms::Segments::ChunkedEllpackBase< Device, Index, DefaultElementsOrganization< Device >::getOrganization() >
using ConstOffsetsView
 The type for representing the constant vector view with segment offsets.
using ConstSliceInfoContainerView
using DeviceType
 The device where the segments are operating.
using IndexType
 The type used for indexing of segments elements.
using OffsetsView
 The type for representing the vector view with segment offsets.
using SegmentViewType
 Accessor type for one particular segment.
using SliceInfoContainerView
using SliceInfoType
using ViewTemplate
 Templated view type.

Public Member Functions

 ChunkedEllpack ()=default
 Constructor with no parameters to create empty segments.
 ChunkedEllpack (ChunkedEllpack &&segments) noexcept=default
 Move constructor.
 ChunkedEllpack (const ChunkedEllpack &segments)
 Copy constructor (makes deep copy).
template<typename SizesContainer, std::enable_if_t< IsArrayType< SizesContainer >::value, bool > = true>
 ChunkedEllpack (const SizesContainer &segmentsSizes)
 Constructor that initializes segments based on their sizes.
template<typename ListIndex>
 ChunkedEllpack (const std::initializer_list< ListIndex > &segmentsSizes)
 Constructor that initializes segments using an initializer list.
ConstViewType getConstView () const
 Returns a constant view for this instance of segments which can by used for example in lambda functions running in GPU kernels.
ViewType getView ()
 Returns a view for this instance of 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.
ChunkedEllpackoperator= (ChunkedEllpack &&) noexcept(false)
 Move-assignment operator.
ChunkedEllpackoperator= (const ChunkedEllpack &segments)
 Copy-assignment operator (makes a deep copy).
template<typename Device_, typename Index_, typename IndexAllocator_, ElementsOrganization Organization_>
ChunkedEllpackoperator= (const ChunkedEllpack< Device_, Index_, IndexAllocator_, Organization_ > &segments)
 Assignment operator for segments with different template parameters.
void reset ()
 Reset the segments to empty states (it means that there is no segment in the segments).
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.
Public Member Functions inherited from TNL::Algorithms::Segments::ChunkedEllpackBase< Device, Index, DefaultElementsOrganization< Device >::getOrganization() >
__cuda_callable__ ChunkedEllpackBase ()=default
 Default constructor with no parameters to create empty segments view.
void forAllElements (Function &&function) const
void forAllElementsIf (Condition condition, Function function) const
void forAllSegments (Function &&function) const
void forElements (IndexType begin, IndexType end, Function &&function) const
void forElementsIf (IndexType begin, IndexType end, Condition condition, Function function) const
void forSegments (IndexType begin, IndexType end, Function &&function) const
__cuda_callable__ IndexType getChunksInSlice () const
 Returns the number of chunks in a slice.
__cuda_callable__ OffsetsView getChunksToSegmentsMappingView ()
 Returns a modifiable vector view with mapping of chunks to segments.
__cuda_callable__ IndexType getDesiredChunkSize () const
 Returns the desired chunk size.
__cuda_callable__ IndexType getElementCount () const
 Returns the number of elements managed by all segments.
__cuda_callable__ IndexType getGlobalIndex (IndexType segmentIdx, IndexType localIdx) const
 Computes the global index of an element managed by the segments.
__cuda_callable__ IndexType getNumberOfSlices () const
 Returns the number of slices.
__cuda_callable__ IndexType getSegmentCount () const
 Returns the number of segments.
__cuda_callable__ OffsetsView getSegmentPointersView ()
 Returns a modifiable vector view with segment pointers.
__cuda_callable__ IndexType getSegmentsCount () const
 Returns the number of segments. Deprecated, use getSegmentCount().
__cuda_callable__ IndexType getSegmentSize (IndexType segmentIdx) const
 Returns the size of a particular segment denoted by segmentIdx.
__cuda_callable__ OffsetsView getSegmentToChunkMappingView ()
 Returns a modifiable vector view with mapping of segments to chunks.
__cuda_callable__ OffsetsView getSegmentToSliceMappingView ()
 Returns a modifiable vector view with mapping of segments to slices.
__cuda_callable__ SegmentViewType getSegmentView (IndexType segmentIdx) const
 Returns a segment view (i.e., a segment accessor) for the specified segment index.
__cuda_callable__ IndexType getSize () const
 Returns the number of elements managed by all segments.
__cuda_callable__ SliceInfoContainerView getSlicesView ()
 Returns a modifiable view with slice information.
__cuda_callable__ IndexType getStorageSize () const
 Returns number of elements that needs to be allocated by a container connected to this segments.
ChunkedEllpackBaseoperator= (const ChunkedEllpackBase &)=delete
 Copy-assignment operator.
void printStructure (std::ostream &str) const
 Prints the structure of the segments to the output stream.

Protected Member Functions

template<typename SizesContainer>
void resolveSliceSizes (SizesContainer &segmentsSizes)
template<typename SizesContainer>
bool setSlice (SizesContainer &segmentsSizes, Index sliceIndex, Index &elementsToAllocation)
Protected Member Functions inherited from TNL::Algorithms::Segments::ChunkedEllpackBase< Device, Index, DefaultElementsOrganization< Device >::getOrganization() >
__cuda_callable__ void bind (IndexType size, IndexType storageSize, IndexType numberOfSlices, IndexType chunksInSlice, IndexType desiredChunkSize, OffsetsView segmentToChunkMapping, OffsetsView segmentToSliceMapping, OffsetsView chunksToSegmentsMapping, OffsetsView segmentPointers, SliceInfoContainerView slices)
 Re-initializes the internal attributes of the base class.

Protected Attributes

OffsetsContainer chunksToSegmentsMapping
OffsetsContainer segmentPointers
 Keeps index of the first segment index.
OffsetsContainer segmentToChunkMapping
 For each segment, this keeps index of the first chunk within a slice.
OffsetsContainer segmentToSliceMapping
 For each segment, this keeps index of the slice which contains the segment.
SliceInfoContainer slices
Protected Attributes inherited from TNL::Algorithms::Segments::ChunkedEllpackBase< Device, Index, DefaultElementsOrganization< Device >::getOrganization() >
IndexType chunksInSlice
OffsetsView chunksToSegmentsMapping
IndexType desiredChunkSize
IndexType numberOfSlices
OffsetsView segmentPointers
 Keeps index of the first segment index.
OffsetsView segmentToChunkMapping
 For each segment, this keeps index of the first chunk within a slice.
OffsetsView segmentToSliceMapping
 For each segment, this keeps index of the slice which contains the segment.
IndexType size
SliceInfoContainerView slices
IndexType storageSize

Additional Inherited Members

Static Public Member Functions inherited from TNL::Algorithms::Segments::ChunkedEllpackBase< Device, Index, DefaultElementsOrganization< Device >::getOrganization() >
static constexpr ElementsOrganization getOrganization ()
 Returns the data layout for the chunked ellpack 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 chunked ellpack format uses padding elements.

Detailed Description

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
class TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >

Data structure for Chunked Ellpack segments.

Chunked Ellpack segments are inspired by the paper [Heller M., Oberhuber T., Improved Row-grouped CSR Format for Storing of Sparse Matrices on GPU, Proceedings of Algoritmy 2012, 2012, Handlovičová A., Minarechová Z. and Ševčovič D. (ed.), pages 282-290](https://geraldine.fjfi.cvut.cz/~oberhuber/data/vyzkum/publikace/12-heller-oberhuber-improved-rgcsr-format.pdf).

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

Template Parameters
DeviceThe type of device on which the segments will operate.
IndexThe type used for indexing elements managed by the segments.
IndexAllocatorThe allocator used for managing index containers.
OrganizationThe organization of the elements in the segments—either row-major or column-major order.

Member Typedef Documentation

◆ Self

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
template<typename Device_ = Device, typename Index_ = Index, typename IndexAllocator_ = typename Allocators::Default< Device_ >::template Allocator< Index_ >, ElementsOrganization Organization_ = Organization>
using TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::Self = ChunkedEllpack< Device_, Index_, IndexAllocator_, Organization_ >

Templated type for creating ChunkedEllpack segments with different template parameters.

Template Parameters
Device_is alternative device type.
Index_is alternative index type.
IndexAllocator_is alternative index allocator type.

◆ SliceInfoContainer

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
using TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::SliceInfoContainer
Initial value:
Device,
Index,
SliceInfoAllocator >
Array is responsible for memory management, access to array elements, and general array operations.
Definition Array.h:65
Copy const qualifier from Source type to Target type.
Definition TypeTraits.h:288

◆ ViewTemplate

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
template<typename Device_, typename Index_>
using TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::ViewTemplate = ChunkedEllpackView< Device_, Index_, Organization >

Templated view type.

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

Constructor & Destructor Documentation

◆ ChunkedEllpack() [1/2]

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
template<typename SizesContainer, std::enable_if_t< IsArrayType< SizesContainer >::value, bool > = true>
TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::ChunkedEllpack ( const SizesContainer & segmentsSizes)

Constructor that initializes segments based on their sizes.

The number of segments is determined by the size of segmentsSizes. Each element in this container specifies the size of a corresponding segment.

Template Parameters
SizesContainerThe type of container used to store segment sizes. It can be, for example, TNL::Containers::Array or TNL::Containers::Vector.
Parameters
segmentsSizesAn instance of the container holding the sizes of the segments.

See the following example:

1#include <iostream>
2#include <TNL/Containers/Vector.h>
3#include <TNL/Algorithms/Segments/traverse.h>
4#include <TNL/Devices/Host.h>
5#include <TNL/Devices/Cuda.h>
6
7template< typename Segments >
8void
9SegmentsExample()
10{
11 using Device = typename Segments::DeviceType;
12
13 /***
14 * Create segments with given segments sizes.
15 */
16 TNL::Containers::Vector< int, Device > segmentsSizes{ 1, 2, 3, 4, 5 };
17 Segments segments( segmentsSizes );
18 std::cout << "Segments sizes are: " << segments << '\n';
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();
30 segments,
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 std::cout << TNL::Algorithms::Segments::print( segments, fetch ) << '\n';
45}
46
47int
48main( int argc, char* argv[] )
49{
50 std::cout << "Example of CSR segments on host:\n";
51 SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Host, int > >();
52
53 std::cout << "Example of Ellpack segments on host:\n";
54 SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Host, int > >();
55
56#ifdef __CUDACC__
57 std::cout << "Example of CSR segments on CUDA GPU:\n";
58 SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Cuda, int > >();
59
60 std::cout << "Example of Ellpack segments on CUDA GPU:\n";
61 SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Cuda, int > >();
62#endif
63 return EXIT_SUCCESS;
64}
#define __cuda_callable__
Definition Macros.h:49
Vector extends Array with algebraic operations.
Definition Vector.h:37
Namespace for the segments data structures.
Definition _NamespaceDoxy.h:7
SegmentsPrinter< typename Segments::ConstViewType, Fetch > print(const Segments &segments, Fetch fetch)
Print segments sizes, i.e. the segments setup.
void forAllElements(const Segments &segments, Function &&function, LaunchConfiguration launchConfig=Algorithms::Segments::LaunchConfiguration())
Iterates in parallel over all elements of all segments and applies the specified lambda function.

The expected output is:

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

◆ ChunkedEllpack() [2/2]

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
template<typename ListIndex>
TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::ChunkedEllpack ( const std::initializer_list< ListIndex > & segmentsSizes)

Constructor that initializes segments using an initializer list.

The number of segments is determined by the size of segmentsSizes. Each element in this initializer list specifies the size of a corresponding segment.

Template Parameters
ListIndexThe type used for indexing elements in the initializer list.
Parameters
segmentsSizesAn initializer list defining the sizes of the segments.

See the following example:

1#include <iostream>
2#include <TNL/Containers/Vector.h>
3#include <TNL/Algorithms/Segments/traverse.h>
4#include <TNL/Devices/Host.h>
5#include <TNL/Devices/Cuda.h>
6
7template< typename Segments >
8void
9SegmentsExample()
10{
11 using Device = typename Segments::DeviceType;
12
13 /***
14 * Create segments with given segments sizes.
15 */
16 Segments segments{ 1, 2, 3, 4, 5 };
17 std::cout << "Segments sizes are: " << segments << '\n';
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();
29 segments,
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 std::cout << TNL::Algorithms::Segments::print( segments, fetch ) << '\n';
44}
45
46int
47main( int argc, char* argv[] )
48{
49 std::cout << "Example of CSR segments on host:\n";
50 SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Host, int > >();
51
52 std::cout << "Example of Ellpack segments on host:\n";
53 SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Host, int > >();
54
55#ifdef __CUDACC__
56 std::cout << "Example of CSR segments on CUDA GPU:\n";
57 SegmentsExample< TNL::Algorithms::Segments::CSR< TNL::Devices::Cuda, int > >();
58
59 std::cout << "Example of Ellpack segments on CUDA GPU:\n";
60 SegmentsExample< TNL::Algorithms::Segments::Ellpack< TNL::Devices::Cuda, int > >();
61#endif
62 return EXIT_SUCCESS;
63}

The expected output is:

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

Member Function Documentation

◆ load()

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
void TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::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 >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
template<typename Device_, typename Index_, typename IndexAllocator_, ElementsOrganization Organization_>
ChunkedEllpack & TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::operator= ( const ChunkedEllpack< Device_, Index_, IndexAllocator_, Organization_ > & segments)

Assignment operator for segments with different template parameters.

Performs a deep copy of the source segments.

Template Parameters
Device_The device type of the source segments.
Index_The index type of the source segments.
IndexAllocator_The index allocator type of the source segments.
Parameters
segmentsThe source segments object.
Returns
A reference to this instance.

◆ save()

template<typename Device, typename Index, typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
void TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::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 >, ElementsOrganization Organization = DefaultElementsOrganization< Device >::getOrganization()>
template<typename SizesContainer>
void TNL::Algorithms::Segments::ChunkedEllpack< Device, Index, IndexAllocator, Organization >::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 file: