Skip to content
Snippets Groups Projects
Commit fd236cf9 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Jakub Klinkovský
Browse files

Refactoring segments printing.

parent 4df14dcb
No related branches found
No related tags found
1 merge request!105TO/matrices-adaptive-csr
...@@ -49,65 +49,39 @@ std::ostream& printSegments( const Segments& segments, std::ostream& str ) ...@@ -49,65 +49,39 @@ std::ostream& printSegments( const Segments& segments, std::ostream& str )
return str; return str;
} }
/**
* \brief Print segments with related content.
*
* \tparam Segments is type of segments.
* \tparam Fetch is a lambda function for reading related data.
* \param segments is an instance of segments.
* \param fetch is an instance of lambda function reading related data. It is supposed to defined as
*
* ```
* auto fetch = [=] __cuda_callable__ ( IndexType globalIdx ) -> ValueType { return data_view[ globalIdx ]; };
* ```
*
* \param str is output stream.
* \return reference to the output stream.
*
* \par Example
* \include Algorithms/Segments/SegmentsPrintingExample-2.cpp
* \par Output
* \include SegmentsPrintingExample-2.out
*/
template< typename Segments, template< typename Segments,
typename Fetch > typename Fetch >
std::ostream& printSegments( const Segments& segments, Fetch&& fetch, std::ostream& str ) struct SegmentsPrinter
{ {
using IndexType = typename Segments::IndexType; SegmentsPrinter( const Segments& segments, Fetch& fetch )
using DeviceType = typename Segments::DeviceType; : segments( segments ), fetch( fetch ) {}
using ValueType = decltype( fetch( IndexType() ) );
TNL::Containers::Array< ValueType, DeviceType, IndexType > aux( 1 ); std::ostream& print( std::ostream& str ) const
auto view = segments.getConstView();
for( IndexType segmentIdx = 0; segmentIdx < segments.getSegmentsCount(); segmentIdx++ )
{ {
str << "Seg. " << segmentIdx << ": [ "; using IndexType = typename Segments::IndexType;
auto segmentSize = segments.getSegmentSize( segmentIdx ); using DeviceType = typename Segments::DeviceType;
for( IndexType localIdx = 0; localIdx < segmentSize; localIdx++ ) using ValueType = decltype( fetch( IndexType() ) );
TNL::Containers::Array< ValueType, DeviceType, IndexType > aux( 1 );
auto view = segments.getConstView();
for( IndexType segmentIdx = 0; segmentIdx < segments.getSegmentsCount(); segmentIdx++ )
{ {
aux.forAllElements( [=] __cuda_callable__ ( IndexType elementIdx, double& v ) mutable { str << "Seg. " << segmentIdx << ": [ ";
v = fetch( view.getGlobalIndex( segmentIdx, localIdx ) ); auto segmentSize = segments.getSegmentSize( segmentIdx );
} ); for( IndexType localIdx = 0; localIdx < segmentSize; localIdx++ )
auto value = aux.getElement( 0 ); {
str << value; aux.forAllElements( [=] __cuda_callable__ ( IndexType elementIdx, double& v ) mutable {
if( localIdx < segmentSize - 1 ) v = fetch( view.getGlobalIndex( segmentIdx, localIdx ) );
str << ", "; } );
auto value = aux.getElement( 0 );
str << value;
if( localIdx < segmentSize - 1 )
str << ", ";
}
str << " ] " << std::endl;
} }
str << " ] " << std::endl; return str;
} }
return str;
}
template< typename Segments,
typename Fetch >
struct SegmentsPrinter
{
SegmentsPrinter( const Segments& segments, Fetch& fetch )
: segments( segments ), fetch( fetch ) {}
std::ostream& print( std::ostream& str ) const { return printSegments( segments, fetch, str ); }
protected: protected:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment