diff --git a/Documentation/Examples/Algorithms/Segments/SegmentsExample_General.cpp b/Documentation/Examples/Algorithms/Segments/SegmentsExample_General.cpp index 7e4aced7ea0df0d640c2c6177a774fd23b450062..d64fdbfde07cb3d68ee9dd28c43fc4926098c19b 100644 --- a/Documentation/Examples/Algorithms/Segments/SegmentsExample_General.cpp +++ b/Documentation/Examples/Algorithms/Segments/SegmentsExample_General.cpp @@ -35,7 +35,9 @@ void SegmentsExample() /*** * Print the data managed by the segments. */ + std::cerr << data << std::endl; auto fetch = [=] __cuda_callable__ ( IndexType globalIdx ) -> double { return data_view[ globalIdx ]; }; + printSegments( segments, fetch, std::cout ); std::cout << segments.print( fetch ) << std::endl; /*** diff --git a/src/TNL/Algorithms/Segments/SegmentsPrinting.h b/src/TNL/Algorithms/Segments/SegmentsPrinting.h index 7695cc6939f31e1358ee6c68799f7aca272fef00..fa5d6c62841c1a8b4052bd424fd21e5cf1594887 100644 --- a/src/TNL/Algorithms/Segments/SegmentsPrinting.h +++ b/src/TNL/Algorithms/Segments/SegmentsPrinting.h @@ -53,7 +53,7 @@ template< typename Segments, typename Fetch > struct SegmentsPrinter { - SegmentsPrinter( const Segments& segments, Fetch& fetch ) + SegmentsPrinter( const Segments& segments, Fetch&& fetch ) : segments( segments ), fetch( fetch ) {} std::ostream& print( std::ostream& str ) const @@ -71,6 +71,8 @@ struct SegmentsPrinter for( IndexType localIdx = 0; localIdx < segmentSize; localIdx++ ) { aux.forAllElements( [=] __cuda_callable__ ( IndexType elementIdx, double& v ) mutable { + //printf( "####### localIdx = %d, globalIdx = %d \n", localIdx, view.getGlobalIndex( segmentIdx, localIdx ) ); + //v = view.getGlobalIndex( segmentIdx, localIdx ); v = fetch( view.getGlobalIndex( segmentIdx, localIdx ) ); } ); auto value = aux.getElement( 0 ); @@ -87,7 +89,7 @@ struct SegmentsPrinter const Segments& segments; - Fetch& fetch; + Fetch fetch; }; template< typename Segments, @@ -97,6 +99,38 @@ std::ostream& operator<<( std::ostream& str, const SegmentsPrinter< Segments, Fe return printer.print( str ); } +template< typename Segments, + typename Fetch > +std::ostream& printSegments( const Segments& segments, Fetch&& fetch, std::ostream& str ) +{ + using IndexType = typename Segments::IndexType; + using DeviceType = typename Segments::DeviceType; + 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++ ) + { + str << "Seg. " << segmentIdx << ": [ "; + auto segmentSize = segments.getSegmentSize( segmentIdx ); + //std::cerr << "Segment size = " << segmentSize << std::endl; + for( IndexType localIdx = 0; localIdx < segmentSize; localIdx++ ) + { + aux.forAllElements( [=] __cuda_callable__ ( IndexType elementIdx, double& v ) mutable { + //printf( "####### localIdx = %d, globalIdx = %d \n", localIdx, view.getGlobalIndex( segmentIdx, localIdx ) ); + v = fetch( view.getGlobalIndex( segmentIdx, localIdx ) ); + //v = view.getGlobalIndex( segmentIdx, localIdx ); + } ); + auto value = aux.getElement( 0 ); + str << value; + if( localIdx < segmentSize - 1 ) + str << ", "; + } + str << " ] " << std::endl; + } + return str; +} + } // namespace Segments } // namespace Algorithms } // namespace TNL