From 361d8421a79c3c2f9f5fd57d06d55a337ea84bb6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Mar 2021 15:03:27 +0100 Subject: [PATCH 01/54] Fixing benchmark in tutorial on sparse matrices. --- .../Tutorials/Matrices/MultidiagonalMatrixSetup_Benchmark.cpp | 2 +- .../Tutorials/Matrices/SparseMatrixSetup_Benchmark.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/Tutorials/Matrices/MultidiagonalMatrixSetup_Benchmark.cpp b/Documentation/Tutorials/Matrices/MultidiagonalMatrixSetup_Benchmark.cpp index 5743c5e32..d323105cd 100644 --- a/Documentation/Tutorials/Matrices/MultidiagonalMatrixSetup_Benchmark.cpp +++ b/Documentation/Tutorials/Matrices/MultidiagonalMatrixSetup_Benchmark.cpp @@ -146,7 +146,7 @@ void forElements( const int gridSize, Matrix& matrix ) auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int columnIdx, float& value, bool& compute ) mutable { const int i = rowIdx % gridSize; const int j = rowIdx / gridSize; - if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 && localIdx == 0 ) + if( ( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) && localIdx == 0 ) { columnIdx = rowIdx; value = 1.0; diff --git a/Documentation/Tutorials/Matrices/SparseMatrixSetup_Benchmark.cpp b/Documentation/Tutorials/Matrices/SparseMatrixSetup_Benchmark.cpp index d9b668b20..7af7de1e1 100644 --- a/Documentation/Tutorials/Matrices/SparseMatrixSetup_Benchmark.cpp +++ b/Documentation/Tutorials/Matrices/SparseMatrixSetup_Benchmark.cpp @@ -171,7 +171,7 @@ void forElements( const int gridSize, Matrix& matrix ) auto f = [=] __cuda_callable__ ( int rowIdx, int localIdx, int& columnIdx, float& value, bool& compute ) mutable { const int i = rowIdx % gridSize; const int j = rowIdx / gridSize; - if( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 && localIdx == 0 ) + if( ( i == 0 || j == 0 || i == gridSize - 1 || j == gridSize - 1 ) && localIdx == 0 ) { columnIdx = rowIdx; value = 1.0; -- GitLab From b32d83f004bf3827bf96bc6733b3c837f46886ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Mar 2021 15:04:24 +0100 Subject: [PATCH 02/54] Added forSegments to all segments and forRows (just in SparseMatrix for now). --- src/TNL/Algorithms/Segments/BiEllpack.h | 207 +++++++++--------- src/TNL/Algorithms/Segments/BiEllpack.hpp | 39 +++- .../Segments/BiEllpackSegmentView.h | 17 +- src/TNL/Algorithms/Segments/BiEllpackView.h | 21 +- src/TNL/Algorithms/Segments/BiEllpackView.hpp | 50 ++++- src/TNL/Algorithms/Segments/CSR.h | 16 +- src/TNL/Algorithms/Segments/CSR.hpp | 40 +++- src/TNL/Algorithms/Segments/CSRView.h | 15 +- src/TNL/Algorithms/Segments/CSRView.hpp | 45 +++- src/TNL/Algorithms/Segments/ChunkedEllpack.h | 21 +- .../Algorithms/Segments/ChunkedEllpack.hpp | 36 ++- .../Segments/ChunkedEllpackSegmentView.h | 48 +++- .../Algorithms/Segments/ChunkedEllpackView.h | 20 +- .../Segments/ChunkedEllpackView.hpp | 65 ++++-- src/TNL/Algorithms/Segments/Ellpack.h | 13 +- src/TNL/Algorithms/Segments/Ellpack.hpp | 42 +++- src/TNL/Algorithms/Segments/EllpackView.h | 13 +- src/TNL/Algorithms/Segments/EllpackView.hpp | 51 ++++- src/TNL/Algorithms/Segments/SegmentView.h | 36 ++- src/TNL/Algorithms/Segments/SlicedEllpack.h | 13 +- src/TNL/Algorithms/Segments/SlicedEllpack.hpp | 42 +++- .../Algorithms/Segments/SlicedEllpackView.h | 15 +- .../Algorithms/Segments/SlicedEllpackView.hpp | 63 ++++-- .../Algorithms/Segments/details/BiEllpack.h | 9 +- .../Segments/details/ChunkedEllpack.h | 19 +- src/TNL/Matrices/DistributedMatrix.h | 4 +- src/TNL/Matrices/SparseMatrix.h | 58 +++-- src/TNL/Matrices/SparseMatrix.hpp | 76 ++++++- src/TNL/Matrices/SparseMatrixRowView.h | 19 +- src/TNL/Matrices/SparseMatrixRowView.hpp | 26 +++ src/TNL/Matrices/SparseMatrixView.h | 20 +- src/TNL/Matrices/SparseMatrixView.hpp | 78 ++++++- .../initializer/SubentitySeedsCreator.h | 4 +- src/UnitTests/Containers/ArrayViewTest.h | 1 - src/UnitTests/Matrices/SparseMatrixTest.h | 14 ++ src/UnitTests/Matrices/SparseMatrixTest.hpp | 69 +++++- .../SparseMatrixTest_ChunkedEllpack.h | 34 +-- 37 files changed, 1017 insertions(+), 342 deletions(-) diff --git a/src/TNL/Algorithms/Segments/BiEllpack.h b/src/TNL/Algorithms/Segments/BiEllpack.h index c32dc1f22..29959747c 100644 --- a/src/TNL/Algorithms/Segments/BiEllpack.h +++ b/src/TNL/Algorithms/Segments/BiEllpack.h @@ -15,162 +15,173 @@ #include #include -namespace TNL { - namespace Algorithms { - namespace Segments { - -template< typename Device, - typename Index, - typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index >, - ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization(), - int WarpSize = 32 > -class BiEllpack +namespace TNL { - public: + namespace Algorithms + { + namespace Segments + { - using DeviceType = Device; - using IndexType = std::remove_const_t< Index >; - using OffsetsHolder = Containers::Vector< Index, DeviceType, IndexType, IndexAllocator >; - static constexpr ElementsOrganization getOrganization() { return Organization; } - using ViewType = BiEllpackView< Device, Index, Organization >; - template< typename Device_, typename Index_ > - using ViewTemplate = BiEllpackView< Device_, Index_, Organization >; - using ConstViewType = BiEllpackView< Device, std::add_const_t< IndexType >, Organization >; - using SegmentViewType = BiEllpackSegmentView< IndexType, Organization >; + template ::template Allocator, + ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization::getOrganization(), + int WarpSize = 32> + class BiEllpack + { + public: + using DeviceType = Device; + using IndexType = std::remove_const_t; + using OffsetsHolder = Containers::Vector< IndexType, DeviceType, IndexType, IndexAllocator>; + static constexpr ElementsOrganization getOrganization() { return Organization; } + using ViewType = BiEllpackView< Device, Index, Organization, WarpSize >; + template + using ViewTemplate = BiEllpackView; + using ConstViewType = typename ViewType::ConstViewType; + using SegmentViewType = typename ViewType::SegmentViewType; - static constexpr bool havePadding() { return true; }; + static constexpr bool havePadding() { return true; }; - BiEllpack() = default; + BiEllpack() = default; - BiEllpack( const Containers::Vector< IndexType, DeviceType, IndexType >& sizes ); + BiEllpack(const Containers::Vector &sizes); - BiEllpack( const BiEllpack& segments ); + BiEllpack(const BiEllpack &segments); - BiEllpack( const BiEllpack&& segments ); + BiEllpack(const BiEllpack &&segments); - static String getSerializationType(); + static String getSerializationType(); - static String getSegmentsType(); + static String getSegmentsType(); - ViewType getView(); + ViewType getView(); - const ConstViewType getConstView() const; + const ConstViewType getConstView() const; - /** + /** * \brief Number of segments. */ - __cuda_callable__ - IndexType getSegmentsCount() const; + __cuda_callable__ + IndexType + getSegmentsCount() const; - /** + /** * \brief Set sizes of particular segments. */ - template< typename SizesHolder = OffsetsHolder > - void setSegmentsSizes( const SizesHolder& sizes ); + template + void setSegmentsSizes(const SizesHolder &sizes); - void reset(); + void reset(); - IndexType getSegmentSize( const IndexType segmentIdx ) const; + IndexType getSegmentSize(const IndexType segmentIdx) const; - /** + /** * \brief Number segments. */ - __cuda_callable__ - IndexType getSize() const; + __cuda_callable__ + IndexType + getSize() const; - __cuda_callable__ - IndexType getStorageSize() const; + __cuda_callable__ + IndexType + getStorageSize() const; - __cuda_callable__ - IndexType getGlobalIndex( const IndexType segmentIdx, const IndexType localIdx ) const; + __cuda_callable__ + IndexType + getGlobalIndex(const IndexType segmentIdx, const IndexType localIdx) const; - __cuda_callable__ - SegmentViewType getSegmentView( const IndexType segmentIdx ) const; + __cuda_callable__ + SegmentViewType + getSegmentView(const IndexType segmentIdx) const; - /*** + /*** * \brief Go over all segments and for each segment element call * function 'f' with arguments 'args'. The return type of 'f' is bool. * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template + void forElements(IndexType first, IndexType last, Function &&f) const; + + template + void forEachElement(Function &&f) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template + void forSegments(IndexType begin, IndexType end, Function &&f) const; + template + void forEachSegment(Function &&f) const; - /*** + /*** * \brief Go over all segments and perform a reduction in each of them. */ - template< typename Fetch, typename Reduction, typename ResultKeeper, typename Real, typename... Args > - void segmentsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduction& reduction, ResultKeeper& keeper, const Real& zero, Args... args ) const; + template + void segmentsReduction(IndexType first, IndexType last, Fetch &fetch, const Reduction &reduction, ResultKeeper &keeper, const Real &zero, Args... args) const; - template< typename Fetch, typename Reduction, typename ResultKeeper, typename Real, typename... Args > - void allReduction( Fetch& fetch, const Reduction& reduction, ResultKeeper& keeper, const Real& zero, Args... args ) const; + template + void allReduction(Fetch &fetch, const Reduction &reduction, ResultKeeper &keeper, const Real &zero, Args... args) const; - BiEllpack& operator=( const BiEllpack& source ) = default; + BiEllpack &operator=(const BiEllpack &source) = default; - template< typename Device_, typename Index_, typename IndexAllocator_, ElementsOrganization Organization_ > - BiEllpack& operator=( const BiEllpack< Device_, Index_, IndexAllocator_, Organization_, WarpSize >& source ); + template + BiEllpack &operator=(const BiEllpack &source); - void save( File& file ) const; + void save(File &file) const; - void load( File& file ); + void load(File &file); - void printStructure( std::ostream& str ) const; + void printStructure(std::ostream &str) const; - // TODO: nvcc needs this public because of lambda function used inside - template< typename SizesHolder = OffsetsHolder > - void performRowBubbleSort( const SizesHolder& segmentsSize ); + // TODO: nvcc needs this public because of lambda function used inside + template + void performRowBubbleSort(const SizesHolder &segmentsSize); - // TODO: the same as above - template< typename SizesHolder = OffsetsHolder > - void computeColumnSizes( const SizesHolder& segmentsSizes ); + // TODO: the same as above + template + void computeColumnSizes(const SizesHolder &segmentsSizes); - protected: + protected: + static constexpr int getWarpSize() { return WarpSize; }; - static constexpr int getWarpSize() { return WarpSize; }; + static constexpr int getLogWarpSize() { return std::log2(WarpSize); }; - static constexpr int getLogWarpSize() { return std::log2( WarpSize ); }; + template + void verifyRowPerm(const SizesHolder &segmentsSizes); - template< typename SizesHolder = OffsetsHolder > - void verifyRowPerm( const SizesHolder& segmentsSizes ); + template + void verifyRowLengths(const SizesHolder &segmentsSizes); - template< typename SizesHolder = OffsetsHolder > - void verifyRowLengths( const SizesHolder& segmentsSizes ); + IndexType getStripLength(const IndexType stripIdx) const; - IndexType getStripLength( const IndexType stripIdx ) const; + IndexType getGroupLength(const IndexType strip, const IndexType group) const; - IndexType getGroupLength( const IndexType strip, const IndexType group ) const; + IndexType size = 0, storageSize = 0; - IndexType size = 0, storageSize = 0; + IndexType virtualRows = 0; - IndexType virtualRows = 0; + OffsetsHolder rowPermArray; - OffsetsHolder rowPermArray; + OffsetsHolder groupPointers; - OffsetsHolder groupPointers; + // TODO: Replace later + __cuda_callable__ Index power(const IndexType number, const IndexType exponent) const + { + if (exponent >= 0) + { + IndexType result = 1; + for (IndexType i = 0; i < exponent; i++) + result *= number; + return result; + } + return 0; + }; - // TODO: Replace later - __cuda_callable__ Index power( const IndexType number, const IndexType exponent ) const - { - if( exponent >= 0 ) - { - IndexType result = 1; - for( IndexType i = 0; i < exponent; i++ ) - result *= number; - return result; - } - return 0; - }; - - template< typename Device_, typename Index_, typename IndexAllocator_, ElementsOrganization Organization_, int WarpSize_ > - friend class BiEllpack; -}; + template + friend class BiEllpack; + }; } // namespace Segments - } // namespace Algorithms + } // namespace Algorithms } // namespace TNL #include diff --git a/src/TNL/Algorithms/Segments/BiEllpack.hpp b/src/TNL/Algorithms/Segments/BiEllpack.hpp index 2c44eb27a..d85b668ce 100644 --- a/src/TNL/Algorithms/Segments/BiEllpack.hpp +++ b/src/TNL/Algorithms/Segments/BiEllpack.hpp @@ -443,12 +443,12 @@ template< typename Device, typename IndexAllocator, ElementsOrganization Organization, int WarpSize > - template< typename Function, typename... Args > + template< typename Function > void BiEllpack< Device, Index, IndexAllocator, Organization, WarpSize >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { - this->getConstView().forElements( first, last, f, args... ); + this->getConstView().forElements( first, last, f ); } template< typename Device, @@ -456,14 +456,41 @@ template< typename Device, typename IndexAllocator, ElementsOrganization Organization, int WarpSize > - template< typename Function, typename... Args > + template< typename Function > void BiEllpack< Device, Index, IndexAllocator, Organization, WarpSize >:: -forEachElement( Function& f, Args... args ) const +forEachElement( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forElements( 0, this->getSegmentsCount(), f ); } +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization, + int WarpSize > + template< typename Function > +void +BiEllpack< Device, Index, IndexAllocator, Organization, WarpSize >:: +forSegments( IndexType begin, IndexType end, Function&& f ) const +{ + this->getConstView().forSegments( begin, end, f ); +} + +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization, + int WarpSize > + template< typename Function > +void +BiEllpack< Device, Index, IndexAllocator, Organization, WarpSize >:: +forEachSegment( Function&& f ) const +{ + this->getConstView().forEachSegment( f ); +} + + template< typename Device, typename Index, typename IndexAllocator, diff --git a/src/TNL/Algorithms/Segments/BiEllpackSegmentView.h b/src/TNL/Algorithms/Segments/BiEllpackSegmentView.h index 063a8db6a..e24597812 100644 --- a/src/TNL/Algorithms/Segments/BiEllpackSegmentView.h +++ b/src/TNL/Algorithms/Segments/BiEllpackSegmentView.h @@ -24,7 +24,7 @@ template< typename Index, class BiEllpackSegmentView { public: - + static constexpr int getWarpSize() { return WarpSize; }; static constexpr int getLogWarpSize() { static_assert( WarpSize == 32, "nvcc does not allow constexpr log2" ); return 5; }// TODO: return std::log2( WarpSize ); }; @@ -37,17 +37,18 @@ class BiEllpackSegmentView /** * \brief Constructor. - * + * * \param offset is offset of the first group of the strip the segment belongs to. * \param size is the segment size * \param inStripIdx is index of the segment within its strip. * \param groupsWidth is a static vector containing widths of the strip groups */ __cuda_callable__ - BiEllpackSegmentView( const IndexType offset, + BiEllpackSegmentView( const IndexType segmentIdx, + const IndexType offset, const IndexType inStripIdx, const GroupsWidthType& groupsWidth ) - : groupOffset( offset ), inStripIdx( inStripIdx ), segmentSize( TNL::sum( groupsWidth ) ), groupsWidth( groupsWidth ){}; + : segmentIdx( segmentIdx ), groupOffset( offset ), inStripIdx( inStripIdx ), segmentSize( TNL::sum( groupsWidth ) ), groupsWidth( groupsWidth ){}; __cuda_callable__ IndexType getSize() const @@ -79,9 +80,15 @@ class BiEllpackSegmentView return offset + inStripIdx + localIdx * groupHeight; }; + __cuda_callable__ + const IndexType& getSegmentIndex() const + { + return this->segmentIdx; + }; + protected: - IndexType groupOffset, inStripIdx, segmentSize; + IndexType segmentIdx, groupOffset, inStripIdx, segmentSize; GroupsWidthType groupsWidth; }; diff --git a/src/TNL/Algorithms/Segments/BiEllpackView.h b/src/TNL/Algorithms/Segments/BiEllpackView.h index 860f4d213..447807735 100644 --- a/src/TNL/Algorithms/Segments/BiEllpackView.h +++ b/src/TNL/Algorithms/Segments/BiEllpackView.h @@ -32,13 +32,13 @@ class BiEllpackView using DeviceType = Device; using IndexType = std::remove_const_t< Index >; - using OffsetsView = typename Containers::VectorView< Index, DeviceType, IndexType >; + using OffsetsView = typename Containers::VectorView< IndexType, DeviceType, IndexType >; using ConstOffsetsView = typename OffsetsView::ConstViewType; using ViewType = BiEllpackView; template< typename Device_, typename Index_ > - using ViewTemplate = BiEllpackView< Device_, Index_ >; - using ConstViewType = BiEllpackView< Device, std::add_const_t< Index > >; - using SegmentViewType = BiEllpackSegmentView< IndexType, Organization >; + using ViewTemplate = BiEllpackView< Device_, Index_, Organization, WarpSize >; + using ConstViewType = BiEllpackView< Device, std::add_const_t< Index >, Organization, WarpSize >; + using SegmentViewType = BiEllpackSegmentView< IndexType, Organization, WarpSize >; static constexpr bool havePadding() { return true; }; @@ -111,12 +111,17 @@ class BiEllpackView * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType first, IndexType last, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/BiEllpackView.hpp b/src/TNL/Algorithms/Segments/BiEllpackView.hpp index 7b1e2024c..07451a57a 100644 --- a/src/TNL/Algorithms/Segments/BiEllpackView.hpp +++ b/src/TNL/Algorithms/Segments/BiEllpackView.hpp @@ -129,7 +129,12 @@ template< typename Device, __cuda_callable__ auto BiEllpackView< Device, Index, Organization, WarpSize >:: getConstView() const -> const ConstViewType { - return ConstViewType( size, storageSize, virtualRows, rowPermArray.getConstView(), groupPointers.getConstView() ); + BiEllpackView* this_ptr = const_cast< BiEllpackView* >( this ); + return ConstViewType( size, + storageSize, + virtualRows, + this_ptr->rowPermArray.getView(), + this_ptr->groupPointers.getView() ); } template< typename Device, @@ -255,14 +260,14 @@ template< typename Device, typename Index, ElementsOrganization Organization, int WarpSize > - template< typename Function, typename... Args > + template< typename Function > void BiEllpackView< Device, Index, Organization, WarpSize >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { const auto segmentsPermutationView = this->rowPermArray.getConstView(); const auto groupPointersView = this->groupPointers.getConstView(); - auto work = [=] __cuda_callable__ ( IndexType segmentIdx, Args... args ) mutable { + auto work = [=] __cuda_callable__ ( IndexType segmentIdx ) mutable { const IndexType strip = segmentIdx / getWarpSize(); const IndexType firstGroupInStrip = strip * ( getLogWarpSize() + 1 ); const IndexType rowStripPerm = segmentsPermutationView[ segmentIdx ] - strip * getWarpSize(); @@ -298,19 +303,48 @@ forElements( IndexType first, IndexType last, Function& f, Args... args ) const groupHeight /= 2; } }; - Algorithms::ParallelFor< DeviceType >::exec( first, last , work, args... ); + Algorithms::ParallelFor< DeviceType >::exec( first, last , work ); } template< typename Device, typename Index, ElementsOrganization Organization, int WarpSize > - template< typename Function, typename... Args > + template< typename Function > void BiEllpackView< Device, Index, Organization, WarpSize >:: -forEachElement( Function& f, Args... args ) const +forEachElement( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + ElementsOrganization Organization, + int WarpSize > + template< typename Function > +void +BiEllpackView< Device, Index, Organization, WarpSize >:: +forSegments( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = this->getConstView(); + auto f = [=] __cuda_callable__ ( IndexType segmentIdx ) mutable { + auto segment = view.getSegmentView( segmentIdx ); + function( segment ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Device, + typename Index, + ElementsOrganization Organization, + int WarpSize > + template< typename Function > +void +BiEllpackView< Device, Index, Organization, WarpSize >:: +forEachSegment( Function&& f ) const +{ + this->forSegments( 0, this->getSegmentsCount(), f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/CSR.h b/src/TNL/Algorithms/Segments/CSR.h index 998ed4244..902361990 100644 --- a/src/TNL/Algorithms/Segments/CSR.h +++ b/src/TNL/Algorithms/Segments/CSR.h @@ -104,15 +104,21 @@ class CSR /*** * \brief Go over all segments and for each segment element call - * function 'f' with arguments 'args'. The return type of 'f' is bool. + * function 'f'. The return type of 'f' is bool. * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType begin, IndexType end, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/CSR.hpp b/src/TNL/Algorithms/Segments/CSR.hpp index 3e729938e..e5632911c 100644 --- a/src/TNL/Algorithms/Segments/CSR.hpp +++ b/src/TNL/Algorithms/Segments/CSR.hpp @@ -198,7 +198,7 @@ auto CSR< Device, Index, Kernel, IndexAllocator >:: getSegmentView( const IndexType segmentIdx ) const -> SegmentViewType { - return SegmentViewType( offsets[ segmentIdx ], offsets[ segmentIdx + 1 ] - offsets[ segmentIdx ] ); + return SegmentViewType( segmentIdx, offsets[ segmentIdx ], offsets[ segmentIdx + 1 ] - offsets[ segmentIdx ] ); } template< typename Device, @@ -227,24 +227,48 @@ template< typename Device, typename Index, typename Kernel, typename IndexAllocator > - template< typename Function, typename... Args > + template< typename Function > void CSR< Device, Index, Kernel, IndexAllocator >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType begin, IndexType end, Function&& f ) const { - this->getConstView().forElements( first, last, f, args... ); + this->getConstView().forElements( begin, end, f ); } template< typename Device, typename Index, typename Kernel, - typename IndexAllocator> - template< typename Function, typename... Args > + typename IndexAllocator > + template< typename Function > +void +CSR< Device, Index, Kernel, IndexAllocator >:: +forEachElement( Function&& f ) const +{ + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + typename Kernel, + typename IndexAllocator > + template< typename Function > +void +CSR< Device, Index, Kernel, IndexAllocator >:: +forSegments( IndexType begin, IndexType end, Function&& f ) const +{ + this->getConstView().forSegments( begin, end, f ); +} + +template< typename Device, + typename Index, + typename Kernel, + typename IndexAllocator > + template< typename Function > void CSR< Device, Index, Kernel, IndexAllocator >:: -forEachElement( Function& f, Args... args ) const +forEachSegment( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->getConstView().forEachSegment( f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/CSRView.h b/src/TNL/Algorithms/Segments/CSRView.h index 230063c7a..bc0942af5 100644 --- a/src/TNL/Algorithms/Segments/CSRView.h +++ b/src/TNL/Algorithms/Segments/CSRView.h @@ -101,16 +101,21 @@ class CSRView /*** * \brief Go over all segments and for each segment element call - * function 'f' with arguments 'args'. The return type of 'f' is bool. + * function 'f'. The return type of 'f' is bool. * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType begin, IndexType end, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/CSRView.hpp b/src/TNL/Algorithms/Segments/CSRView.hpp index 5d71a2a67..27eba0741 100644 --- a/src/TNL/Algorithms/Segments/CSRView.hpp +++ b/src/TNL/Algorithms/Segments/CSRView.hpp @@ -177,38 +177,65 @@ auto CSRView< Device, Index, Kernel >:: getSegmentView( const IndexType segmentIdx ) const -> SegmentViewType { - return SegmentViewType( offsets[ segmentIdx ], offsets[ segmentIdx + 1 ] - offsets[ segmentIdx ], 1 ); + return SegmentViewType( segmentIdx, offsets[ segmentIdx ], offsets[ segmentIdx + 1 ] - offsets[ segmentIdx ], 1 ); } template< typename Device, typename Index, typename Kernel > - template< typename Function, typename... Args > + template< typename Function > void CSRView< Device, Index, Kernel >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType begin, IndexType end, Function&& f ) const { const auto offsetsView = this->offsets; - auto l = [=] __cuda_callable__ ( const IndexType segmentIdx, Args... args ) mutable { + auto l = [=] __cuda_callable__ ( const IndexType segmentIdx ) mutable { const IndexType begin = offsetsView[ segmentIdx ]; const IndexType end = offsetsView[ segmentIdx + 1 ]; IndexType localIdx( 0 ); bool compute( true ); for( IndexType globalIdx = begin; globalIdx < end && compute; globalIdx++ ) - f( segmentIdx, localIdx++, globalIdx, compute, args... ); + f( segmentIdx, localIdx++, globalIdx, compute ); }; - Algorithms::ParallelFor< Device >::exec( first, last, l, args... ); + Algorithms::ParallelFor< Device >::exec( begin, end, l ); } template< typename Device, typename Index, typename Kernel > - template< typename Function, typename... Args > + template< typename Function > void CSRView< Device, Index, Kernel >:: -forEachElement( Function& f, Args... args ) const +forEachElement( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + typename Kernel > + template< typename Function > +void +CSRView< Device, Index, Kernel >:: +forSegments( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = this->getConstView(); + auto f = [=] __cuda_callable__ ( IndexType segmentIdx ) mutable { + auto segment = view.getSegmentView( segmentIdx ); + function( segment ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Device, + typename Index, + typename Kernel > + template< typename Function > +void +CSRView< Device, Index, Kernel >:: +forEachSegment( Function&& f ) const +{ + this->forSegments( 0, this->getSegmentsCount(), f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpack.h b/src/TNL/Algorithms/Segments/ChunkedEllpack.h index ac9c29f76..cab7a20ab 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpack.h +++ b/src/TNL/Algorithms/Segments/ChunkedEllpack.h @@ -35,11 +35,11 @@ class ChunkedEllpack template< typename Device_, typename Index_ > using ViewTemplate = ChunkedEllpackView< Device_, Index_, Organization >; using ConstViewType = ChunkedEllpackView< Device, std::add_const_t< IndexType >, Organization >; - using SegmentViewType = ChunkedEllpackSegmentView< IndexType, Organization >; - using ChunkedEllpackSliceInfoType = details::ChunkedEllpackSliceInfo< IndexType >; + using SegmentViewType = typename ViewType::SegmentViewType; + using ChunkedEllpackSliceInfoType = typename ViewType::ChunkedEllpackSliceInfoType; // details::ChunkedEllpackSliceInfo< IndexType >; //TODO: using ChunkedEllpackSliceInfoAllocator = typename IndexAllocatorType::retype< ChunkedEllpackSliceInfoType >; - using ChunkedEllpackSliceInfoAllocator = typename Allocators::Default< Device >::template Allocator< ChunkedEllpackSliceInfoType >; - using ChunkedEllpackSliceInfoContainer = Containers::Array< ChunkedEllpackSliceInfoType, DeviceType, IndexType, ChunkedEllpackSliceInfoAllocator >; + using ChunkedEllpackSliceInfoAllocator = typename ViewType::ChunkedEllpackSliceInfoAllocator; // typename Allocators::Default< Device >::template Allocator< ChunkedEllpackSliceInfoType >; + using ChunkedEllpackSliceInfoContainer = typename ViewType::ChunkedEllpackSliceInfoContainer; // Containers::Array< ChunkedEllpackSliceInfoType, DeviceType, IndexType, ChunkedEllpackSliceInfoAllocator >; static constexpr bool havePadding() { return true; }; @@ -96,12 +96,17 @@ class ChunkedEllpack * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType first, IndexType last, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp b/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp index d2ffee06c..41ca6ad76 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp +++ b/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp @@ -391,24 +391,48 @@ template< typename Device, typename Index, typename IndexAllocator, ElementsOrganization Organization > - template< typename Function, typename... Args > + template< typename Function > void ChunkedEllpack< Device, Index, IndexAllocator, Organization >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { - this->getConstView().forElements( first, last, f, args... ); + this->getConstView().forElements( first, last, f ); } template< typename Device, typename Index, typename IndexAllocator, ElementsOrganization Organization > - template< typename Function, typename... Args > + template< typename Function > void ChunkedEllpack< Device, Index, IndexAllocator, Organization >:: -forEachElement( Function& f, Args... args ) const +forEachElement( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization > + template< typename Function > +void +ChunkedEllpack< Device, Index, IndexAllocator, Organization >:: +forSegments( IndexType begin, IndexType end, Function&& f ) const +{ + this->getConstView().forSegments( begin, end, f ); +} + +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization > + template< typename Function > +void +ChunkedEllpack< Device, Index, IndexAllocator, Organization >:: +forEachSegment( Function&& f ) const +{ + this->getConstView().forEachSegment( f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpackSegmentView.h b/src/TNL/Algorithms/Segments/ChunkedEllpackSegmentView.h index 263aaa88d..1f30cbe81 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpackSegmentView.h +++ b/src/TNL/Algorithms/Segments/ChunkedEllpackSegmentView.h @@ -26,15 +26,16 @@ class ChunkedEllpackSegmentView< Index, ColumnMajorOrder > using IndexType = Index; __cuda_callable__ - ChunkedEllpackSegmentView( const IndexType offset, + ChunkedEllpackSegmentView( const IndexType segmentIdx, + const IndexType offset, const IndexType size, const IndexType chunkSize, // this is only for compatibility with the following specialization const IndexType chunksInSlice ) // this one as well - both can be replaced when we could use constexprif in C++17 - : segmentOffset( offset ), segmentSize( size ){}; + : segmentIdx( segmentIdx ), segmentOffset( offset ), segmentSize( size ){}; __cuda_callable__ ChunkedEllpackSegmentView( const ChunkedEllpackSegmentView& view ) - : segmentOffset( view.segmentOffset ), segmentSize( view.segmentSize ){}; + : segmentIdx( view.segmentIdx ), segmentOffset( view.segmentOffset ), segmentSize( view.segmentSize ){}; __cuda_callable__ IndexType getSize() const @@ -49,9 +50,15 @@ class ChunkedEllpackSegmentView< Index, ColumnMajorOrder > return segmentOffset + localIndex; }; + __cuda_callable__ + const IndexType& getSegmentIndex() const + { + return this->segmentIdx; + }; + protected: - - IndexType segmentOffset, segmentSize; + + IndexType segmentIdx, segmentOffset, segmentSize; }; template< typename Index > @@ -62,13 +69,19 @@ class ChunkedEllpackSegmentView< Index, RowMajorOrder > using IndexType = Index; __cuda_callable__ - ChunkedEllpackSegmentView( const IndexType offset, + ChunkedEllpackSegmentView( const IndexType segmentIdx, + const IndexType offset, const IndexType size, const IndexType chunkSize, const IndexType chunksInSlice ) - : segmentOffset( offset ), segmentSize( size ), + : segmentIdx( segmentIdx ), segmentOffset( offset ), segmentSize( size ), chunkSize( chunkSize ), chunksInSlice( chunksInSlice ){}; + __cuda_callable__ + ChunkedEllpackSegmentView( const ChunkedEllpackSegmentView& view ) + : segmentIdx( view.segmentIdx ), segmentOffset( view.segmentOffset ), segmentSize( view.segmentSize ), + chunkSize( view.chunkSize ), chunksInSlice( view.chunksInSlice ){}; + __cuda_callable__ IndexType getSize() const { @@ -84,9 +97,26 @@ class ChunkedEllpackSegmentView< Index, RowMajorOrder > return segmentOffset + inChunkOffset * chunksInSlice + chunkIdx; }; + __cuda_callable__ + const IndexType& getSegmentIndex() const + { + return this->segmentIdx; + }; + + __cuda_callable__ + ChunkedEllpackSegmentView& operator = ( const ChunkedEllpackSegmentView& view ) const + { + this->segmentIdx = view.segmentIdx; + this->segmentOffset = view.segmentOffset; + this->segmentSize = view.segmentSize; + this->chunkSize = view.chunkSize; + this->chunksInSlice = view.chunksInSlice; + return *this; + } + protected: - - IndexType segmentOffset, segmentSize, chunkSize, chunksInSlice; + + IndexType segmentIdx, segmentOffset, segmentSize, chunkSize, chunksInSlice; }; } //namespace Segments diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpackView.h b/src/TNL/Algorithms/Segments/ChunkedEllpackView.h index 18f08544e..68863e274 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpackView.h +++ b/src/TNL/Algorithms/Segments/ChunkedEllpackView.h @@ -12,6 +12,7 @@ #include +#include #include #include #include @@ -31,12 +32,12 @@ class ChunkedEllpackView using DeviceType = Device; using IndexType = std::remove_const_t< Index >; - using OffsetsView = typename Containers::VectorView< Index, DeviceType, IndexType >; + using OffsetsView = typename Containers::VectorView< IndexType, DeviceType, IndexType >; using ConstOffsetsView = typename OffsetsView::ConstViewType; using ViewType = ChunkedEllpackView; template< typename Device_, typename Index_ > - using ViewTemplate = ChunkedEllpackView< Device_, Index_ >; - using ConstViewType = ChunkedEllpackView< Device, std::add_const_t< Index > >; + using ViewTemplate = ChunkedEllpackView< Device_, Index_, Organization >; + using ConstViewType = ChunkedEllpackView< Device, std::add_const_t< Index >, Organization >; using SegmentViewType = ChunkedEllpackSegmentView< IndexType, Organization >; using ChunkedEllpackSliceInfoType = details::ChunkedEllpackSliceInfo< IndexType >; using ChunkedEllpackSliceInfoAllocator = typename Allocators::Default< Device >::template Allocator< ChunkedEllpackSliceInfoType >; @@ -124,12 +125,17 @@ class ChunkedEllpackView * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType begin, IndexType end, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp b/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp index 163ac448e..3a4cba132 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp +++ b/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp @@ -142,7 +142,7 @@ typename ChunkedEllpackView< Device, Index, Organization >::ViewType ChunkedEllpackView< Device, Index, Organization >:: getView() { - return ViewType( size, chunksInSlice, desiredChunkSize, + return ViewType( size, storageSize, chunksInSlice, desiredChunkSize, rowToChunkMapping.getView(), rowToSliceMapping.getView(), chunksToSegmentsMapping.getView(), @@ -157,12 +157,13 @@ template< typename Device, __cuda_callable__ auto ChunkedEllpackView< Device, Index, Organization >:: getConstView() const -> const ConstViewType { - return ConstViewType( size, chunksInSlice, desiredChunkSize, - rowToChunkMapping.getConstView(), - rowToSliceMapping.getConstView(), - chunksToSegmentsMapping.getConstView(), - rowPointers.getConstView(), - slices.getConstView(), + ChunkedEllpackView* this_ptr = const_cast< ChunkedEllpackView* >( this ); + return ConstViewType( size, storageSize, chunksInSlice, desiredChunkSize, + this_ptr->rowToChunkMapping.getView(), + this_ptr->rowToSliceMapping.getView(), + this_ptr->chunksToSegmentsMapping.getView(), + this_ptr->rowPointers.getView(), + this_ptr->slices.getView(), numberOfSlices ); } @@ -297,16 +298,16 @@ getSegmentView( const IndexType segmentIdx ) const -> SegmentViewType template< typename Device, typename Index, ElementsOrganization Organization > - template< typename Function, typename... Args > + template< typename Function > void ChunkedEllpackView< Device, Index, Organization >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { const IndexType chunksInSlice = this->chunksInSlice; auto rowToChunkMapping = this->rowToChunkMapping; auto rowToSliceMapping = this->rowToSliceMapping; auto slices = this->slices; - auto work = [=] __cuda_callable__ ( IndexType segmentIdx, Args... args ) mutable { + auto work = [=] __cuda_callable__ ( IndexType segmentIdx ) mutable { const IndexType sliceIdx = rowToSliceMapping[ segmentIdx ]; IndexType firstChunkOfSegment( 0 ); @@ -328,7 +329,7 @@ forElements( IndexType first, IndexType last, Function& f, Args... args ) const IndexType end = begin + segmentSize; IndexType localIdx( 0 ); for( IndexType j = begin; j < end && compute; j++ ) - f( segmentIdx, localIdx++, j, compute, args...); + f( segmentIdx, localIdx++, j, compute ); } else { @@ -339,25 +340,55 @@ forElements( IndexType first, IndexType last, Function& f, Args... args ) const IndexType end = begin + chunksInSlice * chunkSize; for( IndexType j = begin; j < end && compute; j += chunksInSlice ) { - f( segmentIdx, localIdx++, j, compute, args...); + f( segmentIdx, localIdx++, j, compute ); } } } }; - Algorithms::ParallelFor< DeviceType >::exec( first, last , work, args... ); + Algorithms::ParallelFor< DeviceType >::exec( first, last, work ); } template< typename Device, typename Index, ElementsOrganization Organization > - template< typename Function, typename... Args > + template< typename Function > void ChunkedEllpackView< Device, Index, Organization >:: -forEachElement( Function& f, Args... args ) const +forEachElement( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forElements( 0, this->getSegmentsCount(), f ); } +template< typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +ChunkedEllpackView< Device, Index, Organization >:: +forSegments( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = this->getConstView(); + using SVType = decltype( view.getSegmentView( IndexType() ) ); + static_assert( std::is_same< SVType, SegmentViewType >::value, "" ); + auto f = [=] __cuda_callable__ ( IndexType segmentIdx ) mutable { + auto segment = view.getSegmentView( segmentIdx ); + function( segment ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +ChunkedEllpackView< Device, Index, Organization >:: +forEachSegment( Function&& f ) const +{ + this->forSegments( 0, this->getSegmentsCount(), f ); +} + + template< typename Device, typename Index, ElementsOrganization Organization > @@ -371,7 +402,7 @@ segmentsReduction( IndexType first, IndexType last, Fetch& fetch, const Reductio { //segmentsReductionKernel( 0, first, last, fetch, reduction, keeper, zero, args... ); //return; - + for( IndexType segmentIdx = first; segmentIdx < last; segmentIdx++ ) { const IndexType& sliceIndex = rowToSliceMapping[ segmentIdx ]; diff --git a/src/TNL/Algorithms/Segments/Ellpack.h b/src/TNL/Algorithms/Segments/Ellpack.h index 1c14ced75..9e9725291 100644 --- a/src/TNL/Algorithms/Segments/Ellpack.h +++ b/src/TNL/Algorithms/Segments/Ellpack.h @@ -96,12 +96,17 @@ class Ellpack * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType first, IndexType last, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/Ellpack.hpp b/src/TNL/Algorithms/Segments/Ellpack.hpp index 3feda5dbc..cae6a1802 100644 --- a/src/TNL/Algorithms/Segments/Ellpack.hpp +++ b/src/TNL/Algorithms/Segments/Ellpack.hpp @@ -242,9 +242,9 @@ __cuda_callable__ auto Ellpack< Device, Index, IndexAllocator, Organization, Ali getSegmentView( const IndexType segmentIdx ) const -> SegmentViewType { if( Organization == RowMajorOrder ) - return SegmentViewType( segmentIdx * this->segmentSize, this->segmentSize, 1 ); + return SegmentViewType( segmentIdx, segmentIdx * this->segmentSize, this->segmentSize, 1 ); else - return SegmentViewType( segmentIdx, this->segmentSize, this->alignedSize ); + return SegmentViewType( segmentIdx, segmentIdx, this->segmentSize, this->alignedSize ); } template< typename Device, @@ -252,12 +252,12 @@ template< typename Device, typename IndexAllocator, ElementsOrganization Organization, int Alignment > - template< typename Function, typename... Args > + template< typename Function > void Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { - this->getConstView().forElements( first, last, f, args... ); + this->getConstView().forElements( first, last, f ); } template< typename Device, @@ -265,12 +265,38 @@ template< typename Device, typename IndexAllocator, ElementsOrganization Organization, int Alignment > - template< typename Function, typename... Args > + template< typename Function > void Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: -forEachElement( Function& f, Args... args ) const +forEachElement( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization, + int Alignment > + template< typename Function > +void +Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: +forSegments( IndexType begin, IndexType end, Function&& f ) const +{ + this->getConstView().forSegments( begin, end, f ); +} + +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization, + int Alignment > + template< typename Function > +void +Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: +forEachSegment( Function&& f ) const +{ + this->getConstView().forEachSegment( f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/EllpackView.h b/src/TNL/Algorithms/Segments/EllpackView.h index 4110e8c15..ae5061214 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.h +++ b/src/TNL/Algorithms/Segments/EllpackView.h @@ -92,12 +92,17 @@ class EllpackView * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType begin, IndexType end, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/EllpackView.hpp b/src/TNL/Algorithms/Segments/EllpackView.hpp index 7c657fd49..d8c5aba1e 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.hpp +++ b/src/TNL/Algorithms/Segments/EllpackView.hpp @@ -172,23 +172,23 @@ __cuda_callable__ auto EllpackView< Device, Index, Organization, Alignment >:: getSegmentView( const IndexType segmentIdx ) const -> SegmentViewType { if( Organization == RowMajorOrder ) - return SegmentViewType( segmentIdx * this->segmentSize, this->segmentSize, 1 ); + return SegmentViewType( segmentIdx, segmentIdx * this->segmentSize, this->segmentSize, 1 ); else - return SegmentViewType( segmentIdx, this->segmentSize, this->alignedSize ); + return SegmentViewType( segmentIdx, segmentIdx, this->segmentSize, this->alignedSize ); } template< typename Device, typename Index, ElementsOrganization Organization, int Alignment > - template< typename Function, typename... Args > + template< typename Function > void EllpackView< Device, Index, Organization, Alignment >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { if( Organization == RowMajorOrder ) { const IndexType segmentSize = this->segmentSize; - auto l = [=] __cuda_callable__ ( const IndexType segmentIdx, Args... args ) mutable { + auto l = [=] __cuda_callable__ ( const IndexType segmentIdx ) mutable { const IndexType begin = segmentIdx * segmentSize; const IndexType end = begin + segmentSize; IndexType localIdx( 0 ); @@ -196,21 +196,21 @@ forElements( IndexType first, IndexType last, Function& f, Args... args ) const for( IndexType globalIdx = begin; globalIdx < end && compute; globalIdx++ ) f( segmentIdx, localIdx++, globalIdx, compute ); }; - Algorithms::ParallelFor< Device >::exec( first, last, l, args... ); + Algorithms::ParallelFor< Device >::exec( first, last, l ); } else { const IndexType storageSize = this->getStorageSize(); const IndexType alignedSize = this->alignedSize; - auto l = [=] __cuda_callable__ ( const IndexType segmentIdx, Args... args ) mutable { + auto l = [=] __cuda_callable__ ( const IndexType segmentIdx ) mutable { const IndexType begin = segmentIdx; const IndexType end = storageSize; IndexType localIdx( 0 ); bool compute( true ); for( IndexType globalIdx = begin; globalIdx < end && compute; globalIdx += alignedSize ) - f( segmentIdx, localIdx++, globalIdx, compute, args... ); + f( segmentIdx, localIdx++, globalIdx, compute ); }; - Algorithms::ParallelFor< Device >::exec( first, last, l, args... ); + Algorithms::ParallelFor< Device >::exec( first, last, l ); } } @@ -218,11 +218,38 @@ template< typename Device, typename Index, ElementsOrganization Organization, int Alignment > - template< typename Function, typename... Args > + template< typename Function > +void EllpackView< Device, Index, Organization, Alignment >:: +forEachElement( Function&& f ) const +{ + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + ElementsOrganization Organization, + int Alignment > + template< typename Function > +void EllpackView< Device, Index, Organization, Alignment >:: +forSegments( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = this->getConstView(); + auto f = [=] __cuda_callable__ ( IndexType segmentIdx ) mutable { + auto segment = view.getSegmentView( segmentIdx ); + function( segment ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Device, + typename Index, + ElementsOrganization Organization, + int Alignment > + template< typename Function > void EllpackView< Device, Index, Organization, Alignment >:: -forEachElement( Function& f, Args... args ) const +forEachSegment( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forSegments( 0, this->getSegmentsCount(), f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/SegmentView.h b/src/TNL/Algorithms/Segments/SegmentView.h index a06017e6d..ecf1c95f6 100644 --- a/src/TNL/Algorithms/Segments/SegmentView.h +++ b/src/TNL/Algorithms/Segments/SegmentView.h @@ -28,17 +28,18 @@ class SegmentView< Index, ColumnMajorOrder > using IndexType = Index; __cuda_callable__ - SegmentView( const IndexType offset, + SegmentView( const IndexType segmentIdx, + const IndexType offset, const IndexType size, const IndexType step ) - : segmentOffset( offset ), segmentSize( size ), step( step ){}; + : segmentIdx( segmentIdx ), segmentOffset( offset ), segmentSize( size ), step( step ){}; __cuda_callable__ SegmentView( const SegmentView& view ) - : segmentOffset( view.segmentOffset ), segmentSize( view.segmentSize ), step( view.step ){}; + : segmentIdx( view.segmentIdx ), segmentOffset( view.segmentOffset ), segmentSize( view.segmentSize ), step( view.step ){}; __cuda_callable__ - IndexType getSize() const + const IndexType& getSize() const { return this->segmentSize; }; @@ -50,9 +51,15 @@ class SegmentView< Index, ColumnMajorOrder > return segmentOffset + localIndex * step; }; + __cuda_callable__ + const IndexType& getSegmentIndex() const + { + return this->segmentIdx; + }; + protected: - - IndexType segmentOffset, segmentSize, step; + + IndexType segmentIdx, segmentOffset, segmentSize, step; }; template< typename Index > @@ -63,13 +70,14 @@ class SegmentView< Index, RowMajorOrder > using IndexType = Index; __cuda_callable__ - SegmentView( const IndexType offset, + SegmentView( const IndexType segmentIdx, + const IndexType offset, const IndexType size, const IndexType step = 1 ) // For compatibility with previous specialization - : segmentOffset( offset ), segmentSize( size ){}; + : segmentIdx( segmentIdx ), segmentOffset( offset ), segmentSize( size ){}; __cuda_callable__ - IndexType getSize() const + const IndexType& getSize() const { return this->segmentSize; }; @@ -81,9 +89,15 @@ class SegmentView< Index, RowMajorOrder > return segmentOffset + localIndex; }; + __cuda_callable__ + const IndexType& getSegmentIndex() const + { + return this->segmentIdx; + }; + protected: - - IndexType segmentOffset, segmentSize; + + IndexType segmentIdx, segmentOffset, segmentSize; }; } //namespace Segments diff --git a/src/TNL/Algorithms/Segments/SlicedEllpack.h b/src/TNL/Algorithms/Segments/SlicedEllpack.h index 9b386c139..dce2ea389 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpack.h +++ b/src/TNL/Algorithms/Segments/SlicedEllpack.h @@ -93,12 +93,17 @@ class SlicedEllpack * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType first, IndexType last, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/SlicedEllpack.hpp b/src/TNL/Algorithms/Segments/SlicedEllpack.hpp index 7a0bf838f..373e729c8 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpack.hpp +++ b/src/TNL/Algorithms/Segments/SlicedEllpack.hpp @@ -275,9 +275,9 @@ getSegmentView( const IndexType segmentIdx ) const -> SegmentViewType const IndexType& segmentSize = this->sliceSegmentSizes[ sliceIdx ]; if( Organization == RowMajorOrder ) - return SegmentViewType( sliceOffset + segmentInSliceIdx * segmentSize, segmentSize, 1 ); + return SegmentViewType( segmentIdx, sliceOffset + segmentInSliceIdx * segmentSize, segmentSize, 1 ); else - return SegmentViewType( sliceOffset + segmentInSliceIdx, segmentSize, SliceSize ); + return SegmentViewType( segmentIdx, sliceOffset + segmentInSliceIdx, segmentSize, SliceSize ); } template< typename Device, @@ -285,12 +285,12 @@ template< typename Device, typename IndexAllocator, ElementsOrganization Organization, int SliceSize > - template< typename Function, typename... Args > + template< typename Function > void SlicedEllpack< Device, Index, IndexAllocator, Organization, SliceSize >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { - this->getConstView().forElements( first, last, f, args... ); + this->getConstView().forElements( first, last, f ); } template< typename Device, @@ -298,12 +298,38 @@ template< typename Device, typename IndexAllocator, ElementsOrganization Organization, int SliceSize > - template< typename Function, typename... Args > + template< typename Function > void SlicedEllpack< Device, Index, IndexAllocator, Organization, SliceSize >:: -forEachElement( Function& f, Args... args ) const +forEachElement( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization, + int SliceSize > + template< typename Function > +void +SlicedEllpack< Device, Index, IndexAllocator, Organization, SliceSize >:: +forSegments( IndexType begin, IndexType end, Function&& f ) const +{ + this->getConstView().forSegments( begin, end, f ); +} + +template< typename Device, + typename Index, + typename IndexAllocator, + ElementsOrganization Organization, + int SliceSize > + template< typename Function > +void +SlicedEllpack< Device, Index, IndexAllocator, Organization, SliceSize >:: +forEachSegment( Function&& f ) const +{ + this->getConstView().forEachSegment( f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/SlicedEllpackView.h b/src/TNL/Algorithms/Segments/SlicedEllpackView.h index e05e2df87..37169d365 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpackView.h +++ b/src/TNL/Algorithms/Segments/SlicedEllpackView.h @@ -36,7 +36,7 @@ class SlicedEllpackView template< typename Device_, typename Index_ > using ViewTemplate = SlicedEllpackView< Device_, Index_, Organization, SliceSize >; using ViewType = SlicedEllpackView; - using ConstViewType = ViewType; + using ConstViewType = SlicedEllpackView< Device, std::add_const_t< Index >, Organization, SliceSize >; using SegmentViewType = SegmentView< IndexType, Organization >; static constexpr bool havePadding() { return true; }; @@ -94,12 +94,17 @@ class SlicedEllpackView * When its true, the for-loop continues. Once 'f' returns false, the for-loop * is terminated. */ - template< typename Function, typename... Args > - void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; + template< typename Function > + void forElements( IndexType first, IndexType last, Function&& f ) const; - template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + template< typename Function > + void forEachElement( Function&& f ) const; + template< typename Function > + void forSegments( IndexType begin, IndexType end, Function&& f ) const; + + template< typename Function > + void forEachSegment( Function&& f ) const; /*** * \brief Go over all segments and perform a reduction in each of them. diff --git a/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp b/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp index 8ec4e237e..698324169 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp +++ b/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp @@ -217,25 +217,25 @@ getSegmentView( const IndexType segmentIdx ) const -> SegmentViewType const IndexType& segmentSize = this->sliceSegmentSizes[ sliceIdx ]; if( Organization == RowMajorOrder ) - return SegmentViewType( sliceOffset + segmentInSliceIdx * segmentSize, segmentSize, 1 ); + return SegmentViewType( segmentIdx, sliceOffset + segmentInSliceIdx * segmentSize, segmentSize, 1 ); else - return SegmentViewType( sliceOffset + segmentInSliceIdx, segmentSize, SliceSize ); + return SegmentViewType( segmentIdx, sliceOffset + segmentInSliceIdx, segmentSize, SliceSize ); } template< typename Device, typename Index, ElementsOrganization Organization, int SliceSize > - template< typename Function, typename... Args > + template< typename Function > void SlicedEllpackView< Device, Index, Organization, SliceSize >:: -forElements( IndexType first, IndexType last, Function& f, Args... args ) const +forElements( IndexType first, IndexType last, Function&& f ) const { const auto sliceSegmentSizes_view = this->sliceSegmentSizes.getConstView(); const auto sliceOffsets_view = this->sliceOffsets.getConstView(); if( Organization == RowMajorOrder ) { - auto l = [=] __cuda_callable__ ( const IndexType segmentIdx, Args... args ) mutable { + auto l = [=] __cuda_callable__ ( const IndexType segmentIdx ) mutable { const IndexType sliceIdx = segmentIdx / SliceSize; const IndexType segmentInSliceIdx = segmentIdx % SliceSize; const IndexType segmentSize = sliceSegmentSizes_view[ sliceIdx ]; @@ -246,19 +246,19 @@ forElements( IndexType first, IndexType last, Function& f, Args... args ) const for( IndexType globalIdx = begin; globalIdx < end && compute; globalIdx++ ) { // The following is a workaround of a bug in nvcc 11.2 -#if CUDART_VERSION == 11020 - f( segmentIdx, localIdx, globalIdx, compute, args... ); +#if CUDART_VERSION == 11020 + f( segmentIdx, localIdx, globalIdx, compute ); localIdx++; #else - f( segmentIdx, localIdx++, globalIdx, compute, args... ); + f( segmentIdx, localIdx++, globalIdx, compute ); #endif } }; - Algorithms::ParallelFor< Device >::exec( first, last, l, args... ); + Algorithms::ParallelFor< Device >::exec( first, last, l ); } else { - auto l = [=] __cuda_callable__ ( const IndexType segmentIdx, Args... args ) mutable { + auto l = [=] __cuda_callable__ ( const IndexType segmentIdx ) mutable { const IndexType sliceIdx = segmentIdx / SliceSize; const IndexType segmentInSliceIdx = segmentIdx % SliceSize; //const IndexType segmentSize = sliceSegmentSizes_view[ sliceIdx ]; @@ -269,15 +269,15 @@ forElements( IndexType first, IndexType last, Function& f, Args... args ) const for( IndexType globalIdx = begin; globalIdx < end && compute; globalIdx += SliceSize ) { // The following is a workaround of a bug in nvcc 11.2 -#if CUDART_VERSION == 11020 - f( segmentIdx, localIdx, globalIdx, compute, args... ); +#if CUDART_VERSION == 11020 + f( segmentIdx, localIdx, globalIdx, compute ); localIdx++; #else - f( segmentIdx, localIdx++, globalIdx, compute, args... ); + f( segmentIdx, localIdx++, globalIdx, compute ); #endif } }; - Algorithms::ParallelFor< Device >::exec( first, last, l, args... ); + Algorithms::ParallelFor< Device >::exec( first, last, l ); } } @@ -285,12 +285,41 @@ template< typename Device, typename Index, ElementsOrganization Organization, int SliceSize > - template< typename Function, typename... Args > + template< typename Function > +void +SlicedEllpackView< Device, Index, Organization, SliceSize >:: +forEachElement( Function&& f ) const +{ + this->forElements( 0, this->getSegmentsCount(), f ); +} + +template< typename Device, + typename Index, + ElementsOrganization Organization, + int SliceSize > + template< typename Function > +void +SlicedEllpackView< Device, Index, Organization, SliceSize >:: +forSegments( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = this->getConstView(); + auto f = [=] __cuda_callable__ ( IndexType segmentIdx ) mutable { + auto segment = view.getSegmentView( segmentIdx ); + function( segment ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Device, + typename Index, + ElementsOrganization Organization, + int SliceSize > + template< typename Function > void SlicedEllpackView< Device, Index, Organization, SliceSize >:: -forEachElement( Function& f, Args... args ) const +forEachSegment( Function&& f ) const { - this->forElements( 0, this->getSegmentsCount(), f, args... ); + this->forSegments( 0, this->getSegmentsCount(), f ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/details/BiEllpack.h b/src/TNL/Algorithms/Segments/details/BiEllpack.h index d4dfcd521..29551eb1d 100644 --- a/src/TNL/Algorithms/Segments/details/BiEllpack.h +++ b/src/TNL/Algorithms/Segments/details/BiEllpack.h @@ -36,7 +36,7 @@ class BiEllpack using ConstOffsetsHolderView = typename OffsetsHolderView::ConstViewType; using SegmentsSizes = OffsetsHolder; using SegmentViewType = BiEllpackSegmentView< IndexType, Organization >; - + static constexpr int getWarpSize() { return WarpSize; }; static constexpr int getLogWarpSize() { return std::log2( WarpSize ); }; @@ -90,7 +90,6 @@ class BiEllpack return groupPointers[ groupOffset + 1 ] - groupPointers[ groupOffset ]; } - static IndexType getGroupSize( const ConstOffsetsHolderView& groupPointers, const IndexType strip, const IndexType group ) @@ -237,7 +236,8 @@ class BiEllpack groupHeight /= 2; //std::cerr << " ROW INIT: groupIdx = " << i << " groupSize = " << groupSize << " groupWidth = " << groupsWidth[ i ] << std::endl; } - return SegmentViewType( groupPointers[ groupIdx ], + return SegmentViewType( segmentIdx, + groupPointers[ groupIdx ], inStripIdx, groupsWidth ); } @@ -261,7 +261,8 @@ class BiEllpack groupsWidth[ i ] = groupSize / groupHeight; groupHeight /= 2; } - return SegmentViewType( groupPointers[ groupIdx ], + return SegmentViewType( segmentIdx, + groupPointers[ groupIdx ], inStripIdx, groupsWidth ); } diff --git a/src/TNL/Algorithms/Segments/details/ChunkedEllpack.h b/src/TNL/Algorithms/Segments/details/ChunkedEllpack.h index af9cbc55e..41e4ca415 100644 --- a/src/TNL/Algorithms/Segments/details/ChunkedEllpack.h +++ b/src/TNL/Algorithms/Segments/details/ChunkedEllpack.h @@ -52,10 +52,9 @@ struct ChunkedEllpackSliceInfo Index pointer; }; - template< typename Index, typename Device, - ElementsOrganization Organization = Algorithms::Segments::DefaultElementsOrganization< Device >::getOrganization() > + ElementsOrganization Organization > class ChunkedEllpack { public: @@ -170,8 +169,8 @@ class ChunkedEllpack SegmentViewType getSegmentViewDirect( const OffsetsHolderView& segmentsToSlicesMapping, const ChunkedEllpackSliceInfoContainerView& slices, const OffsetsHolderView& segmentsToChunksMapping, - const IndexType chunksInSlice, - const IndexType segmentIdx ) + const IndexType& chunksInSlice, + const IndexType& segmentIdx ) { const IndexType& sliceIndex = segmentsToSlicesMapping[ segmentIdx ]; IndexType firstChunkOfSegment( 0 ); @@ -185,12 +184,14 @@ class ChunkedEllpack const IndexType segmentSize = segmentChunksCount * chunkSize; if( Organization == RowMajorOrder ) - return SegmentViewType( sliceOffset + firstChunkOfSegment * chunkSize, + return SegmentViewType( segmentIdx, + sliceOffset + firstChunkOfSegment * chunkSize, segmentSize, chunkSize, chunksInSlice ); else - return SegmentViewType( sliceOffset + firstChunkOfSegment, + return SegmentViewType( segmentIdx, + sliceOffset + firstChunkOfSegment, segmentSize, chunkSize, chunksInSlice ); @@ -215,12 +216,14 @@ class ChunkedEllpack const IndexType segmentSize = segmentChunksCount * chunkSize; if( Organization == RowMajorOrder ) - return SegmentViewType( sliceOffset + firstChunkOfSegment * chunkSize, + return SegmentViewType( segmentIdx, + sliceOffset + firstChunkOfSegment * chunkSize, segmentSize, chunkSize, chunksInSlice ); else - return SegmentViewType( sliceOffset + firstChunkOfSegment, + return SegmentViewType( segmentIdx, + sliceOffset + firstChunkOfSegment, segmentSize, chunkSize, chunksInSlice ); diff --git a/src/TNL/Matrices/DistributedMatrix.h b/src/TNL/Matrices/DistributedMatrix.h index 2deed3abf..778e1a9c3 100644 --- a/src/TNL/Matrices/DistributedMatrix.h +++ b/src/TNL/Matrices/DistributedMatrix.h @@ -36,8 +36,8 @@ public: using RowsCapacitiesType = Containers::DistributedVector< IndexType, DeviceType, IndexType >; - using MatrixRow = typename Matrix::RowView; - using ConstMatrixRow = typename Matrix::ConstRowView; + using MatrixRow = typename Matrix::RowViewType; + using ConstMatrixRow = typename Matrix::ConstRowViewType; template< typename _Real = RealType, typename _Device = DeviceType, diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 0e2b091a4..cc7392d8f 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -151,12 +151,12 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Type for accessing matrix rows. */ - using RowView = SparseMatrixRowView< typename SegmentsType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; + using RowViewType = typename ViewType::RowViewType; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowView = typename RowView::ConstViewType; + using ConstRowViewType = typename ViewType::ConstRowViewType; /** * \brief Helper type for getting self type or its modifications. @@ -502,7 +502,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref SparseMatrixRowView. */ __cuda_callable__ - const ConstRowView getRow( const IndexType& rowIdx ) const; + const ConstRowViewType getRow( const IndexType& rowIdx ) const; /** * \brief Non-constant getter of simple structure for accessing given matrix row. @@ -519,7 +519,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref SparseMatrixRowView. */ __cuda_callable__ - RowView getRow( const IndexType& rowIdx ); + RowViewType getRow( const IndexType& rowIdx ); /** * \brief Sets element at given \e row and \e column to given \e value. @@ -708,45 +708,55 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \brief Method for iteration over all matrix rows for constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. - * It is should have form like - * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. - * The \e localIdx parameter is a rank of the non-zero element in given row. - * If the 'compute' variable is set to false the iteration over the row can - * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. * + * The lambda function `function` should be declared like follows: + * + * ``` + * function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute ) + * ``` + * + * The \e localIdx parameter is a rank of the non-zero element in given row. + * If the 'compute' variable is set to false the iteration over the row can + * be interrupted. + * * \par Example * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp * \par Output * \include SparseMatrixExample_forRows.out */ template< typename Function > - void forElements( IndexType begin, IndexType end, Function& function ) const; + void forElements( IndexType begin, IndexType end, Function&& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. - * It is should have form like - * `function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute )`. - * The \e localIdx parameter is a rank of the non-zero element in given row. - * If the 'compute' variable is set to false the iteration over the row can - * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. * \param end defines ending of the range [begin,end) of rows to be processed. * \param function is an instance of the lambda function to be called in each row. * + * The lambda function `function` should be declared like follows: + * + * ``` + * function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute ) + * ``` + * + * The \e localIdx parameter is a rank of the non-zero element in given row. + * If the 'compute' variable is set to false the iteration over the row can + * be interrupted. + * * \par Example * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp * \par Output * \include SparseMatrixExample_forRows.out */ template< typename Function > - void forElements( IndexType begin, IndexType end, Function& function ); + void forElements( IndexType begin, IndexType end, Function&& function ); /** * \brief This method calls \e forElements for all matrix rows (for constant instances). @@ -762,7 +772,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include SparseMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forEachElement( Function&& function ) const; /** * \brief This method calls \e forElements for all matrix rows. @@ -778,7 +788,19 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include SparseMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forEachElement( Function&& function ); + + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + template< typename Function > + void forEachRow( Function&& function ); + + template< typename Function > + void forEachRow( Function&& function ) const; /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 1c4524d3f..f41feafa0 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -434,7 +434,7 @@ template< typename Real, typename IndexAllocator > __cuda_callable__ auto SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) const -> const ConstRowView +getRow( const IndexType& rowIdx ) const -> const ConstRowViewType { return this->view.getRow( rowIdx ); } @@ -449,7 +449,7 @@ template< typename Real, typename IndexAllocator > __cuda_callable__ auto SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) -> RowView +getRow( const IndexType& rowIdx ) -> RowViewType { return this->view.getRow( rowIdx ); } @@ -603,7 +603,7 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forElements( IndexType begin, IndexType end, Function& function ) const +forElements( IndexType begin, IndexType end, Function&& function ) const { this->view.forElements( begin, end, function ); } @@ -619,7 +619,7 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forElements( IndexType begin, IndexType end, Function& function ) +forElements( IndexType begin, IndexType end, Function&& function ) { this->view.forElements( begin, end, function ); } @@ -635,7 +635,7 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forEachElement( Function& function ) const +forEachElement( Function&& function ) const { this->forElements( 0, this->getRows(), function ); } @@ -651,11 +651,75 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forEachElement( Function& function ) +forEachElement( Function&& function ) { this->forElements( 0, this->getRows(), function ); } +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename ComputeReal, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + this->getView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename ComputeReal, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + this->getConstView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename ComputeReal, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: +forEachRow( Function&& function ) +{ + this->getView().forEachRow( function ); +} + +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename, typename > class Segments, + typename ComputeReal, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: +forEachRow( Function&& function ) const +{ + this->getConsView().forEachRow( function ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/SparseMatrixRowView.h b/src/TNL/Matrices/SparseMatrixRowView.h index 84da4e064..91381434f 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.h +++ b/src/TNL/Matrices/SparseMatrixRowView.h @@ -80,10 +80,16 @@ class SparseMatrixRowView */ using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; + /** + * \brief Type of sparse matrix row view. + */ + using RowViewType = SparseMatrixRowView< SegmentView, ValuesViewType, ColumnsIndexesViewType, isBinary_ >; + + /** * \brief Type of constant sparse matrix row view. */ - using ConstViewType = SparseMatrixRowView< SegmentView, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary_ >; + using ConstRowViewType = SparseMatrixRowView< SegmentView, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary_ >; /** * \brief Tells whether the parent matrix is a binary matrix. @@ -111,6 +117,14 @@ class SparseMatrixRowView __cuda_callable__ IndexType getSize() const; + /** + * \brief Returns the matrix row index. + * + * \return matrix row index. + */ + __cuda_callable__ + const IndexType& getRowIndex(); + /** * \brief Returns constants reference to a column index of an element with given rank in the row. * @@ -169,7 +183,7 @@ class SparseMatrixRowView */ __cuda_callable__ void setColumnIndex( const IndexType localIdx, - const RealType& columnIndex ); + const IndexType& columnIndex ); /** * \brief Sets both a value and a column index of matrix element with given rank in the matrix row. @@ -198,6 +212,7 @@ class SparseMatrixRowView __cuda_callable__ bool operator==( const SparseMatrixRowView< _SegmentView, _ValuesView, _ColumnsIndexesView, _isBinary >& other ) const; + protected: SegmentViewType segmentView; diff --git a/src/TNL/Matrices/SparseMatrixRowView.hpp b/src/TNL/Matrices/SparseMatrixRowView.hpp index a0a9c2604..c31da8e70 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.hpp +++ b/src/TNL/Matrices/SparseMatrixRowView.hpp @@ -40,6 +40,18 @@ getSize() const -> IndexType return segmentView.getSize(); } +template< typename SegmentView, + typename ValuesView, + typename ColumnsIndexesView, + bool isBinary_ > +__cuda_callable__ +auto +SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >:: +getRowIndex() -> const IndexType& +{ + return segmentView.getSegmentIndex(); +} + template< typename SegmentView, typename ValuesView, typename ColumnsIndexesView, @@ -106,6 +118,20 @@ setValue( const IndexType localIdx, } } +template< typename SegmentView, + typename ValuesView, + typename ColumnsIndexesView, + bool isBinary_ > +__cuda_callable__ void +SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >:: +setColumnIndex( const IndexType localIdx, + const IndexType& columnIndex ) +{ + TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." ); + const IndexType globalIdx = segmentView.getGlobalIndex( localIdx ); + this->columnIndexes[ globalIdx ] = columnIndex; +} + template< typename SegmentView, typename ValuesView, typename ColumnsIndexesView, diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index a74dab43f..3e81cc787 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -135,12 +135,12 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing matrix rows. */ - using RowView = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; + using RowViewType = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowView = typename RowView::ConstViewType; + using ConstRowViewType = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary() >;; /** * \brief Helper type for getting self type or its modifications. @@ -293,7 +293,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * See \ref SparseMatrixRowView. */ __cuda_callable__ - ConstRowView getRow( const IndexType& rowIdx ) const; + ConstRowViewType getRow( const IndexType& rowIdx ) const; /** * \brief Non-constant getter of simple structure for accessing given matrix row. @@ -310,7 +310,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * See \ref SparseMatrixRowView. */ __cuda_callable__ - RowView getRow( const IndexType& rowIdx ); + RowViewType getRow( const IndexType& rowIdx ); /** * \brief Sets element at given \e row and \e column to given \e value. @@ -570,6 +570,18 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > template< typename Function > void forEachElement( Function& function ); + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + template< typename Function > + void forEachRow( Function&& function ); + + template< typename Function > + void forEachRow( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index e7842a50a..9e3118b29 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -223,10 +223,10 @@ template< typename Real, typename ComputeReal > __cuda_callable__ auto SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -getRow( const IndexType& rowIdx ) const -> ConstRowView +getRow( const IndexType& rowIdx ) const -> ConstRowViewType { TNL_ASSERT_LT( rowIdx, this->getRows(), "Row index is larger than number of matrix rows." ); - return ConstRowView( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); + return ConstRowViewType( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); } template< typename Real, @@ -237,10 +237,10 @@ template< typename Real, typename ComputeReal > __cuda_callable__ auto SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -getRow( const IndexType& rowIdx ) -> RowView +getRow( const IndexType& rowIdx ) -> RowViewType { TNL_ASSERT_LT( rowIdx, this->getRows(), "Row index is larger than number of matrix rows." ); - return RowView( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); + return RowViewType( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); } template< typename Real, @@ -658,6 +658,76 @@ forEachElement( Function& function ) this->forElements( 0, this->getRows(), function ); } +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename > class SegmentsView, + typename ComputeReal > + template< typename Function > +void +SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + auto columns_view = this->columnIndexes.getView(); + auto values_view = this->values.getView(); + using SegmentViewType = typename SegmentsViewType::SegmentViewType; + auto f = [=] __cuda_callable__ ( SegmentViewType& segmentView ) mutable { + auto rowView = RowViewType( segmentView, values_view, columns_view ); + function( rowView ); + }; + this->segments.forSegments( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename > class SegmentsView, + typename ComputeReal > + template< typename Function > +void +SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + const auto columns_view = this->columnIndexes.getConstView(); + const auto values_view = this->values.getConstView(); + using SegmentViewType = typename SegmentsViewType::SegmentViewType; + auto f = [=] __cuda_callable__ ( SegmentViewType&& segmentView ) mutable { + const auto rowView = RowViewType( segmentView, values_view, columns_view ); + function( rowView ); + }; + this->segments.forSegments( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename > class SegmentsView, + typename ComputeReal > + template< typename Function > +void +SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: +forEachRow( Function&& function ) +{ + this->forRows( 0, this->getRows(), function ); +} + +template< typename Real, + typename Device, + typename Index, + typename MatrixType, + template< typename, typename > class SegmentsView, + typename ComputeReal > + template< typename Function > +void +SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: +forEachRow( Function&& function ) const +{ + this->forRows( 0, this->getRows(), function ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h b/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h index 1e2edb332..f504a8561 100644 --- a/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h +++ b/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h @@ -31,7 +31,7 @@ class SubentitySeedsCreator using LocalIndexType = typename MeshTraitsType::LocalIndexType; using EntityTraitsType = typename MeshTraitsType::template EntityTraits< EntityDimensionTag::value >; using EntityTopology = typename EntityTraitsType::EntityTopology; - using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowView; + using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowViewType; using SubentityTraits = typename MeshTraitsType::template SubentityTraits< EntityTopology, SubentityDimensionTag::value >; using SubentityTopology = typename SubentityTraits::SubentityTopology; @@ -82,7 +82,7 @@ class SubentitySeedsCreator< MeshConfig, EntityDimensionTag, DimensionTag< 0 > > using LocalIndexType = typename MeshTraitsType::LocalIndexType; using EntityTraitsType = typename MeshTraitsType::template EntityTraits< EntityDimensionTag::value >; using EntityTopology = typename EntityTraitsType::EntityTopology; - using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowView; + using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowViewType; using SubentityTraits = typename MeshTraitsType::template SubentityTraits< EntityTopology, 0 >; using SubentityTopology = typename SubentityTraits::SubentityTopology; diff --git a/src/UnitTests/Containers/ArrayViewTest.h b/src/UnitTests/Containers/ArrayViewTest.h index 97ddc3da8..06c005f50 100644 --- a/src/UnitTests/Containers/ArrayViewTest.h +++ b/src/UnitTests/Containers/ArrayViewTest.h @@ -48,7 +48,6 @@ std::ostream& operator<<( std::ostream& str, const MyData& v ) return str << v.data; } - // test fixture for typed tests template< typename View > class ArrayViewTest : public ::testing::Test diff --git a/src/UnitTests/Matrices/SparseMatrixTest.h b/src/UnitTests/Matrices/SparseMatrixTest.h index 928e8336c..f26012975 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.h +++ b/src/UnitTests/Matrices/SparseMatrixTest.h @@ -95,6 +95,20 @@ TYPED_TEST( MatrixTest, vectorProductTest ) test_VectorProduct< MatrixType >(); } +TYPED_TEST( MatrixTest, forElements ) +{ + using MatrixType = typename TestFixture::MatrixType; + + test_ForElements< MatrixType >(); +} + +TYPED_TEST( MatrixTest, forRows ) +{ + using MatrixType = typename TestFixture::MatrixType; + + test_ForRows< MatrixType >(); +} + TYPED_TEST( MatrixTest, rowsReduction ) { using MatrixType = typename TestFixture::MatrixType; diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 5c61606b5..2401d588f 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1391,6 +1391,72 @@ void test_VectorProduct() } } +template< typename Matrix > +void test_ForElements() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + /* + * Sets up the following 8x3 sparse matrix: + * + * / 1 1 1 \ + * | 2 2 2 | + * | 3 3 3 | + * | 4 4 4 | + * | 5 5 5 | + * | 6 6 6 | + * | 7 7 7 | + * \ 8 8 8 / + */ + + const IndexType cols = 3; + const IndexType rows = 8; + + Matrix m( { 3, 3, 3, 3, 3, 3, 3, 3, 3 }, cols ); + m.forEachElement( [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value, bool compute ) mutable { + value = rowIdx + 1.0; + columnIdx = localIdx; + } ); + + for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) + for( IndexType colIdx = 0; colIdx < cols; colIdx++ ) + EXPECT_EQ( m.getElement( rowIdx, colIdx ), rowIdx + 1.0 ); +} + +template< typename Matrix > +void test_ForRows() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + ///// + // Setup lower triangular matrix + const IndexType cols = 3; + const IndexType rows = 8; + + Matrix m( { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, cols ); + using RowViewType = typename Matrix::RowViewType; + m.forEachRow( [] __cuda_callable__ ( RowViewType& row ) mutable { + for( IndexType localIdx = 0; localIdx <= row.getRowIndex(); localIdx++ ) + { + row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); + row.setColumnIndex( localIdx, localIdx ); + } + } ); + + for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) + for( IndexType colIdx = 0; colIdx < cols; colIdx++ ) + { + if( colIdx <= rowIdx ) + EXPECT_EQ( m.getElement( rowIdx, colIdx ), rowIdx - colIdx + 1.0 ); + else + EXPECT_EQ( m.getElement( rowIdx, colIdx ), 0.0 ); + } +} + template< typename Matrix > void test_RowsReduction() { @@ -1414,8 +1480,7 @@ void test_RowsReduction() const IndexType rows = 8; const IndexType cols = 8; - Matrix m; - m.setDimensions( rows, cols ); + Matrix m( rows, cols ); typename Matrix::RowsCapacitiesType rowsCapacities{ 6, 3, 4, 5, 2, 7, 8, 8 }; m.setRowCapacities( rowsCapacities ); diff --git a/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h b/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h index be28b8ecf..0186f0776 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h +++ b/src/UnitTests/Matrices/SparseMatrixTest_ChunkedEllpack.h @@ -30,24 +30,24 @@ using ColumnMajorChunkedEllpack = TNL::Algorithms::Segments::ChunkedEllpack< Dev // types for which MatrixTest is instantiated using MatrixTypes = ::testing::Types < - TNL::Matrices::SparseMatrix< int, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< int, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< long, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< float, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< double, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< int, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< long, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< float, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< double, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + TNL::Matrices::SparseMatrix< int, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< int, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< long, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< float, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< double, TNL::Devices::Host, int, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< int, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< long, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< float, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< double, TNL::Devices::Host, long, TNL::Matrices::GeneralMatrix, RowMajorChunkedEllpack > #ifdef HAVE_CUDA - ,TNL::Matrices::SparseMatrix< int, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< long, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< float, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< double, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< int, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< long, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< float, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack >, - TNL::Matrices::SparseMatrix< double, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< int, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< long, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< float, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< double, TNL::Devices::Cuda, int, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< int, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< long, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< float, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > + ,TNL::Matrices::SparseMatrix< double, TNL::Devices::Cuda, long, TNL::Matrices::GeneralMatrix, ColumnMajorChunkedEllpack > #endif >; -- GitLab From ca7f000e718ffd5790170726b143352f97eca709 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Mar 2021 17:45:34 +0100 Subject: [PATCH 03/54] Fixing targets in CMakeLists in Containers examples. --- Documentation/Examples/Containers/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/Examples/Containers/CMakeLists.txt b/Documentation/Examples/Containers/CMakeLists.txt index 158149e3b..bd7d9b714 100644 --- a/Documentation/Examples/Containers/CMakeLists.txt +++ b/Documentation/Examples/Containers/CMakeLists.txt @@ -10,8 +10,8 @@ set( COMMON_EXAMPLES if( BUILD_CUDA ) foreach( target IN ITEMS ${COMMON_EXAMPLES} ) - cuda_add_executable( ${target} ${target}.cu OPTIONS ) - add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) endforeach() else() -- GitLab From c1f2d02df093ae0d0f6aa3cca3a9f0fd196ddc63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Mar 2021 17:46:26 +0100 Subject: [PATCH 04/54] Refactoring CMakeLists for sparse matrices examples. --- .../Matrices/SparseMatrix/CMakeLists.txt | 369 +++--------------- 1 file changed, 47 insertions(+), 322 deletions(-) diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index f0f62f49e..d5be9669e 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -1,326 +1,51 @@ -IF( BUILD_CUDA ) - CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_1_cuda SparseMatrixExample_Constructor_init_list_1.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_1_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_1.out - OUTPUT SparseMatrixExample_Constructor_init_list_1.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_2_cuda SparseMatrixExample_Constructor_init_list_2.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_2_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_2.out - OUTPUT SparseMatrixExample_Constructor_init_list_2.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_rowCapacities_vector_cuda SparseMatrixExample_Constructor_rowCapacities_vector.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_rowCapacities_vector_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_rowCapacities_vector.out - OUTPUT SparseMatrixExample_Constructor_rowCapacities_vector.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_std_map_cuda SparseMatrixExample_Constructor_std_map.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_std_map_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_std_map.out - OUTPUT SparseMatrixExample_Constructor_std_map.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_getSerializationType_cuda SparseMatrixExample_getSerializationType.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getSerializationType_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getSerializationType.out - OUTPUT SparseMatrixExample_getSerializationType.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setRowCapacities_cuda SparseMatrixExample_setRowCapacities.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setRowCapacities_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setRowCapacities.out - OUTPUT SparseMatrixExample_setRowCapacities.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_cuda SparseMatrixExample_setElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out - OUTPUT SparseMatrixExample_setElements.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_map_cuda SparseMatrixExample_setElements_map.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_map_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements_map.out - OUTPUT SparseMatrixExample_setElements_map.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths_cuda SparseMatrixExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out - OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_getConstRow_cuda SparseMatrixExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out - OUTPUT SparseMatrixExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_getRow_cuda SparseMatrixExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out - OUTPUT SparseMatrixExample_getRow.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement_cuda SparseMatrixExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out - OUTPUT SparseMatrixExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement_cuda SparseMatrixExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out - OUTPUT SparseMatrixExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_getElement_cuda SparseMatrixExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out - OUTPUT SparseMatrixExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_cuda SparseMatrixExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out - OUTPUT SparseMatrixExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction_cuda SparseMatrixExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out - OUTPUT SparseMatrixExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_forElements_cuda SparseMatrixExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forElements.out - OUTPUT SparseMatrixExample_forElements.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_forEachElement_cuda SparseMatrixExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forEachElement.out - OUTPUT SparseMatrixExample_forEachElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getSerializationType_cuda SparseMatrixViewExample_getSerializationType.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getSerializationType_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getSerializationType.out - OUTPUT SparseMatrixViewExample_getSerializationType.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths_cuda SparseMatrixViewExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out - OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow_cuda SparseMatrixViewExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out - OUTPUT SparseMatrixViewExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getRow_cuda SparseMatrixViewExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out - OUTPUT SparseMatrixViewExample_getRow.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement_cuda SparseMatrixViewExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out - OUTPUT SparseMatrixViewExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_addElement_cuda SparseMatrixViewExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out - OUTPUT SparseMatrixViewExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_getElement_cuda SparseMatrixViewExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out - OUTPUT SparseMatrixViewExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction_cuda SparseMatrixViewExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out - OUTPUT SparseMatrixViewExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction_cuda SparseMatrixViewExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out - OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forElements_cuda SparseMatrixViewExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forElements.out - OUTPUT SparseMatrixViewExample_forElements.out ) +set( COMMON_EXAMPLES + SparseMatrixExample_Constructor_init_list_1 + SparseMatrixExample_Constructor_init_list_2 + SparseMatrixExample_Constructor_rowCapacities_vector + SparseMatrixExample_Constructor_std_map + SparseMatrixExample_getSerializationType + SparseMatrixExample_setRowCapacities + SparseMatrixExample_setElements + SparseMatrixExample_setElements_map + SparseMatrixExample_getCompressedRowLengths + SparseMatrixExample_getConstRow + SparseMatrixExample_getRow + SparseMatrixExample_setElement + SparseMatrixExample_addElement + SparseMatrixExample_getElement + SparseMatrixExample_rowsReduction + SparseMatrixExample_allRowsReduction + SparseMatrixExample_forElements + SparseMatrixExample_forEachElement + SparseMatrixViewExample_getSerializationType + SparseMatrixViewExample_getCompressedRowLengths + SparseMatrixViewExample_getConstRow + SparseMatrixViewExample_getRow + SparseMatrixViewExample_setElement + SparseMatrixViewExample_addElement + SparseMatrixViewExample_getElement + SparseMatrixViewExample_rowsReduction + SparseMatrixViewExample_allRowsReduction + SparseMatrixViewExample_forElements + SparseMatrixViewExample_forEachElement +) - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_forEachElement_cuda SparseMatrixViewExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forEachElement.out - OUTPUT SparseMatrixViewExample_forEachElement.out ) +if( BUILD_CUDA ) + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) + endforeach() +else() + foreach( target IN ITEMS ${HOST_EXAMPLES} ) + add_executable( ${target} ${target}.cpp ) + add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) + endforeach() +endif() +IF( BUILD_CUDA ) + ADD_CUSTOM_TARGET( RunSparseMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() - ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_1 SparseMatrixExample_Constructor_init_list_1.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_1 > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_1.out - OUTPUT SparseMatrixExample_Constructor_init_list_1.out ) - - ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_2 SparseMatrixExample_Constructor_init_list_2.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_2 > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_2.out - OUTPUT SparseMatrixExample_Constructor_init_list_2.out ) - - ADD_EXECUTABLE( SparseMatrixExample_Constructor_rowCapacities_vector SparseMatrixExample_Constructor_rowCapacities_vector.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_rowCapacities_vector > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_rowCapacities_vector.out - OUTPUT SparseMatrixExample_Constructor_rowCapacities_vector.out ) - - ADD_EXECUTABLE( SparseMatrixExample_Constructor_std_map SparseMatrixExample_Constructor_std_map.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_std_map > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_std_map.out - OUTPUT SparseMatrixExample_Constructor_std_map.out ) - - ADD_EXECUTABLE( SparseMatrixExample_getSerializationType SparseMatrixExample_getSerializationType.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getSerializationType > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getSerializationType.out - OUTPUT SparseMatrixExample_getSerializationType.out ) - - ADD_EXECUTABLE( SparseMatrixExample_setRowCapacities SparseMatrixExample_setRowCapacities.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setRowCapacities > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setRowCapacities.out - OUTPUT SparseMatrixExample_setRowCapacities.out ) - - ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out - OUTPUT SparseMatrixExample_setElements.out ) - - ADD_EXECUTABLE( SparseMatrixExample_setElements_map SparseMatrixExample_setElements_map.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_map > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements_map.out - OUTPUT SparseMatrixExample_setElements_map.out ) - - ADD_EXECUTABLE( SparseMatrixExample_getCompressedRowLengths SparseMatrixExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getCompressedRowLengths.out - OUTPUT SparseMatrixExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( SparseMatrixExample_getConstRow SparseMatrixExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getConstRow.out - OUTPUT SparseMatrixExample_getConstRow.out ) - - ADD_EXECUTABLE( SparseMatrixExample_getRow SparseMatrixExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getRow.out - OUTPUT SparseMatrixExample_getRow.out ) - - ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out - OUTPUT SparseMatrixExample_setElement.out ) - - ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out - OUTPUT SparseMatrixExample_addElement.out ) - - ADD_EXECUTABLE( SparseMatrixExample_getElement SparseMatrixExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_getElement.out - OUTPUT SparseMatrixExample_getElement.out ) - - ADD_EXECUTABLE( SparseMatrixExample_rowsReduction SparseMatrixExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction.out - OUTPUT SparseMatrixExample_rowsReduction.out ) - - ADD_EXECUTABLE( SparseMatrixExample_allRowsReduction SparseMatrixExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_allRowsReduction.out - OUTPUT SparseMatrixExample_allRowsReduction.out ) - - ADD_EXECUTABLE( SparseMatrixExample_forElements SparseMatrixExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forElements.out - OUTPUT SparseMatrixExample_forElements.out ) - - ADD_EXECUTABLE( SparseMatrixExample_forEachElement SparseMatrixExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forEachElement.out - OUTPUT SparseMatrixExample_forEachElement.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_getSerializationType SparseMatrixViewExample_getSerializationType.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getSerializationType > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getSerializationType.out - OUTPUT SparseMatrixViewExample_getSerializationType.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getCompressedRowLengths.out - OUTPUT SparseMatrixViewExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_getConstRow SparseMatrixViewExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getConstRow.out - OUTPUT SparseMatrixViewExample_getConstRow.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_getRow SparseMatrixViewExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getRow.out - OUTPUT SparseMatrixViewExample_getRow.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out - OUTPUT SparseMatrixViewExample_setElement.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_addElement SparseMatrixViewExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_addElement.out - OUTPUT SparseMatrixViewExample_addElement.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_getElement SparseMatrixViewExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_getElement.out - OUTPUT SparseMatrixViewExample_getElement.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_rowsReduction.out - OUTPUT SparseMatrixViewExample_rowsReduction.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_allRowsReduction.out - OUTPUT SparseMatrixViewExample_allRowsReduction.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_forElements SparseMatrixViewExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forElements.out - OUTPUT SparseMatrixViewExample_forElements.out ) - - ADD_EXECUTABLE( SparseMatrixViewExample_forEachElement SparseMatrixViewExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_forEachElement.out - OUTPUT SparseMatrixViewExample_forEachElement.out ) - + ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) ENDIF() - -ADD_CUSTOM_TARGET( RunSparseMatricesExamples ALL DEPENDS - SparseMatrixExample_Constructor_init_list_1.out - SparseMatrixExample_Constructor_init_list_2.out - SparseMatrixExample_Constructor_rowCapacities_vector.out - SparseMatrixExample_Constructor_std_map.out - SparseMatrixExample_getSerializationType.out - SparseMatrixExample_setRowCapacities.out - SparseMatrixExample_setElements.out - SparseMatrixExample_setElements_map.out - SparseMatrixExample_getCompressedRowLengths.out - SparseMatrixExample_getConstRow.out - SparseMatrixExample_getRow.out - SparseMatrixExample_setElement.out - SparseMatrixExample_addElement.out - SparseMatrixExample_getElement.out - SparseMatrixExample_rowsReduction.out - SparseMatrixExample_allRowsReduction.out - SparseMatrixExample_forElements.out - SparseMatrixExample_forEachElement.out - SparseMatrixViewExample_getSerializationType.out - SparseMatrixViewExample_getCompressedRowLengths.out - SparseMatrixViewExample_getConstRow.out - SparseMatrixViewExample_getRow.out - SparseMatrixViewExample_setElement.out - SparseMatrixViewExample_addElement.out - SparseMatrixViewExample_getElement.out - SparseMatrixViewExample_rowsReduction.out - SparseMatrixViewExample_allRowsReduction.out - SparseMatrixViewExample_forElements.out - SparseMatrixViewExample_forEachElement.out -) - -- GitLab From 4351c45fb7303e903af201c0080fd993ef8eefc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Mar 2021 22:04:58 +0100 Subject: [PATCH 05/54] Added documentation and examples for SparseMatrix[,View]::forRows. --- .../Matrices/SparseMatrix/CMakeLists.txt | 2 + .../SparseMatrixExample_forRows.cpp | 37 +++++ .../SparseMatrixExample_forRows.cu | 1 + .../SparseMatrixViewExample_forRows.cpp | 38 ++++++ .../SparseMatrixViewExample_forRows.cu | 1 + src/TNL/Matrices/SparseMatrix.h | 128 +++++++++++++++--- src/TNL/Matrices/SparseMatrixView.h | 84 ++++++++++++ 7 files changed, 269 insertions(+), 22 deletions(-) create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index d5be9669e..48bef64aa 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -17,6 +17,7 @@ set( COMMON_EXAMPLES SparseMatrixExample_allRowsReduction SparseMatrixExample_forElements SparseMatrixExample_forEachElement + SparseMatrixExample_forRows SparseMatrixViewExample_getSerializationType SparseMatrixViewExample_getCompressedRowLengths SparseMatrixViewExample_getConstRow @@ -27,6 +28,7 @@ set( COMMON_EXAMPLES SparseMatrixViewExample_rowsReduction SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_forElements + SparseMatrixViewExample_forRows SparseMatrixViewExample_forEachElement ) diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp new file mode 100644 index 000000000..98d771cbe --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + using RowViewType = typename MatrixType::RowViewType; + MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + + auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { + for( int localIdx = 0; + localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements + localIdx++ ) // than we requested. These padding elements are processed here as well. + // and so we cannot use row.getSize() + { + row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); + row.setColumnIndex( localIdx, localIdx ); + } + }; + matrix.forEachRow( f ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu new file mode 120000 index 000000000..87c20fbe0 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cu @@ -0,0 +1 @@ +SparseMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..128083513 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + using RowViewType = typename MatrixType::ViewType::RowViewType; + MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + auto view = matrix.getView(); + + auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { + for( int localIdx = 0; + localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements + localIdx++ ) // than we requested. These padding elements are processed here as well. + // and so we cannot use row.getSize() + { + row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); + row.setColumnIndex( localIdx, localIdx ); + } + }; + view.forEachRow( f ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu new file mode 120000 index 000000000..5058dc6cf --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_forRows.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index cc7392d8f..147308915 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -705,18 +705,18 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** - * \brief Method for iteration over all matrix rows for constant instances. + * \brief Method for parallel iteration over matrix elements of given rows for constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. * - * \param begin defines beginning of the range [begin,end) of rows to be processed. - * \param end defines ending of the range [begin,end) of rows to be processed. - * \param function is an instance of the lambda function to be called in each row. + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for element of given rows. * * The lambda function `function` should be declared like follows: * * ``` - * function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute ) + * auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute ) { ... }; * ``` * * The \e localIdx parameter is a rank of the non-zero element in given row. @@ -724,26 +724,26 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * be interrupted. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_forElements.cpp * \par Output - * \include SparseMatrixExample_forRows.out + * \include SparseMatrixExample_forElements.out */ template< typename Function > void forElements( IndexType begin, IndexType end, Function&& function ) const; /** - * \brief Method for iteration over all matrix rows for non-constant instances. + * \brief Method for parallel iteration over all matrix elements of given rows for non-constant instances. * * \tparam Function is type of lambda function that will operate on matrix elements. * - * \param begin defines beginning of the range [begin,end) of rows to be processed. - * \param end defines ending of the range [begin,end) of rows to be processed. - * \param function is an instance of the lambda function to be called in each row. + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each element of given rows. * * The lambda function `function` should be declared like follows: * * ``` - * function( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute ) + * auto function = [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, const RealType& value, bool& compute ) mutable { ... } * ``` * * The \e localIdx parameter is a rank of the non-zero element in given row. @@ -751,54 +751,138 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * be interrupted. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_forElements.cpp * \par Output - * \include SparseMatrixExample_forRows.out + * \include SparseMatrixExample_forElements.out */ template< typename Function > void forElements( IndexType begin, IndexType end, Function&& function ); /** - * \brief This method calls \e forElements for all matrix rows (for constant instances). + * \brief Method for parallel iteration over all matrix elements for constant instances. * * See \ref SparseMatrix::forElements. * * \tparam Function is a type of lambda function that will operate on matrix elements. - * \param function is an instance of the lambda function to be called in each row. + * \param function is an instance of the lambda function to be called for each matrix element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_forElements.cpp * \par Output - * \include SparseMatrixExample_forAllRows.out + * \include SparseMatrixExample_forElements.out */ template< typename Function > void forEachElement( Function&& function ) const; /** - * \brief This method calls \e forElements for all matrix rows. + * \brief Method for parallel iteration over all matrix elements for non-constant instances. * * See \ref SparseMatrix::forElements. * * \tparam Function is a type of lambda function that will operate on matrix elements. - * \param function is an instance of the lambda function to be called in each row. + * \param function is an instance of the lambda function to be called for each matrix element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_forAllRows.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_forElements.cpp * \par Output - * \include SparseMatrixExample_forAllRows.out + * \include SparseMatrixExample_forElements.out */ template< typename Function > void forEachElement( Function&& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \par Output + * \include SparseMatrixExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function&& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \par Output + * \include SparseMatrixExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function&& function ) const; + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \par Output + * \include SparseMatrixExample_forRows.out + */ template< typename Function > void forEachRow( Function&& function ); + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp + * \par Output + * \include SparseMatrixExample_forRows.out + */ template< typename Function > void forEachRow( Function&& function ) const; diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 3e81cc787..05f063c89 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -570,15 +570,99 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > template< typename Function > void forEachElement( Function& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp + * \par Output + * \include SparseMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function&& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp + * \par Output + * \include SparseMatrixViewExample_forRows.out + */ template< typename Function > void forRows( IndexType begin, IndexType end, Function&& function ) const; + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp + * \par Output + * \include SparseMatrixViewExample_forRows.out + */ template< typename Function > void forEachRow( Function&& function ); + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * + * \par Example + * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp + * \par Output + * \include SparseMatrixViewExample_forRows.out + */ template< typename Function > void forEachRow( Function&& function ) const; -- GitLab From b712392859eb0f2d45fd8402b2d9ff25a46d0a98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 09:47:17 +0100 Subject: [PATCH 06/54] Fixing documentation for SparseMatrix[,View]::forRows. --- src/TNL/Matrices/SparseMatrix.h | 12 ++++++++---- src/TNL/Matrices/SparseMatrixView.h | 13 +++++++++---- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 147308915..212061125 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -793,7 +793,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrix::forElements where more than one thread can be mapped to each row. * * \tparam Function is type of the lambda function. * @@ -818,7 +819,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrix::forElements where more than one thread can be mapped to each row. * * \tparam Function is type of the lambda function. * @@ -843,7 +845,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for parallel iteration over all matrix rows. * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrix::forAllElements where more than one thread can be mapped to each row. * * \tparam Function is type of the lambda function. * @@ -866,7 +869,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Method for parallel iteration over all matrix rows for constant instances. * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrix::forAllElements where more than one thread can be mapped to each row. * * \tparam Function is type of the lambda function. * diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 05f063c89..efd771eca 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -573,7 +573,9 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrixView::forElements where more than one thread can be mapped to each row. + * * \tparam Function is type of the lambda function. * @@ -598,7 +600,8 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrixView::forElements where more than one thread can be mapped to each row. * * \tparam Function is type of the lambda function. * @@ -623,7 +626,8 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Method for parallel iteration over all matrix rows. * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrixView::forAllElements where more than one thread can be mapped to each row. * * \tparam Function is type of the lambda function. * @@ -646,7 +650,8 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Method for parallel iteration over all matrix rows for constant instances. * - * In each row, given lambda function is performed. + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref SparseMatrixView::forAllElements where more than one thread can be mapped to each row. * * \tparam Function is type of the lambda function. * -- GitLab From 360030b4880906fffbe3c2cd0e03e8b4b07db6cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 10:02:29 +0100 Subject: [PATCH 07/54] Renaming forEachRow -> forAllRows. --- .../Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp | 2 +- .../SparseMatrix/SparseMatrixViewExample_forRows.cpp | 2 +- src/TNL/Matrices/SparseMatrix.h | 4 ++-- src/TNL/Matrices/SparseMatrix.hpp | 8 ++++---- src/TNL/Matrices/SparseMatrixView.h | 4 ++-- src/TNL/Matrices/SparseMatrixView.hpp | 4 ++-- src/UnitTests/Matrices/SparseMatrixTest.hpp | 2 +- 7 files changed, 13 insertions(+), 13 deletions(-) diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp index 98d771cbe..0ddaa4d6c 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -20,7 +20,7 @@ void forRowsExample() row.setColumnIndex( localIdx, localIdx ); } }; - matrix.forEachRow( f ); + matrix.forAllRows( f ); std::cout << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp index 128083513..bb4145a54 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp @@ -21,7 +21,7 @@ void forRowsExample() row.setColumnIndex( localIdx, localIdx ); } }; - view.forEachRow( f ); + view.forAllRows( f ); std::cout << matrix << std::endl; } diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 212061125..e5f9fa277 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -864,7 +864,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include SparseMatrixExample_forRows.out */ template< typename Function > - void forEachRow( Function&& function ); + void forAllRows( Function&& function ); /** * \brief Method for parallel iteration over all matrix rows for constant instances. @@ -888,7 +888,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include SparseMatrixExample_forRows.out */ template< typename Function > - void forEachRow( Function&& function ) const; + void forAllRows( Function&& function ) const; /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index f41feafa0..87dea06c2 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -699,9 +699,9 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forEachRow( Function&& function ) +forAllRows( Function&& function ) { - this->getView().forEachRow( function ); + this->getView().forAllRows( function ); } template< typename Real, @@ -715,9 +715,9 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forEachRow( Function&& function ) const +forAllRows( Function&& function ) const { - this->getConsView().forEachRow( function ); + this->getConsView().forAllRows( function ); } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index efd771eca..5c1a71f50 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -645,7 +645,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \include SparseMatrixViewExample_forRows.out */ template< typename Function > - void forEachRow( Function&& function ); + void forAllRows( Function&& function ); /** * \brief Method for parallel iteration over all matrix rows for constant instances. @@ -669,7 +669,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \include SparseMatrixViewExample_forRows.out */ template< typename Function > - void forEachRow( Function&& function ) const; + void forAllRows( Function&& function ) const; /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 9e3118b29..a1e82d3f2 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -709,7 +709,7 @@ template< typename Real, template< typename Function > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -forEachRow( Function&& function ) +forAllRows( Function&& function ) { this->forRows( 0, this->getRows(), function ); } @@ -723,7 +723,7 @@ template< typename Real, template< typename Function > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -forEachRow( Function&& function ) const +forAllRows( Function&& function ) const { this->forRows( 0, this->getRows(), function ); } diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 2401d588f..0728549f4 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1439,7 +1439,7 @@ void test_ForRows() Matrix m( { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, cols ); using RowViewType = typename Matrix::RowViewType; - m.forEachRow( [] __cuda_callable__ ( RowViewType& row ) mutable { + m.forAllRows( [] __cuda_callable__ ( RowViewType& row ) mutable { for( IndexType localIdx = 0; localIdx <= row.getRowIndex(); localIdx++ ) { row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); -- GitLab From dd3d517b463d88938e22f22cb06e86e95d326c7c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 10:26:21 +0100 Subject: [PATCH 08/54] Renaming forEachElement -> forAllElements. --- .../Examples/Algorithms/ParallelForExample.cu | 4 +-- .../Containers/ArrayExample_forElements.cpp | 2 +- .../ArrayExample_reduceElements.cpp | 2 +- .../Examples/Containers/ArrayViewExample.cpp | 2 +- .../ArrayViewExample_forElements.cpp | 2 +- .../ArrayViewExample_reduceElements.cpp | 2 +- .../Matrices/DenseMatrix/CMakeLists.txt | 36 +++++++++---------- ... => DenseMatrixExample_forAllElements.cpp} | 8 ++--- .../DenseMatrixExample_forAllElements.cu | 1 + .../DenseMatrixExample_forEachElement.cu | 1 - ...DenseMatrixViewExample_forAllElements.cpp} | 8 ++--- .../DenseMatrixViewExample_forAllElements.cu | 1 + .../DenseMatrixViewExample_forEachElement.cu | 1 - .../Matrices/LambdaMatrix/CMakeLists.txt | 18 +++++----- ...=> LambdaMatrixExample_forAllElements.cpp} | 8 ++--- .../LambdaMatrixExample_forAllElements.cu | 1 + .../LambdaMatrixExample_forEachElement.cu | 1 - .../MultidiagonalMatrix/CMakeLists.txt | 36 +++++++++---------- ...idiagonalMatrixExample_forAllElements.cpp} | 8 ++--- ...ltidiagonalMatrixExample_forAllElements.cu | 1 + ...ltidiagonalMatrixExample_forEachElement.cu | 1 - ...gonalMatrixViewExample_forAllElements.cpp} | 8 ++--- ...iagonalMatrixViewExample_forAllElements.cu | 1 + ...iagonalMatrixViewExample_forEachElement.cu | 1 - .../Matrices/SparseMatrix/CMakeLists.txt | 4 +-- ...=> SparseMatrixExample_forAllElements.cpp} | 8 ++--- .../SparseMatrixExample_forAllElements.cu | 1 + .../SparseMatrixExample_forEachElement.cu | 1 - ...parseMatrixViewExample_forAllElements.cpp} | 8 ++--- .../SparseMatrixViewExample_forAllElements.cu | 1 + .../SparseMatrixViewExample_forEachElement.cu | 1 - .../Matrices/TridiagonalMatrix/CMakeLists.txt | 36 +++++++++---------- ...idiagonalMatrixExample_forAllElements.cpp} | 8 ++--- ...TridiagonalMatrixExample_forAllElements.cu | 1 + ...TridiagonalMatrixExample_forEachElement.cu | 1 - ...gonalMatrixViewExample_forAllElements.cpp} | 8 ++--- ...iagonalMatrixViewExample_forAllElements.cu | 1 + ...iagonalMatrixViewExample_forEachElement.cu | 1 - .../Tutorials/Arrays/ArrayViewForElements.cpp | 2 +- .../Tutorials/Arrays/ContainsValue.cpp | 2 +- .../Tutorials/Arrays/tutorial_Arrays.md | 2 +- .../ForLoops/ParallelForExample_ug.cpp | 4 +-- .../ReductionAndScan/ComparisonExample.cpp | 4 +-- .../ReductionAndScan/MapReduceExample-1.cpp | 2 +- .../ReductionAndScan/MaximumNormExample.cpp | 4 +-- .../ReductionWithArgument.cpp | 4 +-- .../ReductionAndScan/ScalarProductExample.cpp | 4 +-- .../Tutorials/Vectors/Expressions.cpp | 2 +- Documentation/Tutorials/Vectors/Reduction.cpp | 4 +-- src/TNL/Algorithms/Segments/BiEllpack.h | 2 +- src/TNL/Algorithms/Segments/BiEllpack.hpp | 4 +-- src/TNL/Algorithms/Segments/BiEllpackView.h | 2 +- src/TNL/Algorithms/Segments/BiEllpackView.hpp | 2 +- src/TNL/Algorithms/Segments/CSR.h | 2 +- src/TNL/Algorithms/Segments/CSR.hpp | 2 +- src/TNL/Algorithms/Segments/CSRView.h | 2 +- src/TNL/Algorithms/Segments/CSRView.hpp | 2 +- src/TNL/Algorithms/Segments/ChunkedEllpack.h | 2 +- .../Algorithms/Segments/ChunkedEllpack.hpp | 2 +- .../Algorithms/Segments/ChunkedEllpackView.h | 2 +- .../Segments/ChunkedEllpackView.hpp | 2 +- src/TNL/Algorithms/Segments/Ellpack.h | 2 +- src/TNL/Algorithms/Segments/Ellpack.hpp | 2 +- src/TNL/Algorithms/Segments/EllpackView.h | 2 +- src/TNL/Algorithms/Segments/EllpackView.hpp | 2 +- src/TNL/Algorithms/Segments/SlicedEllpack.h | 2 +- src/TNL/Algorithms/Segments/SlicedEllpack.hpp | 2 +- .../Algorithms/Segments/SlicedEllpackView.h | 2 +- .../Algorithms/Segments/SlicedEllpackView.hpp | 2 +- src/TNL/Algorithms/Segments/details/CSR.h | 2 +- src/TNL/Containers/Array.h | 4 +-- src/TNL/Containers/Array.hpp | 8 ++--- src/TNL/Containers/ArrayView.h | 4 +-- src/TNL/Containers/ArrayView.hpp | 4 +-- src/TNL/Matrices/DenseMatrix.h | 12 +++---- src/TNL/Matrices/DenseMatrix.hpp | 8 ++--- src/TNL/Matrices/DenseMatrixView.h | 12 +++---- src/TNL/Matrices/DenseMatrixView.hpp | 4 +-- src/TNL/Matrices/LambdaMatrix.h | 2 +- src/TNL/Matrices/LambdaMatrix.hpp | 2 +- src/TNL/Matrices/MultidiagonalMatrix.h | 10 +++--- src/TNL/Matrices/MultidiagonalMatrix.hpp | 6 ++-- src/TNL/Matrices/MultidiagonalMatrixView.h | 10 +++--- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 12 +++---- src/TNL/Matrices/SparseMatrix.h | 10 +++--- src/TNL/Matrices/SparseMatrix.hpp | 8 ++--- src/TNL/Matrices/SparseMatrixView.h | 10 +++--- src/TNL/Matrices/SparseMatrixView.hpp | 4 +-- src/TNL/Matrices/TridiagonalMatrix.h | 10 +++--- src/TNL/Matrices/TridiagonalMatrix.hpp | 8 ++--- src/TNL/Matrices/TridiagonalMatrixView.h | 10 +++--- src/TNL/Matrices/TridiagonalMatrixView.hpp | 10 +++--- .../Linear/Preconditioners/Diagonal_impl.h | 2 +- .../Algorithms/Segments/SegmentsTest.hpp | 2 +- src/UnitTests/Containers/ArrayTest.h | 2 +- src/UnitTests/Containers/ArrayViewTest.h | 2 +- src/UnitTests/Containers/VectorTest.h | 2 +- src/UnitTests/Matrices/SparseMatrixTest.hpp | 12 +++---- 98 files changed, 251 insertions(+), 251 deletions(-) rename Documentation/Examples/Matrices/DenseMatrix/{DenseMatrixExample_forEachElement.cpp => DenseMatrixExample_forAllElements.cpp} (80%) create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forEachElement.cu rename Documentation/Examples/Matrices/DenseMatrix/{DenseMatrixViewExample_forEachElement.cpp => DenseMatrixViewExample_forAllElements.cpp} (81%) create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forEachElement.cu rename Documentation/Examples/Matrices/LambdaMatrix/{LambdaMatrixExample_forEachElement.cpp => LambdaMatrixExample_forAllElements.cpp} (89%) create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forEachElement.cu rename Documentation/Examples/Matrices/MultidiagonalMatrix/{MultidiagonalMatrixExample_forEachElement.cpp => MultidiagonalMatrixExample_forAllElements.cpp} (91%) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forEachElement.cu rename Documentation/Examples/Matrices/MultidiagonalMatrix/{MultidiagonalMatrixViewExample_forEachElement.cpp => MultidiagonalMatrixViewExample_forAllElements.cpp} (92%) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forEachElement.cu rename Documentation/Examples/Matrices/SparseMatrix/{SparseMatrixExample_forEachElement.cpp => SparseMatrixExample_forAllElements.cpp} (85%) create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forEachElement.cu rename Documentation/Examples/Matrices/SparseMatrix/{SparseMatrixViewExample_forEachElement.cpp => SparseMatrixViewExample_forAllElements.cpp} (85%) create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forEachElement.cu rename Documentation/Examples/Matrices/TridiagonalMatrix/{TridiagonalMatrixExample_forEachElement.cpp => TridiagonalMatrixExample_forAllElements.cpp} (91%) create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forEachElement.cu rename Documentation/Examples/Matrices/TridiagonalMatrix/{TridiagonalMatrixViewExample_forEachElement.cpp => TridiagonalMatrixViewExample_forAllElements.cpp} (91%) create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllElements.cu delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forEachElement.cu diff --git a/Documentation/Examples/Algorithms/ParallelForExample.cu b/Documentation/Examples/Algorithms/ParallelForExample.cu index d63e71183..316ea3cb9 100644 --- a/Documentation/Examples/Algorithms/ParallelForExample.cu +++ b/Documentation/Examples/Algorithms/ParallelForExample.cu @@ -36,7 +36,7 @@ int main( int argc, char* argv[] ) */ Vector< double, Devices::Host > host_v1( 10 ), host_v2( 10 ), host_result( 10 ); host_v1 = 1.0; - host_v2.forEachElement( []__cuda_callable__ ( int i, double& v ) { v = i; } ); + host_v2.forAllElements( []__cuda_callable__ ( int i, double& v ) { v = i; } ); vectorSum( host_v1, host_v2, 2.0, host_result ); std::cout << "host_v1 = " << host_v1 << std::endl; std::cout << "host_v2 = " << host_v2 << std::endl; @@ -48,7 +48,7 @@ int main( int argc, char* argv[] ) #ifdef HAVE_CUDA Vector< double, Devices::Cuda > cuda_v1( 10 ), cuda_v2( 10 ), cuda_result( 10 ); cuda_v1 = 1.0; - cuda_v2.forEachElement( []__cuda_callable__ ( int i, double& v ) { v = i; } ); + cuda_v2.forAllElements( []__cuda_callable__ ( int i, double& v ) { v = i; } ); vectorSum( cuda_v1, cuda_v2, 2.0, cuda_result ); std::cout << "cuda_v1 = " << cuda_v1 << std::endl; std::cout << "cuda_v2 = " << cuda_v2 << std::endl; diff --git a/Documentation/Examples/Containers/ArrayExample_forElements.cpp b/Documentation/Examples/Containers/ArrayExample_forElements.cpp index ba29b8361..dd58f87c7 100644 --- a/Documentation/Examples/Containers/ArrayExample_forElements.cpp +++ b/Documentation/Examples/Containers/ArrayExample_forElements.cpp @@ -17,7 +17,7 @@ void forElementsExample() /**** * Initiate the elements of array `a` */ - a.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); + a.forAllElements( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); /**** * Initiate elements of array `b` with indexes 0-4 using `a_view` diff --git a/Documentation/Examples/Containers/ArrayExample_reduceElements.cpp b/Documentation/Examples/Containers/ArrayExample_reduceElements.cpp index b847d0620..bdf943732 100644 --- a/Documentation/Examples/Containers/ArrayExample_reduceElements.cpp +++ b/Documentation/Examples/Containers/ArrayExample_reduceElements.cpp @@ -17,7 +17,7 @@ void reduceElementsExample() /**** * Initiate the elements of array `a` */ - a.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); + a.forAllElements( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); /**** * Sum all elements of array `a` diff --git a/Documentation/Examples/Containers/ArrayViewExample.cpp b/Documentation/Examples/Containers/ArrayViewExample.cpp index 8103f8b3d..fdc1897c5 100644 --- a/Documentation/Examples/Containers/ArrayViewExample.cpp +++ b/Documentation/Examples/Containers/ArrayViewExample.cpp @@ -45,7 +45,7 @@ void arrayViewExample() ArrayType a3( size ); ViewType a3_view = a3.getView(); auto f1 = [] __cuda_callable__ ( IndexType i, int& value ) { value = 2 * i; }; - a3_view.forEachElement( f1 ); + a3_view.forAllElements( f1 ); for( int i = 0; i < size; i++ ) if( a3_view.getElement( i ) != 2 * i ) diff --git a/Documentation/Examples/Containers/ArrayViewExample_forElements.cpp b/Documentation/Examples/Containers/ArrayViewExample_forElements.cpp index f01c2972f..9a9966318 100644 --- a/Documentation/Examples/Containers/ArrayViewExample_forElements.cpp +++ b/Documentation/Examples/Containers/ArrayViewExample_forElements.cpp @@ -18,7 +18,7 @@ void forElementsExample() * Create an ArrayView and use it for initiation of elements of array `a` */ auto a_view = a.getView(); - a_view.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); + a_view.forAllElements( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); /**** * Initiate elements of array `b` with indexes 0-4 using `a_view` diff --git a/Documentation/Examples/Containers/ArrayViewExample_reduceElements.cpp b/Documentation/Examples/Containers/ArrayViewExample_reduceElements.cpp index ed767c7db..1357ac8d0 100644 --- a/Documentation/Examples/Containers/ArrayViewExample_reduceElements.cpp +++ b/Documentation/Examples/Containers/ArrayViewExample_reduceElements.cpp @@ -18,7 +18,7 @@ void reduceElementsExample() /**** * Initiate the elements of array `a` */ - a_view.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); + a_view.forAllElements( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); /**** * Sum all elements of array `a` diff --git a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt index 0f87cdc6e..c9d549188 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt @@ -59,10 +59,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forElements.out OUTPUT DenseMatrixExample_forElements.out ) - CUDA_ADD_EXECUTABLE( DenseMatrixExample_forEachElement_cuda DenseMatrixExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forEachElement.out - OUTPUT DenseMatrixExample_forEachElement.out ) + CUDA_ADD_EXECUTABLE( DenseMatrixExample_forAllElements_cuda DenseMatrixExample_forAllElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forAllElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forAllElements.out + OUTPUT DenseMatrixExample_forAllElements.out ) CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_constructor_cuda DenseMatrixViewExample_constructor.cu ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_constructor_cuda > @@ -119,10 +119,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forElements.out OUTPUT DenseMatrixViewExample_forElements.out ) - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_forEachElement_cuda DenseMatrixViewExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forEachElement.out - OUTPUT DenseMatrixViewExample_forEachElement.out ) + CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_forAllElements_cuda DenseMatrixViewExample_forAllElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forAllElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forAllElements.out + OUTPUT DenseMatrixViewExample_forAllElements.out ) ELSE() ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cpp ) @@ -185,10 +185,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forElements.out OUTPUT DenseMatrixExample_forElements.out ) - ADD_EXECUTABLE( DenseMatrixExample_forEachElement DenseMatrixExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forEachElement.out - OUTPUT DenseMatrixExample_forEachElement.out ) + ADD_EXECUTABLE( DenseMatrixExample_forAllElements DenseMatrixExample_forAllElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forAllElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forAllElements.out + OUTPUT DenseMatrixExample_forAllElements.out ) ADD_EXECUTABLE( DenseMatrixViewExample_constructor DenseMatrixViewExample_constructor.cpp ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_constructor > @@ -245,10 +245,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forElements.out OUTPUT DenseMatrixViewExample_forElements.out ) - ADD_EXECUTABLE( DenseMatrixViewExample_forEachElement DenseMatrixViewExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forEachElement.out - OUTPUT DenseMatrixViewExample_forEachElement.out ) + ADD_EXECUTABLE( DenseMatrixViewExample_forAllElements DenseMatrixViewExample_forAllElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forAllElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forAllElements.out + OUTPUT DenseMatrixViewExample_forAllElements.out ) ENDIF() @@ -265,7 +265,7 @@ ADD_CUSTOM_TARGET( RunDenseMatricesExamples ALL DEPENDS DenseMatrixExample_rowsReduction.out DenseMatrixExample_allRowsReduction.out DenseMatrixExample_forElements.out - DenseMatrixExample_forEachElement.out + DenseMatrixExample_forAllElements.out DenseMatrixViewExample_constructor.out DenseMatrixViewExample_getCompressedRowLengths.out DenseMatrixViewExample_getElementsCount.out @@ -277,7 +277,7 @@ ADD_CUSTOM_TARGET( RunDenseMatricesExamples ALL DEPENDS DenseMatrixViewExample_rowsReduction.out DenseMatrixViewExample_allRowsReduction.out DenseMatrixViewExample_forElements.out - DenseMatrixViewExample_forEachElement.out + DenseMatrixViewExample_forAllElements.out ) diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forEachElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllElements.cpp similarity index 80% rename from Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forEachElement.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllElements.cpp index 8b205e824..4fd7d3b47 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); @@ -15,17 +15,17 @@ void forEachElementExample() value = rowIdx + columnIdx; }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllElements.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllElements.cu new file mode 120000 index 000000000..e2c1d6f8a --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forAllElements.cu @@ -0,0 +1 @@ +DenseMatrixExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forEachElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forEachElement.cu deleted file mode 120000 index 8d658cfdb..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forEachElement.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllElements.cpp similarity index 81% rename from Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forEachElement.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllElements.cpp index d2eae02e0..66b394130 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); auto matrixView = matrix.getView(); @@ -16,17 +16,17 @@ void forEachElementExample() value = rowIdx + columnIdx; }; - matrixView.forEachElement( f ); + matrixView.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllElements.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllElements.cu new file mode 120000 index 000000000..2485aa620 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forAllElements.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forEachElement.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forEachElement.cu deleted file mode 120000 index 1094e7baa..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixViewExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt index 49a39b7fb..e295c3fcc 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt @@ -39,10 +39,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forElements.out OUTPUT LambdaMatrixExample_forElements.out ) - CUDA_ADD_EXECUTABLE( LambdaMatrixExample_forEachElement_cuda LambdaMatrixExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forEachElement.out - OUTPUT LambdaMatrixExample_forEachElement.out ) + CUDA_ADD_EXECUTABLE( LambdaMatrixExample_forAllElements_cuda LambdaMatrixExample_forAllElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forAllElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forAllElements.out + OUTPUT LambdaMatrixExample_forAllElements.out ) ELSE() ADD_EXECUTABLE( LambdaMatrixExample_Laplace LambdaMatrixExample_Laplace.cpp ) @@ -70,10 +70,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forElements.out OUTPUT LambdaMatrixExample_forElements.out ) - ADD_EXECUTABLE( LambdaMatrixExample_forEachElement LambdaMatrixExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forEachElement.out - OUTPUT LambdaMatrixExample_forEachElement.out ) + ADD_EXECUTABLE( LambdaMatrixExample_forAllElements LambdaMatrixExample_forAllElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forAllElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forAllElements.out + OUTPUT LambdaMatrixExample_forAllElements.out ) ENDIF() ADD_CUSTOM_TARGET( RunLambdaMatricesExamples ALL DEPENDS @@ -85,6 +85,6 @@ ADD_CUSTOM_TARGET( RunLambdaMatricesExamples ALL DEPENDS LambdaMatrixExample_rowsReduction.out LambdaMatrixExample_allRowsReduction.out LambdaMatrixExample_forElements.out - LambdaMatrixExample_forEachElement.out + LambdaMatrixExample_forAllElements.out ) diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forEachElement.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllElements.cpp similarity index 89% rename from Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forEachElement.cpp rename to Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllElements.cpp index 282dae100..293f173d2 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllElements.cpp @@ -5,7 +5,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { /*** * Lambda functions defining the matrix. @@ -26,7 +26,7 @@ void forEachElementExample() denseView.setElement( rowIdx, columnIdx, value ); }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); std::cout << "Original lambda matrix:" << std::endl << matrix << std::endl; std::cout << "Dense matrix:" << std::endl << denseMatrix << std::endl; } @@ -34,10 +34,10 @@ void forEachElementExample() int main( int argc, char* argv[] ) { std::cout << "Copying matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Copying matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllElements.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllElements.cu new file mode 120000 index 000000000..493efb523 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forAllElements.cu @@ -0,0 +1 @@ +LambdaMatrixExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forEachElement.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forEachElement.cu deleted file mode 120000 index 0b12a40da..000000000 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -LambdaMatrixExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index ded692be2..b05125805 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -70,10 +70,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forElements.out OUTPUT MultidiagonalMatrixExample_forElements.out ) - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forEachElement_cuda MultidiagonalMatrixExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forEachElement.out - OUTPUT MultidiagonalMatrixExample_forEachElement.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllElements_cuda MultidiagonalMatrixExample_forAllElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllElements.out + OUTPUT MultidiagonalMatrixExample_forAllElements.out ) CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda MultidiagonalMatrixViewExample_getCompressedRowLengths.cu ) ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda > @@ -120,10 +120,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forElements.out OUTPUT MultidiagonalMatrixViewExample_forElements.out ) - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forEachElement_cuda MultidiagonalMatrixViewExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forEachElement.out - OUTPUT MultidiagonalMatrixViewExample_forEachElement.out ) + CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllElements_cuda MultidiagonalMatrixViewExample_forAllElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllElements.out + OUTPUT MultidiagonalMatrixViewExample_forAllElements.out ) ELSE() ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor MultidiagonalMatrixExample_Constructor.cpp ) @@ -197,10 +197,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forElements.out OUTPUT MultidiagonalMatrixExample_forElements.out ) - ADD_EXECUTABLE( MultidiagonalMatrixExample_forEachElement MultidiagonalMatrixExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forEachElement.out - OUTPUT MultidiagonalMatrixExample_forEachElement.out ) + ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllElements MultidiagonalMatrixExample_forAllElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllElements.out + OUTPUT MultidiagonalMatrixExample_forAllElements.out ) ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp ) ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths > @@ -247,10 +247,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forElements.out OUTPUT MultidiagonalMatrixViewExample_forElements.out ) - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forEachElement MultidiagonalMatrixViewExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forEachElement.out - OUTPUT MultidiagonalMatrixViewExample_forEachElement.out ) + ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllElements MultidiagonalMatrixViewExample_forAllElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllElements.out + OUTPUT MultidiagonalMatrixViewExample_forAllElements.out ) ENDIF() @@ -275,7 +275,7 @@ ADD_CUSTOM_TARGET( RunMultidiagonalMatricesExamples ALL DEPENDS MultidiagonalMatrixExample_rowsReduction.out MultidiagonalMatrixExample_allRowsReduction.out MultidiagonalMatrixExample_forElements.out - MultidiagonalMatrixExample_forEachElement.out + MultidiagonalMatrixExample_forAllElements.out MultidiagonalMatrixViewExample_getCompressedRowLengths.out MultidiagonalMatrixViewExample_getConstRow.out MultidiagonalMatrixViewExample_getRow.out @@ -285,6 +285,6 @@ ADD_CUSTOM_TARGET( RunMultidiagonalMatricesExamples ALL DEPENDS MultidiagonalMatrixViewExample_rowsReduction.out MultidiagonalMatrixViewExample_allRowsReduction.out MultidiagonalMatrixViewExample_forElements.out - MultidiagonalMatrixViewExample_forEachElement.out + MultidiagonalMatrixViewExample_forAllElements.out ) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forEachElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllElements.cpp similarity index 91% rename from Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forEachElement.cpp rename to Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllElements.cpp index a3af45733..b29543d9e 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are @@ -40,17 +40,17 @@ void forEachElementExample() */ value = 3 - localIdx; }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllElements.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllElements.cu new file mode 120000 index 000000000..4de1d606d --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forAllElements.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forEachElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forEachElement.cu deleted file mode 120000 index 758a054ff..000000000 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -MultidiagonalMatrixExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forEachElement.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllElements.cpp similarity index 92% rename from Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forEachElement.cpp rename to Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllElements.cpp index 92c9ee9e6..b05da1d82 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are @@ -41,17 +41,17 @@ void forEachElementExample() */ value = 3 - localIdx; }; - view.forEachElement( f ); + view.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllElements.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllElements.cu new file mode 120000 index 000000000..f8872fb27 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forAllElements.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forEachElement.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forEachElement.cu deleted file mode 120000 index 140f4ccf9..000000000 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -MultidiagonalMatrixViewExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index 48bef64aa..25c72648f 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -16,7 +16,7 @@ set( COMMON_EXAMPLES SparseMatrixExample_rowsReduction SparseMatrixExample_allRowsReduction SparseMatrixExample_forElements - SparseMatrixExample_forEachElement + SparseMatrixExample_forAllElements SparseMatrixExample_forRows SparseMatrixViewExample_getSerializationType SparseMatrixViewExample_getCompressedRowLengths @@ -29,7 +29,7 @@ set( COMMON_EXAMPLES SparseMatrixViewExample_allRowsReduction SparseMatrixViewExample_forElements SparseMatrixViewExample_forRows - SparseMatrixViewExample_forEachElement + SparseMatrixViewExample_forAllElements ) if( BUILD_CUDA ) diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forEachElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllElements.cpp similarity index 85% rename from Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forEachElement.cpp rename to Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllElements.cpp index 059f0cea0..c603fe32f 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); @@ -19,17 +19,17 @@ void forEachElementExample() } }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllElements.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllElements.cu new file mode 120000 index 000000000..e639034de --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forAllElements.cu @@ -0,0 +1 @@ +SparseMatrixExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forEachElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forEachElement.cu deleted file mode 120000 index ea7c8fde8..000000000 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -SparseMatrixExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forEachElement.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllElements.cpp similarity index 85% rename from Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forEachElement.cpp rename to Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllElements.cpp index 99807428d..4000107eb 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 2, 3, 4, 5 }, 5 ); auto view = matrix.getView(); @@ -20,17 +20,17 @@ void forEachElementExample() } }; - view.forEachElement( f ); + view.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllElements.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllElements.cu new file mode 120000 index 000000000..3bfc383b3 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forAllElements.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forEachElement.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forEachElement.cu deleted file mode 120000 index 2d7beae44..000000000 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -SparseMatrixViewExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt index 7094123bb..44cd5e336 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt @@ -60,10 +60,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forElements.out OUTPUT TridiagonalMatrixExample_forElements.out ) - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forEachElement_cuda TridiagonalMatrixExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forEachElement.out - OUTPUT TridiagonalMatrixExample_forEachElement.out ) + CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forAllElements_cuda TridiagonalMatrixExample_forAllElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllElements.out + OUTPUT TridiagonalMatrixExample_forAllElements.out ) CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths_cuda TridiagonalMatrixViewExample_getCompressedRowLengths.cu ) ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths_cuda > @@ -110,10 +110,10 @@ IF( BUILD_CUDA ) ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forElements.out OUTPUT TridiagonalMatrixViewExample_forElements.out ) - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forEachElement_cuda TridiagonalMatrixViewExample_forEachElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forEachElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forEachElement.out - OUTPUT TridiagonalMatrixViewExample_forEachElement.out ) + CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllElements_cuda TridiagonalMatrixViewExample_forAllElements.cu ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllElements_cuda > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllElements.out + OUTPUT TridiagonalMatrixViewExample_forAllElements.out ) ELSE() @@ -178,10 +178,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forElements.out OUTPUT TridiagonalMatrixExample_forElements.out ) - ADD_EXECUTABLE( TridiagonalMatrixExample_forEachElement TridiagonalMatrixExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forEachElement.out - OUTPUT TridiagonalMatrixExample_forEachElement.out ) + ADD_EXECUTABLE( TridiagonalMatrixExample_forAllElements TridiagonalMatrixExample_forAllElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllElements.out + OUTPUT TridiagonalMatrixExample_forAllElements.out ) ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths TridiagonalMatrixViewExample_getCompressedRowLengths.cpp ) ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths > @@ -228,10 +228,10 @@ ELSE() ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forElements.out OUTPUT TridiagonalMatrixViewExample_forElements.out ) - ADD_EXECUTABLE( TridiagonalMatrixViewExample_forEachElement TridiagonalMatrixViewExample_forEachElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forEachElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forEachElement.out - OUTPUT TridiagonalMatrixViewExample_forEachElement.out ) + ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllElements TridiagonalMatrixViewExample_forAllElements.cpp ) + ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllElements > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllElements.out + OUTPUT TridiagonalMatrixViewExample_forAllElements.out ) ENDIF() @@ -254,7 +254,7 @@ ADD_CUSTOM_TARGET( RunTridiagonalMatricesExamples ALL DEPENDS TridiagonalMatrixExample_rowsReduction.out TridiagonalMatrixExample_allRowsReduction.out TridiagonalMatrixExample_forElements.out - TridiagonalMatrixExample_forEachElement.out + TridiagonalMatrixExample_forAllElements.out TridiagonalMatrixViewExample_getCompressedRowLengths.out TridiagonalMatrixViewExample_getConstRow.out TridiagonalMatrixViewExample_getRow.out @@ -264,6 +264,6 @@ ADD_CUSTOM_TARGET( RunTridiagonalMatricesExamples ALL DEPENDS TridiagonalMatrixViewExample_rowsReduction.out TridiagonalMatrixViewExample_allRowsReduction.out TridiagonalMatrixViewExample_forElements.out - TridiagonalMatrixViewExample_forEachElement.out + TridiagonalMatrixViewExample_forAllElements.out ) diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forEachElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllElements.cpp similarity index 91% rename from Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forEachElement.cpp rename to Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllElements.cpp index 93b56f850..c29b439a6 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are @@ -37,17 +37,17 @@ void forEachElementExample() */ value = 3 - localIdx; }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllElements.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllElements.cu new file mode 120000 index 000000000..704b370b7 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forAllElements.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forEachElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forEachElement.cu deleted file mode 120000 index 13b73c374..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forEachElement.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllElements.cpp similarity index 91% rename from Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forEachElement.cpp rename to Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllElements.cpp index a3a482230..0ef430462 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forEachElement.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllElements.cpp @@ -4,7 +4,7 @@ #include template< typename Device > -void forEachElementExample() +void forAllElementsExample() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are @@ -38,17 +38,17 @@ void forEachElementExample() */ value = 3 - localIdx; }; - view.forEachElement( f ); + view.forAllElements( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { std::cout << "Creating matrix on host: " << std::endl; - forEachElementExample< TNL::Devices::Host >(); + forAllElementsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Creating matrix on CUDA device: " << std::endl; - forEachElementExample< TNL::Devices::Cuda >(); + forAllElementsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllElements.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllElements.cu new file mode 120000 index 000000000..4651fbd7f --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forAllElements.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_forAllElements.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forEachElement.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forEachElement.cu deleted file mode 120000 index 98972cb8b..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forEachElement.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixViewExample_forEachElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Arrays/ArrayViewForElements.cpp b/Documentation/Tutorials/Arrays/ArrayViewForElements.cpp index a78d27b80..c4ed37bf1 100644 --- a/Documentation/Tutorials/Arrays/ArrayViewForElements.cpp +++ b/Documentation/Tutorials/Arrays/ArrayViewForElements.cpp @@ -18,7 +18,7 @@ int main( int argc, char* argv[] ) * Create an ArrayView and use it for initiation */ auto a_view = a.getView(); - a_view.forEachElement( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); + a_view.forAllElements( [] __cuda_callable__ ( int i, float& value ) { value = i; } ); /**** * Initiate elements of b with indexes 0-4 using a_view diff --git a/Documentation/Tutorials/Arrays/ContainsValue.cpp b/Documentation/Tutorials/Arrays/ContainsValue.cpp index 6211e26b8..4b726a7bd 100644 --- a/Documentation/Tutorials/Arrays/ContainsValue.cpp +++ b/Documentation/Tutorials/Arrays/ContainsValue.cpp @@ -13,7 +13,7 @@ int main( int argc, char* argv[] ) const int size = 10; Array< float, Devices::Cuda > a( size ), b( size ); a = 0; - b.forEachElement( [=] __cuda_callable__ ( int i, float& value ) { value = i; } ); + b.forAllElements( [=] __cuda_callable__ ( int i, float& value ) { value = i; } ); /**** * Test the values stored in the arrays diff --git a/Documentation/Tutorials/Arrays/tutorial_Arrays.md b/Documentation/Tutorials/Arrays/tutorial_Arrays.md index fd6552df3..ad3016411 100644 --- a/Documentation/Tutorials/Arrays/tutorial_Arrays.md +++ b/Documentation/Tutorials/Arrays/tutorial_Arrays.md @@ -84,7 +84,7 @@ Output: ### Arrays and parallel for -More efficient and still quite simple method for (not only) array elements initiation is with the use of C++ lambda functions and methods `forElements` and `forEachElement`. As an argument a lambda function is passed which is then applied for all elements. Optionally one may define only subinterval of element indexes where the lambda shall be applied. If the underlying array is allocated on GPU, the lambda function is called from CUDA kernel. This is why it is more efficient than use of `setElement`. On the other hand, one must be careful to use only `__cuda_callable__` methods inside the lambda. The use of the methods `forElements` and `forEachElement` is demonstrated in the following example. +More efficient and still quite simple method for (not only) array elements initiation is with the use of C++ lambda functions and methods `forElements` and `forAllElements`. As an argument a lambda function is passed which is then applied for all elements. Optionally one may define only subinterval of element indexes where the lambda shall be applied. If the underlying array is allocated on GPU, the lambda function is called from CUDA kernel. This is why it is more efficient than use of `setElement`. On the other hand, one must be careful to use only `__cuda_callable__` methods inside the lambda. The use of the methods `forElements` and `forAllElements` is demonstrated in the following example. \include ArrayExample_forElements.cpp diff --git a/Documentation/Tutorials/ForLoops/ParallelForExample_ug.cpp b/Documentation/Tutorials/ForLoops/ParallelForExample_ug.cpp index a9ff6afb6..cf91d69ed 100644 --- a/Documentation/Tutorials/ForLoops/ParallelForExample_ug.cpp +++ b/Documentation/Tutorials/ForLoops/ParallelForExample_ug.cpp @@ -35,7 +35,7 @@ int main( int argc, char* argv[] ) */ Vector< double, Devices::Host > host_v1( 10 ), host_v2( 10 ), host_result( 10 ); host_v1 = 1.0; - host_v2.forEachElement( []__cuda_callable__ ( int i, double& value ) { value = i; } ); + host_v2.forAllElements( []__cuda_callable__ ( int i, double& value ) { value = i; } ); vectorSum( host_v1, host_v2, 2.0, host_result ); std::cout << "host_v1 = " << host_v1 << std::endl; std::cout << "host_v2 = " << host_v2 << std::endl; @@ -47,7 +47,7 @@ int main( int argc, char* argv[] ) #ifdef HAVE_CUDA Vector< double, Devices::Cuda > cuda_v1( 10 ), cuda_v2( 10 ), cuda_result( 10 ); cuda_v1 = 1.0; - cuda_v2.forEachElement( []__cuda_callable__ ( int i, double& value ) { value = i; } ); + cuda_v2.forAllElements( []__cuda_callable__ ( int i, double& value ) { value = i; } ); vectorSum( cuda_v1, cuda_v2, 2.0, cuda_result ); std::cout << "cuda_v1 = " << cuda_v1 << std::endl; std::cout << "cuda_v2 = " << cuda_v2 << std::endl; diff --git a/Documentation/Tutorials/ReductionAndScan/ComparisonExample.cpp b/Documentation/Tutorials/ReductionAndScan/ComparisonExample.cpp index 3ef168a25..931d07d2b 100644 --- a/Documentation/Tutorials/ReductionAndScan/ComparisonExample.cpp +++ b/Documentation/Tutorials/ReductionAndScan/ComparisonExample.cpp @@ -29,7 +29,7 @@ int main( int argc, char* argv[] ) { Vector< double, Devices::Host > host_u( 10 ), host_v( 10 ); host_u = 1.0; - host_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); + host_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); std::cout << "host_u = " << host_u << std::endl; std::cout << "host_v = " << host_v << std::endl; std::cout << "Comparison of host_u and host_v is: " << ( comparison( host_u, host_v ) ? "'true'" : "'false'" ) << "." << std::endl; @@ -37,7 +37,7 @@ int main( int argc, char* argv[] ) #ifdef HAVE_CUDA Vector< double, Devices::Cuda > cuda_u( 10 ), cuda_v( 10 ); cuda_u = 1.0; - cuda_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); + cuda_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); std::cout << "cuda_u = " << cuda_u << std::endl; std::cout << "cuda_v = " << cuda_v << std::endl; std::cout << "Comparison of cuda_u and cuda_v is: " << ( comparison( cuda_u, cuda_v ) ? "'true'" : "'false'" ) << "." << std::endl; diff --git a/Documentation/Tutorials/ReductionAndScan/MapReduceExample-1.cpp b/Documentation/Tutorials/ReductionAndScan/MapReduceExample-1.cpp index eeccc728f..90a069c8a 100644 --- a/Documentation/Tutorials/ReductionAndScan/MapReduceExample-1.cpp +++ b/Documentation/Tutorials/ReductionAndScan/MapReduceExample-1.cpp @@ -20,7 +20,7 @@ double mapReduce( Vector< double, Device >& u ) int main( int argc, char* argv[] ) { Vector< double, Devices::Host > host_u( 10 ); - host_u.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = sin( ( double ) i ); } ); + host_u.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = sin( ( double ) i ); } ); double result = mapReduce( host_u ); std::cout << "host_u = " << host_u << std::endl; std::cout << "Sum of the positive numbers is:" << result << std::endl; diff --git a/Documentation/Tutorials/ReductionAndScan/MaximumNormExample.cpp b/Documentation/Tutorials/ReductionAndScan/MaximumNormExample.cpp index 1b31eb5e5..8d503cbd4 100644 --- a/Documentation/Tutorials/ReductionAndScan/MaximumNormExample.cpp +++ b/Documentation/Tutorials/ReductionAndScan/MaximumNormExample.cpp @@ -19,12 +19,12 @@ double maximumNorm( const Vector< double, Device >& v ) int main( int argc, char* argv[] ) { Vector< double, Devices::Host > host_v( 10 ); - host_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); + host_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); std::cout << "host_v = " << host_v << std::endl; std::cout << "The maximum norm of the host vector elements is " << maximumNorm( host_v ) << "." << std::endl; #ifdef HAVE_CUDA Vector< double, Devices::Cuda > cuda_v( 10 ); - cuda_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); + cuda_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); std::cout << "cuda_v = " << cuda_v << std::endl; std::cout << "The maximum norm of the CUDA vector elements is " << maximumNorm( cuda_v ) << "." << std::endl; #endif diff --git a/Documentation/Tutorials/ReductionAndScan/ReductionWithArgument.cpp b/Documentation/Tutorials/ReductionAndScan/ReductionWithArgument.cpp index 689d8b599..18ac3363b 100644 --- a/Documentation/Tutorials/ReductionAndScan/ReductionWithArgument.cpp +++ b/Documentation/Tutorials/ReductionAndScan/ReductionWithArgument.cpp @@ -28,13 +28,13 @@ maximumNorm( const Vector< double, Device >& v ) int main( int argc, char* argv[] ) { Vector< double, Devices::Host > host_v( 10 ); - host_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); + host_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); std::cout << "host_v = " << host_v << std::endl; auto maxNormHost = maximumNorm( host_v ); std::cout << "The maximum norm of the host vector elements is " << maxNormHost.first << " at position " << maxNormHost.second << "." << std::endl; #ifdef HAVE_CUDA Vector< double, Devices::Cuda > cuda_v( 10 ); - cuda_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); + cuda_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = i - 7; } ); std::cout << "cuda_v = " << cuda_v << std::endl; auto maxNormCuda = maximumNorm( cuda_v ); std::cout << "The maximum norm of the device vector elements is " << maxNormCuda.first << " at position " << maxNormCuda.second << "." << std::endl; diff --git a/Documentation/Tutorials/ReductionAndScan/ScalarProductExample.cpp b/Documentation/Tutorials/ReductionAndScan/ScalarProductExample.cpp index 5a63b460b..680075f84 100644 --- a/Documentation/Tutorials/ReductionAndScan/ScalarProductExample.cpp +++ b/Documentation/Tutorials/ReductionAndScan/ScalarProductExample.cpp @@ -28,7 +28,7 @@ int main( int argc, char* argv[] ) */ Vector< double, Devices::Host > host_u( 10 ), host_v( 10 ); host_u = 1.0; - host_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); + host_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); std::cout << "host_u = " << host_u << std::endl; std::cout << "host_v = " << host_v << std::endl; std::cout << "The scalar product ( host_u, host_v ) is " << scalarProduct( host_u, host_v ) << "." << std::endl; @@ -40,7 +40,7 @@ int main( int argc, char* argv[] ) #ifdef HAVE_CUDA Vector< double, Devices::Cuda > cuda_u( 10 ), cuda_v( 10 ); cuda_u = 1.0; - cuda_v.forEachElement( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); + cuda_v.forAllElements( [] __cuda_callable__ ( int i, double& value ) { value = 2 * ( i % 2 ) - 1; } ); std::cout << "cuda_u = " << cuda_u << std::endl; std::cout << "cuda_v = " << cuda_v << std::endl; std::cout << "The scalar product ( cuda_u, cuda_v ) is " << scalarProduct( cuda_u, cuda_v ) << "." << std::endl; diff --git a/Documentation/Tutorials/Vectors/Expressions.cpp b/Documentation/Tutorials/Vectors/Expressions.cpp index 5ccad7c6d..b41d2188e 100644 --- a/Documentation/Tutorials/Vectors/Expressions.cpp +++ b/Documentation/Tutorials/Vectors/Expressions.cpp @@ -20,7 +20,7 @@ void expressions() ViewType a = a_v.getView(); ViewType b = b_v.getView(); ViewType c = c_v.getView(); - a.forEachElement( [] __cuda_callable__ ( int i, RealType& value ) { value = 3.14 * ( i - 5.0 ) / 5.0; } ); + a.forAllElements( [] __cuda_callable__ ( int i, RealType& value ) { value = 3.14 * ( i - 5.0 ) / 5.0; } ); b = a * a; c = 3 * a + sign( a ) * sin( a ); std::cout << "a = " << a << std::endl; diff --git a/Documentation/Tutorials/Vectors/Reduction.cpp b/Documentation/Tutorials/Vectors/Reduction.cpp index 5646b4869..b0034f6d5 100644 --- a/Documentation/Tutorials/Vectors/Reduction.cpp +++ b/Documentation/Tutorials/Vectors/Reduction.cpp @@ -20,8 +20,8 @@ void expressions() ViewType a = a_v.getView(); ViewType b = b_v.getView(); ViewType c = c_v.getView(); - a.forEachElement( [] __cuda_callable__ ( int i, RealType& value ) { value = i; } ); - b.forEachElement( [] __cuda_callable__ ( int i, RealType& value ) { value = i - 5.0; } ); + a.forAllElements( [] __cuda_callable__ ( int i, RealType& value ) { value = i; } ); + b.forAllElements( [] __cuda_callable__ ( int i, RealType& value ) { value = i - 5.0; } ); c = -5; std::cout << "a = " << a << std::endl; diff --git a/src/TNL/Algorithms/Segments/BiEllpack.h b/src/TNL/Algorithms/Segments/BiEllpack.h index 29959747c..3a5a7c202 100644 --- a/src/TNL/Algorithms/Segments/BiEllpack.h +++ b/src/TNL/Algorithms/Segments/BiEllpack.h @@ -104,7 +104,7 @@ namespace TNL void forElements(IndexType first, IndexType last, Function &&f) const; template - void forEachElement(Function &&f) const; + void forAllElements(Function &&f) const; template void forSegments(IndexType begin, IndexType end, Function &&f) const; diff --git a/src/TNL/Algorithms/Segments/BiEllpack.hpp b/src/TNL/Algorithms/Segments/BiEllpack.hpp index d85b668ce..d0847b6a3 100644 --- a/src/TNL/Algorithms/Segments/BiEllpack.hpp +++ b/src/TNL/Algorithms/Segments/BiEllpack.hpp @@ -131,7 +131,7 @@ performRowBubbleSort( const SizesHolder& segmentsSizes ) if( segmentsSizes.getSize() == 0 ) return; - this->rowPermArray.forEachElement( [] __cuda_callable__ ( const IndexType idx, IndexType& value ) { value = idx; } ); + this->rowPermArray.forAllElements( [] __cuda_callable__ ( const IndexType idx, IndexType& value ) { value = idx; } ); //if( std::is_same< DeviceType, Devices::Host >::value ) { @@ -459,7 +459,7 @@ template< typename Device, template< typename Function > void BiEllpack< Device, Index, IndexAllocator, Organization, WarpSize >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/BiEllpackView.h b/src/TNL/Algorithms/Segments/BiEllpackView.h index 447807735..44629ea71 100644 --- a/src/TNL/Algorithms/Segments/BiEllpackView.h +++ b/src/TNL/Algorithms/Segments/BiEllpackView.h @@ -115,7 +115,7 @@ class BiEllpackView void forElements( IndexType first, IndexType last, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/BiEllpackView.hpp b/src/TNL/Algorithms/Segments/BiEllpackView.hpp index 07451a57a..e861e8f76 100644 --- a/src/TNL/Algorithms/Segments/BiEllpackView.hpp +++ b/src/TNL/Algorithms/Segments/BiEllpackView.hpp @@ -313,7 +313,7 @@ template< typename Device, template< typename Function > void BiEllpackView< Device, Index, Organization, WarpSize >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/CSR.h b/src/TNL/Algorithms/Segments/CSR.h index 902361990..e63b4c8da 100644 --- a/src/TNL/Algorithms/Segments/CSR.h +++ b/src/TNL/Algorithms/Segments/CSR.h @@ -112,7 +112,7 @@ class CSR void forElements( IndexType begin, IndexType end, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/CSR.hpp b/src/TNL/Algorithms/Segments/CSR.hpp index e5632911c..823393c2f 100644 --- a/src/TNL/Algorithms/Segments/CSR.hpp +++ b/src/TNL/Algorithms/Segments/CSR.hpp @@ -242,7 +242,7 @@ template< typename Device, template< typename Function > void CSR< Device, Index, Kernel, IndexAllocator >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/CSRView.h b/src/TNL/Algorithms/Segments/CSRView.h index bc0942af5..cd9e44a2a 100644 --- a/src/TNL/Algorithms/Segments/CSRView.h +++ b/src/TNL/Algorithms/Segments/CSRView.h @@ -109,7 +109,7 @@ class CSRView void forElements( IndexType begin, IndexType end, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/CSRView.hpp b/src/TNL/Algorithms/Segments/CSRView.hpp index 27eba0741..bb40dc9f6 100644 --- a/src/TNL/Algorithms/Segments/CSRView.hpp +++ b/src/TNL/Algorithms/Segments/CSRView.hpp @@ -206,7 +206,7 @@ template< typename Device, template< typename Function > void CSRView< Device, Index, Kernel >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpack.h b/src/TNL/Algorithms/Segments/ChunkedEllpack.h index cab7a20ab..b6bdd5bf1 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpack.h +++ b/src/TNL/Algorithms/Segments/ChunkedEllpack.h @@ -100,7 +100,7 @@ class ChunkedEllpack void forElements( IndexType first, IndexType last, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp b/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp index 41ca6ad76..69e6b4c67 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp +++ b/src/TNL/Algorithms/Segments/ChunkedEllpack.hpp @@ -406,7 +406,7 @@ template< typename Device, template< typename Function > void ChunkedEllpack< Device, Index, IndexAllocator, Organization >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpackView.h b/src/TNL/Algorithms/Segments/ChunkedEllpackView.h index 68863e274..196c0764e 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpackView.h +++ b/src/TNL/Algorithms/Segments/ChunkedEllpackView.h @@ -129,7 +129,7 @@ class ChunkedEllpackView void forElements( IndexType begin, IndexType end, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp b/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp index 3a4cba132..147b362d1 100644 --- a/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp +++ b/src/TNL/Algorithms/Segments/ChunkedEllpackView.hpp @@ -354,7 +354,7 @@ template< typename Device, template< typename Function > void ChunkedEllpackView< Device, Index, Organization >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/Ellpack.h b/src/TNL/Algorithms/Segments/Ellpack.h index 9e9725291..c88ba6a1d 100644 --- a/src/TNL/Algorithms/Segments/Ellpack.h +++ b/src/TNL/Algorithms/Segments/Ellpack.h @@ -100,7 +100,7 @@ class Ellpack void forElements( IndexType first, IndexType last, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/Ellpack.hpp b/src/TNL/Algorithms/Segments/Ellpack.hpp index cae6a1802..d7210386c 100644 --- a/src/TNL/Algorithms/Segments/Ellpack.hpp +++ b/src/TNL/Algorithms/Segments/Ellpack.hpp @@ -268,7 +268,7 @@ template< typename Device, template< typename Function > void Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/EllpackView.h b/src/TNL/Algorithms/Segments/EllpackView.h index ae5061214..e1c2bd423 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.h +++ b/src/TNL/Algorithms/Segments/EllpackView.h @@ -96,7 +96,7 @@ class EllpackView void forElements( IndexType begin, IndexType end, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/EllpackView.hpp b/src/TNL/Algorithms/Segments/EllpackView.hpp index d8c5aba1e..f8c14c2fb 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.hpp +++ b/src/TNL/Algorithms/Segments/EllpackView.hpp @@ -220,7 +220,7 @@ template< typename Device, int Alignment > template< typename Function > void EllpackView< Device, Index, Organization, Alignment >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/SlicedEllpack.h b/src/TNL/Algorithms/Segments/SlicedEllpack.h index dce2ea389..942306c75 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpack.h +++ b/src/TNL/Algorithms/Segments/SlicedEllpack.h @@ -97,7 +97,7 @@ class SlicedEllpack void forElements( IndexType first, IndexType last, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/SlicedEllpack.hpp b/src/TNL/Algorithms/Segments/SlicedEllpack.hpp index 373e729c8..82e7a8571 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpack.hpp +++ b/src/TNL/Algorithms/Segments/SlicedEllpack.hpp @@ -301,7 +301,7 @@ template< typename Device, template< typename Function > void SlicedEllpack< Device, Index, IndexAllocator, Organization, SliceSize >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/SlicedEllpackView.h b/src/TNL/Algorithms/Segments/SlicedEllpackView.h index 37169d365..2955ee351 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpackView.h +++ b/src/TNL/Algorithms/Segments/SlicedEllpackView.h @@ -98,7 +98,7 @@ class SlicedEllpackView void forElements( IndexType first, IndexType last, Function&& f ) const; template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; template< typename Function > void forSegments( IndexType begin, IndexType end, Function&& f ) const; diff --git a/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp b/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp index 698324169..42fdae7ea 100644 --- a/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp +++ b/src/TNL/Algorithms/Segments/SlicedEllpackView.hpp @@ -288,7 +288,7 @@ template< typename Device, template< typename Function > void SlicedEllpackView< Device, Index, Organization, SliceSize >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSegmentsCount(), f ); } diff --git a/src/TNL/Algorithms/Segments/details/CSR.h b/src/TNL/Algorithms/Segments/details/CSR.h index 2e2a934cb..b9392815d 100644 --- a/src/TNL/Algorithms/Segments/details/CSR.h +++ b/src/TNL/Algorithms/Segments/details/CSR.h @@ -97,7 +97,7 @@ class CSR void forElements( IndexType first, IndexType last, Function& f, Args... args ) const; template< typename Function, typename... Args > - void forEachElement( Function& f, Args... args ) const; + void forAllElements( Function& f, Args... args ) const; /*** diff --git a/src/TNL/Containers/Array.h b/src/TNL/Containers/Array.h index 9c02ac9d4..53c9290ca 100644 --- a/src/TNL/Containers/Array.h +++ b/src/TNL/Containers/Array.h @@ -678,7 +678,7 @@ class Array * */ template< typename Function > - void forEachElement( Function&& f ); + void forAllElements( Function&& f ); /** * \brief Process the lambda function \e f for each array element for constant instances. @@ -706,7 +706,7 @@ class Array * */ template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; /** * \brief Computes reduction with array elements on interval [ \e begin, \e end). diff --git a/src/TNL/Containers/Array.hpp b/src/TNL/Containers/Array.hpp index 402168d11..f313a4cf5 100644 --- a/src/TNL/Containers/Array.hpp +++ b/src/TNL/Containers/Array.hpp @@ -715,9 +715,9 @@ template< typename Value, template< typename Function > void Array< Value, Device, Index, Allocator >:: -forEachElement( Function&& f ) +forAllElements( Function&& f ) { - this->getView().forEachElement( f ); + this->getView().forAllElements( f ); } template< typename Value, @@ -727,10 +727,10 @@ template< typename Value, template< typename Function > void Array< Value, Device, Index, Allocator >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { const auto view = this->getConstView(); - view.forEachElement( f ); + view.forAllElements( f ); } template< typename Value, diff --git a/src/TNL/Containers/ArrayView.h b/src/TNL/Containers/ArrayView.h index 1d3ae60de..4b3846037 100644 --- a/src/TNL/Containers/ArrayView.h +++ b/src/TNL/Containers/ArrayView.h @@ -496,7 +496,7 @@ public: * */ template< typename Function > - void forEachElement( Function&& f ); + void forAllElements( Function&& f ); /** * \brief Process the lambda function \e f for each array element for constant instances. @@ -524,7 +524,7 @@ public: * */ template< typename Function > - void forEachElement( Function&& f ) const; + void forAllElements( Function&& f ) const; /** * \brief Computes reduction with array view elements on interval [ \e begin, \e end). diff --git a/src/TNL/Containers/ArrayView.hpp b/src/TNL/Containers/ArrayView.hpp index 9143dea1a..eeb0b1b4b 100644 --- a/src/TNL/Containers/ArrayView.hpp +++ b/src/TNL/Containers/ArrayView.hpp @@ -351,7 +351,7 @@ template< typename Value, typename Index > template< typename Function > void ArrayView< Value, Device, Index >:: -forEachElement( Function&& f ) +forAllElements( Function&& f ) { this->forElements( 0, this->getSize(), f ); } @@ -361,7 +361,7 @@ template< typename Value, typename Index > template< typename Function > void ArrayView< Value, Device, Index >:: -forEachElement( Function&& f ) const +forAllElements( Function&& f ) const { this->forElements( 0, this->getSize(), f ); } diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index f0b49128d..63a1ecf07 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -363,7 +363,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref DenseMatrix::getRow - * or \ref DenseMatrix::forElements and \ref DenseMatrix::forEachElement. + * or \ref DenseMatrix::forElements and \ref DenseMatrix::forAllElements. * * \param row is row index of the element. * \param column is columns index of the element. @@ -387,7 +387,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref DenseMatrix::getRow - * or \ref DenseMatrix::forElements and \ref DenseMatrix::forEachElement. + * or \ref DenseMatrix::forElements and \ref DenseMatrix::forAllElements. * * \param row is row index of the element. * \param column is columns index of the element. @@ -415,7 +415,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref DenseMatrix::getRow - * or \ref DenseMatrix::forElements and \ref DenseMatrix::forEachElement. + * or \ref DenseMatrix::forElements and \ref DenseMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -594,12 +594,12 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief This method calls \e forElements for all matrix rows. * - * See \ref DenseMatrix::forEachElement. + * See \ref DenseMatrix::forAllElements. * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. @@ -610,7 +610,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forAllElements( Function& function ); /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 9e220ebac..79484607b 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -431,7 +431,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { this->forElements( 0, this->getRows(), function ); } @@ -444,7 +444,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -forEachElement( Function& function ) +forAllElements( Function& function ) { this->forElements( 0, this->getRows(), function ); } @@ -1048,7 +1048,7 @@ operator=( const DenseMatrix< RHSReal, RHSDevice, RHSIndex, RHSOrganization, RHS auto f = [=] __cuda_callable__ ( RHSIndexType rowIdx, RHSIndexType localIdx, RHSIndexType columnIdx, const RHSRealType& value, bool& compute ) mutable { this_view( rowIdx, columnIdx ) = value; }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); } else { @@ -1124,7 +1124,7 @@ operator=( const RHSMatrix& matrix ) if( value != 0.0 && columnIdx != padding_index ) values_view[ segments_view.getGlobalIndex( rowIdx, columnIdx ) ] = value; }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); } else { diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index 53b8fb324..705bf5b82 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -305,7 +305,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref DenseMatrix::getRow - * or \ref DenseMatrix::forElements and \ref DenseMatrix::forEachElement. + * or \ref DenseMatrix::forElements and \ref DenseMatrix::forAllElements. * * \param row is row index of the element. * \param column is columns index of the element. @@ -329,7 +329,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref DenseMatrix::getRow - * or \ref DenseMatrix::forElements and \ref DenseMatrix::forEachElement. + * or \ref DenseMatrix::forElements and \ref DenseMatrix::forAllElements. * * \param row is row index of the element. * \param column is columns index of the element. @@ -357,7 +357,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref DenseMatrix::getRow - * or \ref DenseMatrix::forElements and \ref DenseMatrix::forEachElement. + * or \ref DenseMatrix::forElements and \ref DenseMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -536,12 +536,12 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief This method calls \e forElements for all matrix rows. * - * See \ref DenseMatrix::forEachElement. + * See \ref DenseMatrix::forAllElements. * * \tparam Function is a type of lambda function that will operate on matrix elements. * \param function is an instance of the lambda function to be called in each row. @@ -552,7 +552,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forAllElements( Function& function ); /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index 97e82af0e..94fa3d1e5 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -368,7 +368,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { this->forElements( 0, this->getRows(), function ); } @@ -380,7 +380,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forEachElement( Function& function ) +forAllElements( Function& function ) { this->forElements( 0, this->getRows(), function ); } diff --git a/src/TNL/Matrices/LambdaMatrix.h b/src/TNL/Matrices/LambdaMatrix.h index 56d168989..83bd76da1 100644 --- a/src/TNL/Matrices/LambdaMatrix.h +++ b/src/TNL/Matrices/LambdaMatrix.h @@ -283,7 +283,7 @@ class LambdaMatrix * \include LambdaMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/LambdaMatrix.hpp b/src/TNL/Matrices/LambdaMatrix.hpp index ee59799c5..aab21b085 100644 --- a/src/TNL/Matrices/LambdaMatrix.hpp +++ b/src/TNL/Matrices/LambdaMatrix.hpp @@ -295,7 +295,7 @@ template< typename MatrixElementsLambda, template< typename Function > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { forElements( 0, this->getRows(), function ); /*const IndexType rows = this->getRows(); diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index 4c07354cd..0e14a0660 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -526,7 +526,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow - * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forEachElement. + * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -551,7 +551,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow - * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forEachElement. + * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -580,7 +580,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow - * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forEachElement. + * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -783,7 +783,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include MultidiagonalMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief This method calls \e forElements for all matrix rows. @@ -799,7 +799,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include MultidiagonalMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forAllElements( Function& function ); /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 2a7704fc4..283d12a18 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -561,7 +561,7 @@ template< typename Real, template< typename Function > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { this->view.forElements( 0, this->getRows(), function ); } @@ -575,7 +575,7 @@ template< typename Real, template< typename Function > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -forEachElement( Function& function ) +forAllElements( Function& function ) { this->view.forElements( 0, this->getRows(), function ); } @@ -820,7 +820,7 @@ operator=( const MultidiagonalMatrix< Real_, Device_, Index_, Organization_, Rea auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value = matrix_view.getValues()[ matrix_view.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; - this->forEachElement( f ); + this->forAllElements( f ); } else { diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index a66431b18..55c33e68a 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -285,7 +285,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow - * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forEachElement. + * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -310,7 +310,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow - * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forEachElement. + * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -338,7 +338,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref MultidiagonalMatrix::getRow - * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forEachElement. + * or \ref MultidiagonalMatrix::forElements and \ref MultidiagonalMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -540,7 +540,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \include MultidiagonalMatrixViewExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief This method calls \e forElements for all matrix rows. @@ -556,7 +556,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \include MultidiagonalMatrixViewExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forAllElements( Function& function ); /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index 44c43da7f..a2d2a4a35 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -219,7 +219,7 @@ setValue( const RealType& v ) auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType columnIdx, RealType& value, bool& compute ) mutable { value = newValue; }; - this->forEachElement( f ); + this->forAllElements( f ); } template< typename Real, @@ -490,7 +490,7 @@ template< typename Real, template< typename Function > void MultidiagonalMatrixView< Real, Device, Index, Organization >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { this->forElements( 0, this->indxer.getNonEmptyRowsCount(), function ); } @@ -502,7 +502,7 @@ template< typename Real, template< typename Function > void MultidiagonalMatrixView< Real, Device, Index, Organization >:: -forEachElement( Function& function ) +forAllElements( Function& function ) { this->forElements( 0, this->indexer.getNonemptyRowsCount(), function ); } @@ -634,11 +634,11 @@ addMatrix( const MultidiagonalMatrixView< Real_, Device_, Index_, Organization_ value = thisMult * value + matrixMult * matrix.getValues()[ matrix.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; if( thisMult == 0.0 ) - this->forEachElement( add0 ); + this->forAllElements( add0 ); else if( thisMult == 1.0 ) - this->forEachElement( add1 ); + this->forAllElements( add1 ); else - this->forEachElement( addGen ); + this->forAllElements( addGen ); }*/ } diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index e5f9fa277..cf8bc2da5 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -529,7 +529,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref SparseMatrix::getRow - * or \ref SparseMatrix::forElements and \ref SparseMatrix::forEachElement. + * or \ref SparseMatrix::forElements and \ref SparseMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -554,7 +554,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref SparseMatrix::getRow - * or \ref SparseMatrix::forElements and \ref SparseMatrix::forEachElement. + * or \ref SparseMatrix::forElements and \ref SparseMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -583,7 +583,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref SparseMatrix::getRow - * or \ref SparseMatrix::forElements and \ref SparseMatrix::forEachElement. + * or \ref SparseMatrix::forElements and \ref SparseMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -772,7 +772,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include SparseMatrixExample_forElements.out */ template< typename Function > - void forEachElement( Function&& function ) const; + void forAllElements( Function&& function ) const; /** * \brief Method for parallel iteration over all matrix elements for non-constant instances. @@ -788,7 +788,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include SparseMatrixExample_forElements.out */ template< typename Function > - void forEachElement( Function&& function ); + void forAllElements( Function&& function ); /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 87dea06c2..2c5f4eff8 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -635,7 +635,7 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forEachElement( Function&& function ) const +forAllElements( Function&& function ) const { this->forElements( 0, this->getRows(), function ); } @@ -651,7 +651,7 @@ template< typename Real, template< typename Function > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -forEachElement( Function&& function ) +forAllElements( Function&& function ) { this->forElements( 0, this->getRows(), function ); } @@ -901,7 +901,7 @@ operator=( const DenseMatrix< Real_, Device_, Index_, Organization, RealAllocato values_view[ thisGlobalIdx ] = value; } }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); } else { @@ -1014,7 +1014,7 @@ operator=( const RHSMatrix& matrix ) rowLocalIndexes_view[ rowIdx ] = localIdx; } }; - matrix.forEachElement( f ); + matrix.forAllElements( f ); } else { diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 5c1a71f50..e6693b5d5 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -320,7 +320,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref SparseMatrix::getRow - * or \ref SparseMatrix::forElements and \ref SparseMatrix::forEachElement. + * or \ref SparseMatrix::forElements and \ref SparseMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -345,7 +345,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref SparseMatrix::getRow - * or \ref SparseMatrix::forElements and \ref SparseMatrix::forEachElement. + * or \ref SparseMatrix::forElements and \ref SparseMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -373,7 +373,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref SparseMatrix::getRow - * or \ref SparseMatrix::forElements and \ref SparseMatrix::forEachElement. + * or \ref SparseMatrix::forElements and \ref SparseMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -552,7 +552,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \include SparseMatrixViewExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief This method calls \e forElements for all matrix rows. @@ -568,7 +568,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \include SparseMatrixViewExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forAllElements( Function& function ); /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index a1e82d3f2..1c0098825 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -639,7 +639,7 @@ template< typename Real, template< typename Function > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { this->forElements( 0, this->getRows(), function ); } @@ -653,7 +653,7 @@ template< typename Real, template< typename Function > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -forEachElement( Function& function ) +forAllElements( Function& function ) { this->forElements( 0, this->getRows(), function ); } diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h index dc6b31cb5..eab681bf0 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.h +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -418,7 +418,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref TridiagonalMatrix::getRow - * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forEachElement. + * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -442,7 +442,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref TridiagonalMatrix::getRow - * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forEachElement. + * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -470,7 +470,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref TridiagonalMatrix::getRow - * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forEachElement. + * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -657,7 +657,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include TridiagonalMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. @@ -679,7 +679,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include TridiagonalMatrixExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forAllElements( Function& function ); /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/TridiagonalMatrix.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp index cbdba8299..c6f89e5b8 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -426,7 +426,7 @@ template< typename Real, template< typename Function > void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { this->view.forElements( 0, this->getRows(), function ); } @@ -439,7 +439,7 @@ template< typename Real, template< typename Function > void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -forEachElement( Function& function ) +forAllElements( Function& function ) { this->view.forElements( 0, this->getRows(), function ); } @@ -668,7 +668,7 @@ operator=( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealA auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value = matrix_view.getValues()[ matrix_view.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; - this->forEachElement( f ); + this->forAllElements( f ); } else { @@ -678,7 +678,7 @@ operator=( const TridiagonalMatrix< Real_, Device_, Index_, Organization_, RealA auto f = [=] __cuda_callable__ ( const IndexType& rowIdx, const IndexType& localIdx, const IndexType& column, Real& value, bool& compute ) mutable { value = matrix_view.getValues()[ matrix_view.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; - this->forEachElement( f ); + this->forAllElements( f ); } } return *this; diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index 324caea86..92d977714 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -272,7 +272,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref TridiagonalMatrix::getRow - * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forEachElement. + * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -297,7 +297,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref TridiagonalMatrix::getRow - * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forEachElement. + * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forAllElements. * The call may fail if the matrix row capacity is exhausted. * * \param row is row index of the element. @@ -325,7 +325,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * can be called even from device kernels. If the matrix is allocated in GPU device * this method is called from CPU, it transfers values of each matrix element separately and so the * performance is very low. For higher performance see. \ref TridiagonalMatrix::getRow - * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forEachElement. + * or \ref TridiagonalMatrix::forElements and \ref TridiagonalMatrix::forAllElements. * * \param row is a row index of the matrix element. * \param column i a column index of the matrix element. @@ -503,7 +503,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \include TridiagonalMatrixViewExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ) const; + void forAllElements( Function& function ) const; /** * \brief This method calls \e forElements for all matrix rows. @@ -519,7 +519,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \include TridiagonalMatrixViewExample_forAllRows.out */ template< typename Function > - void forEachElement( Function& function ); + void forAllElements( Function& function ); /** * \brief Method for sequential iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index c125ffe22..226dfbae8 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -461,7 +461,7 @@ template< typename Real, template< typename Function > void TridiagonalMatrixView< Real, Device, Index, Organization >:: -forEachElement( Function& function ) const +forAllElements( Function& function ) const { this->forElements( 0, this->indxer.getNonEmptyRowsCount(), function ); } @@ -473,7 +473,7 @@ template< typename Real, template< typename Function > void TridiagonalMatrixView< Real, Device, Index, Organization >:: -forEachElement( Function& function ) +forAllElements( Function& function ) { this->forElements( 0, this->indexer.getNonemptyRowsCount(), function ); } @@ -617,11 +617,11 @@ addMatrix( const TridiagonalMatrixView< Real_, Device_, Index_, Organization_ >& value = thisMult * value + matrixMult * matrix.getValues()[ matrix.getIndexer().getGlobalIndex( rowIdx, localIdx ) ]; }; if( thisMult == 0.0 ) - this->forEachElement( add0 ); + this->forAllElements( add0 ); else if( thisMult == 1.0 ) - this->forEachElement( add1 ); + this->forAllElements( add1 ); else - this->forEachElement( addGen ); + this->forAllElements( addGen ); } } diff --git a/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h b/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h index 0a3b8d43a..845637e48 100644 --- a/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h +++ b/src/TNL/Solvers/Linear/Preconditioners/Diagonal_impl.h @@ -35,7 +35,7 @@ update( const MatrixPointer& matrixPointer ) const auto kernel_matrix = matrixPointer->getView(); - // TODO: Rewrite this with SparseMatrix::forEachElement + // TODO: Rewrite this with SparseMatrix::forAllElements auto kernel = [=] __cuda_callable__ ( IndexType i ) mutable { diag_view[ i ] = kernel_matrix.getElement( i, i ); diff --git a/src/UnitTests/Algorithms/Segments/SegmentsTest.hpp b/src/UnitTests/Algorithms/Segments/SegmentsTest.hpp index de634cf01..7073bdb8a 100644 --- a/src/UnitTests/Algorithms/Segments/SegmentsTest.hpp +++ b/src/UnitTests/Algorithms/Segments/SegmentsTest.hpp @@ -132,7 +132,7 @@ void test_AllReduction_MaximumInSegments() view[ globalIdx ] = segmentIdx * 5 + localIdx + 1; return true; }; - segments.forEachElement( init ); + segments.forAllElements( init ); TNL::Containers::Vector< IndexType, DeviceType, IndexType >result( segmentsCount ); diff --git a/src/UnitTests/Containers/ArrayTest.h b/src/UnitTests/Containers/ArrayTest.h index 148d92aa6..1ed8052ee 100644 --- a/src/UnitTests/Containers/ArrayTest.h +++ b/src/UnitTests/Containers/ArrayTest.h @@ -445,7 +445,7 @@ void testArrayForEachElement() using ValueType = typename ArrayType::ValueType; ArrayType a( 10 ); - a.forEachElement( [] __cuda_callable__ ( IndexType i, ValueType& v ) mutable { v = i; } ); + a.forAllElements( [] __cuda_callable__ ( IndexType i, ValueType& v ) mutable { v = i; } ); for( int i = 0; i < 10; i++ ) EXPECT_EQ( a.getElement( i ), i ); diff --git a/src/UnitTests/Containers/ArrayViewTest.h b/src/UnitTests/Containers/ArrayViewTest.h index 06c005f50..d620b8bbb 100644 --- a/src/UnitTests/Containers/ArrayViewTest.h +++ b/src/UnitTests/Containers/ArrayViewTest.h @@ -273,7 +273,7 @@ void ArrayViewEvaluateTest( ArrayType& u ) using ViewType = ArrayView< ValueType, DeviceType, IndexType >; ViewType v( u ); - v.forEachElement( [] __cuda_callable__ ( IndexType i, ValueType& value ) { value = 3 * i % 4; } ); + v.forAllElements( [] __cuda_callable__ ( IndexType i, ValueType& value ) { value = 3 * i % 4; } ); for( int i = 0; i < 10; i++ ) { diff --git a/src/UnitTests/Containers/VectorTest.h b/src/UnitTests/Containers/VectorTest.h index fa99547ec..2c5deb59d 100644 --- a/src/UnitTests/Containers/VectorTest.h +++ b/src/UnitTests/Containers/VectorTest.h @@ -89,7 +89,7 @@ void testVectorReduceElements() using ValueType = typename VectorType::ValueType; VectorType a( 10 ); - a.forEachElement( [=] __cuda_callable__ ( IndexType i, ValueType& v ) mutable { v = 1; } ); + a.forAllElements( [=] __cuda_callable__ ( IndexType i, ValueType& v ) mutable { v = 1; } ); auto fetch = [] __cuda_callable__ ( IndexType i, ValueType& v ) -> ValueType { return v; }; auto reduce = [] __cuda_callable__ ( const ValueType v1, const ValueType v2 ) { return v1 + v2; }; EXPECT_EQ( a.reduceEachElement( fetch, reduce, ( ValueType ) 0.0 ), diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 0728549f4..e9aedce71 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1319,7 +1319,7 @@ void test_VectorProduct() // Test with large diagonal matrix Matrix m1( size, size ); TNL::Containers::Vector< IndexType, DeviceType, IndexType > rowCapacities( size ); - rowCapacities.forEachElement( [] __cuda_callable__ ( IndexType i, IndexType& value ) { value = 1; } ); + rowCapacities.forAllElements( [] __cuda_callable__ ( IndexType i, IndexType& value ) { value = 1; } ); m1.setRowCapacities( rowCapacities ); auto f1 = [=] __cuda_callable__ ( IndexType row, IndexType localIdx, IndexType& column, RealType& value, bool& compute ) { if( localIdx == 0 ) @@ -1328,7 +1328,7 @@ void test_VectorProduct() column = row; } }; - m1.forEachElement( f1 ); + m1.forAllElements( f1 ); // check that the matrix was initialized m1.getCompressedRowLengths( rowCapacities ); EXPECT_EQ( rowCapacities, 1 ); @@ -1343,7 +1343,7 @@ void test_VectorProduct() const int rows( size ), columns( size ); Matrix m2( rows, columns ); rowCapacities.setSize( rows ); - rowCapacities.forEachElement( [=] __cuda_callable__ ( IndexType i, IndexType& value ) { value = i + 1; } ); + rowCapacities.forAllElements( [=] __cuda_callable__ ( IndexType i, IndexType& value ) { value = i + 1; } ); m2.setRowCapacities( rowCapacities ); auto f2 = [=] __cuda_callable__ ( IndexType row, IndexType localIdx, IndexType& column, RealType& value, bool& compute ) { if( localIdx <= row ) @@ -1352,7 +1352,7 @@ void test_VectorProduct() column = localIdx; } }; - m2.forEachElement( f2 ); + m2.forAllElements( f2 ); // check that the matrix was initialized TNL::Containers::Vector< IndexType, DeviceType, IndexType > rowLengths( rows ); m2.getCompressedRowLengths( rowLengths ); @@ -1384,7 +1384,7 @@ void test_VectorProduct() column = localIdx; value = localIdx + 1; }; - m3.forEachElement( f ); + m3.forAllElements( f ); TNL::Containers::Vector< double, DeviceType, IndexType > in( columns, 1.0 ), out( rows, 0.0 ); m3.vectorProduct( in, out ); EXPECT_EQ( out.getElement( 0 ), ( double ) columns * ( double ) (columns + 1 ) / 2.0 ); @@ -1415,7 +1415,7 @@ void test_ForElements() const IndexType rows = 8; Matrix m( { 3, 3, 3, 3, 3, 3, 3, 3, 3 }, cols ); - m.forEachElement( [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value, bool compute ) mutable { + m.forAllElements( [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value, bool compute ) mutable { value = rowIdx + 1.0; columnIdx = localIdx; } ); -- GitLab From f202d352327c883aa4a7b3031ae5c1a03772078a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 12:05:06 +0100 Subject: [PATCH 09/54] Added for[,All]Rows methods to DenseMatrix[,View]. --- .../Tutorials/Matrices/tutorial_Matrices.md | 2 + src/TNL/Matrices/DenseMatrix.h | 100 +++++++++++ src/TNL/Matrices/DenseMatrix.hpp | 52 ++++++ src/TNL/Matrices/DenseMatrixView.h | 100 +++++++++++ src/TNL/Matrices/DenseMatrixView.hpp | 60 +++++++ src/UnitTests/Matrices/DenseMatrixTest.h | 162 ++++++++++++++++++ src/UnitTests/Matrices/SparseMatrixTest.hpp | 2 +- 7 files changed, 477 insertions(+), 1 deletion(-) diff --git a/Documentation/Tutorials/Matrices/tutorial_Matrices.md b/Documentation/Tutorials/Matrices/tutorial_Matrices.md index 925d748e8..0c8aa3baa 100644 --- a/Documentation/Tutorials/Matrices/tutorial_Matrices.md +++ b/Documentation/Tutorials/Matrices/tutorial_Matrices.md @@ -2,6 +2,8 @@ [TOC] +TODO: Add description of forRows and sequentialForRows. + ## Introduction TNL offers several types of matrices like dense (\ref TNL::Matrices::DenseMatrix), sparse (\ref TNL::Matrices::SparseMatrix), tridiagonal (\ref TNL::Matrices::TridiagonalMatrix), multidiagonal (\ref TNL::Matrices::MultidiagonalMatrix) and lambda matrices (\ref TNL::Matrices::LambdaMatrix). The sparse matrices can be symmetric to lower the memory requirements. The interfaces of given matrix types are designed to be as unified as possible to ensure that the user can easily switch between different matrix types while making no or only a little changes in the source code. All matrix types allows traversing all matrix elements and manipulate them using lambda functions as well as performing flexible reduction in matrix rows. The following text describes particular matrix types and their unified interface in details. diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 63a1ecf07..8aea413d9 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -612,6 +612,106 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Function > void forAllElements( Function& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrix::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp + * \par Output + * \include DenseMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrix::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp + * \par Output + * \include DenseMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrix::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrix::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp + * \par Output + * \include DenseMatrixExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ); + + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrix::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrix::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp + * \par Output + * \include DenseMatrixExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 79484607b..0ed448337 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -449,6 +449,58 @@ forAllElements( Function& function ) this->forElements( 0, this->getRows(), function ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + this->getView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + this->getConstView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: +forAllRows( Function&& function ) +{ + this->getView().forAllRows( function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: +forAllRows( Function&& function ) const +{ + this->getConsView().forAllRows( function ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index 705bf5b82..e0326459a 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -554,6 +554,106 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > template< typename Function > void forAllElements( Function& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrix::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp + * \par Output + * \include DenseMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrixView::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrixView::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp + * \par Output + * \include DenseMatrixViewExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrixView::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrixView::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp + * \par Output + * \include DenseMatrixViewExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ); + + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref DenseMatrixView::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::DenseMatrixView::RowViewType. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp + * \par Output + * \include DenseMatrixViewExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index 94fa3d1e5..65dc7c6fc 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -385,6 +385,66 @@ forAllElements( Function& function ) this->forElements( 0, this->getRows(), function ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +DenseMatrixView< Real, Device, Index, Organization >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + auto values_view = this->values.getView(); + using SegmentViewType = typename SegmentsViewType::SegmentViewType; + auto f = [=] __cuda_callable__ ( SegmentViewType& segmentView ) mutable { + auto rowView = RowViewType( segmentView, values_view ); + function( rowView ); + }; + this->segments.forSegments( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +DenseMatrixView< Real, Device, Index, Organization >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + const auto values_view = this->values.getConstView(); + using SegmentViewType = typename SegmentsViewType::SegmentViewType; + auto f = [=] __cuda_callable__ ( SegmentViewType&& segmentView ) mutable { + const auto rowView = RowViewType( segmentView, values_view ); + function( rowView ); + }; + this->segments.forSegments( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +DenseMatrixView< Real, Device, Index, Organization >:: +forAllRows( Function&& function ) +{ + this->forRows( 0, this->getRows(), function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +DenseMatrixView< Real, Device, Index, Organization >:: +forAllRows( Function&& function ) const +{ + this->forRows( 0, this->getRows(), function ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h index 9cd7c3db0..61ab14ba5 100644 --- a/src/UnitTests/Matrices/DenseMatrixTest.h +++ b/src/UnitTests/Matrices/DenseMatrixTest.h @@ -769,6 +769,168 @@ void test_AddRow() EXPECT_EQ( m.getElement( 5, 4 ), 150 ); } +template< typename Matrix > +void test_ForElements() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + /* + * Sets up the following 8x3 sparse matrix: + * + * / 1 1 1 \ + * | 2 2 2 | + * | 3 3 3 | + * | 4 4 4 | + * | 5 5 5 | + * | 6 6 6 | + * | 7 7 7 | + * \ 8 8 8 / + */ + + const IndexType cols = 3; + const IndexType rows = 8; + + Matrix m( rows, cols ); + m.forAllElements( [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value, bool compute ) mutable { + value = rowIdx + 1.0; + columnIdx = localIdx; + } ); + + for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) + for( IndexType colIdx = 0; colIdx < cols; colIdx++ ) + EXPECT_EQ( m.getElement( rowIdx, colIdx ), rowIdx + 1.0 ); +} + +template< typename Matrix > +void test_ForRows() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + ///// + // Setup lower triangular matrix + const IndexType cols = 8; + const IndexType rows = 8; + + Matrix m( rows, cols ); + using RowViewType = typename Matrix::RowViewType; + m.forAllRows( [] __cuda_callable__ ( RowViewType& row ) mutable { + for( IndexType localIdx = 0; localIdx <= row.getRowIndex(); localIdx++ ) + { + row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); + row.setColumnIndex( localIdx, localIdx ); + } + } ); + + for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) + for( IndexType colIdx = 0; colIdx < cols; colIdx++ ) + { + if( colIdx <= rowIdx ) + EXPECT_EQ( m.getElement( rowIdx, colIdx ), rowIdx - colIdx + 1.0 ); + else + EXPECT_EQ( m.getElement( rowIdx, colIdx ), 0.0 ); + } +} + +template< typename Matrix > +void test_RowsReduction() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + /* + * Sets up the following 8x8 sparse matrix: + * + * / 1 2 3 0 4 5 0 1 \ 6 + * | 0 6 0 7 0 0 0 1 | 3 + * | 0 8 9 0 10 0 0 1 | 4 + * | 0 11 12 13 14 0 0 1 | 5 + * | 0 15 0 0 0 0 0 1 | 2 + * | 0 16 17 18 19 20 21 1 | 7 + * | 22 23 24 25 26 27 28 1 | 8 + * \ 29 30 31 32 33 34 35 36 / 8 + */ + + const IndexType rows = 8; + const IndexType cols = 8; + + Matrix m( { + { 1, 2, 3, 0, 4, 5, 0, 1 }, + { 0, 6, 0, 7, 0, 0, 0, 1 }, + { 0, 8, 9, 0, 10, 0, 0, 1 }, + { 0, 11, 12, 13, 14, 0, 0, 1 }, + { 0, 15, 0, 0, 0, 0, 0, 1 }, + { 0, 16, 17, 18, 19, 20, 21, 1 }, + { 22, 23, 24, 25, 26, 27, 28, 1 }, + { 29, 30, 31, 32, 33, 34, 35, 36 } } ); + typename Matrix::RowsCapacitiesType rowsCapacities{ 6, 3, 4, 5, 2, 7, 8, 8 }; + + RealType value = 1; + for( IndexType i = 0; i < 3; i++ ) // 0th row + m.setElement( 0, i, value++ ); + + m.setElement( 0, 4, value++ ); // 0th row + m.setElement( 0, 5, value++ ); + + m.setElement( 1, 1, value++ ); // 1st row + m.setElement( 1, 3, value++ ); + + for( IndexType i = 1; i < 3; i++ ) // 2nd row + m.setElement( 2, i, value++ ); + + m.setElement( 2, 4, value++ ); // 2nd row + + for( IndexType i = 1; i < 5; i++ ) // 3rd row + m.setElement( 3, i, value++ ); + + m.setElement( 4, 1, value++ ); // 4th row + + for( IndexType i = 1; i < 7; i++ ) // 5th row + m.setElement( 5, i, value++ ); + + for( IndexType i = 0; i < 7; i++ ) // 6th row + m.setElement( 6, i, value++ ); + + for( IndexType i = 0; i < 8; i++ ) // 7th row + m.setElement( 7, i, value++ ); + + for( IndexType i = 0; i < 7; i++ ) // 1s at the end of rows + m.setElement( i, 7, 1); + + //// + // Compute number of non-zero elements in rows. + typename Matrix::RowsCapacitiesType rowLengths( rows ); + auto rowLengths_view = rowLengths.getView(); + auto fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { + return ( value != 0.0 ); + }; + auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { + rowLengths_view[ rowIdx ] = value; + }; + m.allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + EXPECT_EQ( rowsCapacities, rowLengths ); + m.getCompressedRowLengths( rowLengths ); + EXPECT_EQ( rowsCapacities, rowLengths ); + + //// + // Compute max norm + TNL::Containers::Vector< RealType, DeviceType, IndexType > rowSums( rows ); + auto rowSums_view = rowSums.getView(); + auto max_fetch = [] __cuda_callable__ ( IndexType row, IndexType column, const RealType& value ) -> IndexType { + return TNL::abs( value ); + }; + auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { + rowSums_view[ rowIdx ] = value; + }; + m.allRowsReduction( max_fetch, std::plus<>{}, max_keep, 0 ); + const RealType maxNorm = TNL::max( rowSums ); + EXPECT_EQ( maxNorm, 260 ) ; // 29+30+31+32+33+34+35+36 +} + template< typename Matrix > void test_VectorProduct() { diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index e9aedce71..300a1f91e 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1434,7 +1434,7 @@ void test_ForRows() ///// // Setup lower triangular matrix - const IndexType cols = 3; + const IndexType cols = 8; const IndexType rows = 8; Matrix m( { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, cols ); -- GitLab From 484b0e687c98f6094f9b97b170e8fbaed724a05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 12:28:50 +0100 Subject: [PATCH 10/54] Fixing CMakeLists in Documentation/Examples/Matrices/SparseMatrix. --- Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index 25c72648f..03f033cb5 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -39,7 +39,7 @@ if( BUILD_CUDA ) set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) endforeach() else() - foreach( target IN ITEMS ${HOST_EXAMPLES} ) + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) add_executable( ${target} ${target}.cpp ) add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) -- GitLab From af967af89641e818990dbc88cd625214a8d26215 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 12:29:26 +0100 Subject: [PATCH 11/54] Refactoring CMakeLists in Documentation/Examples/Matrices/DenseMatrix. --- .../Matrices/DenseMatrix/CMakeLists.txt | 323 +++--------------- .../DenseMatrixExample_forRows.cpp | 37 ++ .../DenseMatrix/DenseMatrixExample_forRows.cu | 1 + .../DenseMatrixViewExample_forRows.cpp | 38 +++ .../DenseMatrixViewExample_forRows.cu | 1 + 5 files changed, 122 insertions(+), 278 deletions(-) create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cu diff --git a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt index c9d549188..36b25eeda 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt @@ -1,283 +1,50 @@ -IF( BUILD_CUDA ) - CUDA_ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list_cuda DenseMatrixExample_Constructor_init_list.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_Constructor_init_list_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_Constructor_init_list.out - OUTPUT DenseMatrixExample_Constructor_init_list.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_setElements_cuda DenseMatrixExample_setElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_setElements.out - OUTPUT DenseMatrixExample_setElements.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_getCompressedRowLengths_cuda DenseMatrixExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getCompressedRowLengths.out - OUTPUT DenseMatrixExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_getElementsCount_cuda DenseMatrixExample_getElementsCount.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getElementsCount_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getElementsCount.out - OUTPUT DenseMatrixExample_getElementsCount.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_getConstRow_cuda DenseMatrixExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getConstRow.out - OUTPUT DenseMatrixExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_getRow_cuda DenseMatrixExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getRow.out - OUTPUT DenseMatrixExample_getRow.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_setElement_cuda DenseMatrixExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_setElement.out - OUTPUT DenseMatrixExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_addElement_cuda DenseMatrixExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_addElement.out - OUTPUT DenseMatrixExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_getElement_cuda DenseMatrixExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getElement.out - OUTPUT DenseMatrixExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_rowsReduction_cuda DenseMatrixExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_rowsReduction.out - OUTPUT DenseMatrixExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_allRowsReduction_cuda DenseMatrixExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_allRowsReduction.out - OUTPUT DenseMatrixExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_forElements_cuda DenseMatrixExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forElements.out - OUTPUT DenseMatrixExample_forElements.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_forAllElements_cuda DenseMatrixExample_forAllElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forAllElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forAllElements.out - OUTPUT DenseMatrixExample_forAllElements.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_constructor_cuda DenseMatrixViewExample_constructor.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_constructor_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_constructor.out - OUTPUT DenseMatrixViewExample_constructor.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getCompressedRowLengths_cuda DenseMatrixViewExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getCompressedRowLengths.out - OUTPUT DenseMatrixViewExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getElementsCount_cuda DenseMatrixViewExample_getElementsCount.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElementsCount_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElementsCount.out - OUTPUT DenseMatrixViewExample_getElementsCount.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getConstRow_cuda DenseMatrixViewExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getConstRow.out - OUTPUT DenseMatrixViewExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getRow_cuda DenseMatrixViewExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getRow.out - OUTPUT DenseMatrixViewExample_getRow.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_setElement_cuda DenseMatrixViewExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_setElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_setElement.out - OUTPUT DenseMatrixViewExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_addElement_cuda DenseMatrixViewExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_addElement.out - OUTPUT DenseMatrixViewExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_getElement_cuda DenseMatrixViewExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElement.out - OUTPUT DenseMatrixViewExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_rowsReduction_cuda DenseMatrixViewExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_rowsReduction.out - OUTPUT DenseMatrixViewExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_allRowsReduction_cuda DenseMatrixViewExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_allRowsReduction.out - OUTPUT DenseMatrixViewExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_forElements_cuda DenseMatrixViewExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forElements.out - OUTPUT DenseMatrixViewExample_forElements.out ) +set( COMMON_EXAMPLES + DenseMatrixExample_Constructor_init_list + DenseMatrixExample_setElements + DenseMatrixExample_getCompressedRowLengths + DenseMatrixExample_getElementsCount + DenseMatrixExample_getConstRow + DenseMatrixExample_getRow + DenseMatrixExample_setElement + DenseMatrixExample_addElement + DenseMatrixExample_getElement + DenseMatrixExample_rowsReduction + DenseMatrixExample_allRowsReduction + DenseMatrixExample_forElements + DenseMatrixExample_forAllElements + DenseMatrixExample_forRows + DenseMatrixViewExample_constructor + DenseMatrixViewExample_getCompressedRowLengths + DenseMatrixViewExample_getElementsCount + DenseMatrixViewExample_getConstRow + DenseMatrixViewExample_getRow + DenseMatrixViewExample_setElement + DenseMatrixViewExample_addElement + DenseMatrixViewExample_getElement + DenseMatrixViewExample_rowsReduction + DenseMatrixViewExample_allRowsReduction + DenseMatrixViewExample_forElements + DenseMatrixViewExample_forRows + DenseMatrixViewExample_forAllElements +) - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_forAllElements_cuda DenseMatrixViewExample_forAllElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forAllElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forAllElements.out - OUTPUT DenseMatrixViewExample_forAllElements.out ) +if( BUILD_CUDA ) + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) + endforeach() +else() + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + add_executable( ${target} ${target}.cpp ) + add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) + endforeach() +endif() +IF( BUILD_CUDA ) + ADD_CUSTOM_TARGET( Run DenseMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() - ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_Constructor_init_list > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_Constructor_init_list.out - OUTPUT DenseMatrixExample_Constructor_init_list.out ) - - ADD_EXECUTABLE( DenseMatrixExample_setElements DenseMatrixExample_setElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_setElements.out - OUTPUT DenseMatrixExample_setElements.out ) - - ADD_EXECUTABLE( DenseMatrixExample_getCompressedRowLengths DenseMatrixExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getCompressedRowLengths.out - OUTPUT DenseMatrixExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( DenseMatrixExample_getElementsCount DenseMatrixExample_getElementsCount.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getElementsCount > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getElementsCount.out - OUTPUT DenseMatrixExample_getElementsCount.out ) - - ADD_EXECUTABLE( DenseMatrixExample_getConstRow DenseMatrixExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getConstRow.out - OUTPUT DenseMatrixExample_getConstRow.out ) - - ADD_EXECUTABLE( DenseMatrixExample_getRow DenseMatrixExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getRow.out - OUTPUT DenseMatrixExample_getRow.out ) - - ADD_EXECUTABLE( DenseMatrixExample_setElement DenseMatrixExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_setElement.out - OUTPUT DenseMatrixExample_setElement.out ) - - ADD_EXECUTABLE( DenseMatrixExample_addElement DenseMatrixExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_addElement.out - OUTPUT DenseMatrixExample_addElement.out ) - - ADD_EXECUTABLE( DenseMatrixExample_getElement DenseMatrixExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_getElement.out - OUTPUT DenseMatrixExample_getElement.out ) - - ADD_EXECUTABLE( DenseMatrixExample_rowsReduction DenseMatrixExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_rowsReduction.out - OUTPUT DenseMatrixExample_rowsReduction.out ) - - ADD_EXECUTABLE( DenseMatrixExample_allRowsReduction DenseMatrixExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_allRowsReduction.out - OUTPUT DenseMatrixExample_allRowsReduction.out ) - - ADD_EXECUTABLE( DenseMatrixExample_forElements DenseMatrixExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forElements.out - OUTPUT DenseMatrixExample_forElements.out ) - - ADD_EXECUTABLE( DenseMatrixExample_forAllElements DenseMatrixExample_forAllElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forAllElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forAllElements.out - OUTPUT DenseMatrixExample_forAllElements.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_constructor DenseMatrixViewExample_constructor.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_constructor > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_constructor.out - OUTPUT DenseMatrixViewExample_constructor.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_getCompressedRowLengths DenseMatrixViewExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getCompressedRowLengths.out - OUTPUT DenseMatrixViewExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_getElementsCount DenseMatrixViewExample_getElementsCount.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElementsCount > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElementsCount.out - OUTPUT DenseMatrixViewExample_getElementsCount.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_getConstRow DenseMatrixViewExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getConstRow.out - OUTPUT DenseMatrixViewExample_getConstRow.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_getRow DenseMatrixViewExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getRow.out - OUTPUT DenseMatrixViewExample_getRow.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_setElement DenseMatrixViewExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_setElement.out - OUTPUT DenseMatrixViewExample_setElement.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_addElement DenseMatrixViewExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_addElement.out - OUTPUT DenseMatrixViewExample_addElement.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_getElement DenseMatrixViewExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_getElement.out - OUTPUT DenseMatrixViewExample_getElement.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_rowsReduction DenseMatrixViewExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_rowsReduction.out - OUTPUT DenseMatrixViewExample_rowsReduction.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_allRowsReduction DenseMatrixViewExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_allRowsReduction.out - OUTPUT DenseMatrixViewExample_allRowsReduction.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_forElements DenseMatrixViewExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forElements.out - OUTPUT DenseMatrixViewExample_forElements.out ) - - ADD_EXECUTABLE( DenseMatrixViewExample_forAllElements DenseMatrixViewExample_forAllElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_forAllElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_forAllElements.out - OUTPUT DenseMatrixViewExample_forAllElements.out ) - + ADD_CUSTOM_TARGET( Run DenseMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) ENDIF() -ADD_CUSTOM_TARGET( RunDenseMatricesExamples ALL DEPENDS - DenseMatrixExample_Constructor_init_list.out - DenseMatrixExample_setElements.out - DenseMatrixExample_getCompressedRowLengths.out - DenseMatrixExample_getElementsCount.out - DenseMatrixExample_getConstRow.out - DenseMatrixExample_getRow.out - DenseMatrixExample_setElement.out - DenseMatrixExample_addElement.out - DenseMatrixExample_getElement.out - DenseMatrixExample_rowsReduction.out - DenseMatrixExample_allRowsReduction.out - DenseMatrixExample_forElements.out - DenseMatrixExample_forAllElements.out - DenseMatrixViewExample_constructor.out - DenseMatrixViewExample_getCompressedRowLengths.out - DenseMatrixViewExample_getElementsCount.out - DenseMatrixViewExample_getConstRow.out - DenseMatrixViewExample_getRow.out - DenseMatrixViewExample_setElement.out - DenseMatrixViewExample_addElement.out - DenseMatrixViewExample_getElement.out - DenseMatrixViewExample_rowsReduction.out - DenseMatrixViewExample_allRowsReduction.out - DenseMatrixViewExample_forElements.out - DenseMatrixViewExample_forAllElements.out - -) - diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp new file mode 100644 index 000000000..0ddaa4d6c --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp @@ -0,0 +1,37 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + using RowViewType = typename MatrixType::RowViewType; + MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + + auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { + for( int localIdx = 0; + localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements + localIdx++ ) // than we requested. These padding elements are processed here as well. + // and so we cannot use row.getSize() + { + row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); + row.setColumnIndex( localIdx, localIdx ); + } + }; + matrix.forAllRows( f ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu new file mode 120000 index 000000000..f97a66ee3 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cu @@ -0,0 +1 @@ +DenseMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..9ec36d7ea --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; + using RowViewType = typename MatrixType::RowViewType; + MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + auto view = matrix.getView(); + + auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { + for( int localIdx = 0; + localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements + localIdx++ ) // than we requested. These padding elements are processed here as well. + // and so we cannot use row.getSize() + { + row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); + row.setColumnIndex( localIdx, localIdx ); + } + }; + view.forAllRows( f ); + + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cu new file mode 120000 index 000000000..8111505a3 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_forRows.cpp \ No newline at end of file -- GitLab From 7e7a9e5de95bdc88461ed319f2532560ae402351 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 12:38:41 +0100 Subject: [PATCH 12/54] Fixing Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt. --- Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt index 36b25eeda..9656d54a7 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt @@ -43,8 +43,8 @@ else() endif() IF( BUILD_CUDA ) - ADD_CUSTOM_TARGET( Run DenseMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) + ADD_CUSTOM_TARGET( RunDenseMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() - ADD_CUSTOM_TARGET( Run DenseMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) + ADD_CUSTOM_TARGET( RunDenseMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) ENDIF() -- GitLab From f5cf459f841f0ad667a78d45e11572f3dce4d6f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 12:39:13 +0100 Subject: [PATCH 13/54] Refactoring Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt. --- .../Matrices/LambdaMatrix/CMakeLists.txt | 114 +++++------------- 1 file changed, 28 insertions(+), 86 deletions(-) diff --git a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt index e295c3fcc..cbb621b46 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt @@ -1,90 +1,32 @@ -ADD_EXECUTABLE( LambdaMatrixExample_Constructor LambdaMatrixExample_Constructor.cpp ) -ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_Constructor > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_Constructor.out - OUTPUT LambdaMatrixExample_Constructor.out ) - -ADD_EXECUTABLE( LambdaMatrixExample_getCompressedRowLengths LambdaMatrixExample_getCompressedRowLengths.cpp ) -ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_getCompressedRowLengths.out - OUTPUT LambdaMatrixExample_getCompressedRowLengths.out ) +set( COMMON_EXAMPLES + LambdaMatrixExample_Constructor + LambdaMatrixExample_getCompressedRowLengths + LambdaMatrixExample_getNonzeroElementsCount + LambdaMatrixExample_Laplace + LambdaMatrixExample_Laplace_2 + LambdaMatrixExample_rowsReduction + LambdaMatrixExample_allRowsReduction + LambdaMatrixExample_forElements + LambdaMatrixExample_forAllElements + #LambdaMatrixExample_forRows +) -ADD_EXECUTABLE( LambdaMatrixExample_getNonzeroElementsCount LambdaMatrixExample_getNonzeroElementsCount.cpp ) -ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_getNonzeroElementsCount > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_getNonzeroElementsCount.out - OUTPUT LambdaMatrixExample_getNonzeroElementsCount.out ) +if( BUILD_CUDA ) + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) + endforeach() +else() + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + add_executable( ${target} ${target}.cpp ) + add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) + endforeach() +endif() IF( BUILD_CUDA ) - CUDA_ADD_EXECUTABLE( LambdaMatrixExample_Laplace_cuda LambdaMatrixExample_Laplace.cu ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_Laplace_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_Laplace.out - OUTPUT LambdaMatrixExample_Laplace.out ) - - CUDA_ADD_EXECUTABLE( LambdaMatrixExample_Laplace_2_cuda LambdaMatrixExample_Laplace_2.cu ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_Laplace_2_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_Laplace_2.out - OUTPUT LambdaMatrixExample_Laplace_2.out ) - - CUDA_ADD_EXECUTABLE( LambdaMatrixExample_rowsReduction_cuda LambdaMatrixExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_rowsReduction.out - OUTPUT LambdaMatrixExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( LambdaMatrixExample_allRowsReduction_cuda LambdaMatrixExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_allRowsReduction.out - OUTPUT LambdaMatrixExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( LambdaMatrixExample_forElements_cuda LambdaMatrixExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forElements.out - OUTPUT LambdaMatrixExample_forElements.out ) - - CUDA_ADD_EXECUTABLE( LambdaMatrixExample_forAllElements_cuda LambdaMatrixExample_forAllElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forAllElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forAllElements.out - OUTPUT LambdaMatrixExample_forAllElements.out ) - + ADD_CUSTOM_TARGET( RunLambdaMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() - ADD_EXECUTABLE( LambdaMatrixExample_Laplace LambdaMatrixExample_Laplace.cpp ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_Laplace > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_Laplace.out - OUTPUT LambdaMatrixExample_Laplace.out ) - - ADD_EXECUTABLE( LambdaMatrixExample_Laplace_2 LambdaMatrixExample_Laplace_2.cpp ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_Laplace_2 > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_Laplace_2.out - OUTPUT LambdaMatrixExample_Laplace_2.out ) - - ADD_EXECUTABLE( LambdaMatrixExample_rowsReduction LambdaMatrixExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_rowsReduction.out - OUTPUT LambdaMatrixExample_rowsReduction.out ) - - ADD_EXECUTABLE( LambdaMatrixExample_allRowsReduction LambdaMatrixExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_allRowsReduction.out - OUTPUT LambdaMatrixExample_allRowsReduction.out ) - - ADD_EXECUTABLE( LambdaMatrixExample_forElements LambdaMatrixExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forElements.out - OUTPUT LambdaMatrixExample_forElements.out ) - - ADD_EXECUTABLE( LambdaMatrixExample_forAllElements LambdaMatrixExample_forAllElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND LambdaMatrixExample_forAllElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/LambdaMatrixExample_forAllElements.out - OUTPUT LambdaMatrixExample_forAllElements.out ) -ENDIF() - -ADD_CUSTOM_TARGET( RunLambdaMatricesExamples ALL DEPENDS - LambdaMatrixExample_Constructor.out - LambdaMatrixExample_Laplace.out - LambdaMatrixExample_Laplace_2.out - LambdaMatrixExample_getCompressedRowLengths.out - LambdaMatrixExample_getNonzeroElementsCount.out - LambdaMatrixExample_rowsReduction.out - LambdaMatrixExample_allRowsReduction.out - LambdaMatrixExample_forElements.out - LambdaMatrixExample_forAllElements.out -) - + ADD_CUSTOM_TARGET( RunLambdaMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) +ENDIF() \ No newline at end of file -- GitLab From de910258567c0d317f3095baef94563c23f1746a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 20:57:07 +0100 Subject: [PATCH 14/54] Fixed sequentialForRows in DenseMatrix. --- src/TNL/Matrices/DenseMatrixView.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index 65dc7c6fc..d33aaf602 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -455,7 +455,7 @@ DenseMatrixView< Real, Device, Index, Organization >:: sequentialForRows( IndexType begin, IndexType end, Function& function ) const { for( IndexType row = begin; row < end; row ++ ) - this->forElements( row, row + 1, function ); + this->forRows( row, row + 1, function ); } template< typename Real, @@ -468,7 +468,7 @@ DenseMatrixView< Real, Device, Index, Organization >:: sequentialForRows( IndexType begin, IndexType end, Function& function ) { for( IndexType row = begin; row < end; row ++ ) - this->forElements( row, row + 1, function ); + this->forRows( row, row + 1, function ); } template< typename Real, -- GitLab From a11b65025a3d3ff934934fe4ba2202dba63b4bad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 20:57:35 +0100 Subject: [PATCH 15/54] Fixed sequentialForRows in SparseMatrix. --- src/TNL/Matrices/SparseMatrixView.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 1c0098825..9f09aebae 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -740,7 +740,7 @@ SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: sequentialForRows( IndexType begin, IndexType end, Function& function ) const { for( IndexType row = begin; row < end; row ++ ) - this->forElements( row, row + 1, function ); + this->forRows( row, row + 1, function ); } template< typename Real, @@ -755,7 +755,7 @@ SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: sequentialForRows( IndexType begin, IndexType end, Function& function ) { for( IndexType row = begin; row < end; row ++ ) - this->forElements( row, row + 1, function ); + this->forRows( row, row + 1, function ); } template< typename Real, -- GitLab From ed7aa7cda308871ec53b367212bef8d1e0d7896f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 20:58:06 +0100 Subject: [PATCH 16/54] Added lambda matrix row view. --- src/TNL/Matrices/LambdaMatrixRowView.h | 182 +++++++++++++++++++++++ src/TNL/Matrices/LambdaMatrixRowView.hpp | 134 +++++++++++++++++ 2 files changed, 316 insertions(+) create mode 100644 src/TNL/Matrices/LambdaMatrixRowView.h create mode 100644 src/TNL/Matrices/LambdaMatrixRowView.hpp diff --git a/src/TNL/Matrices/LambdaMatrixRowView.h b/src/TNL/Matrices/LambdaMatrixRowView.h new file mode 100644 index 000000000..56578f0b1 --- /dev/null +++ b/src/TNL/Matrices/LambdaMatrixRowView.h @@ -0,0 +1,182 @@ + /*************************************************************************** + LambdaMatrixRowView.h - description + ------------------- + begin : Mar 17, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +#include + +namespace TNL { +namespace Matrices { + +/** + * \brief RowView is a simple structure for accessing rows of Lambda matrix. + * + * \tparam MatrixElementsLambda is a lambda function returning matrix elements values and positions. + * + * It has the following form: + * + * ``` + * auto matrixElements = [] __cuda_callable__ ( Index rows, Index columns, Index rowIdx, Index localIdx, Index& columnIdx, Real& value ) { ... } + * ``` + * + * where \e rows is the number of matrix rows, \e columns is the number of matrix columns, \e rowIdx is the index of matrix row being queried, + * \e localIdx is the rank of the non-zero element in given row, \e columnIdx is a column index of the matrix element computed by + * this lambda and \e value is a value of the matrix element computed by this lambda. + * \tparam CompressedRowLengthsLambda is a lambda function returning a number of non-zero elements in each row. + * + * It has the following form: + * + * ``` + * auto rowLengths = [] __cuda_callable__ ( Index rows, Index columns, Index rowIdx ) -> IndexType { ... } + * ``` + * + * where \e rows is the number of matrix rows, \e columns is the number of matrix columns and \e rowIdx is an index of the row being queried. + * + * \tparam Real is a type of matrix elements values. + * \tparam Index is a type to be used for indexing. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp + * \par Output + * \include LambdaMatrixExample_getRow.out + */ +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real = double, + typename Index = int > +class LambdaMatrixRowView +{ + public: + + /** + * \brief The type of matrix elements. + */ + using RealType = Real; + + /** + * \brief The type used for matrix elements indexing. + */ + using IndexType = Index; + + /** + * \brief Type of the lambda function returning the matrix elements. + */ + using MatrixElementsLambdaType = MatrixElementsLambda; + + /** + * \brief Type of the lambda function returning the number of non-zero elements in each row. + */ + using CompressedRowLengthsLambdaType = CompressedRowLengthsLambda; + + /** + * \brief Type of Lambda matrix row view. + */ + using RowViewType = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType >; + + /** + * \brief Type of constant Lambda matrix row view. + */ + using ConstRowViewType = RowViewType; + + /** + * \brief Constructor with related lambda functions, matrix dimensions and row index. + * + * \param matrixElementsLambda is a constant reference to the lambda function evaluating matrix elements. + * \param compressedRowLengthsLambda is a constant reference to the lambda function returning the number of nonzero elements in each row. + * \param rows is number of matrix rows. + * \param columns is number of matrix columns. + * \param rowIdx is the matrix row index. + */ + __cuda_callable__ + LambdaMatrixRowView( const MatrixElementsLambdaType& matrixElementsLambda, + const CompressedRowLengthsLambdaType& compressedRowLengthsLambda, + const IndexType& rows, + const IndexType& columns, + const IndexType& rowIdx ); + + /** + * \brief Returns size of the matrix row, i.e. number of matrix elements in this row. + * + * \return Size of the matrix row. + */ + __cuda_callable__ + IndexType getSize() const; + + /** + * \brief Returns the matrix row index. + * + * \return matrix row index. + */ + __cuda_callable__ + const IndexType& getRowIndex() const; + + /** + * \brief Returns constants reference to a column index of an element with given rank in the row. + * + * \param localIdx is the rank of the non-zero element in given row. + * + * \return constant reference to the matrix element column index. + */ + __cuda_callable__ + IndexType getColumnIndex( const IndexType localIdx ) const; + + /** + * \brief Returns constants reference to value of an element with given rank in the row. + * + * \param localIdx is the rank of the non-zero element in given row. + * + * \return constant reference to the matrix element value. + */ + __cuda_callable__ + RealType getValue( const IndexType localIdx ) const; + + /** + * \brief Comparison of two matrix rows. + * + * The other matrix row can be from any other matrix. + * + * \param other is another matrix row. + * \return \e true if both rows are the same, \e false otherwise. + */ + template< typename MatrixElementsLambda_, + typename CompressedRowLengthsLambda_, + typename Real_, + typename Index_ > + __cuda_callable__ + bool operator==( const LambdaMatrixRowView< MatrixElementsLambda_, CompressedRowLengthsLambda_, Real_, Index_ >& other ) const; + + protected: + + const MatrixElementsLambda& matrixElementsLambda; + + const CompressedRowLengthsLambda& compressedRowLengthsLambda; + + IndexType rowIdx, rows, columns; +}; + +/** + * \brief Insertion operator for a Lambda matrix row. + * + * \param str is an output stream. + * \param row is an input Lambda matrix row. + * \return reference to the output stream. + */ +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +std::ostream& operator<<( std::ostream& str, const LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >& row ); + +} // namespace Matrices +} // namespace TNL + +#include diff --git a/src/TNL/Matrices/LambdaMatrixRowView.hpp b/src/TNL/Matrices/LambdaMatrixRowView.hpp new file mode 100644 index 000000000..fce6076cc --- /dev/null +++ b/src/TNL/Matrices/LambdaMatrixRowView.hpp @@ -0,0 +1,134 @@ +/*************************************************************************** + LambdaMatrixRowView.hpp - description + ------------------- + begin : Mar 17, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include +#include + +namespace TNL { +namespace Matrices { + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +LambdaMatrixRowView( const MatrixElementsLambdaType& matrixElementsLambda, + const CompressedRowLengthsLambdaType& compressedRowLengthsLambda, + const IndexType& rows, + const IndexType& columns, + const IndexType& rowIdx ) + : matrixElementsLambda( matrixElementsLambda ), + compressedRowLengthsLambda( compressedRowLengthsLambda ), + rows( rows ), + columns( columns ), + rowIdx( rowIdx ) +{ +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +getSize() const -> IndexType +{ + return this->compressedRowLengthsLambda( this->rows, this->columns, this->rowIdx ); +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ +auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +getRowIndex() const -> const IndexType& +{ + return this->rowIdx; +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +getColumnIndex( const IndexType localIdx ) const -> IndexType +{ + TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." ); + RealType value; + IndexType columnIdx; + this->matrixElementsLambda( this->rows, this->columns, this->rowIdx, localIdx, columnIdx, value ); + return columnIdx; +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +getValue( const IndexType localIdx ) const -> RealType +{ + TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." ); + RealType value; + IndexType columnIdx; + this->matrixElementsLambda( this->rows, this->columns, this->rowIdx, localIdx, columnIdx, value ); + return value; +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > + template< typename MatrixElementsLambda_, + typename CompressedRowLengthsLambda_, + typename Real_, + typename Index_ > +__cuda_callable__ +bool +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +operator==( const LambdaMatrixRowView< MatrixElementsLambda_, CompressedRowLengthsLambda_, Real_, Index_ >& other ) const +{ + IndexType i = 0; + while( i < getSize() && i < other.getSize() ) { + if( getColumnIndex( i ) != other.getColumnIndex( i ) ) + return false; + ++i; + } + for( IndexType j = i; j < getSize(); j++ ) + // TODO: use ... != getPaddingIndex() + if( getColumnIndex( j ) >= 0 ) + return false; + for( IndexType j = i; j < other.getSize(); j++ ) + // TODO: use ... != getPaddingIndex() + if( other.getColumnIndex( j ) >= 0 ) + return false; + return true; +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +std::ostream& operator<<( std::ostream& str, const LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >& row ) +{ + using NonConstIndex = std::remove_const_t< typename LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >::IndexType >; + for( NonConstIndex i = 0; i < row.getSize(); i++ ) + str << " [ " << row.getColumnIndex( i ) << " ] = " << row.getValue( i ) << ", "; + return str; +} + +} // namespace Matrices +} // namespace TNL -- GitLab From c25f2e0a05d1b4a001c8fa728e3d846ea2f35114 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Wed, 17 Mar 2021 21:00:48 +0100 Subject: [PATCH 17/54] Added for[,All]Rows to lambda matrix. --- src/TNL/Matrices/LambdaMatrix.h | 226 +++++++++++++++----- src/TNL/Matrices/LambdaMatrix.hpp | 95 ++++++-- src/UnitTests/Matrices/LambdaMatrixTest.h | 8 + src/UnitTests/Matrices/LambdaMatrixTest.hpp | 58 +++++ 4 files changed, 310 insertions(+), 77 deletions(-) diff --git a/src/TNL/Matrices/LambdaMatrix.h b/src/TNL/Matrices/LambdaMatrix.h index 83bd76da1..2bcd412cb 100644 --- a/src/TNL/Matrices/LambdaMatrix.h +++ b/src/TNL/Matrices/LambdaMatrix.h @@ -13,6 +13,7 @@ #include #include #include +#include namespace TNL { namespace Matrices { @@ -25,24 +26,30 @@ namespace Matrices { * * \tparam MatrixElementsLambda is a lambda function returning matrix elements values and positions. * - * It has the following form: + * \tparam MatrixElementsLambda is a lambda function returning matrix elements values and positions. + * + * It has the following form: * - * `matrixElements( Index rows, Index columns, Index rowIdx, Index localIdx, Index& columnIdx, Real& value )` + * ``` + * auto matrixElements = [] __cuda_callable__ ( Index rows, Index columns, Index rowIdx, Index localIdx, Index& columnIdx, Real& value ) { ... } + * ``` * * where \e rows is the number of matrix rows, \e columns is the number of matrix columns, \e rowIdx is the index of matrix row being queried, * \e localIdx is the rank of the non-zero element in given row, \e columnIdx is a column index of the matrix element computed by * this lambda and \e value is a value of the matrix element computed by this lambda. * \tparam CompressedRowLengthsLambda is a lambda function returning a number of non-zero elements in each row. * - * It has the following form: + * It has the following form: * - * `rowLengths( Index rows, Index columns, Index rowIdx ) -> IndexType` + * ``` + * auto rowLengths = [] __cuda_callable__ ( Index rows, Index columns, Index rowIdx ) -> IndexType { ... } + * ``` * * where \e rows is the number of matrix rows, \e columns is the number of matrix columns and \e rowIdx is an index of the row being queried. * * \tparam Real is a type of matrix elements values. * \tparam Device is a device on which the lambda functions will be evaluated. - * \ẗparam Index is a type to be used for indexing. + * \tparam Index is a type to be used for indexing. */ template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, @@ -68,6 +75,26 @@ class LambdaMatrix */ using IndexType = Index; + /** + * \brief Type of the lambda function returning the matrix elements. + */ + using MatrixElementsLambdaType = MatrixElementsLambda; + + /** + * \brief Type of the lambda function returning the number of non-zero elements in each row. + */ + using CompressedRowLengthsLambdaType = CompressedRowLengthsLambda; + + /** + * \brief Type of Lambda matrix row view. + */ + using RowViewType = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType >; + + /** + * \brief Type of constant Lambda matrix row view. + */ + using ConstRowViewType = RowViewType; + static constexpr bool isSymmetric() { return false; }; static constexpr bool isBinary() { return false; }; @@ -130,7 +157,7 @@ class LambdaMatrix * \param columns is the number of matrix columns. */ void setDimensions( const IndexType& rows, - const IndexType& columns ); + const IndexType& columns ); /** * \brief Returns a number of matrix rows. @@ -148,6 +175,22 @@ class LambdaMatrix __cuda_callable__ IndexType getColumns() const; + /** + * \brief Get reference to the lambda function returning number of non-zero elements in each row. + * + * \return constant reference to CompressedRowLengthsLambda. + */ + __cuda_callable__ + const CompressedRowLengthsLambda& getCompressedRowLengthsLambda() const; + + /** + * \brief Get reference to the lambda function returning the matrix elements values and column indexes. + * + * \return constant reference to MatrixElementsLambda. + */ + __cuda_callable__ + const MatrixElementsLambda& getMatrixElementsLambda() const; + /** * \brief Compute capacities of all rows. * @@ -185,67 +228,32 @@ class LambdaMatrix IndexType getNonzeroElementsCount() const; /** - * \brief Returns value of matrix element at position given by its row and column index. + * \brief Getter of simple structure for accessing given matrix row. * - * \param row is a row index of the matrix element. - * \param column i a column index of the matrix element. + * \param rowIdx is matrix row index. * - * \return value of given matrix element. - */ - RealType getElement( const IndexType row, - const IndexType column ) const; - - /** - * \brief Method for performing general reduction on matrix rows. - * - * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. - * The return type of this lambda can be any non void. - * \tparam Reduce is a type of lambda function for reduction declared as - * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. - * \tparam Keep is a type of lambda function for storing results of reduction in each row. - * It is declared as `keep( const IndexType rowIdx, const double& value )`. - * \tparam FetchValue is type returned by the Fetch lambda function. - * - * \param begin defines beginning of the range [begin,end) of rows to be processed. - * \param end defines ending of the range [begin,end) of rows to be processed. - * \param fetch is an instance of lambda function for data fetch. - * \param reduce is an instance of lambda function for reduction. - * \param keep in an instance of lambda function for storing results. - * \param zero is zero of given reduction operation also known as idempotent element. + * \return RowView for accessing given matrix row. * * \par Example - * \include Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp + * \include Matrices/SparseMatrix/LambdaMatrixExample_getRow.cpp * \par Output - * \include LambdaMatrixExample_rowsReduction.out + * \include LambdaMatrixExample_getRow.out + * + * See \ref LambdaMatrixRowView. */ - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + __cuda_callable__ + const RowViewType getRow( const IndexType& rowIdx ) const; /** - * \brief Method for performing general reduction on ALL matrix rows. - * - * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. - * The return type of this lambda can be any non void. - * \tparam Reduce is a type of lambda function for reduction declared as - * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. - * \tparam Keep is a type of lambda function for storing results of reduction in each row. - * It is declared as `keep( const IndexType rowIdx, const double& value )`. - * \tparam FetchValue is type returned by the Fetch lambda function. + * \brief Returns value of matrix element at position given by its row and column index. * - * \param fetch is an instance of lambda function for data fetch. - * \param reduce is an instance of lambda function for reduction. - * \param keep in an instance of lambda function for storing results. - * \param zero is zero of given reduction operation also known as idempotent element. + * \param row is a row index of the matrix element. + * \param column i a column index of the matrix element. * - * \par Example - * \include Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp - * \par Output - * \include LambdaMatrixExample_allRowsReduction.out + * \return value of given matrix element. */ - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + RealType getElement( const IndexType row, + const IndexType column ) const; /** * \brief Method for iteration over all matrix rows for constant instances. @@ -285,6 +293,56 @@ class LambdaMatrix template< typename Function > void forAllElements( Function& function ) const; + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref LambdaMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::LambdaMatrix::RowViewType. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp + * \par Output + * \include LambdaMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref LambdaMatrix::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::LambdaMatrix::RowViewType. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp + * \par Output + * \include LambdaMatrixExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * @@ -300,7 +358,7 @@ class LambdaMatrix * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForRows( IndexType begin, IndexType end, Function& function ) const; + void sequentialForRows( IndexType begin, IndexType end, Function&& function ) const; /** * \brief This method calls \e sequentialForRows for all matrix rows (for constant instances). @@ -311,7 +369,59 @@ class LambdaMatrix * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForAllRows( Function& function ) const; + void sequentialForAllRows( Function&& function ) const; + + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp + * \par Output + * \include LambdaMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + + /** + * \brief Method for performing general reduction on ALL matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp + * \par Output + * \include LambdaMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Computes product of matrix and vector. diff --git a/src/TNL/Matrices/LambdaMatrix.hpp b/src/TNL/Matrices/LambdaMatrix.hpp index aab21b085..9a9225b5f 100644 --- a/src/TNL/Matrices/LambdaMatrix.hpp +++ b/src/TNL/Matrices/LambdaMatrix.hpp @@ -87,6 +87,32 @@ getColumns() const return this->columns; } +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Device, + typename Index > +__cuda_callable__ +const CompressedRowLengthsLambda& +LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: +getCompressedRowLengthsLambda() const +{ + return this->compressedRowLengthsLambda; +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Device, + typename Index > +__cuda_callable__ +const MatrixElementsLambda& +LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: +getMatrixElementsLambda() const +{ + return this->matrixElementsLambda; +} + template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real, @@ -171,6 +197,23 @@ getElement( const IndexType row, return valueView.getElement( 0 ); } +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Device, + typename Index > +__cuda_callable__ +auto +LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: +getRow( const IndexType& rowIdx ) const -> const RowViewType +{ + return RowViewType( this->getMatrixElementsLambda(), + this->getCompressedRowLengthsLambda(), + this->getRows(), + this->getColumns(), + rowIdx ); +} + template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real, @@ -298,23 +341,37 @@ LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, In forAllElements( Function& function ) const { forElements( 0, this->getRows(), function ); - /*const IndexType rows = this->getRows(); - const IndexType columns = this->getColumns(); - auto rowLengths = this->compressedRowLengthsLambda; - auto matrixElements = this->matrixElementsLambda; - auto processRow = [=] __cuda_callable__ ( IndexType rowIdx ) mutable { - const IndexType rowLength = rowLengths( rows, columns, rowIdx ); - bool compute( true ); - for( IndexType localIdx = 0; localIdx < rowLength && compute; localIdx++ ) - { - IndexType elementColumn( 0 ); - RealType elementValue( 0.0 ); - matrixElements( rows, columns, rowIdx, localIdx, elementColumn, elementValue ); - if( elementValue != 0.0 ) - function( rowIdx, localIdx, elementColumn, elementValue, compute ); - } +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Device, + typename Index > + template< typename Function > +void +LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = *this; + auto f = [=] __cuda_callable__ ( const IndexType rowIdx ) mutable { + auto rowView = view.getRow( rowIdx ); + function( rowView ); }; - Algorithms::ParallelFor< DeviceType >::exec( 0, this->getRows(), processRow );*/ + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Device, + typename Index > + template< typename Function > +void +LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: +forAllRows( Function&& function ) const +{ + this->forRows( 0, this->getRows(), function ); } template< typename MatrixElementsLambda, @@ -325,10 +382,10 @@ template< typename MatrixElementsLambda, template< typename Function > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -sequentialForRows( IndexType begin, IndexType end, Function& function ) const +sequentialForRows( IndexType begin, IndexType end, Function&& function ) const { for( IndexType row = begin; row < end; row ++ ) - this->forElements( row, row + 1, function ); + this->forRows( row, row + 1, function ); } template< typename MatrixElementsLambda, @@ -339,7 +396,7 @@ template< typename MatrixElementsLambda, template< typename Function > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -sequentialForAllRows( Function& function ) const +sequentialForAllRows( Function&& function ) const { sequentialForRows( 0, this->getRows(), function ); } diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.h b/src/UnitTests/Matrices/LambdaMatrixTest.h index cc2893d9b..25d2be160 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.h +++ b/src/UnitTests/Matrices/LambdaMatrixTest.h @@ -89,6 +89,14 @@ TYPED_TEST( LambdaMatrixTest, getElementTest ) test_GetElement< LambdaMatrixParametersType >(); } +TYPED_TEST( LambdaMatrixTest, getRowTest ) +{ + using LambdaMatrixParametersType = typename TestFixture::LambdaMatrixType; + + test_GetRow< LambdaMatrixParametersType >(); +} + + TYPED_TEST( LambdaMatrixTest, vectorProductTest ) { using LambdaMatrixParametersType = typename TestFixture::LambdaMatrixType; diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index 256a84bf7..af8d9f6b6 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -13,6 +13,7 @@ #ifdef HAVE_GTEST #include +#include template< typename Matrix > void test_Constructors() @@ -161,6 +162,63 @@ void test_GetElement() EXPECT_EQ( m.getElement( 4, 4 ), 1.0 ); } +template< typename Matrix > +void test_GetRow() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + ///// + // Prepare lambda matrix of the following form: + // + // / 1 0 0 0 0 \ + // | -2 1 -2 0 0 | + // | 0 -2 1 -2 0 | + // | 0 0 -2 1 -2 | + // | 0 0 0 -2 1 | + // \ 0 0 0 0 1 / + + IndexType size = 5; + auto rowLengths = [=] __cuda_callable__ ( const IndexType rows, const IndexType columns, const IndexType rowIdx ) -> IndexType { + if( rowIdx == 0 || rowIdx == size - 1 ) + return 1; + return 3; + }; + + auto matrixElements = [=] __cuda_callable__ ( const IndexType rows, const IndexType columns, const IndexType rowIdx, const IndexType localIdx, IndexType& columnIdx, RealType& value ) { + if( rowIdx == 0 || rowIdx == size -1 ) + { + columnIdx = rowIdx; + value = 1.0; + } + else + { + columnIdx = rowIdx + localIdx - 1; + value = ( columnIdx == rowIdx ) ? -2.0 : 1.0; + } + }; + + using MatrixType = decltype( TNL::Matrices::LambdaMatrixFactory< RealType, DeviceType, IndexType >::create( matrixElements, rowLengths ) ); + + MatrixType m( size, size, matrixElements, rowLengths ); + + TNL::Matrices::DenseMatrix< RealType, DeviceType, IndexType > denseMatrix( size, size ); + denseMatrix.setValue( 0.0 ); + auto dense_view = denseMatrix.getView(); + auto f = [=] __cuda_callable__ ( const typename MatrixType::RowViewType& row ) mutable { + auto dense_row = dense_view.getRow( row.getRowIndex() ); + for( IndexType localIdx = 0; localIdx < row.getSize(); localIdx++ ) + dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); + }; + m.forAllRows( f ); + + for( IndexType row = 0; row < size; row++ ) + for( IndexType column = 0; column < size; column++ ) + EXPECT_EQ( m.getElement( row, column ), denseMatrix.getElement( row, column ) ); + +} + template< typename Matrix > void test_VectorProduct() { -- GitLab From 1e882f4ce13369124cfe538d967493d7449ecac7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 18 Mar 2021 14:20:21 +0100 Subject: [PATCH 18/54] Added const_cast to ArrayView constructor. I am not sure about this solution, it will need discussion. --- src/TNL/Containers/ArrayView.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/TNL/Containers/ArrayView.h b/src/TNL/Containers/ArrayView.h index 4b3846037..793683ef7 100644 --- a/src/TNL/Containers/ArrayView.h +++ b/src/TNL/Containers/ArrayView.h @@ -143,7 +143,8 @@ public: template< typename Value_ > __cuda_callable__ ArrayView( const ArrayView< Value_, Device, Index >& view ) - : data(view.getData()), size(view.getSize()) {} + : data( const_cast< Value* >( view.getData()) ), size(view.getSize()) {} + // ^ TODO: Is it correct? Check implementation of recursive constant proxy. /** * \brief Move constructor for initialization from \e rvalues. -- GitLab From 1bcdc8924fcb80942f99a3585b72917b8cbb9aad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 18 Mar 2021 14:20:42 +0100 Subject: [PATCH 19/54] Fixes of matrix row views. --- src/TNL/Matrices/DenseMatrix.h | 208 +++++++++++------------ src/TNL/Matrices/DenseMatrix.hpp | 3 +- src/TNL/Matrices/MatrixView.h | 2 +- src/TNL/Matrices/MatrixWriter.hpp | 16 +- src/TNL/Matrices/SparseMatrix.h | 10 +- src/TNL/Matrices/SparseMatrix.hpp | 4 +- src/TNL/Matrices/SparseMatrixRowView.h | 2 +- src/TNL/Matrices/SparseMatrixRowView.hpp | 2 +- src/TNL/Matrices/SparseMatrixView.h | 2 +- src/TNL/Matrices/SparseMatrixView.hpp | 8 +- 10 files changed, 132 insertions(+), 125 deletions(-) diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 8aea413d9..4364f3bbc 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -432,110 +432,6 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > Real getElement( const IndexType row, const IndexType column ) const; - /** - * \brief Method for performing general reduction on matrix rows. - * - * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. - * The return type of this lambda can be any non void. - * \tparam Reduce is a type of lambda function for reduction declared as - * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. - * \tparam Keep is a type of lambda function for storing results of reduction in each row. - * It is declared as `keep( const IndexType rowIdx, const double& value )`. - * \tparam FetchValue is type returned by the Fetch lambda function. - * - * \param begin defines beginning of the range [begin,end) of rows to be processed. - * \param end defines ending of the range [begin,end) of rows to be processed. - * \param fetch is an instance of lambda function for data fetch. - * \param reduce is an instance of lambda function for reduction. - * \param keep in an instance of lambda function for storing results. - * \param zero is zero of given reduction operation also known as idempotent element. - * - * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp - * \par Output - * \include DenseMatrixExample_rowsReduction.out - */ - template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ); - - /** - * \brief Method for performing general reduction on matrix rows for constant instances. - * - * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. - * The return type of this lambda can be any non void. - * \tparam Reduce is a type of lambda function for reduction declared as - * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. - * \tparam Keep is a type of lambda function for storing results of reduction in each row. - * It is declared as `keep( const IndexType rowIdx, const double& value )`. - * \tparam FetchValue is type returned by the Fetch lambda function. - * - * \param begin defines beginning of the range [begin,end) of rows to be processed. - * \param end defines ending of the range [begin,end) of rows to be processed. - * \param fetch is an instance of lambda function for data fetch. - * \param reduce is an instance of lambda function for reduction. - * \param keep in an instance of lambda function for storing results. - * \param zero is zero of given reduction operation also known as idempotent element. - * - * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp - * \par Output - * \include DenseMatrixExample_rowsReduction.out - */ - template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const; - - /** - * \brief Method for performing general reduction on ALL matrix rows. - * - * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. - * The return type of this lambda can be any non void. - * \tparam Reduce is a type of lambda function for reduction declared as - * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. - * \tparam Keep is a type of lambda function for storing results of reduction in each row. - * It is declared as `keep( const IndexType rowIdx, const double& value )`. - * \tparam FetchValue is type returned by the Fetch lambda function. - * - * \param fetch is an instance of lambda function for data fetch. - * \param reduce is an instance of lambda function for reduction. - * \param keep in an instance of lambda function for storing results. - * \param zero is zero of given reduction operation also known as idempotent element. - * - * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp - * \par Output - * \include DenseMatrixExample_allRowsReduction.out - */ - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); - - /** - * \brief Method for performing general reduction on ALL matrix rows for constant instances. - * - * \tparam Fetch is a type of lambda function for data fetch declared as - * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. - * The return type of this lambda can be any non void. - * \tparam Reduce is a type of lambda function for reduction declared as - * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. - * \tparam Keep is a type of lambda function for storing results of reduction in each row. - * It is declared as `keep( const IndexType rowIdx, const double& value )`. - * \tparam FetchValue is type returned by the Fetch lambda function. - * - * \param fetch is an instance of lambda function for data fetch. - * \param reduce is an instance of lambda function for reduction. - * \param keep in an instance of lambda function for storing results. - * \param zero is zero of given reduction operation also known as idempotent element. - * - * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp - * \par Output - * \include DenseMatrixExample_allRowsReduction.out - */ - template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; - /** * \brief Method for iteration over all matrix rows for constant instances. * @@ -768,6 +664,110 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Function > void sequentialForAllRows( Function& function ); + /** + * \brief Method for performing general reduction on matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp + * \par Output + * \include DenseMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ); + + /** + * \brief Method for performing general reduction on matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param begin defines beginning of the range [begin,end) of rows to be processed. + * \param end defines ending of the range [begin,end) of rows to be processed. + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp + * \par Output + * \include DenseMatrixExample_rowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > + void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const; + + /** + * \brief Method for performing general reduction on ALL matrix rows. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp + * \par Output + * \include DenseMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + + /** + * \brief Method for performing general reduction on ALL matrix rows for constant instances. + * + * \tparam Fetch is a type of lambda function for data fetch declared as + * `fetch( IndexType rowIdx, IndexType columnIdx, RealType elementValue ) -> FetchValue`. + * The return type of this lambda can be any non void. + * \tparam Reduce is a type of lambda function for reduction declared as + * `reduce( const FetchValue& v1, const FetchValue& v2 ) -> FetchValue`. + * \tparam Keep is a type of lambda function for storing results of reduction in each row. + * It is declared as `keep( const IndexType rowIdx, const double& value )`. + * \tparam FetchValue is type returned by the Fetch lambda function. + * + * \param fetch is an instance of lambda function for data fetch. + * \param reduce is an instance of lambda function for reduction. + * \param keep in an instance of lambda function for storing results. + * \param zero is zero of given reduction operation also known as idempotent element. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp + * \par Output + * \include DenseMatrixExample_allRowsReduction.out + */ + template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > + void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + /** * \brief Computes product of matrix and vector. * diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 0ed448337..851b7b2de 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -119,9 +119,10 @@ auto DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: getConstView() const -> ConstViewType { + DenseMatrix* this_ptr = const_cast< DenseMatrix* >( this ); return ConstViewType( this->getRows(), this->getColumns(), - this->getValues().getConstView() ); + this_ptr->getValues().getView() ); } template< typename Real, diff --git a/src/TNL/Matrices/MatrixView.h b/src/TNL/Matrices/MatrixView.h index 7d8d9102d..a1d5a86bc 100644 --- a/src/TNL/Matrices/MatrixView.h +++ b/src/TNL/Matrices/MatrixView.h @@ -38,7 +38,7 @@ class MatrixView : public Object using RowsCapacitiesType = Containers::Vector< Index, Device, Index >; using RowsCapacitiesTypeView = Containers::VectorView< Index, Device, Index >; using ConstRowsCapacitiesTypeView = typename RowsCapacitiesTypeView::ConstViewType; - using ValuesView = Containers::VectorView< Real, Device, Index >; + using ValuesView = Containers::VectorView< std::remove_const_t< Real >, Device, Index >; /** * \brief The type of matrix elements. diff --git a/src/TNL/Matrices/MatrixWriter.hpp b/src/TNL/Matrices/MatrixWriter.hpp index 97310c19e..98be21107 100644 --- a/src/TNL/Matrices/MatrixWriter.hpp +++ b/src/TNL/Matrices/MatrixWriter.hpp @@ -152,12 +152,18 @@ writeMtx( std::ostream& str, str << std::setw( 9 ) << matrix.getRows() << " " << std::setw( 9 ) << matrix.getColumns() << " " << std::setw( 12 ) << matrix.getNonzeroElementsCount() << std::endl; std::ostream* str_ptr = &str; auto cout_ptr = &std::cout; - auto f = [=] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType columnIdx, RealType value, bool& compute ) mutable { - if( value != 0 ) + auto f = [=] __cuda_callable__ ( const typename Matrix::ConstRowViewType& row ) mutable { + auto rowIdx = row.getRowIndex(); + for( IndexType localIdx = 0; localIdx < row.getSize(); localIdx++ ) { - *str_ptr << std::setw( 9 ) << rowIdx + 1 << std::setw( 9 ) << columnIdx + 1 << std::setw( 12 ) << value << std::endl; - if( verbose ) - *cout_ptr << "Drawing the row " << rowIdx << " \r" << std::flush; + IndexType columnIdx = row.getColumnIndex( localIdx ); + RealType value = row.getValue( localIdx ); + if( value != 0 ) + { + *str_ptr << std::setw( 9 ) << rowIdx + 1 << std::setw( 9 ) << columnIdx + 1 << std::setw( 12 ) << value << std::endl; + if( verbose ) + *cout_ptr << "Drawing the row " << rowIdx << " \r" << std::flush; + } } }; matrix.sequentialForAllRows( f ); diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index cf8bc2da5..e2e282ef5 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -61,14 +61,14 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > // Supporting types - they are not important for the user using BaseType = Matrix< Real, Device, Index, RealAllocator >; - using ValuesVectorType = typename Matrix< Real, Device, Index, RealAllocator >::ValuesVectorType; + using ValuesVectorType = typename Matrix< std::remove_const_t< Real >, Device, Index, RealAllocator >::ValuesVectorType; using ValuesViewType = typename ValuesVectorType::ViewType; using ConstValuesViewType = typename ValuesViewType::ConstViewType; - using ColumnsIndexesVectorType = Containers::Vector< Index, Device, Index, IndexAllocator >; + using ColumnsIndexesVectorType = Containers::Vector< std::remove_const_t< Index >, Device, Index, IndexAllocator >; using ColumnsIndexesViewType = typename ColumnsIndexesVectorType::ViewType; using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; - using RowsCapacitiesType = Containers::Vector< Index, Device, Index, IndexAllocator >; - using RowsCapacitiesView = Containers::VectorView< Index, Device, Index >; + using RowsCapacitiesType = Containers::Vector< std::remove_const_t< Index >, Device, Index, IndexAllocator >; + using RowsCapacitiesView = Containers::VectorView< std::remove_const_t< Index >, Device, Index >; using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType; /** @@ -88,7 +88,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief The type of matrix elements. */ - using RealType = Real; + using RealType = std::remove_const_t< Real >; using ComputeRealType = ComputeReal; diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 2c5f4eff8..beab59549 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -498,10 +498,10 @@ template< typename Real, typename RealAllocator, typename IndexAllocator > __cuda_callable__ -Real +auto SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: getElement( const IndexType row, - const IndexType column ) const + const IndexType column ) const -> RealType { return this->view.getElement( row, column ); } diff --git a/src/TNL/Matrices/SparseMatrixRowView.h b/src/TNL/Matrices/SparseMatrixRowView.h index 91381434f..5a3148af4 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.h +++ b/src/TNL/Matrices/SparseMatrixRowView.h @@ -123,7 +123,7 @@ class SparseMatrixRowView * \return matrix row index. */ __cuda_callable__ - const IndexType& getRowIndex(); + const IndexType& getRowIndex() const; /** * \brief Returns constants reference to a column index of an element with given rank in the row. diff --git a/src/TNL/Matrices/SparseMatrixRowView.hpp b/src/TNL/Matrices/SparseMatrixRowView.hpp index c31da8e70..5a9fd475e 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.hpp +++ b/src/TNL/Matrices/SparseMatrixRowView.hpp @@ -47,7 +47,7 @@ template< typename SegmentView, __cuda_callable__ auto SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >:: -getRowIndex() -> const IndexType& +getRowIndex() const -> const IndexType& { return segmentView.getSegmentIndex(); } diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index e6693b5d5..2766d6b13 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -97,7 +97,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief The type of matrix elements. */ - using RealType = Real; + using RealType = std::remove_const_t< Real >; using ComputeRealType = ComputeReal; diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index 9f09aebae..c69165828 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -348,10 +348,10 @@ template< typename Real, template< typename, typename > class SegmentsView, typename ComputeReal > __cuda_callable__ -Real +auto SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: getElement( IndexType row, - IndexType column ) const + IndexType column ) const -> RealType { TNL_ASSERT_GE( row, 0, "Sparse matrix row index cannot be negative." ); TNL_ASSERT_LT( row, this->getRows(), "Sparse matrix row index is larger than number of matrix rows." ); @@ -693,8 +693,8 @@ forRows( IndexType begin, IndexType end, Function&& function ) const const auto columns_view = this->columnIndexes.getConstView(); const auto values_view = this->values.getConstView(); using SegmentViewType = typename SegmentsViewType::SegmentViewType; - auto f = [=] __cuda_callable__ ( SegmentViewType&& segmentView ) mutable { - const auto rowView = RowViewType( segmentView, values_view, columns_view ); + auto f = [=] __cuda_callable__ ( const SegmentViewType& segmentView ) mutable { + const auto rowView = ConstRowViewType( segmentView, values_view, columns_view ); function( rowView ); }; this->segments.forSegments( begin, end, f ); -- GitLab From 4309f3f60bdfa99a44cf382ba7d314245a159940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 18 Mar 2021 17:52:45 +0100 Subject: [PATCH 20/54] Added forRows to MultidiagonalMatrix. --- src/TNL/Matrices/MultidiagonalMatrix.h | 108 ++++++++++++++++- src/TNL/Matrices/MultidiagonalMatrix.hpp | 60 +++++++++- src/TNL/Matrices/MultidiagonalMatrixRowView.h | 36 +++--- .../Matrices/MultidiagonalMatrixRowView.hpp | 12 +- src/TNL/Matrices/MultidiagonalMatrixView.h | 112 +++++++++++++++++- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 70 ++++++++++- .../Matrices/MultidiagonalMatrixTest.h | 55 +++++++++ 7 files changed, 423 insertions(+), 30 deletions(-) diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index 0e14a0660..dc06ed0a7 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -134,12 +134,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Type for accessing matrix rows. */ - using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; + using RowViewType = typename ViewType::RowViewType; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowView = typename RowView::ConstViewType; + using ConstRowViewType = typename ViewType::ConstViewType; /** * \brief Helper type for getting self type or its modifications. @@ -492,7 +492,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - RowView getRow( const IndexType& rowIdx ); + RowViewType getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -509,7 +509,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - const RowView getRow( const IndexType& rowIdx ) const; + const ConstRowViewType getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -801,6 +801,106 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Function > void forAllElements( Function& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrix::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ); + + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrix::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 283d12a18..6d94c7cd6 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -404,7 +404,7 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) const -> const RowView +getRow( const IndexType& rowIdx ) const -> const ConstRowViewType { return this->view.getRow( rowIdx ); } @@ -418,7 +418,7 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) -> RowView +getRow( const IndexType& rowIdx ) -> RowViewType { return this->view.getRow( rowIdx ); } @@ -580,6 +580,62 @@ forAllElements( Function& function ) this->view.forElements( 0, this->getRows(), function ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + this->getView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + this->getConstView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +forAllRows( Function&& function ) +{ + this->getView().forAllRows( function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator, + typename IndexAllocator > + template< typename Function > +void +MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: +forAllRows( Function&& function ) const +{ + this->getConsView().forAllRows( function ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.h b/src/TNL/Matrices/MultidiagonalMatrixRowView.h index 6c89ffc1b..88b976aa3 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.h @@ -11,24 +11,24 @@ #pragma once namespace TNL { -namespace Matrices { +namespace Matrices { /** * \brief RowView is a simple structure for accessing rows of multidiagonal matrix. - * + * * \tparam ValuesView is a vector view storing the matrix elements values. * \tparam Indexer is type of object responsible for indexing and organization of * matrix elements. * \tparam DiagonalsOffsetsView_ is a container view holding offsets of * diagonals of multidiagonal matrix. - * + * * See \ref MultidiagonalMatrix and \ref MultidiagonalMatrixView. - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp * \par Output * \include MultidiagonalatrixExample_getRow.out - * + * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cpp * \par Output @@ -90,7 +90,7 @@ class MultidiagonalMatrixRowView /** * \brief Constructor with all necessary data. - * + * * \param rowIdx is index of the matrix row this RowView refer to. * \param diagonalsOffsets is a vector view holding offsets of matrix diagonals, * \param values is a vector view holding values of matrix elements. @@ -104,17 +104,25 @@ class MultidiagonalMatrixRowView /** * \brief Returns number of diagonals of the multidiagonal matrix. - * + * * \return number of diagonals of the multidiagonal matrix. */ __cuda_callable__ IndexType getSize() const; + /** + * \brief Returns the matrix row index. + * + * \return matrix row index. + */ + __cuda_callable__ + const IndexType& getRowIndex() const; + /** * \brief Computes column index of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the subdiagonal. - * + * * \return column index of matrix element on given subdiagonal. */ __cuda_callable__ @@ -122,9 +130,9 @@ class MultidiagonalMatrixRowView /** * \brief Returns value of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the subdiagonal. - * + * * \return constant reference to matrix element value. */ __cuda_callable__ @@ -132,9 +140,9 @@ class MultidiagonalMatrixRowView /** * \brief Returns value of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the subdiagonal. - * + * * \return non-constant reference to matrix element value. */ __cuda_callable__ @@ -142,7 +150,7 @@ class MultidiagonalMatrixRowView /** * \brief Changes value of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the matrix subdiagonal. * \param value is the new value of the matrix element. */ diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp b/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp index 37cdd455a..f63a64b44 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp @@ -11,7 +11,7 @@ #pragma once namespace TNL { -namespace Matrices { +namespace Matrices { template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ @@ -33,6 +33,16 @@ getSize() const -> IndexType return diagonalsOffsets.getSize();//indexer.getRowSize( rowIdx ); } + +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > +__cuda_callable__ +auto +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: +getRowIndex() const -> const IndexType& +{ + return this->rowIdx; +} + template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ auto diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index 55c33e68a..94f1f1aef 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -76,7 +76,12 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing matrix rows. */ - using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; + using RowViewType = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; + + /** + * \brief Type for accessing constant matrix rows. + */ + using ConstRowViewType = typename RowViewType::ConstViewType; /** * \brief Helper type for getting self type or its modifications. @@ -251,7 +256,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - RowView getRow( const IndexType& rowIdx ); + RowViewType getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -268,7 +273,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - const RowView getRow( const IndexType& rowIdx ) const; + const ConstRowViewType getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -558,6 +563,107 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > template< typename Function > void forAllElements( Function& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrixView::forElements where more than one thread can be mapped to each row. + + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrixView::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrixView::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ); + + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref MultidiagonalMatrixView::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include MultidiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index a2d2a4a35..c21bb83bb 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -229,9 +229,9 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) const -> const RowView +getRow( const IndexType& rowIdx ) const -> const ConstRowViewType { - return RowView( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); + return ConstRowViewType( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); } template< typename Real, @@ -241,9 +241,9 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) -> RowView +getRow( const IndexType& rowIdx ) -> RowViewType { - return RowView( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); + return RowViewType( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); } template< typename Real, @@ -507,6 +507,64 @@ forAllElements( Function& function ) this->forElements( 0, this->indexer.getNonemptyRowsCount(), function ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +MultidiagonalMatrixView< Real, Device, Index, Organization >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + auto view = *this; + auto f = [=] __cuda_callable__ ( const IndexType rowIdx ) mutable { + auto rowView = view.getRow( rowIdx ); + function( rowView ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +MultidiagonalMatrixView< Real, Device, Index, Organization >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = *this; + auto f = [=] __cuda_callable__ ( const IndexType rowIdx ) mutable { + auto rowView = view.getRow( rowIdx ); + function( rowView ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +MultidiagonalMatrixView< Real, Device, Index, Organization >:: +forAllRows( Function&& function ) +{ + this->forRows( 0, this->getRows(), function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +MultidiagonalMatrixView< Real, Device, Index, Organization >:: +forAllRows( Function&& function ) const +{ + this->forRows( 0, this->getRows(), function ); +} + template< typename Real, typename Device, typename Index, @@ -517,7 +575,7 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: sequentialForRows( IndexType begin, IndexType end, Function& function ) const { for( IndexType row = begin; row < end; row ++ ) - this->forElements( row, row + 1, function ); + this->forRows( row, row + 1, function ); } template< typename Real, @@ -530,7 +588,7 @@ MultidiagonalMatrixView< Real, Device, Index, Organization >:: sequentialForRows( IndexType begin, IndexType end, Function& function ) { for( IndexType row = begin; row < end; row ++ ) - this->forElements( row, row + 1, function ); + this->forRows( row, row + 1, function ); } template< typename Real, diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index cd7538224..7cc384499 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -778,6 +778,53 @@ void test_AddRow() EXPECT_EQ( m.getElement( 5, 4 ), 0 ); } +template< typename Matrix > +void test_GetRow() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + ///// + // Prepare lambda matrix of the following form: + // + // / 1 -2 0 0 0 \ + // | -2 1 -2 0 0 | + // | 0 -2 1 -2 0 | + // | 0 0 -2 1 -2 | + // | 0 0 0 -2 1 | + // \ 0 0 0 0 1 / + + const IndexType size( 5 ); + Matrix m( size, size, { -1, 0, 1 } ); + + auto f = [=] __cuda_callable__ ( typename Matrix::RowViewType& row ) mutable { + const IndexType rowIdx = row.getRowIndex(); + if( rowIdx > 0 ) + row.setElement( 0, -2.0 ); + row.setElement( 1, 1.0 ); + if( rowIdx < size -1 ) + row.setElement( 2, -2.0 ); + }; + m.forAllRows( f ); + + for( IndexType row = 0; row < size; row++ ) + for( IndexType column = 0; column < size; column++ ) + { + const IndexType diff = row - column; + if( diff == 0 ) + EXPECT_EQ( m.getElement( row, column ), 1.0 ); + else if( diff == 1 && row > 0 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else if( diff == -1 && row < size - 1 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else + EXPECT_EQ( m.getElement( row, column ), 0.0 ); + } + +} + + template< typename Matrix > void test_VectorProduct() { @@ -1449,6 +1496,14 @@ TYPED_TEST( MatrixTest, addRowTest ) test_AddRow< MatrixType >(); } +TYPED_TEST( MatrixTest, getRowTest ) +{ + using MatrixType = typename TestFixture::MatrixType; + + test_GetRow< MatrixType >(); +} + + TYPED_TEST( MatrixTest, vectorProductTest ) { using MatrixType = typename TestFixture::MatrixType; -- GitLab From 603b69afec27ded0c39be151c43442697c962f20 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 18 Mar 2021 18:27:16 +0100 Subject: [PATCH 21/54] Added example for LambdaMatrix::forRows. --- .../Matrices/LambdaMatrix/CMakeLists.txt | 2 +- .../LambdaMatrixExample_forRows.cpp | 71 +++++++++++++++++++ .../LambdaMatrixExample_forRows.cu | 1 + 3 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu diff --git a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt index cbb621b46..5483d90f3 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt @@ -8,7 +8,7 @@ set( COMMON_EXAMPLES LambdaMatrixExample_allRowsReduction LambdaMatrixExample_forElements LambdaMatrixExample_forAllElements - #LambdaMatrixExample_forRows + LambdaMatrixExample_forRows ) if( BUILD_CUDA ) diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp new file mode 100644 index 000000000..eaba26dcf --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp @@ -0,0 +1,71 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void forRowsExample() +{ + /** + * Prepare lambda matrix of the following form: + * + * / 1 0 0 0 0 \ + * | -2 1 -2 0 0 | + * | 0 -2 1 -2 0 | + * | 0 0 -2 1 -2 | + * | 0 0 0 -2 1 | + * \ 0 0 0 0 1 / + */ + + int size = 5; + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { + if( rowIdx == 0 || rowIdx == size - 1 ) + return 1; + return 3; + }; + + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { + if( rowIdx == 0 || rowIdx == size -1 ) + { + columnIdx = rowIdx; + value = 1.0; + } + else + { + columnIdx = rowIdx + localIdx - 1; + value = ( columnIdx == rowIdx ) ? -2.0 : 1.0; + } + }; + + using MatrixType = decltype( TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( matrixElements, rowLengths ) ); + MatrixType matrix( size, size, matrixElements, rowLengths ); + + /** + * Use the `forRows` method to copy the matrix elements to a dense matrix. + */ + TNL::Matrices::DenseMatrix< double, Device, int > denseMatrix( size, size ); + denseMatrix.setValue( 0.0 ); + auto dense_view = denseMatrix.getView(); + auto f = [=] __cuda_callable__ ( const typename MatrixType::RowViewType& row ) mutable { + auto dense_row = dense_view.getRow( row.getRowIndex() ); + for( int localIdx = 0; localIdx < row.getSize(); localIdx++ ) + dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); + }; + matrix.forAllRows( f ); + + std::cout << "Lambda matrix looks as:" << std::endl << matrix << std::endl; + std::cout << "Dense matrix looks as:" << std::endl << denseMatrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Running example on CPU ... " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Running example on CUDA GPU ... " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu new file mode 120000 index 000000000..6df275619 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cu @@ -0,0 +1 @@ +LambdaMatrixExample_forRows.cpp \ No newline at end of file -- GitLab From e393684539338e27fb47d33aa89097d3d553d51b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 18 Mar 2021 21:31:02 +0100 Subject: [PATCH 22/54] Refactoring Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt. --- .../MultidiagonalMatrix/CMakeLists.txt | 335 +++--------------- 1 file changed, 49 insertions(+), 286 deletions(-) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index b05125805..9158509fa 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -1,290 +1,53 @@ -IF( BUILD_CUDA ) - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_cuda MultidiagonalMatrixExample_Constructor.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor.out - OUTPUT MultidiagonalMatrixExample_Constructor.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_1_cuda MultidiagonalMatrixExample_Constructor_init_list_1.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_1_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_1.out - OUTPUT MultidiagonalMatrixExample_Constructor_init_list_1.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_2_cuda MultidiagonalMatrixExample_Constructor_init_list_2.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_2_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_2.out - OUTPUT MultidiagonalMatrixExample_Constructor_init_list_2.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getSerializationType_cuda MultidiagonalMatrixExample_getSerializationType.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getSerializationType_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getSerializationType.out - OUTPUT MultidiagonalMatrixExample_getSerializationType.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements_cuda MultidiagonalMatrixExample_setElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out - OUTPUT MultidiagonalMatrixExample_setElements.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths_cuda MultidiagonalMatrixExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out - OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow_cuda MultidiagonalMatrixExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out - OUTPUT MultidiagonalMatrixExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow_cuda MultidiagonalMatrixExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out - OUTPUT MultidiagonalMatrixExample_getRow.out ) - +set( COMMON_EXAMPLES + MultidiagonalMatrixExample_Constructor + MultidiagonalMatrixExample_Constructor_init_list_1 + MultidiagonalMatrixExample_Constructor_init_list_2 + MultidiagonalMatrixExample_getSerializationType + MultidiagonalMatrixExample_setElements + MultidiagonalMatrixExample_getCompressedRowLengths +# MultidiagonalMatrixExample_getElementsCount + MultidiagonalMatrixExample_getConstRow + MultidiagonalMatrixExample_getRow # This example does not work with nvcc 10.1. Restore it here when it works. -# CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement_cuda MultidiagonalMatrixExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out -# OUTPUT MultidiagonalMatrixExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement_cuda MultidiagonalMatrixExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out - OUTPUT MultidiagonalMatrixExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement_cuda MultidiagonalMatrixExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out - OUTPUT MultidiagonalMatrixExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction_cuda MultidiagonalMatrixExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out - OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction_cuda MultidiagonalMatrixExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out - OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forElements_cuda MultidiagonalMatrixExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forElements.out - OUTPUT MultidiagonalMatrixExample_forElements.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllElements_cuda MultidiagonalMatrixExample_forAllElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllElements.out - OUTPUT MultidiagonalMatrixExample_forAllElements.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda MultidiagonalMatrixViewExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out - OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow_cuda MultidiagonalMatrixViewExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out - OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow_cuda MultidiagonalMatrixViewExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out - OUTPUT MultidiagonalMatrixViewExample_getRow.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement_cuda MultidiagonalMatrixViewExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out - OUTPUT MultidiagonalMatrixViewExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement_cuda MultidiagonalMatrixViewExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out - OUTPUT MultidiagonalMatrixViewExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement_cuda MultidiagonalMatrixViewExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out - OUTPUT MultidiagonalMatrixViewExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction_cuda MultidiagonalMatrixViewExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out - OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction_cuda MultidiagonalMatrixViewExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out - OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forElements_cuda MultidiagonalMatrixViewExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forElements.out - OUTPUT MultidiagonalMatrixViewExample_forElements.out ) +# MultidiagonalMatrixExample_setElement + MultidiagonalMatrixExample_addElement + MultidiagonalMatrixExample_getElement + MultidiagonalMatrixExample_rowsReduction + MultidiagonalMatrixExample_allRowsReduction + MultidiagonalMatrixExample_forElements + MultidiagonalMatrixExample_forAllElements + #MultidiagonalMatrixExample_forRows +# MultidiagonalMatrixViewExample_constructor + MultidiagonalMatrixViewExample_getCompressedRowLengths +# MultidiagonalMatrixViewExample_getElementsCount + MultidiagonalMatrixViewExample_getConstRow + MultidiagonalMatrixViewExample_getRow + MultidiagonalMatrixViewExample_setElement + MultidiagonalMatrixViewExample_addElement + MultidiagonalMatrixViewExample_getElement + MultidiagonalMatrixViewExample_rowsReduction + MultidiagonalMatrixViewExample_allRowsReduction + MultidiagonalMatrixViewExample_forElements + MultidiagonalMatrixViewExample_forAllElements + #MultidiagonalMatrixViewExample_forRows +) - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllElements_cuda MultidiagonalMatrixViewExample_forAllElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllElements.out - OUTPUT MultidiagonalMatrixViewExample_forAllElements.out ) +if( BUILD_CUDA ) + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) + endforeach() +else() + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + add_executable( ${target} ${target}.cpp ) + add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) + endforeach() +endif() +IF( BUILD_CUDA ) + ADD_CUSTOM_TARGET( RunMultidiagonalMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() - ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor MultidiagonalMatrixExample_Constructor.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor.out - OUTPUT MultidiagonalMatrixExample_Constructor.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_1 MultidiagonalMatrixExample_Constructor_init_list_1.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_1 > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_1.out - OUTPUT MultidiagonalMatrixExample_Constructor_init_list_1.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_Constructor_init_list_2 MultidiagonalMatrixExample_Constructor_init_list_2.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_Constructor_init_list_2 > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_Constructor_init_list_2.out - OUTPUT MultidiagonalMatrixExample_Constructor_init_list_2.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_getSerializationType MultidiagonalMatrixExample_getSerializationType.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getSerializationType > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getSerializationType.out - OUTPUT MultidiagonalMatrixExample_getSerializationType.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_setElements MultidiagonalMatrixExample_setElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElements.out - OUTPUT MultidiagonalMatrixExample_setElements.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_getCompressedRowLengths MultidiagonalMatrixExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getCompressedRowLengths.out - OUTPUT MultidiagonalMatrixExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_getConstRow MultidiagonalMatrixExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getConstRow.out - OUTPUT MultidiagonalMatrixExample_getConstRow.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_getRow MultidiagonalMatrixExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getRow.out - OUTPUT MultidiagonalMatrixExample_getRow.out ) - -# This example does not work with nvcc 10.1. Restore it here when it works. -# ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out -# OUTPUT MultidiagonalMatrixExample_setElement.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_addElement MultidiagonalMatrixExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_addElement.out - OUTPUT MultidiagonalMatrixExample_addElement.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_getElement MultidiagonalMatrixExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_getElement.out - OUTPUT MultidiagonalMatrixExample_getElement.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_rowsReduction MultidiagonalMatrixExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_rowsReduction.out - OUTPUT MultidiagonalMatrixExample_rowsReduction.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_allRowsReduction MultidiagonalMatrixExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_allRowsReduction.out - OUTPUT MultidiagonalMatrixExample_allRowsReduction.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_forElements MultidiagonalMatrixExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forElements.out - OUTPUT MultidiagonalMatrixExample_forElements.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixExample_forAllElements MultidiagonalMatrixExample_forAllElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_forAllElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_forAllElements.out - OUTPUT MultidiagonalMatrixExample_forAllElements.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getCompressedRowLengths MultidiagonalMatrixViewExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getCompressedRowLengths.out - OUTPUT MultidiagonalMatrixViewExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getConstRow MultidiagonalMatrixViewExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getConstRow.out - OUTPUT MultidiagonalMatrixViewExample_getConstRow.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getRow MultidiagonalMatrixViewExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getRow.out - OUTPUT MultidiagonalMatrixViewExample_getRow.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_setElement MultidiagonalMatrixViewExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_setElement.out - OUTPUT MultidiagonalMatrixViewExample_setElement.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_addElement MultidiagonalMatrixViewExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_addElement.out - OUTPUT MultidiagonalMatrixViewExample_addElement.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_getElement MultidiagonalMatrixViewExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_getElement.out - OUTPUT MultidiagonalMatrixViewExample_getElement.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_rowsReduction MultidiagonalMatrixViewExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_rowsReduction.out - OUTPUT MultidiagonalMatrixViewExample_rowsReduction.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_allRowsReduction MultidiagonalMatrixViewExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_allRowsReduction.out - OUTPUT MultidiagonalMatrixViewExample_allRowsReduction.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forElements MultidiagonalMatrixViewExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forElements.out - OUTPUT MultidiagonalMatrixViewExample_forElements.out ) - - ADD_EXECUTABLE( MultidiagonalMatrixViewExample_forAllElements MultidiagonalMatrixViewExample_forAllElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixViewExample_forAllElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixViewExample_forAllElements.out - OUTPUT MultidiagonalMatrixViewExample_forAllElements.out ) - -ENDIF() - - ADD_EXECUTABLE( MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND MultidiagonalMatrixExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/MultidiagonalMatrixExample_setElement.out - OUTPUT MultidiagonalMatrixExample_setElement.out ) - - -ADD_CUSTOM_TARGET( RunMultidiagonalMatricesExamples ALL DEPENDS - MultidiagonalMatrixExample_Constructor.out - MultidiagonalMatrixExample_Constructor_init_list_1.out - MultidiagonalMatrixExample_Constructor_init_list_2.out - MultidiagonalMatrixExample_getSerializationType.out - MultidiagonalMatrixExample_setElements.out - MultidiagonalMatrixExample_getCompressedRowLengths.out - MultidiagonalMatrixExample_getConstRow.out - MultidiagonalMatrixExample_getRow.out - MultidiagonalMatrixExample_setElement.out - MultidiagonalMatrixExample_addElement.out - MultidiagonalMatrixExample_getElement.out - MultidiagonalMatrixExample_rowsReduction.out - MultidiagonalMatrixExample_allRowsReduction.out - MultidiagonalMatrixExample_forElements.out - MultidiagonalMatrixExample_forAllElements.out - MultidiagonalMatrixViewExample_getCompressedRowLengths.out - MultidiagonalMatrixViewExample_getConstRow.out - MultidiagonalMatrixViewExample_getRow.out - MultidiagonalMatrixViewExample_setElement.out - MultidiagonalMatrixViewExample_addElement.out - MultidiagonalMatrixViewExample_getElement.out - MultidiagonalMatrixViewExample_rowsReduction.out - MultidiagonalMatrixViewExample_allRowsReduction.out - MultidiagonalMatrixViewExample_forElements.out - MultidiagonalMatrixViewExample_forAllElements.out -) - + ADD_CUSTOM_TARGET( RunMultidiagonalMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) +ENDIF() \ No newline at end of file -- GitLab From 55469dd7b05cbacd433c326d1690a17ebae19d4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 18 Mar 2021 21:42:28 +0100 Subject: [PATCH 23/54] Added examle for MultidiagonalMatrix[,View]::forRows. --- .../MultidiagonalMatrix/CMakeLists.txt | 4 +- .../MultidiagonalMatrixExample_forRows.cpp | 63 ++++++++++++++++++ .../MultidiagonalMatrixExample_forRows.cu | 1 + ...MultidiagonalMatrixViewExample_forRows.cpp | 65 +++++++++++++++++++ .../MultidiagonalMatrixViewExample_forRows.cu | 1 + .../details/MultidiagonalMatrixIndexer.h | 4 +- 6 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index 9158509fa..72397bcb7 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -16,7 +16,7 @@ set( COMMON_EXAMPLES MultidiagonalMatrixExample_allRowsReduction MultidiagonalMatrixExample_forElements MultidiagonalMatrixExample_forAllElements - #MultidiagonalMatrixExample_forRows + MultidiagonalMatrixExample_forRows # MultidiagonalMatrixViewExample_constructor MultidiagonalMatrixViewExample_getCompressedRowLengths # MultidiagonalMatrixViewExample_getElementsCount @@ -29,7 +29,7 @@ set( COMMON_EXAMPLES MultidiagonalMatrixViewExample_allRowsReduction MultidiagonalMatrixViewExample_forElements MultidiagonalMatrixViewExample_forAllElements - #MultidiagonalMatrixViewExample_forRows + MultidiagonalMatrixViewExample_forRows ) if( BUILD_CUDA ) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp new file mode 100644 index 000000000..d1f0d625e --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 -2 . . . \ -> { 0, 0, 1 } + * | -2 1 -2 . . | -> { 0, 2, 1 } + * | . -2 1 -2. . | -> { 3, 2, 1 } + * | . . -2 1 -2 | -> { 3, 2, 1 } + * \ . . . -2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -1, 0, 1 }. + */ + const int size = 5; + MatrixType matrix( + size, // number of matrix rows + size, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + + auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { + /*** + * 'forElements' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ---------- + * 0 / 1 -2 . . . \ -> { 0, 1, -2 } + * | -2 1 -2 . . | -> { -2, 1, -2 } + * | . -2 1 -2. . | -> { -2, 1, -2 } + * | . . -2 1 -2 | -> { -2, 1, -2 } + * \ . . . -2 1 / -> { -2, 1, 0 } + * + */ + const int& rowIdx = row.getRowIndex(); + row.setElement( 1, 1.0 ); + if( rowIdx > 0 ) + row.setElement( 0, -2.0 ); + if( rowIdx < size - 1 ) + row.setElement( 2, -2.0 ); + }; + matrix.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu new file mode 120000 index 000000000..aff0dad0c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..37e5f2724 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 -2 . . . \ -> { 0, 0, 1 } + * | -2 1 -2 . . | -> { 0, 2, 1 } + * | . -2 1 -2. . | -> { 3, 2, 1 } + * | . . -2 1 -2 | -> { 3, 2, 1 } + * \ . . . -2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -1, 0, 1 }. + */ + + const int size = 5; + MatrixType matrix( + size, // number of matrix rows + size, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { + /*** + * 'forElements' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ---------- + * 0 / 1 -2 . . . \ -> { 0, 1, -2 } + * | -2 1 -2 . . | -> { -2, 1, -2 } + * | . -2 1 -2. . | -> { -2, 1, -2 } + * | . . -2 1 -2 | -> { -2, 1, -2 } + * \ . . . -2 1 / -> { -2, 1, 0 } + * + */ + const int& rowIdx = row.getRowIndex(); + row.setElement( 1, 1.0 ); + if( rowIdx > 0 ) + row.setElement( 0, -2.0 ); + if( rowIdx < size - 1 ) + row.setElement( 2, -2.0 ); + }; + view.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu new file mode 120000 index 000000000..ec3f1ad70 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_forRows.cpp \ No newline at end of file diff --git a/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h b/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h index cb657e8df..0ba1f1488 100644 --- a/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h +++ b/src/TNL/Matrices/details/MultidiagonalMatrixIndexer.h @@ -34,7 +34,7 @@ class MultidiagonalMatrixIndexer const IndexType& columns, const IndexType& diagonals, const IndexType& nonemptyRows ) - : rows( rows ), + : rows( rows ), columns( columns ), diagonals( diagonals ), nonemptyRows( nonemptyRows ) {}; @@ -79,7 +79,7 @@ class MultidiagonalMatrixIndexer TNL_ASSERT_LT( localIdx, diagonals, "" ); TNL_ASSERT_GE( rowIdx, 0, "" ); TNL_ASSERT_LT( rowIdx, this->rows, "" ); - + if( RowMajorOrder ) return diagonals * rowIdx + localIdx; else -- GitLab From cd4f351ae09619a306b4ad3015768dc0580b416f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Klinkovsk=C3=BD?= Date: Thu, 18 Mar 2021 22:05:38 +0100 Subject: [PATCH 24/54] Lambda function for sequentialForAllRows does not have to be __cuda_callable__ and can capture by-reference --- src/TNL/Matrices/MatrixWriter.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/TNL/Matrices/MatrixWriter.hpp b/src/TNL/Matrices/MatrixWriter.hpp index 98be21107..ffe1c5a8d 100644 --- a/src/TNL/Matrices/MatrixWriter.hpp +++ b/src/TNL/Matrices/MatrixWriter.hpp @@ -151,8 +151,7 @@ writeMtx( std::ostream& str, str << "%%" << std::setw( 9 ) << " ROWS " << std::setw( 9 ) << " COLUMNS " << std::setw( 12 ) << " ELEMENTS " << std::endl; str << std::setw( 9 ) << matrix.getRows() << " " << std::setw( 9 ) << matrix.getColumns() << " " << std::setw( 12 ) << matrix.getNonzeroElementsCount() << std::endl; std::ostream* str_ptr = &str; - auto cout_ptr = &std::cout; - auto f = [=] __cuda_callable__ ( const typename Matrix::ConstRowViewType& row ) mutable { + auto f = [&] ( const typename Matrix::ConstRowViewType& row ) mutable { auto rowIdx = row.getRowIndex(); for( IndexType localIdx = 0; localIdx < row.getSize(); localIdx++ ) { @@ -162,7 +161,7 @@ writeMtx( std::ostream& str, { *str_ptr << std::setw( 9 ) << rowIdx + 1 << std::setw( 9 ) << columnIdx + 1 << std::setw( 12 ) << value << std::endl; if( verbose ) - *cout_ptr << "Drawing the row " << rowIdx << " \r" << std::flush; + std::cout << "Drawing the row " << rowIdx << " \r" << std::flush; } } }; -- GitLab From 33c03cf19d83b00eafdc78dbcf092c2e7752ded3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Thu, 18 Mar 2021 22:49:36 +0100 Subject: [PATCH 25/54] Fixed comments in LambdaMatrixTest. --- src/UnitTests/Matrices/LambdaMatrixTest.hpp | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index af8d9f6b6..b1d207da8 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -169,15 +169,16 @@ void test_GetRow() using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - ///// - // Prepare lambda matrix of the following form: - // - // / 1 0 0 0 0 \ - // | -2 1 -2 0 0 | - // | 0 -2 1 -2 0 | - // | 0 0 -2 1 -2 | - // | 0 0 0 -2 1 | - // \ 0 0 0 0 1 / + /** + * Prepare lambda matrix of the following form: + * + * / 1 0 0 0 0 \ + * | -2 1 -2 0 0 | + * | 0 -2 1 -2 0 | + * | 0 0 -2 1 -2 | + * | 0 0 0 -2 1 | + * \ 0 0 0 0 1 / + */ IndexType size = 5; auto rowLengths = [=] __cuda_callable__ ( const IndexType rows, const IndexType columns, const IndexType rowIdx ) -> IndexType { -- GitLab From 91c934c496bb01c69fc8bde930df57cc637b8b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 09:07:53 +0100 Subject: [PATCH 26/54] Revert "Lambda function for sequentialForAllRows does not have to be __cuda_callable__ and can capture by-reference" This reverts commit cb9ab5c06a7f344fd4db4725123e526ed0a93eb6. --- src/TNL/Matrices/MatrixWriter.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/TNL/Matrices/MatrixWriter.hpp b/src/TNL/Matrices/MatrixWriter.hpp index ffe1c5a8d..98be21107 100644 --- a/src/TNL/Matrices/MatrixWriter.hpp +++ b/src/TNL/Matrices/MatrixWriter.hpp @@ -151,7 +151,8 @@ writeMtx( std::ostream& str, str << "%%" << std::setw( 9 ) << " ROWS " << std::setw( 9 ) << " COLUMNS " << std::setw( 12 ) << " ELEMENTS " << std::endl; str << std::setw( 9 ) << matrix.getRows() << " " << std::setw( 9 ) << matrix.getColumns() << " " << std::setw( 12 ) << matrix.getNonzeroElementsCount() << std::endl; std::ostream* str_ptr = &str; - auto f = [&] ( const typename Matrix::ConstRowViewType& row ) mutable { + auto cout_ptr = &std::cout; + auto f = [=] __cuda_callable__ ( const typename Matrix::ConstRowViewType& row ) mutable { auto rowIdx = row.getRowIndex(); for( IndexType localIdx = 0; localIdx < row.getSize(); localIdx++ ) { @@ -161,7 +162,7 @@ writeMtx( std::ostream& str, { *str_ptr << std::setw( 9 ) << rowIdx + 1 << std::setw( 9 ) << columnIdx + 1 << std::setw( 12 ) << value << std::endl; if( verbose ) - std::cout << "Drawing the row " << rowIdx << " \r" << std::flush; + *cout_ptr << "Drawing the row " << rowIdx << " \r" << std::flush; } } }; -- GitLab From 2a41d28b502ca05521c1e478cb441808f4657221 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 09:08:42 +0100 Subject: [PATCH 27/54] Fixed comments in LambdaMatrixTest - again. --- src/UnitTests/Matrices/LambdaMatrixTest.hpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index b1d207da8..af6375126 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -172,12 +172,11 @@ void test_GetRow() /** * Prepare lambda matrix of the following form: * - * / 1 0 0 0 0 \ + * / 1 0 0 0 0 \. * | -2 1 -2 0 0 | * | 0 -2 1 -2 0 | * | 0 0 -2 1 -2 | - * | 0 0 0 -2 1 | - * \ 0 0 0 0 1 / + * \ 0 0 0 0 1 /. */ IndexType size = 5; -- GitLab From 3a919ad255d690e1b6ee9e525b5238fd78a76f43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 09:46:44 +0100 Subject: [PATCH 28/54] Fixed documentation in Multidiagonal matrix. --- src/TNL/Matrices/MultidiagonalMatrixView.h | 1 - 1 file changed, 1 deletion(-) diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index 94f1f1aef..08dc9e15c 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -568,7 +568,6 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method * \ref MultidiagonalMatrixView::forElements where more than one thread can be mapped to each row. - * * \tparam Function is type of the lambda function. * -- GitLab From 6dac07364b0c97229d7fb6cc04ca81643587225d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 09:47:36 +0100 Subject: [PATCH 29/54] Added forRows to tridiagonal matrix. --- src/TNL/Matrices/TridiagonalMatrix.h | 110 ++++++++++++++++- src/TNL/Matrices/TridiagonalMatrix.hpp | 56 ++++++++- src/TNL/Matrices/TridiagonalMatrixRowView.h | 51 +++++--- src/TNL/Matrices/TridiagonalMatrixRowView.hpp | 11 +- src/TNL/Matrices/TridiagonalMatrixView.h | 113 +++++++++++++++++- src/TNL/Matrices/TridiagonalMatrixView.hpp | 66 +++++++++- .../details/TridiagonalMatrixIndexer.h | 10 +- .../Matrices/TridiagonalMatrixTest.h | 53 ++++++++ 8 files changed, 433 insertions(+), 37 deletions(-) diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h index eab681bf0..933e84bb0 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.h +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -117,8 +117,12 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Type for accessing matrix rows. */ - using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; + using RowViewType = typename ViewType::RowViewType; + /** + * \brief Type for accessing constant matrix rows. + */ + using ConstRowViewType = typename ViewType::ConstRowViewType; /** * \brief Helper type for getting self type or its modifications. @@ -384,7 +388,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - RowView getRow( const IndexType& rowIdx ); + RowViewType getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -401,7 +405,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - const RowView getRow( const IndexType& rowIdx ) const; + const ConstRowViewType getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -681,6 +685,106 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > template< typename Function > void forAllElements( Function& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrix::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrix::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ); + + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrix::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * diff --git a/src/TNL/Matrices/TridiagonalMatrix.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp index c6f89e5b8..17b0c6501 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -283,7 +283,7 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getRow( const IndexType& rowIdx ) const -> const RowView +getRow( const IndexType& rowIdx ) const -> const ConstRowViewType { return this->view.getRow( rowIdx ); } @@ -296,7 +296,7 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getRow( const IndexType& rowIdx ) -> RowView +getRow( const IndexType& rowIdx ) -> RowViewType { return this->view.getRow( rowIdx ); } @@ -444,6 +444,58 @@ forAllElements( Function& function ) this->view.forElements( 0, this->getRows(), function ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + this->getView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + this->getConstView().forRows( begin, end, function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +forAllRows( Function&& function ) +{ + this->getView().forAllRows( function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization, + typename RealAllocator > + template< typename Function > +void +TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: +forAllRows( Function&& function ) const +{ + this->getConsView().forAllRows( function ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/TridiagonalMatrixRowView.h b/src/TNL/Matrices/TridiagonalMatrixRowView.h index 939e4893f..06d80cd28 100644 --- a/src/TNL/Matrices/TridiagonalMatrixRowView.h +++ b/src/TNL/Matrices/TridiagonalMatrixRowView.h @@ -11,22 +11,22 @@ #pragma once namespace TNL { -namespace Matrices { +namespace Matrices { /** * \brief RowView is a simple structure for accessing rows of tridiagonal matrix. - * + * * \tparam ValuesView is a vector view storing the matrix elements values. * \tparam Indexer is type of object responsible for indexing and organization of * matrix elements. - * + * * See \ref TridiagonalMatrix and \ref TridiagonalMatrixView. - * + * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_getRow.cpp * \par Output * \include TridiagonalatrixExample_getRow.out - * + * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp * \par Output @@ -59,9 +59,24 @@ class TridiagonalMatrixRowView */ using IndexerType = Indexer; + /** + * \brief Type of constant container view used for storing the matrix elements values. + */ + using ConstValuesViewType = typename ValuesViewType::ConstViewType; + + /** + * \brief Type of constant indexer view. + */ + using ConstIndexerViewType = typename Indexer::ConstType; + + /** + * \brief Type of constant sparse matrix row view. + */ + using ConstViewType = TridiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType >; + /** * \brief Constructor with all necessary data. - * + * * \param rowIdx is index of the matrix row this RowView refer to. * \param values is a vector view holding values of matrix elements. * \param indexer is object responsible for indexing and organization of matrix elements @@ -73,17 +88,25 @@ class TridiagonalMatrixRowView /** * \brief Returns number of diagonals of the tridiagonal matrix which is three. - * + * * \return number three. */ __cuda_callable__ IndexType getSize() const; + /** + * \brief Returns the matrix row index. + * + * \return matrix row index. + */ + __cuda_callable__ + const IndexType& getRowIndex() const; + /** * \brief Computes column index of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the subdiagonal. - * + * * \return column index of matrix element on given subdiagonal. */ __cuda_callable__ @@ -91,9 +114,9 @@ class TridiagonalMatrixRowView /** * \brief Returns value of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the subdiagonal. - * + * * \return constant reference to matrix element value. */ __cuda_callable__ @@ -101,9 +124,9 @@ class TridiagonalMatrixRowView /** * \brief Returns value of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the subdiagonal. - * + * * \return non-constant reference to matrix element value. */ __cuda_callable__ @@ -111,7 +134,7 @@ class TridiagonalMatrixRowView /** * \brief Changes value of matrix element on given subdiagonal. - * + * * \param localIdx is an index of the matrix subdiagonal. * \param value is the new value of the matrix element. */ diff --git a/src/TNL/Matrices/TridiagonalMatrixRowView.hpp b/src/TNL/Matrices/TridiagonalMatrixRowView.hpp index 80fc1a26d..7b502416c 100644 --- a/src/TNL/Matrices/TridiagonalMatrixRowView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixRowView.hpp @@ -11,7 +11,7 @@ #pragma once namespace TNL { -namespace Matrices { +namespace Matrices { template< typename ValuesView, typename Indexer > __cuda_callable__ @@ -32,6 +32,15 @@ getSize() const -> IndexType return indexer.getRowSize( rowIdx ); } +template< typename ValuesView, typename Indexer > +__cuda_callable__ +auto +TridiagonalMatrixRowView< ValuesView, Indexer >:: +getRowIndex() const -> const IndexType& +{ + return rowIdx; +} + template< typename ValuesView, typename Indexer > __cuda_callable__ auto diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index 92d977714..509ba240e 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -26,7 +26,7 @@ namespace Matrices { * matrix to lambda functions. SparseMatrix view can be also created in CUDA kernels. * * See \ref TridiagonalMatrix for more details. - * + * * \tparam Real is a type of matrix elements. * \tparam Device is a device where the matrix is allocated. * \tparam Index is a type for indexing of the matrix elements. @@ -74,7 +74,12 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing matrix rows. */ - using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; + using RowViewType = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; + + /** + * \brief Type for accessing constant matrix rows. + */ + using ConstRowViewType = typename RowViewType::ConstViewType; /** * \brief Helper type for getting self type or its modifications. @@ -238,7 +243,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - RowView getRow( const IndexType& rowIdx ); + RowViewType getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -255,7 +260,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - const RowView getRow( const IndexType& rowIdx ) const; + const ConstRowViewType getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -521,6 +526,106 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > template< typename Function > void forAllElements( Function& function ); + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrixView::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ); + + /** + * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end) for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrixView::forElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param begin defines beginning of the range [ \e begin,\e end ) of rows to be processed. + * \param end defines ending of the range [ \e begin, \e end ) of rows to be processed. + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forRows( IndexType begin, IndexType end, Function&& function ) const; + + /** + * \brief Method for parallel iteration over all matrix rows. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrixView::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ); + + /** + * \brief Method for parallel iteration over all matrix rows for constant instances. + * + * In each row, given lambda function is performed. Each row is processed by at most one thread unlike the method + * \ref TridiagonalMatrixView::forAllElements where more than one thread can be mapped to each row. + * + * \tparam Function is type of the lambda function. + * + * \param function is an instance of the lambda function to be called for each row. + * + * ``` + * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * ``` + * + * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * + * \par Example + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp + * \par Output + * \include TridiagonalMatrixViewExample_forRows.out + */ + template< typename Function > + void forAllRows( Function&& function ) const; + /** * \brief Method for sequential iteration over all matrix rows for constant instances. * diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index 226dfbae8..cdb20d703 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -183,9 +183,9 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) const -> const RowView +getRow( const IndexType& rowIdx ) const -> const ConstRowViewType { - return RowView( rowIdx, this->values.getView(), this->indexer ); + return ConstRowViewType( rowIdx, this->values.getView(), this->indexer ); } template< typename Real, @@ -195,9 +195,9 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) -> RowView +getRow( const IndexType& rowIdx ) -> RowViewType { - return RowView( rowIdx, this->values.getView(), this->indexer ); + return RowViewType( rowIdx, this->values.getView(), this->indexer ); } template< typename Real, @@ -478,6 +478,64 @@ forAllElements( Function& function ) this->forElements( 0, this->indexer.getNonemptyRowsCount(), function ); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +TridiagonalMatrixView< Real, Device, Index, Organization >:: +forRows( IndexType begin, IndexType end, Function&& function ) +{ + auto view = *this; + auto f = [=] __cuda_callable__ ( const IndexType rowIdx ) mutable { + auto rowView = view.getRow( rowIdx ); + function( rowView ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +TridiagonalMatrixView< Real, Device, Index, Organization >:: +forRows( IndexType begin, IndexType end, Function&& function ) const +{ + auto view = *this; + auto f = [=] __cuda_callable__ ( const IndexType rowIdx ) mutable { + auto rowView = view.getRow( rowIdx ); + function( rowView ); + }; + TNL::Algorithms::ParallelFor< DeviceType >::exec( begin, end, f ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +TridiagonalMatrixView< Real, Device, Index, Organization >:: +forAllRows( Function&& function ) +{ + this->forRows( 0, this->getRows(), function ); +} + +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Function > +void +TridiagonalMatrixView< Real, Device, Index, Organization >:: +forAllRows( Function&& function ) const +{ + this->forRows( 0, this->getRows(), function ); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/details/TridiagonalMatrixIndexer.h b/src/TNL/Matrices/details/TridiagonalMatrixIndexer.h index 670719314..002bb8a73 100644 --- a/src/TNL/Matrices/details/TridiagonalMatrixIndexer.h +++ b/src/TNL/Matrices/details/TridiagonalMatrixIndexer.h @@ -21,6 +21,7 @@ class TridiagonalMatrixIndexer public: using IndexType = Index; + using ConstType = TridiagonalMatrixIndexer< std::add_const_t< Index >, RowMajorOrder >; static constexpr bool getRowMajorOrder() { return RowMajorOrder; }; @@ -46,15 +47,6 @@ class TridiagonalMatrixIndexer __cuda_callable__ IndexType getRowSize( const IndexType rowIdx ) const { - /*if( rowIdx == 0 ) - return 2; - if( columns <= rows ) - { - if( rowIdx == columns - 1 ) - return 2; - if( rowIdx == columns ) - return 1; - }*/ return 3; }; diff --git a/src/UnitTests/Matrices/TridiagonalMatrixTest.h b/src/UnitTests/Matrices/TridiagonalMatrixTest.h index a52c7551c..3244c33e6 100644 --- a/src/UnitTests/Matrices/TridiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/TridiagonalMatrixTest.h @@ -729,6 +729,52 @@ void test_AddRow() EXPECT_EQ( m.getElement( 5, 4 ), 216 ); } +template< typename Matrix > +void test_forRows() +{ + using RealType = typename Matrix::RealType; + using DeviceType = typename Matrix::DeviceType; + using IndexType = typename Matrix::IndexType; + + /** + * Prepare lambda matrix of the following form: + * + * / 1 -2 0 0 0 \ + * | -2 1 -2 0 0 | + * | 0 -2 1 -2 0 | + * | 0 0 -2 1 -2 | + * \ 0 0 0 -2 1 / + */ + + const IndexType size( 5 ); + Matrix m( size, size ); + + auto f = [=] __cuda_callable__ ( typename Matrix::RowViewType& row ) mutable { + const IndexType rowIdx = row.getRowIndex(); + if( rowIdx > 0 ) + row.setElement( 0, -2.0 ); + row.setElement( 1, 1.0 ); + if( rowIdx < size -1 ) + row.setElement( 2, -2.0 ); + }; + m.forAllRows( f ); + + for( IndexType row = 0; row < size; row++ ) + for( IndexType column = 0; column < size; column++ ) + { + const IndexType diff = row - column; + if( diff == 0 ) + EXPECT_EQ( m.getElement( row, column ), 1.0 ); + else if( diff == 1 && row > 0 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else if( diff == -1 && row < size - 1 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else + EXPECT_EQ( m.getElement( row, column ), 0.0 ); + } +} + + template< typename Matrix > void test_VectorProduct() { @@ -1387,6 +1433,13 @@ TYPED_TEST( MatrixTest, addRowTest ) test_AddRow< MatrixType >(); } +TYPED_TEST( MatrixTest, forRowsTest ) +{ + using MatrixType = typename TestFixture::MatrixType; + + test_forRows< MatrixType >(); +} + TYPED_TEST( MatrixTest, vectorProductTest ) { using MatrixType = typename TestFixture::MatrixType; -- GitLab From 804edecb804dc4e10be49e831eb900a1dba030b6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 09:48:19 +0100 Subject: [PATCH 30/54] Fixed multidiagonal matrix test. --- .../Matrices/MultidiagonalMatrixTest.h | 27 +++++++++---------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index 7cc384499..0dadfe158 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -779,21 +779,21 @@ void test_AddRow() } template< typename Matrix > -void test_GetRow() +void test_ForRows() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; using IndexType = typename Matrix::IndexType; - ///// - // Prepare lambda matrix of the following form: - // - // / 1 -2 0 0 0 \ - // | -2 1 -2 0 0 | - // | 0 -2 1 -2 0 | - // | 0 0 -2 1 -2 | - // | 0 0 0 -2 1 | - // \ 0 0 0 0 1 / + /** + * Prepare lambda matrix of the following form: + * + * / 1 -2 0 0 0 \ + * | -2 1 -2 0 0 | + * | 0 -2 1 -2 0 | + * | 0 0 -2 1 -2 | + * \ 0 0 0 -2 1 / + */ const IndexType size( 5 ); Matrix m( size, size, { -1, 0, 1 } ); @@ -821,10 +821,8 @@ void test_GetRow() else EXPECT_EQ( m.getElement( row, column ), 0.0 ); } - } - template< typename Matrix > void test_VectorProduct() { @@ -1496,14 +1494,13 @@ TYPED_TEST( MatrixTest, addRowTest ) test_AddRow< MatrixType >(); } -TYPED_TEST( MatrixTest, getRowTest ) +TYPED_TEST( MatrixTest, forRowsTest ) { using MatrixType = typename TestFixture::MatrixType; - test_GetRow< MatrixType >(); + test_ForRows< MatrixType >(); } - TYPED_TEST( MatrixTest, vectorProductTest ) { using MatrixType = typename TestFixture::MatrixType; -- GitLab From 1473fd5fbb37d55b5088eb070af7763857091789 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 09:48:56 +0100 Subject: [PATCH 31/54] Editing src/UnitTests/Matrices/CMakeLists.txt. --- src/UnitTests/Matrices/CMakeLists.txt | 1 - 1 file changed, 1 deletion(-) diff --git a/src/UnitTests/Matrices/CMakeLists.txt b/src/UnitTests/Matrices/CMakeLists.txt index a65411fc0..ef639fc44 100644 --- a/src/UnitTests/Matrices/CMakeLists.txt +++ b/src/UnitTests/Matrices/CMakeLists.txt @@ -5,7 +5,6 @@ set( COMMON_TESTS DenseMatrixCopyTest TridiagonalMatrixTest MultidiagonalMatrixTest - SparseMatrixTest_CSRScalar SparseMatrixTest_CSRVector SparseMatrixTest_CSRHybrid -- GitLab From c8ad755bc019bdef8bb451263eab03af2c84e3b5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 09:49:27 +0100 Subject: [PATCH 32/54] Added missing files for multidiagonal matrix examples. --- .../Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cu | 1 + .../LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cu | 1 + 2 files changed, 2 insertions(+) create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cu create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cu diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cu new file mode 120000 index 000000000..b191c4ff1 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_Constructor.cu @@ -0,0 +1 @@ +LambdaMatrixExample_Constructor.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cu new file mode 120000 index 000000000..2427f3fb9 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getNonzeroElementsCount.cu @@ -0,0 +1 @@ +LambdaMatrixExample_getNonzeroElementsCount.cpp \ No newline at end of file -- GitLab From 044caa8f1a7e520563a1c8c1a6d0d062f282374f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 10:03:36 +0100 Subject: [PATCH 33/54] Added example for TridiagonalMatrix::forRows. --- .../Matrices/TridiagonalMatrix/CMakeLists.txt | 310 +++--------------- .../TridiagonalMatrixExample_forRows.cpp | 63 ++++ .../TridiagonalMatrixExample_forRows.cu | 1 + .../TridiagonalMatrixViewExample_forRows.cpp | 65 ++++ .../TridiagonalMatrixViewExample_forRows.cu | 1 + 5 files changed, 176 insertions(+), 264 deletions(-) create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu create mode 100644 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt index 44cd5e336..2cd34daf1 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt @@ -1,269 +1,51 @@ -IF( BUILD_CUDA ) - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list_1_cuda TridiagonalMatrixExample_Constructor_init_list_1.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list_1_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list_1.out - OUTPUT TridiagonalMatrixExample_Constructor_init_list_1.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getSerializationType_cuda TridiagonalMatrixExample_getSerializationType.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getSerializationType_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getSerializationType.out - OUTPUT TridiagonalMatrixExample_getSerializationType.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_setElements_cuda TridiagonalMatrixExample_setElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out - OUTPUT TridiagonalMatrixExample_setElements.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths_cuda TridiagonalMatrixExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out - OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow_cuda TridiagonalMatrixExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out - OUTPUT TridiagonalMatrixExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getRow_cuda TridiagonalMatrixExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out - OUTPUT TridiagonalMatrixExample_getRow.out ) - +set( COMMON_EXAMPLES + TridiagonalMatrixExample_Constructor_init_list_1 + TridiagonalMatrixExample_getSerializationType + TridiagonalMatrixExample_setElements + TridiagonalMatrixExample_getCompressedRowLengths +# TridiagonalMatrixExample_getElementsCount + TridiagonalMatrixExample_getConstRow + TridiagonalMatrixExample_getRow # This example does not work with nvcc 10.1. Restore it here when it works. -# CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_setElement_cuda TridiagonalMatrixExample_setElement.cu ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement_cuda > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out -# OUTPUT TridiagonalMatrixExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_addElement_cuda TridiagonalMatrixExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out - OUTPUT TridiagonalMatrixExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_getElement_cuda TridiagonalMatrixExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out - OUTPUT TridiagonalMatrixExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction_cuda TridiagonalMatrixExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out - OUTPUT TridiagonalMatrixExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction_cuda TridiagonalMatrixExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out - OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forElements_cuda TridiagonalMatrixExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forElements.out - OUTPUT TridiagonalMatrixExample_forElements.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixExample_forAllElements_cuda TridiagonalMatrixExample_forAllElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllElements.out - OUTPUT TridiagonalMatrixExample_forAllElements.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths_cuda TridiagonalMatrixViewExample_getCompressedRowLengths.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out - OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow_cuda TridiagonalMatrixViewExample_getConstRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out - OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow_cuda TridiagonalMatrixViewExample_getRow.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out - OUTPUT TridiagonalMatrixViewExample_getRow.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement_cuda TridiagonalMatrixViewExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out - OUTPUT TridiagonalMatrixViewExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement_cuda TridiagonalMatrixViewExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out - OUTPUT TridiagonalMatrixViewExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement_cuda TridiagonalMatrixViewExample_getElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out - OUTPUT TridiagonalMatrixViewExample_getElement.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction_cuda TridiagonalMatrixViewExample_rowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out - OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction_cuda TridiagonalMatrixViewExample_allRowsReduction.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out - OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) - - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forElements_cuda TridiagonalMatrixViewExample_forElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forElements.out - OUTPUT TridiagonalMatrixViewExample_forElements.out ) +# TridiagonalMatrixExample_setElement + TridiagonalMatrixExample_addElement + TridiagonalMatrixExample_getElement + TridiagonalMatrixExample_rowsReduction + TridiagonalMatrixExample_allRowsReduction + TridiagonalMatrixExample_forElements + TridiagonalMatrixExample_forAllElements + TridiagonalMatrixExample_forRows +# TridiagonalMatrixViewExample_constructor + TridiagonalMatrixViewExample_getCompressedRowLengths +# TridiagonalMatrixViewExample_getElementsCount + TridiagonalMatrixViewExample_getConstRow + TridiagonalMatrixViewExample_getRow + TridiagonalMatrixViewExample_setElement + TridiagonalMatrixViewExample_addElement + TridiagonalMatrixViewExample_getElement + TridiagonalMatrixViewExample_rowsReduction + TridiagonalMatrixViewExample_allRowsReduction + TridiagonalMatrixViewExample_forElements + TridiagonalMatrixViewExample_forAllElements + TridiagonalMatrixViewExample_forRows +) - CUDA_ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllElements_cuda TridiagonalMatrixViewExample_forAllElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllElements_cuda > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllElements.out - OUTPUT TridiagonalMatrixViewExample_forAllElements.out ) +if( BUILD_CUDA ) + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) + endforeach() +else() + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + add_executable( ${target} ${target}.cpp ) + add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) + endforeach() +endif() +IF( BUILD_CUDA ) + ADD_CUSTOM_TARGET( RunTridiagonalMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() - - ADD_EXECUTABLE( TridiagonalMatrixExample_Constructor_init_list_1 TridiagonalMatrixExample_Constructor_init_list_1.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_Constructor_init_list_1 > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_Constructor_init_list_1.out - OUTPUT TridiagonalMatrixExample_Constructor_init_list_1.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_getSerializationType TridiagonalMatrixExample_getSerializationType.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getSerializationType > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getSerializationType.out - OUTPUT TridiagonalMatrixExample_getSerializationType.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_setElements TridiagonalMatrixExample_setElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElements.out - OUTPUT TridiagonalMatrixExample_setElements.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_getCompressedRowLengths TridiagonalMatrixExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getCompressedRowLengths.out - OUTPUT TridiagonalMatrixExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_getConstRow TridiagonalMatrixExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getConstRow.out - OUTPUT TridiagonalMatrixExample_getConstRow.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_getRow TridiagonalMatrixExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getRow.out - OUTPUT TridiagonalMatrixExample_getRow.out ) - -# This example does not work with nvcc 10.1. Restore it here when it works. -# ADD_EXECUTABLE( TridiagonalMatrixExample_setElement TridiagonalMatrixExample_setElement.cpp ) -# ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out -# OUTPUT TridiagonalMatrixExample_setElement.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_addElement TridiagonalMatrixExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_addElement.out - OUTPUT TridiagonalMatrixExample_addElement.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_getElement TridiagonalMatrixExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_getElement.out - OUTPUT TridiagonalMatrixExample_getElement.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_rowsReduction TridiagonalMatrixExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_rowsReduction.out - OUTPUT TridiagonalMatrixExample_rowsReduction.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_allRowsReduction TridiagonalMatrixExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_allRowsReduction.out - OUTPUT TridiagonalMatrixExample_allRowsReduction.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_forElements TridiagonalMatrixExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forElements.out - OUTPUT TridiagonalMatrixExample_forElements.out ) - - ADD_EXECUTABLE( TridiagonalMatrixExample_forAllElements TridiagonalMatrixExample_forAllElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_forAllElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_forAllElements.out - OUTPUT TridiagonalMatrixExample_forAllElements.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_getCompressedRowLengths TridiagonalMatrixViewExample_getCompressedRowLengths.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getCompressedRowLengths > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getCompressedRowLengths.out - OUTPUT TridiagonalMatrixViewExample_getCompressedRowLengths.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_getConstRow TridiagonalMatrixViewExample_getConstRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getConstRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getConstRow.out - OUTPUT TridiagonalMatrixViewExample_getConstRow.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_getRow TridiagonalMatrixViewExample_getRow.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getRow > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getRow.out - OUTPUT TridiagonalMatrixViewExample_getRow.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_setElement TridiagonalMatrixViewExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_setElement.out - OUTPUT TridiagonalMatrixViewExample_setElement.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_addElement TridiagonalMatrixViewExample_addElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_addElement.out - OUTPUT TridiagonalMatrixViewExample_addElement.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_getElement TridiagonalMatrixViewExample_getElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_getElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_getElement.out - OUTPUT TridiagonalMatrixViewExample_getElement.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_rowsReduction TridiagonalMatrixViewExample_rowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_rowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_rowsReduction.out - OUTPUT TridiagonalMatrixViewExample_rowsReduction.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_allRowsReduction TridiagonalMatrixViewExample_allRowsReduction.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_allRowsReduction > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_allRowsReduction.out - OUTPUT TridiagonalMatrixViewExample_allRowsReduction.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_forElements TridiagonalMatrixViewExample_forElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forElements.out - OUTPUT TridiagonalMatrixViewExample_forElements.out ) - - ADD_EXECUTABLE( TridiagonalMatrixViewExample_forAllElements TridiagonalMatrixViewExample_forAllElements.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixViewExample_forAllElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixViewExample_forAllElements.out - OUTPUT TridiagonalMatrixViewExample_forAllElements.out ) - + ADD_CUSTOM_TARGET( RunTridiagonalMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) ENDIF() - - ADD_EXECUTABLE( TridiagonalMatrixExample_setElement TridiagonalMatrixExample_setElement.cpp ) - ADD_CUSTOM_COMMAND( COMMAND TridiagonalMatrixExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/TridiagonalMatrixExample_setElement.out - OUTPUT TridiagonalMatrixExample_setElement.out ) - - -ADD_CUSTOM_TARGET( RunTridiagonalMatricesExamples ALL DEPENDS - TridiagonalMatrixExample_Constructor_init_list_1.out - TridiagonalMatrixExample_getSerializationType.out - TridiagonalMatrixExample_setElements.out - TridiagonalMatrixExample_getCompressedRowLengths.out - TridiagonalMatrixExample_getConstRow.out - TridiagonalMatrixExample_getRow.out - TridiagonalMatrixExample_setElement.out - TridiagonalMatrixExample_addElement.out - TridiagonalMatrixExample_getElement.out - TridiagonalMatrixExample_rowsReduction.out - TridiagonalMatrixExample_allRowsReduction.out - TridiagonalMatrixExample_forElements.out - TridiagonalMatrixExample_forAllElements.out - TridiagonalMatrixViewExample_getCompressedRowLengths.out - TridiagonalMatrixViewExample_getConstRow.out - TridiagonalMatrixViewExample_getRow.out - TridiagonalMatrixViewExample_setElement.out - TridiagonalMatrixViewExample_addElement.out - TridiagonalMatrixViewExample_getElement.out - TridiagonalMatrixViewExample_rowsReduction.out - TridiagonalMatrixViewExample_allRowsReduction.out - TridiagonalMatrixViewExample_forElements.out - TridiagonalMatrixViewExample_forAllElements.out -) - diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp new file mode 100644 index 000000000..d1f0d625e --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -0,0 +1,63 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 -2 . . . \ -> { 0, 0, 1 } + * | -2 1 -2 . . | -> { 0, 2, 1 } + * | . -2 1 -2. . | -> { 3, 2, 1 } + * | . . -2 1 -2 | -> { 3, 2, 1 } + * \ . . . -2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -1, 0, 1 }. + */ + const int size = 5; + MatrixType matrix( + size, // number of matrix rows + size, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + + auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { + /*** + * 'forElements' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ---------- + * 0 / 1 -2 . . . \ -> { 0, 1, -2 } + * | -2 1 -2 . . | -> { -2, 1, -2 } + * | . -2 1 -2. . | -> { -2, 1, -2 } + * | . . -2 1 -2 | -> { -2, 1, -2 } + * \ . . . -2 1 / -> { -2, 1, 0 } + * + */ + const int& rowIdx = row.getRowIndex(); + row.setElement( 1, 1.0 ); + if( rowIdx > 0 ) + row.setElement( 0, -2.0 ); + if( rowIdx < size - 1 ) + row.setElement( 2, -2.0 ); + }; + matrix.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu new file mode 120000 index 000000000..a187b1e67 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_forRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp new file mode 100644 index 000000000..37e5f2724 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -0,0 +1,65 @@ +#include +#include +#include +#include + +template< typename Device > +void forRowsExample() +{ + using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 1 -2 . . . \ -> { 0, 0, 1 } + * | -2 1 -2 . . | -> { 0, 2, 1 } + * | . -2 1 -2. . | -> { 3, 2, 1 } + * | . . -2 1 -2 | -> { 3, 2, 1 } + * \ . . . -2 1 / -> { 3, 2, 1 } + * + * The diagonals offsets are { -1, 0, 1 }. + */ + + const int size = 5; + MatrixType matrix( + size, // number of matrix rows + size, // number of matrix columns + { -2, -1, 0 } ); // matrix diagonals offsets + auto view = matrix.getView(); + + auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { + /*** + * 'forElements' method iterates only over matrix elements lying on given subdiagonals + * and so we do not need to check anything. The element value can be expressed + * by the 'localIdx' variable, see the following figure: + * + * 0 1 2 <- localIdx values + * ---------- + * 0 / 1 -2 . . . \ -> { 0, 1, -2 } + * | -2 1 -2 . . | -> { -2, 1, -2 } + * | . -2 1 -2. . | -> { -2, 1, -2 } + * | . . -2 1 -2 | -> { -2, 1, -2 } + * \ . . . -2 1 / -> { -2, 1, 0 } + * + */ + const int& rowIdx = row.getRowIndex(); + row.setElement( 1, 1.0 ); + if( rowIdx > 0 ) + row.setElement( 0, -2.0 ); + if( rowIdx < size - 1 ) + row.setElement( 2, -2.0 ); + }; + view.forAllRows( f ); + std::cout << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Creating matrix on host: " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Creating matrix on CUDA device: " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu new file mode 120000 index 000000000..ea70e5b9e --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_forRows.cpp \ No newline at end of file -- GitLab From 8cb05c24d0c094d1af440a46e35ccb4751622a1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 10:07:35 +0100 Subject: [PATCH 34/54] Revert "Added const_cast to ArrayView constructor. I am not sure about this solution, it will need discussion." This reverts commit d17dbffd5bb146a736cda00fc7e241a06f84caca. --- src/TNL/Containers/ArrayView.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/TNL/Containers/ArrayView.h b/src/TNL/Containers/ArrayView.h index 793683ef7..4b3846037 100644 --- a/src/TNL/Containers/ArrayView.h +++ b/src/TNL/Containers/ArrayView.h @@ -143,8 +143,7 @@ public: template< typename Value_ > __cuda_callable__ ArrayView( const ArrayView< Value_, Device, Index >& view ) - : data( const_cast< Value* >( view.getData()) ), size(view.getSize()) {} - // ^ TODO: Is it correct? Check implementation of recursive constant proxy. + : data(view.getData()), size(view.getSize()) {} /** * \brief Move constructor for initialization from \e rvalues. -- GitLab From 345dcc1f734f47978bf787ef53f2a5a536f8f055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 15:39:17 +0100 Subject: [PATCH 35/54] Fixing examplefor dense matrix. --- .../DenseMatrixViewExample_getCompressedRowLengths.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cpp index 4572f41a3..5c9e83f39 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getCompressedRowLengths.cpp @@ -6,19 +6,19 @@ template< typename Device > void getCompressedRowLengthsExample() { - TNL::Matrices::DenseMatrix< double, Device > triangularMatrix { + TNL::Matrices::DenseMatrix< double, Device > denseMatrix { { 1 }, { 2, 3 }, { 4, 5, 6 }, { 7, 8, 9, 10 }, { 11, 12, 13, 14, 15 } }; - auto triangularMatrixView = triangularMatrix.getConstView(); + auto denseMatrixView = denseMatrix.getConstView(); - std::cout << triangularMatrixView << std::endl; + std::cout << denseMatrixView << std::endl; TNL::Containers::Vector< int, Device > rowLengths; - triangularMatrixView.getCompressedRowLengths( rowLengths ); + denseMatrixView.getCompressedRowLengths( rowLengths ); std::cout << "Compressed row lengths are: " << rowLengths << std::endl; } -- GitLab From f243db3b865fad8970bc32d09913322282bb53cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 15:40:17 +0100 Subject: [PATCH 36/54] Added type trait for copying const qualifier. --- src/TNL/TypeTraits.h | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/TNL/TypeTraits.h b/src/TNL/TypeTraits.h index 63b8fc273..c5d0fea36 100644 --- a/src/TNL/TypeTraits.h +++ b/src/TNL/TypeTraits.h @@ -270,4 +270,19 @@ public: static constexpr bool value = ( sizeof( test< std::decay_t >(0) ) == sizeof( YesType ) ); }; +/** + * \brief Copy const qualifier from Source type to Target type. + */ +template< typename Target > +struct copy_const +{ + template< typename Source > + struct from + { + using type = typename std::conditional< + std::is_const< Source >::value, + std::add_const_t< Target >, Target >::type; + }; +}; + } //namespace TNL -- GitLab From d4cbcc7cd67a2034f275c70fdf620615b212fcfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 15:40:40 +0100 Subject: [PATCH 37/54] Fixing const view for matrices. --- .../SpMV/ReferenceFormats/Legacy/Sparse.h | 2 +- src/TNL/Containers/VectorView.h | 2 +- src/TNL/Matrices/DenseMatrix.h | 24 +++++++++++-- src/TNL/Matrices/DenseMatrix.hpp | 7 ++-- src/TNL/Matrices/DenseMatrixView.h | 35 ++++++++++++++++--- src/TNL/Matrices/DenseMatrixView.hpp | 16 +++++++++ src/TNL/Matrices/Matrix.h | 31 ++++++++++++---- src/TNL/Matrices/Matrix.hpp | 8 ++--- src/TNL/Matrices/MatrixView.h | 18 +++++++--- src/TNL/Matrices/MultidiagonalMatrix.h | 4 +-- src/TNL/Matrices/SparseMatrix.h | 4 +-- src/TNL/Matrices/SparseMatrixView.h | 9 ++--- src/TNL/Matrices/TridiagonalMatrix.h | 2 +- 13 files changed, 128 insertions(+), 34 deletions(-) diff --git a/src/Benchmarks/SpMV/ReferenceFormats/Legacy/Sparse.h b/src/Benchmarks/SpMV/ReferenceFormats/Legacy/Sparse.h index 2e50843c2..f65527b09 100644 --- a/src/Benchmarks/SpMV/ReferenceFormats/Legacy/Sparse.h +++ b/src/Benchmarks/SpMV/ReferenceFormats/Legacy/Sparse.h @@ -29,7 +29,7 @@ class Sparse : public TNL::Matrices::Matrix< Real, Device, Index > typedef Real RealType; typedef Device DeviceType; typedef Index IndexType; - typedef typename TNL::Matrices::Matrix< RealType, DeviceType, IndexType >::ValuesVectorType ValuesVector; + typedef typename TNL::Matrices::Matrix< RealType, DeviceType, IndexType >::ValuesType ValuesVector; typedef Containers::Vector< IndexType, DeviceType, IndexType > ColumnIndexesVector; typedef TNL::Matrices::Matrix< Real, Device, Index > BaseType; typedef SparseRow< RealType, IndexType > MatrixRow; diff --git a/src/TNL/Containers/VectorView.h b/src/TNL/Containers/VectorView.h index 77c25b3a2..83ec6d0b0 100644 --- a/src/TNL/Containers/VectorView.h +++ b/src/TNL/Containers/VectorView.h @@ -46,7 +46,7 @@ public: /** * \brief Device where the vector is allocated. - * + * * See \ref Devices::Host or \ref Devices::Cuda. */ using DeviceType = Device; diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 4364f3bbc..baf7c8c8c 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -39,7 +39,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > { protected: using BaseType = Matrix< Real, Device, Index, RealAllocator >; - using ValuesVectorType = typename BaseType::ValuesVectorType; + using ValuesVectorType = typename BaseType::ValuesType; using ValuesViewType = typename ValuesVectorType::ViewType; using SegmentsType = Algorithms::Segments::Ellpack< Device, Index, typename Allocators::Default< Device >::template Allocator< Index >, Organization, 1 >; using SegmentViewType = typename SegmentsType::SegmentViewType; @@ -92,13 +92,33 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * * See \ref DenseMatrixView. */ - using ConstViewType = DenseMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; + using ConstViewType = typename DenseMatrixView< Real, Device, Index, Organization >::ConstViewType; /** * \brief Type for accessing matrix rows. */ using RowView = DenseMatrixRowView< SegmentViewType, ValuesViewType >; + /** + * \brief Type of vector holding values of matrix elements. + */ + using typename Matrix< Real, Device, Index, RealAllocator >::ValuesType; + + /** + * \brief Type of constant vector holding values of matrix elements. + */ + using typename Matrix< Real, Device, Index, RealAllocator >::ConstValuesType; + + /** + * \brief Type of vector view holding values of matrix elements. + */ + using typename Matrix< Real, Device, Index, RealAllocator >::ValuesView; + + /** + * \brief Type of constant vector view holding values of matrix elements. + */ + using typename Matrix< Real, Device, Index, RealAllocator >::ConstValuesView; + /** * \brief Helper type for getting self type or its modifications. */ diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 851b7b2de..c09ee52ff 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -105,9 +105,11 @@ auto DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: getView() -> ViewType { + ValuesView values_view = this->getValues().getView(); + // note this is improtant here to avoid const qualifier to appear in - somehow :( return ViewType( this->getRows(), this->getColumns(), - this->getValues().getView() ); + values_view ); } template< typename Real, @@ -119,10 +121,9 @@ auto DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: getConstView() const -> ConstViewType { - DenseMatrix* this_ptr = const_cast< DenseMatrix* >( this ); return ConstViewType( this->getRows(), this->getColumns(), - this_ptr->getValues().getView() ); + this->getValues().getConstView() ); } template< typename Real, diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index e0326459a..3add96177 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -43,7 +43,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > { protected: using BaseType = Matrix< Real, Device, Index >; - using ValuesVectorType = typename BaseType::ValuesVectorType; + using ValuesType = typename BaseType::ValuesType; using SegmentsType = Algorithms::Segments::Ellpack< Device, Index, typename Allocators::Default< Device >::template Allocator< Index >, Organization, 1 >; using SegmentsViewType = typename SegmentsType::ViewType; using SegmentViewType = typename SegmentsType::SegmentViewType; @@ -77,7 +77,14 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * * Use this for embedding of the matrix elements values. */ - using ValuesViewType = typename ValuesVectorType::ViewType; + using ValuesViewType = typename ValuesType::ViewType; + + /** + * \brief Matrix elements container view type. + * + * Use this for embedding of the matrix elements values. + */ + using ConstValuesViewType = typename ValuesType::ConstViewType; /** * \brief Matrix view type. @@ -91,7 +98,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * * See \ref DenseMatrixView. */ - using ConstViewType = DenseMatrixView< typename std::add_const< Real >::type, Device, Index, Organization >; + using ConstViewType = DenseMatrixView< std::add_const_t< Real >, Device, Index, Organization >; /** * \brief Type for accessing matrix row. @@ -125,13 +132,33 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include Matrices/DenseMatrix/DenseMatrixViewExample_constructor.cpp * \par Output * \include DenseMatrixViewExample_constructor.out - */ __cuda_callable__ DenseMatrixView( const IndexType rows, const IndexType columns, const ValuesViewType& values ); + /** + * \brief Constructor with matrix dimensions and values. + * + * Organization of matrix elements values in + * + * \param rows number of matrix rows. + * \param columns number of matrix columns. + * \param values is vector view with matrix elements values. + * + * \par Example + * \include Matrices/DenseMatrix/DenseMatrixViewExample_constructor.cpp + * \par Output + * \include DenseMatrixViewExample_constructor.out + */ + template< typename Real_ > + __cuda_callable__ + DenseMatrixView( const IndexType rows, + const IndexType columns, + const Containers::VectorView< Real_, Device, Index >& values ); + + /** * \brief Copy constructor. * diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index d33aaf602..654e3a166 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -44,6 +44,22 @@ DenseMatrixView( const IndexType rows, segments = a.getView(); } +template< typename Real, + typename Device, + typename Index, + ElementsOrganization Organization > + template< typename Value_ > +__cuda_callable__ +DenseMatrixView< Real, Device, Index, Organization >:: +DenseMatrixView( const IndexType rows, + const IndexType columns, + const Containers::VectorView< Value_, Device, Index >& values ) + : MatrixView< Real, Device, Index >( rows, columns, values ) +{ + SegmentsType a( rows, columns ); + segments = a.getView(); +} + template< typename Real, typename Device, typename Index, diff --git a/src/TNL/Matrices/Matrix.h b/src/TNL/Matrices/Matrix.h index 702e79162..1156a897e 100644 --- a/src/TNL/Matrices/Matrix.h +++ b/src/TNL/Matrices/Matrix.h @@ -37,11 +37,10 @@ using Algorithms::Segments::ElementsOrganization; template< typename Real = double, typename Device = Devices::Host, typename Index = int, - typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real > > + typename RealAllocator = typename Allocators::Default< Device >::template Allocator< std::remove_const_t< Real > > > class Matrix : public Object { public: - using ValuesVectorType = Containers::Vector< Real, Device, Index, RealAllocator >; using RealAllocatorType = RealAllocator; using RowsCapacitiesType = Containers::Vector< Index, Device, Index >; using RowsCapacitiesView = Containers::VectorView< Index, Device, Index >; @@ -72,7 +71,27 @@ class Matrix : public Object * \brief Type of base matrix view for constant instances. * */ - using ConstViewType = MatrixView< std::add_const_t< Real >, Device, Index >; + using ConstViewType = typename MatrixView< Real, Device, Index >::ConstViewType; + + /** + * \brief Type of vector holding values of matrix elements. + */ + using ValuesType = Containers::Vector< Real, Device, Index, RealAllocator >; + + /** + * \brief Type of constant vector holding values of matrix elements. + */ + using ConstValuesType = Containers::Vector< std::add_const_t< Real >, Device, Index, RealAllocator >; + + /** + * \brief Type of vector view holding values of matrix elements. + */ + using ValuesView = typename ViewType::ValuesView; + + /** + * \brief Type of constant vector view holding values of matrix elements. + */ + using ConstValuesView = typename ViewType::ConstValuesView; /** * \brief Construct a new Matrix object possibly with user defined allocator of the matrix values. @@ -155,14 +174,14 @@ class Matrix : public Object * * \return constant reference to a vector with the matrix elements values. */ - const ValuesVectorType& getValues() const; + const ValuesType& getValues() const; /** * \brief Returns a reference to a vector with the matrix elements values. * * \return constant reference to a vector with the matrix elements values. */ - ValuesVectorType& getValues(); + ValuesType& getValues(); /** * \brief Comparison operator with another arbitrary matrix type. @@ -220,7 +239,7 @@ class Matrix : public Object // TODO: remove //IndexType numberOfColors; - ValuesVectorType values; + ValuesType values; }; /** diff --git a/src/TNL/Matrices/Matrix.hpp b/src/TNL/Matrices/Matrix.hpp index 57c79cd76..c715f4dc3 100644 --- a/src/TNL/Matrices/Matrix.hpp +++ b/src/TNL/Matrices/Matrix.hpp @@ -112,9 +112,9 @@ template< typename Real, typename Device, typename Index, typename RealAllocator > -const typename Matrix< Real, Device, Index, RealAllocator >::ValuesVectorType& +auto Matrix< Real, Device, Index, RealAllocator >:: -getValues() const +getValues() const -> const ValuesType& { return this->values; } @@ -123,9 +123,9 @@ template< typename Real, typename Device, typename Index, typename RealAllocator > -typename Matrix< Real, Device, Index, RealAllocator >::ValuesVectorType& +auto Matrix< Real, Device, Index, RealAllocator >:: -getValues() +getValues() -> ValuesType& { return this->values; } diff --git a/src/TNL/Matrices/MatrixView.h b/src/TNL/Matrices/MatrixView.h index a1d5a86bc..6e4b89da3 100644 --- a/src/TNL/Matrices/MatrixView.h +++ b/src/TNL/Matrices/MatrixView.h @@ -38,7 +38,6 @@ class MatrixView : public Object using RowsCapacitiesType = Containers::Vector< Index, Device, Index >; using RowsCapacitiesTypeView = Containers::VectorView< Index, Device, Index >; using ConstRowsCapacitiesTypeView = typename RowsCapacitiesTypeView::ConstViewType; - using ValuesView = Containers::VectorView< std::remove_const_t< Real >, Device, Index >; /** * \brief The type of matrix elements. @@ -59,16 +58,27 @@ class MatrixView : public Object * \brief Type of base matrix view. * */ - using ViewType = MatrixView< typename std::remove_const< Real >::type, Device, Index >; + using ViewType = MatrixView< Real, Device, Index >; /** * \brief Type of base matrix view for constant instances. * */ - using ConstViewType = MatrixView< typename std::add_const< Real >::type, Device, Index >; + using ConstViewType = MatrixView< typename std::add_const_t< Real >, Device, Index >; /** - * \brief Basic construtor with no parameters. + * \brief Type of vector view holding values of matrix elements. + */ + using ValuesView = Containers::VectorView< Real, Device, Index >; + + /** + * \brief Type of constant vector view holding values of matrix elements. + */ + using ConstValuesView = typename ValuesView::ConstViewType; + + + /** + * \brief Basic constructor with no parameters. */ __cuda_callable__ MatrixView(); diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index dc06ed0a7..dc56f15c4 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -77,8 +77,8 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > // Supporting types - they are not important for the user using BaseType = Matrix< Real, Device, Index, RealAllocator >; - using ValuesVectorType = typename BaseType::ValuesVectorType; - using ValuesViewType = typename ValuesVectorType::ViewType; + using ValuesType = typename BaseType::ValuesType; + using ValuesView = typename ValuesType::ViewType; using IndexerType = details::MultidiagonalMatrixIndexer< Index, Organization >; using DiagonalsOffsetsType = Containers::Vector< Index, Device, Index, IndexAllocator >; using DiagonalsOffsetsView = typename DiagonalsOffsetsType::ViewType; diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index e2e282ef5..9989c694b 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -61,10 +61,10 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > // Supporting types - they are not important for the user using BaseType = Matrix< Real, Device, Index, RealAllocator >; - using ValuesVectorType = typename Matrix< std::remove_const_t< Real >, Device, Index, RealAllocator >::ValuesVectorType; + using ValuesVectorType = typename Matrix< Real, Device, Index, RealAllocator >::ValuesType; using ValuesViewType = typename ValuesVectorType::ViewType; using ConstValuesViewType = typename ValuesViewType::ConstViewType; - using ColumnsIndexesVectorType = Containers::Vector< std::remove_const_t< Index >, Device, Index, IndexAllocator >; + using ColumnsIndexesVectorType = Containers::Vector< typename TNL::copy_const< Index >::template from< Real >::type, Device, Index, IndexAllocator >; using ColumnsIndexesViewType = typename ColumnsIndexesVectorType::ViewType; using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; using RowsCapacitiesType = Containers::Vector< std::remove_const_t< Index >, Device, Index, IndexAllocator >; diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 2766d6b13..1e0bb66a5 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -15,6 +15,7 @@ #include #include #include +#include namespace TNL { namespace Matrices { @@ -75,7 +76,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > using BaseType = MatrixView< Real, Device, Index >; using ValuesViewType = typename BaseType::ValuesView; using ConstValuesViewType = typename ValuesViewType::ConstViewType; - using ColumnsIndexesViewType = Containers::VectorView< Index, Device, Index >; + using ColumnsIndexesViewType = Containers::VectorView< typename TNL::copy_const< Index >::template from< Real >::type, Device, Index >; using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType; using RowsCapacitiesView = Containers::VectorView< Index, Device, Index >; using ConstRowsCapacitiesView = typename RowsCapacitiesView::ConstViewType; @@ -97,7 +98,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief The type of matrix elements. */ - using RealType = std::remove_const_t< Real >; + using RealType = Real; using ComputeRealType = ComputeReal; @@ -125,12 +126,12 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type of related matrix view. */ - using ViewType = SparseMatrixView< std::remove_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate >; + using ViewType = SparseMatrixView< Real, Device, Index, MatrixType, SegmentsViewTemplate >; /** * \brief Matrix view type for constant instances. */ - using ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, std::add_const_t< Index >, MatrixType, SegmentsViewTemplate >; + using ConstViewType = SparseMatrixView< std::add_const_t< Real >, Device, Index, MatrixType, SegmentsViewTemplate >; /** * \brief Type for accessing matrix rows. diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h index 933e84bb0..66c9540ff 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.h +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -70,7 +70,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > // Supporting types - they are not important for the user using BaseType = Matrix< Real, Device, Index, RealAllocator >; using IndexerType = details::TridiagonalMatrixIndexer< Index, Organization >; - using ValuesVectorType = typename BaseType::ValuesVectorType; + using ValuesVectorType = typename BaseType::ValuesType; using ValuesViewType = typename ValuesVectorType::ViewType; /** -- GitLab From 7d8f29ff4e365df5932d236f2b635f133663618f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 16:44:51 +0100 Subject: [PATCH 38/54] Fixed ordering warning in LambdaMatrixRowView. --- src/TNL/Matrices/LambdaMatrixRowView.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TNL/Matrices/LambdaMatrixRowView.h b/src/TNL/Matrices/LambdaMatrixRowView.h index 56578f0b1..e989b9c02 100644 --- a/src/TNL/Matrices/LambdaMatrixRowView.h +++ b/src/TNL/Matrices/LambdaMatrixRowView.h @@ -160,7 +160,7 @@ class LambdaMatrixRowView const CompressedRowLengthsLambda& compressedRowLengthsLambda; - IndexType rowIdx, rows, columns; + IndexType rows, columns, rowIdx; }; /** -- GitLab From 7499d6bdc4e3a8626065023760a54a034d4cd845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 16:47:47 +0100 Subject: [PATCH 39/54] Fixed unused variable in CSRAdaptiveKernelView. --- src/TNL/Algorithms/Segments/CSRAdaptiveKernelView.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/TNL/Algorithms/Segments/CSRAdaptiveKernelView.hpp b/src/TNL/Algorithms/Segments/CSRAdaptiveKernelView.hpp index a9f921c73..979a54524 100644 --- a/src/TNL/Algorithms/Segments/CSRAdaptiveKernelView.hpp +++ b/src/TNL/Algorithms/Segments/CSRAdaptiveKernelView.hpp @@ -54,7 +54,7 @@ segmentsReductionCSRAdaptiveKernel( BlocksView blocks, __shared__ Real streamShared[ WarpsCount ][ StreamedSharedElementsPerWarp ]; __shared__ Real multivectorShared[ CudaBlockSize / WarpSize ]; - __shared__ BlockType sharedBlocks[ WarpsCount ]; + //__shared__ BlockType sharedBlocks[ WarpsCount ]; const Index index = ( ( gridIdx * TNL::Cuda::getMaxGridSize() + blockIdx.x ) * blockDim.x ) + threadIdx.x; const Index blockIdx = index / WarpSize; -- GitLab From 4e2fe80e211b217ab7752299ee7df084e34ebec9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 18:12:21 +0100 Subject: [PATCH 40/54] Renaming size to segmentsCount in EllpackView. --- src/TNL/Algorithms/Segments/EllpackView.h | 4 ++-- src/TNL/Algorithms/Segments/EllpackView.hpp | 24 ++++++++++----------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/TNL/Algorithms/Segments/EllpackView.h b/src/TNL/Algorithms/Segments/EllpackView.h index e1c2bd423..785165053 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.h +++ b/src/TNL/Algorithms/Segments/EllpackView.h @@ -47,7 +47,7 @@ class EllpackView EllpackView(); __cuda_callable__ - EllpackView( IndexType segmentSize, IndexType size, IndexType alignedSize ); + EllpackView( IndexType segmentSize, IndexType segmentsCount, IndexType alignedSize ); __cuda_callable__ EllpackView( const EllpackView& ellpackView ); @@ -121,7 +121,7 @@ class EllpackView protected: - IndexType segmentSize, size, alignedSize; + IndexType segmentSize, segmentsCount, alignedSize; }; } // namespace Segments diff --git a/src/TNL/Algorithms/Segments/EllpackView.hpp b/src/TNL/Algorithms/Segments/EllpackView.hpp index f8c14c2fb..101cfee82 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.hpp +++ b/src/TNL/Algorithms/Segments/EllpackView.hpp @@ -27,7 +27,7 @@ template< typename Device, __cuda_callable__ EllpackView< Device, Index, Organization, Alignment >:: EllpackView() - : segmentSize( 0 ), size( 0 ), alignedSize( 0 ) + : segmentSize( 0 ), segmentsCount( 0 ), alignedSize( 0 ) { } @@ -37,8 +37,8 @@ template< typename Device, int Alignment > __cuda_callable__ EllpackView< Device, Index, Organization, Alignment >:: -EllpackView( IndexType segmentSize, IndexType size, IndexType alignedSize ) - : segmentSize( segmentSize ), size( size ), alignedSize( alignedSize ) +EllpackView( IndexType segmentSize, IndexType segmentsCount, IndexType alignedSize ) + : segmentSize( segmentSize ), segmentsCount( segmentsCount ), alignedSize( alignedSize ) { } @@ -49,7 +49,7 @@ template< typename Device, __cuda_callable__ EllpackView< Device, Index, Organization, Alignment >:: EllpackView( const EllpackView& ellpack ) - : segmentSize( ellpack.segmentSize ), size( ellpack.size ), alignedSize( ellpack.alignedSize ) + : segmentSize( ellpack.segmentSize ), segmentsCount( ellpack.segmentsCount ), alignedSize( ellpack.alignedSize ) { } @@ -60,7 +60,7 @@ template< typename Device, __cuda_callable__ EllpackView< Device, Index, Organization, Alignment >:: EllpackView( const EllpackView&& ellpack ) - : segmentSize( ellpack.segmentSize ), size( ellpack.size ), alignedSize( ellpack.alignedSize ) + : segmentSize( ellpack.segmentSize ), segmentsCount( ellpack.segmentsCount ), alignedSize( ellpack.alignedSize ) { } @@ -95,7 +95,7 @@ typename EllpackView< Device, Index, Organization, Alignment >::ViewType EllpackView< Device, Index, Organization, Alignment >:: getView() { - return ViewType( segmentSize, size, alignedSize ); + return ViewType( segmentSize, segmentsCount, alignedSize ); } template< typename Device, @@ -107,7 +107,7 @@ auto EllpackView< Device, Index, Organization, Alignment >:: getConstView() const -> const ConstViewType { - return ConstViewType( segmentSize, size, alignedSize ); + return ConstViewType( segmentSize, segmentsCount, alignedSize ); } template< typename Device, @@ -117,7 +117,7 @@ template< typename Device, __cuda_callable__ auto EllpackView< Device, Index, Organization, Alignment >:: getSegmentsCount() const -> IndexType { - return this->size; + return this->segmentsCount; } template< typename Device, @@ -137,7 +137,7 @@ template< typename Device, __cuda_callable__ auto EllpackView< Device, Index, Organization, Alignment >:: getSize() const -> IndexType { - return this->size * this->segmentSize; + return this->segmentsCount * this->segmentSize; } @@ -315,7 +315,7 @@ EllpackView< Device, Index, Organization, Alignment >:: operator=( const EllpackView< Device, Index, Organization, Alignment >& view ) { this->segmentSize = view.segmentSize; - this->size = view.size; + this->segmentsCount = view.segmentsCount; this->alignedSize = view.alignedSize; return *this; } @@ -328,7 +328,7 @@ void EllpackView< Device, Index, Organization, Alignment >:: save( File& file ) const { file.save( &segmentSize ); - file.save( &size ); + file.save( &segmentsCount ); file.save( &alignedSize ); } @@ -340,7 +340,7 @@ void EllpackView< Device, Index, Organization, Alignment >:: load( File& file ) { file.load( &segmentSize ); - file.load( &size ); + file.load( &segmentsCount ); file.load( &alignedSize ); } -- GitLab From 4a0a98cb0adda3431a969cc9d434534ebf21e223 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 20:22:42 +0100 Subject: [PATCH 41/54] Fixed constructors of DenseMatrixView and EllpackView. --- src/TNL/Algorithms/Segments/Ellpack.hpp | 4 ++-- src/TNL/Algorithms/Segments/EllpackView.h | 5 ++++- src/TNL/Algorithms/Segments/EllpackView.hpp | 19 +++++++++++++++++-- src/TNL/Matrices/DenseMatrixView.h | 20 ++++++++++---------- src/TNL/Matrices/DenseMatrixView.hpp | 8 ++------ 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/src/TNL/Algorithms/Segments/Ellpack.hpp b/src/TNL/Algorithms/Segments/Ellpack.hpp index d7210386c..124e3dfc2 100644 --- a/src/TNL/Algorithms/Segments/Ellpack.hpp +++ b/src/TNL/Algorithms/Segments/Ellpack.hpp @@ -109,7 +109,7 @@ auto Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: getView() -> ViewType { - return ViewType( segmentSize, size, alignedSize ); + return ViewType( size, segmentSize, alignedSize ); } template< typename Device, @@ -121,7 +121,7 @@ auto Ellpack< Device, Index, IndexAllocator, Organization, Alignment >:: getConstView() const -> const ConstViewType { - return ConstViewType( segmentSize, size, alignedSize ); + return ConstViewType( size, segmentSize, alignedSize ); } template< typename Device, diff --git a/src/TNL/Algorithms/Segments/EllpackView.h b/src/TNL/Algorithms/Segments/EllpackView.h index 785165053..77d0d8b7b 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.h +++ b/src/TNL/Algorithms/Segments/EllpackView.h @@ -47,7 +47,10 @@ class EllpackView EllpackView(); __cuda_callable__ - EllpackView( IndexType segmentSize, IndexType segmentsCount, IndexType alignedSize ); + EllpackView( IndexType segmentsCount, IndexType segmentSize, IndexType alignedSize ); + + __cuda_callable__ + EllpackView( IndexType segmentsCount, IndexType segmentSize ); __cuda_callable__ EllpackView( const EllpackView& ellpackView ); diff --git a/src/TNL/Algorithms/Segments/EllpackView.hpp b/src/TNL/Algorithms/Segments/EllpackView.hpp index 101cfee82..724774b53 100644 --- a/src/TNL/Algorithms/Segments/EllpackView.hpp +++ b/src/TNL/Algorithms/Segments/EllpackView.hpp @@ -37,11 +37,26 @@ template< typename Device, int Alignment > __cuda_callable__ EllpackView< Device, Index, Organization, Alignment >:: -EllpackView( IndexType segmentSize, IndexType segmentsCount, IndexType alignedSize ) +EllpackView( IndexType segmentsCount, IndexType segmentSize, IndexType alignedSize ) : segmentSize( segmentSize ), segmentsCount( segmentsCount ), alignedSize( alignedSize ) { } +template< typename Device, + typename Index, + ElementsOrganization Organization, + int Alignment > +__cuda_callable__ +EllpackView< Device, Index, Organization, Alignment >:: +EllpackView( IndexType segmentsCount, IndexType segmentSize ) + : segmentSize( segmentSize ), segmentsCount( segmentsCount ) +{ + if( Organization == RowMajorOrder ) + this->alignedSize = this->segmentsCount; + else + this->alignedSize = roundUpDivision( segmentsCount, this->getAlignment() ) * this->getAlignment(); +} + template< typename Device, typename Index, ElementsOrganization Organization, @@ -107,7 +122,7 @@ auto EllpackView< Device, Index, Organization, Alignment >:: getConstView() const -> const ConstViewType { - return ConstViewType( segmentSize, segmentsCount, alignedSize ); + return ConstViewType( segmentsCount, segmentSize, alignedSize ); } template< typename Device, diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index 3add96177..c11666874 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -511,8 +511,8 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, const RealType& value, bool& compute )`. - * The column index repeats twice only for compatibility with sparse matrices. - * If the 'compute' variable is set to false the iteration over the row can + * The column index repeats twice only for compatibility with sparse matrices. + * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. @@ -533,8 +533,8 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \tparam Function is type of lambda function that will operate on matrix elements. * It is should have form like * `function( IndexType rowIdx, IndexType columnIdx, IndexType columnIdx, RealType& value, bool& compute )`. - * The column index repeats twice only for compatibility with sparse matrices. - * If the 'compute' variable is set to false the iteration over the row can + * The column index repeats twice only for compatibility with sparse matrices. + * If the 'compute' variable is set to false the iteration over the row can * be interrupted. * * \param begin defines beginning of the range [begin,end) of rows to be processed. @@ -794,7 +794,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Assignment operator with DenseMatrix. - * + * * \param matrix is the right-hand side matrix. * \return reference to this matrix. */ @@ -802,25 +802,25 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Method for saving the matrix view to the file with given filename. - * + * * The ouput file can be loaded by \ref DenseMatrix. - * + * * \param fileName is name of the file. */ void save( const String& fileName ) const; /** * \brief Method for saving the matrix view to a file. - * + * * The ouput file can be loaded by \ref DenseMatrix. - * + * * \param fileName is name of the file. */ void save( File& file ) const; /** * \brief Method for printing the matrix to output stream. - * + * * \param str is the output stream. */ void print( std::ostream& str ) const; diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index 654e3a166..e31b54144 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -38,10 +38,8 @@ DenseMatrixView< Real, Device, Index, Organization >:: DenseMatrixView( const IndexType rows, const IndexType columns, const ValuesViewType& values ) - : MatrixView< Real, Device, Index >( rows, columns, values ) + : MatrixView< Real, Device, Index >( rows, columns, values ), segments( rows, columns ) { - SegmentsType a( rows, columns ); - segments = a.getView(); } template< typename Real, @@ -54,10 +52,8 @@ DenseMatrixView< Real, Device, Index, Organization >:: DenseMatrixView( const IndexType rows, const IndexType columns, const Containers::VectorView< Value_, Device, Index >& values ) - : MatrixView< Real, Device, Index >( rows, columns, values ) + : MatrixView< Real, Device, Index >( rows, columns, values ), segments( rows, columns, true ) { - SegmentsType a( rows, columns ); - segments = a.getView(); } template< typename Real, -- GitLab From 8dc1c1633163d98ecb7c45c936016ae1eaf92bc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 20:32:38 +0100 Subject: [PATCH 42/54] Added Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu. --- .../LambdaMatrixExample_getRow.cu | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu new file mode 100644 index 000000000..7f757e6c8 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu @@ -0,0 +1,75 @@ +#include +#include +#include +#include + + +template< typename Device > +void laplaceOperatorMatrix() +{ + /*** + * Set matrix representing approximation of the Laplace operator on regular + * grid using the finite difference method. + */ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int + { + const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid + const int gridColumn = rowIdx % gridSize; + if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node + gridColumn == 0 || gridColumn == gridSize - 1 ) + return 1; + return 5; + }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value) { + const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid + const int gridColumn = rowIdx % gridSize; + if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node + gridColumn == 0 || gridColumn == gridSize - 1 ) + { + columnIdx = rowIdx; // diagonal element .... + value = 1.0; // ... is set to 1 + } + else // interior grid node + { + switch( localIdx ) // set diagonal element to 4 + { // and the others to -1 + case 0: + columnIdx = rowIdx - gridSize; + value = 1; + break; + case 1: + columnIdx = rowIdx - 1; + value = 1; + break; + case 2: + columnIdx = rowIdx; + value = -4; + break; + case 3: + columnIdx = rowIdx + 1; + value = 1; + break; + case 4: + columnIdx = rowIdx + gridSize; + value = 1; + break; + } + } + }; + auto matrix = TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( + matrixSize, matrixSize, matrixElements, rowLengths ); + std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Running example on CPU ... " << std::endl; + forRowsExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Running example on CUDA GPU ... " << std::endl; + forRowsExample< TNL::Devices::Cuda >(); +#endif +} -- GitLab From c9363dee53198461a03e764f2bf769faeb7170ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 21:09:44 +0100 Subject: [PATCH 43/54] Renaming rowsReduction -> reduceRows and allRowsReduction -> reduceAllRows. --- .../Matrices/DenseMatrix/CMakeLists.txt | 8 ++--- .../DenseMatrixExample_allRowsReduction.cu | 1 - ...p => DenseMatrixExample_reduceAllRows.cpp} | 8 ++--- .../DenseMatrixExample_reduceAllRows.cu | 1 + ....cpp => DenseMatrixExample_reduceRows.cpp} | 8 ++--- .../DenseMatrixExample_reduceRows.cu | 1 + .../DenseMatrixExample_rowsReduction.cu | 1 - ...DenseMatrixViewExample_allRowsReduction.cu | 1 - ... DenseMatrixViewExample_reduceAllRows.cpp} | 8 ++--- .../DenseMatrixViewExample_reduceAllRows.cu | 1 + ... => DenseMatrixViewExample_reduceRows.cpp} | 8 ++--- .../DenseMatrixViewExample_reduceRows.cu | 1 + .../DenseMatrixViewExample_rowsReduction.cu | 1 - .../Matrices/LambdaMatrix/CMakeLists.txt | 4 +-- .../LambdaMatrixExample_allRowsReduction.cu | 1 - ... => LambdaMatrixExample_reduceAllRows.cpp} | 8 ++--- .../LambdaMatrixExample_reduceAllRows.cu | 1 + ...cpp => LambdaMatrixExample_reduceRows.cpp} | 8 ++--- .../LambdaMatrixExample_reduceRows.cu | 1 + .../LambdaMatrixExample_rowsReduction.cu | 1 - .../MultidiagonalMatrix/CMakeLists.txt | 8 ++--- ...idiagonalMatrixExample_allRowsReduction.cu | 1 - ...tidiagonalMatrixExample_reduceAllRows.cpp} | 18 +++++------ ...ultidiagonalMatrixExample_reduceAllRows.cu | 1 + ...MultidiagonalMatrixExample_reduceRows.cpp} | 18 +++++------ .../MultidiagonalMatrixExample_reduceRows.cu | 1 + ...ultidiagonalMatrixExample_rowsReduction.cu | 1 - ...gonalMatrixViewExample_allRowsReduction.cu | 1 - ...agonalMatrixViewExample_reduceAllRows.cpp} | 18 +++++------ ...diagonalMatrixViewExample_reduceAllRows.cu | 1 + ...idiagonalMatrixViewExample_reduceRows.cpp} | 18 +++++------ ...ltidiagonalMatrixViewExample_reduceRows.cu | 1 + ...diagonalMatrixViewExample_rowsReduction.cu | 1 - .../Matrices/SparseMatrix/CMakeLists.txt | 8 ++--- .../SparseMatrixExample_allRowsReduction.cu | 1 - ... => SparseMatrixExample_reduceAllRows.cpp} | 8 ++--- .../SparseMatrixExample_reduceAllRows.cu | 1 + ...cpp => SparseMatrixExample_reduceRows.cpp} | 8 ++--- .../SparseMatrixExample_reduceRows.cu | 1 + .../SparseMatrixExample_rowsReduction.cu | 1 - ...parseMatrixViewExample_allRowsReduction.cu | 1 - ...SparseMatrixViewExample_reduceAllRows.cpp} | 8 ++--- .../SparseMatrixViewExample_reduceAllRows.cu | 1 + ...=> SparseMatrixViewExample_reduceRows.cpp} | 8 ++--- .../SparseMatrixViewExample_reduceRows.cu | 1 + .../SparseMatrixViewExample_rowsReduction.cu | 1 - .../Matrices/TridiagonalMatrix/CMakeLists.txt | 8 ++--- ...idiagonalMatrixExample_allRowsReduction.cu | 1 - ...ridiagonalMatrixExample_reduceAllRows.cpp} | 18 +++++------ .../TridiagonalMatrixExample_reduceAllRows.cu | 1 + ...> TridiagonalMatrixExample_reduceRows.cpp} | 18 +++++------ .../TridiagonalMatrixExample_reduceRows.cu | 1 + .../TridiagonalMatrixExample_rowsReduction.cu | 1 - ...gonalMatrixViewExample_allRowsReduction.cu | 1 - ...agonalMatrixViewExample_reduceAllRows.cpp} | 18 +++++------ ...diagonalMatrixViewExample_reduceAllRows.cu | 1 + ...idiagonalMatrixViewExample_reduceRows.cpp} | 18 +++++------ ...TridiagonalMatrixViewExample_reduceRows.cu | 1 + ...diagonalMatrixViewExample_rowsReduction.cu | 1 - .../Tutorials/Matrices/CMakeLists.txt | 30 +++++++++---------- ...nseMatrixExample_rowsReduction_maxNorm.cpp | 8 ++--- ...rixExample_rowsReduction_vectorProduct.cpp | 8 ++--- ...rixExample_rowsReduction_vectorProduct.cpp | 8 ++--- .../Tutorials/Matrices/tutorial_Matrices.md | 30 +++++++++---------- src/TNL/Matrices/DenseMatrix.h | 24 +++++++-------- src/TNL/Matrices/DenseMatrix.hpp | 16 +++++----- src/TNL/Matrices/DenseMatrixView.h | 24 +++++++-------- src/TNL/Matrices/DenseMatrixView.hpp | 14 ++++----- src/TNL/Matrices/LambdaMatrix.h | 12 ++++---- src/TNL/Matrices/LambdaMatrix.hpp | 10 +++---- src/TNL/Matrices/MultidiagonalMatrix.h | 24 +++++++-------- src/TNL/Matrices/MultidiagonalMatrix.hpp | 16 +++++----- src/TNL/Matrices/MultidiagonalMatrixView.h | 24 +++++++-------- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 18 +++++------ src/TNL/Matrices/SparseMatrix.h | 24 +++++++-------- src/TNL/Matrices/SparseMatrix.hpp | 16 +++++----- src/TNL/Matrices/SparseMatrixView.h | 24 +++++++-------- src/TNL/Matrices/SparseMatrixView.hpp | 16 +++++----- src/TNL/Matrices/TridiagonalMatrix.h | 24 +++++++-------- src/TNL/Matrices/TridiagonalMatrix.hpp | 16 +++++----- src/TNL/Matrices/TridiagonalMatrixView.h | 24 +++++++-------- src/TNL/Matrices/TridiagonalMatrixView.hpp | 18 +++++------ .../Matrices/BinarySparseMatrixTest.hpp | 6 ++-- .../Matrices/BinarySparseMatrixTest_CSR.h | 4 +-- .../Matrices/BinarySparseMatrixTest_Ellpack.h | 4 +-- .../BinarySparseMatrixTest_SlicedEllpack.h | 4 +-- src/UnitTests/Matrices/DenseMatrixTest.h | 6 ++-- src/UnitTests/Matrices/LambdaMatrixTest.h | 4 +-- src/UnitTests/Matrices/LambdaMatrixTest.hpp | 4 +-- .../Legacy/Legacy_SparseMatrixTest.hpp | 6 ++-- src/UnitTests/Matrices/SparseMatrixTest.h | 4 +-- src/UnitTests/Matrices/SparseMatrixTest.hpp | 6 ++-- .../Matrices/SymmetricSparseMatrixTest.h | 4 +-- .../Matrices/SymmetricSparseMatrixTest.hpp | 6 ++-- 94 files changed, 391 insertions(+), 391 deletions(-) delete mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu rename Documentation/Examples/Matrices/DenseMatrix/{DenseMatrixExample_allRowsReduction.cpp => DenseMatrixExample_reduceAllRows.cpp} (88%) create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cu rename Documentation/Examples/Matrices/DenseMatrix/{DenseMatrixExample_rowsReduction.cpp => DenseMatrixExample_reduceRows.cpp} (87%) create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cu rename Documentation/Examples/Matrices/DenseMatrix/{DenseMatrixViewExample_allRowsReduction.cpp => DenseMatrixViewExample_reduceAllRows.cpp} (88%) create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cu rename Documentation/Examples/Matrices/DenseMatrix/{DenseMatrixViewExample_rowsReduction.cpp => DenseMatrixViewExample_reduceRows.cpp} (88%) create mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu rename Documentation/Examples/Matrices/LambdaMatrix/{LambdaMatrixExample_allRowsReduction.cpp => LambdaMatrixExample_reduceAllRows.cpp} (90%) create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cu rename Documentation/Examples/Matrices/LambdaMatrix/{LambdaMatrixExample_rowsReduction.cpp => LambdaMatrixExample_reduceRows.cpp} (90%) create mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu rename Documentation/Examples/Matrices/MultidiagonalMatrix/{MultidiagonalMatrixExample_allRowsReduction.cpp => MultidiagonalMatrixExample_reduceAllRows.cpp} (86%) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cu rename Documentation/Examples/Matrices/MultidiagonalMatrix/{MultidiagonalMatrixExample_rowsReduction.cpp => MultidiagonalMatrixExample_reduceRows.cpp} (86%) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu rename Documentation/Examples/Matrices/MultidiagonalMatrix/{MultidiagonalMatrixViewExample_allRowsReduction.cpp => MultidiagonalMatrixViewExample_reduceAllRows.cpp} (86%) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cu rename Documentation/Examples/Matrices/MultidiagonalMatrix/{MultidiagonalMatrixViewExample_rowsReduction.cpp => MultidiagonalMatrixViewExample_reduceRows.cpp} (86%) create mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu rename Documentation/Examples/Matrices/SparseMatrix/{SparseMatrixExample_allRowsReduction.cpp => SparseMatrixExample_reduceAllRows.cpp} (88%) create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cu rename Documentation/Examples/Matrices/SparseMatrix/{SparseMatrixExample_rowsReduction.cpp => SparseMatrixExample_reduceRows.cpp} (88%) create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu rename Documentation/Examples/Matrices/SparseMatrix/{SparseMatrixViewExample_allRowsReduction.cpp => SparseMatrixViewExample_reduceAllRows.cpp} (89%) create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cu rename Documentation/Examples/Matrices/SparseMatrix/{SparseMatrixViewExample_rowsReduction.cpp => SparseMatrixViewExample_reduceRows.cpp} (88%) create mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu rename Documentation/Examples/Matrices/TridiagonalMatrix/{TridiagonalMatrixExample_allRowsReduction.cpp => TridiagonalMatrixExample_reduceAllRows.cpp} (86%) create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cu rename Documentation/Examples/Matrices/TridiagonalMatrix/{TridiagonalMatrixExample_rowsReduction.cpp => TridiagonalMatrixExample_reduceRows.cpp} (85%) create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu rename Documentation/Examples/Matrices/TridiagonalMatrix/{TridiagonalMatrixViewExample_allRowsReduction.cpp => TridiagonalMatrixViewExample_reduceAllRows.cpp} (86%) create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cu rename Documentation/Examples/Matrices/TridiagonalMatrix/{TridiagonalMatrixViewExample_rowsReduction.cpp => TridiagonalMatrixViewExample_reduceRows.cpp} (86%) create mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cu delete mode 120000 Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu diff --git a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt index 9656d54a7..e28145776 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/DenseMatrix/CMakeLists.txt @@ -8,8 +8,8 @@ set( COMMON_EXAMPLES DenseMatrixExample_setElement DenseMatrixExample_addElement DenseMatrixExample_getElement - DenseMatrixExample_rowsReduction - DenseMatrixExample_allRowsReduction + DenseMatrixExample_reduceRows + DenseMatrixExample_reduceAllRows DenseMatrixExample_forElements DenseMatrixExample_forAllElements DenseMatrixExample_forRows @@ -21,8 +21,8 @@ set( COMMON_EXAMPLES DenseMatrixViewExample_setElement DenseMatrixViewExample_addElement DenseMatrixViewExample_getElement - DenseMatrixViewExample_rowsReduction - DenseMatrixViewExample_allRowsReduction + DenseMatrixViewExample_reduceRows + DenseMatrixViewExample_reduceAllRows DenseMatrixViewExample_forElements DenseMatrixViewExample_forRows DenseMatrixViewExample_forAllElements diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu deleted file mode 120000 index 70f517f68..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cpp similarity index 88% rename from Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cpp index ce323671f..2f63d7f05 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cpp @@ -6,7 +6,7 @@ #include template< typename Device > -void allRowsReduction() +void reduceAllRows() { TNL::Matrices::DenseMatrix< double, Device > matrix { { 1, 0, 0, 0, 0 }, @@ -49,7 +49,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ - matrix.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "Max. elements in rows are: " << rowMax << std::endl; } @@ -57,10 +57,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "All rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + reduceAllRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + reduceAllRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cu new file mode 120000 index 000000000..94d48afbb --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cu @@ -0,0 +1 @@ +DenseMatrixExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cpp similarity index 87% rename from Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cpp index b521d15d2..af9664411 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cpp @@ -5,7 +5,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { TNL::Matrices::DenseMatrix< double, Device > matrix { { 1, 0, 0, 0, 0 }, @@ -48,7 +48,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "Max. elements in rows are: " << rowMax << std::endl; } @@ -56,10 +56,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cu new file mode 120000 index 000000000..4ca91f198 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cu @@ -0,0 +1 @@ +DenseMatrixExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cu deleted file mode 120000 index 41bf46ebc..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cu deleted file mode 120000 index 61dd89125..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixViewExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cpp similarity index 88% rename from Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cpp index b65cb3ea9..5f0084950 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cpp @@ -5,7 +5,7 @@ #include template< typename Device > -void allRowsReduction() +void reduceAllRows() { TNL::Matrices::DenseMatrix< double, Device > matrix { { 1, 0, 0, 0, 0 }, @@ -49,7 +49,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ - matrixView.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrixView.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "Max. elements in rows are: " << rowMax << std::endl; } @@ -57,10 +57,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "All rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + reduceAllRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + reduceAllRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cu new file mode 120000 index 000000000..952b099f7 --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cpp similarity index 88% rename from Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cpp index dbc44f854..a6b46986c 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cpp @@ -5,7 +5,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { TNL::Matrices::DenseMatrix< double, Device > matrix { { 1, 0, 0, 0, 0 }, @@ -49,7 +49,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "Max. elements in rows are: " << rowMax << std::endl; } @@ -57,10 +57,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cu new file mode 120000 index 000000000..e55a80ebf --- /dev/null +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cu @@ -0,0 +1 @@ +DenseMatrixViewExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cu deleted file mode 120000 index 4170aaa62..000000000 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt index 5483d90f3..94cf64959 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt @@ -4,8 +4,8 @@ set( COMMON_EXAMPLES LambdaMatrixExample_getNonzeroElementsCount LambdaMatrixExample_Laplace LambdaMatrixExample_Laplace_2 - LambdaMatrixExample_rowsReduction - LambdaMatrixExample_allRowsReduction + LambdaMatrixExample_reduceRows + LambdaMatrixExample_reduceAllRows LambdaMatrixExample_forElements LambdaMatrixExample_forAllElements LambdaMatrixExample_forRows diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu deleted file mode 120000 index f76fba15c..000000000 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -LambdaMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cpp similarity index 90% rename from Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cpp index a39a1f33b..b90e28f86 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cpp @@ -6,7 +6,7 @@ #include template< typename Device > -void allRowsReduction() +void reduceAllRows() { /*** * Lambda functions defining the matrix. @@ -54,7 +54,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ - matrix.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -63,10 +63,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "All rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + reduceAllRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + reduceAllRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cu new file mode 120000 index 000000000..d2ded9d9f --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cu @@ -0,0 +1 @@ +LambdaMatrixExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cpp similarity index 90% rename from Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cpp index 4cb0aedab..847edbca7 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cpp @@ -6,7 +6,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { /*** * Lambda functions defining the matrix. @@ -54,7 +54,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -63,10 +63,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cu new file mode 120000 index 000000000..50cb7e925 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cu @@ -0,0 +1 @@ +LambdaMatrixExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu deleted file mode 120000 index ecb2401ec..000000000 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -LambdaMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt index 72397bcb7..78ff95236 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/CMakeLists.txt @@ -12,8 +12,8 @@ set( COMMON_EXAMPLES # MultidiagonalMatrixExample_setElement MultidiagonalMatrixExample_addElement MultidiagonalMatrixExample_getElement - MultidiagonalMatrixExample_rowsReduction - MultidiagonalMatrixExample_allRowsReduction + MultidiagonalMatrixExample_reduceRows + MultidiagonalMatrixExample_reduceAllRows MultidiagonalMatrixExample_forElements MultidiagonalMatrixExample_forAllElements MultidiagonalMatrixExample_forRows @@ -25,8 +25,8 @@ set( COMMON_EXAMPLES MultidiagonalMatrixViewExample_setElement MultidiagonalMatrixViewExample_addElement MultidiagonalMatrixViewExample_getElement - MultidiagonalMatrixViewExample_rowsReduction - MultidiagonalMatrixViewExample_allRowsReduction + MultidiagonalMatrixViewExample_reduceRows + MultidiagonalMatrixViewExample_reduceAllRows MultidiagonalMatrixViewExample_forElements MultidiagonalMatrixViewExample_forAllElements MultidiagonalMatrixViewExample_forRows diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu deleted file mode 120000 index c8659a5f4..000000000 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -MultidiagonalMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cpp similarity index 86% rename from Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cpp index 4b102c73b..1fedc197d 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cpp @@ -5,26 +5,26 @@ #include template< typename Device > -void allRowsReduction() +void reduceAllRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 0 / 1 . . . . \ -> { 0, 0, 1 } * 0 | 2 1 . . . | -> { 0, 2, 1 } * | 3 2 1 . . | -> { 3, 2, 1 } * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } - * + * \ . . 3 2 1 / -> { 3, 2, 1 } + * * The diagonals offsets are { -2, -1, 0 }. */ TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { -2, -1, 0 }, // diagonals offsets { { 0, 0, 1 }, // matrix elements - { 0, 2, 1 }, - { 3, 2, 1 }, + { 0, 2, 1 }, + { 3, 2, 1 }, { 3, 2, 1 }, { 3, 2, 1 } } ); @@ -62,7 +62,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ - matrix.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -71,10 +71,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + reduceAllRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + reduceAllRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cu new file mode 120000 index 000000000..cac3890f4 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cpp similarity index 86% rename from Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cpp index 2b579d963..8e895dc0d 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cpp @@ -5,26 +5,26 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 0 / 1 . . . . \ -> { 0, 0, 1 } * 0 | 2 1 . . . | -> { 0, 2, 1 } * | 3 2 1 . . | -> { 3, 2, 1 } * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } - * + * \ . . 3 2 1 / -> { 3, 2, 1 } + * * The diagonals offsets are { -2, -1, 0 }. */ TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { -2, -1, 0 }, // diagonals offsets { { 0, 0, 1 }, // matrix elements - { 0, 2, 1 }, - { 3, 2, 1 }, + { 0, 2, 1 }, + { 3, 2, 1 }, { 3, 2, 1 }, { 3, 2, 1 } } ); @@ -62,7 +62,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -71,10 +71,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cu new file mode 120000 index 000000000..7c5170e06 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu deleted file mode 120000 index adaff28e8..000000000 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -MultidiagonalMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu deleted file mode 120000 index 5e14692de..000000000 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -MultidiagonalMatrixViewExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cpp similarity index 86% rename from Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cpp index 26dac464c..c235f4478 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cpp @@ -5,26 +5,26 @@ #include template< typename Device > -void allRowsReduction() +void reduceAllRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 0 / 1 . . . . \ -> { 0, 0, 1 } * 0 | 2 1 . . . | -> { 0, 2, 1 } * | 3 2 1 . . | -> { 3, 2, 1 } * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } - * + * \ . . 3 2 1 / -> { 3, 2, 1 } + * * The diagonals offsets are { -2, -1, 0 }. */ TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { -2, -1, 0 }, // diagonals offsets { { 0, 0, 1 }, // matrix elements - { 0, 2, 1 }, - { 3, 2, 1 }, + { 0, 2, 1 }, + { 3, 2, 1 }, { 3, 2, 1 }, { 3, 2, 1 } } ); auto view = matrix.getView(); @@ -63,7 +63,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ - view.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + view.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -72,10 +72,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + reduceAllRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + reduceAllRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cu new file mode 120000 index 000000000..326123208 --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cpp similarity index 86% rename from Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cpp index 6793d8bb2..f6dee85f6 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cpp @@ -5,26 +5,26 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 0 / 1 . . . . \ -> { 0, 0, 1 } * 0 | 2 1 . . . | -> { 0, 2, 1 } * | 3 2 1 . . | -> { 3, 2, 1 } * | . 3 2 1 . | -> { 3, 2, 1 } - * \ . . 3 2 1 / -> { 3, 2, 1 } - * + * \ . . 3 2 1 / -> { 3, 2, 1 } + * * The diagonals offsets are { -2, -1, 0 }. */ TNL::Matrices::MultidiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { -2, -1, 0 }, // diagonals offsets { { 0, 0, 1 }, // matrix elements - { 0, 2, 1 }, - { 3, 2, 1 }, + { 0, 2, 1 }, + { 3, 2, 1 }, { 3, 2, 1 }, { 3, 2, 1 } } ); auto view = matrix.getView(); @@ -63,7 +63,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - view.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + view.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -72,10 +72,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cu new file mode 120000 index 000000000..1ebbea34c --- /dev/null +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cu @@ -0,0 +1 @@ +MultidiagonalMatrixViewExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu deleted file mode 120000 index 44df3d3ed..000000000 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -MultidiagonalMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt index 03f033cb5..c2db3879e 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/SparseMatrix/CMakeLists.txt @@ -13,8 +13,8 @@ set( COMMON_EXAMPLES SparseMatrixExample_setElement SparseMatrixExample_addElement SparseMatrixExample_getElement - SparseMatrixExample_rowsReduction - SparseMatrixExample_allRowsReduction + SparseMatrixExample_reduceRows + SparseMatrixExample_reduceAllRows SparseMatrixExample_forElements SparseMatrixExample_forAllElements SparseMatrixExample_forRows @@ -25,8 +25,8 @@ set( COMMON_EXAMPLES SparseMatrixViewExample_setElement SparseMatrixViewExample_addElement SparseMatrixViewExample_getElement - SparseMatrixViewExample_rowsReduction - SparseMatrixViewExample_allRowsReduction + SparseMatrixViewExample_reduceRows + SparseMatrixViewExample_reduceAllRows SparseMatrixViewExample_forElements SparseMatrixViewExample_forRows SparseMatrixViewExample_forAllElements diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu deleted file mode 120000 index f087b816f..000000000 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -SparseMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cpp similarity index 88% rename from Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cpp index 9ce31f09e..b9b9f2c20 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cpp @@ -6,7 +6,7 @@ #include template< typename Device > -void allRowsReduction() +void reduceAllRows() { TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { { 0, 0, 1 }, @@ -49,7 +49,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ - matrix.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -58,10 +58,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "All rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + reduceAllRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + reduceAllRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cu new file mode 120000 index 000000000..105390586 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cu @@ -0,0 +1 @@ +SparseMatrixExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cpp similarity index 88% rename from Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cpp index 20279888a..4fb7f8f7d 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cpp @@ -5,7 +5,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { { 0, 0, 1 }, @@ -48,7 +48,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -57,10 +57,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cu new file mode 120000 index 000000000..ceacbc490 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cu @@ -0,0 +1 @@ +SparseMatrixExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu deleted file mode 120000 index 212f16288..000000000 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -SparseMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu deleted file mode 120000 index d63cf05c5..000000000 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -SparseMatrixViewExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cpp similarity index 89% rename from Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cpp index 98a03b941..7ae5f07f5 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cpp @@ -6,7 +6,7 @@ #include template< typename Device > -void allRowsReduction() +void reduceAllRows() { TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { { 0, 0, 1 }, @@ -50,7 +50,7 @@ void allRowsReduction() /*** * Compute the largest values in each row. */ - matrixView.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrixView.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -59,10 +59,10 @@ void allRowsReduction() int main( int argc, char* argv[] ) { std::cout << "All rows reduction on host:" << std::endl; - allRowsReduction< TNL::Devices::Host >(); + reduceAllRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "All rows reduction on CUDA device:" << std::endl; - allRowsReduction< TNL::Devices::Cuda >(); + reduceAllRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cu new file mode 120000 index 000000000..2af541565 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cpp similarity index 88% rename from Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cpp index 69e2ff6fd..aa4295f8a 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cpp @@ -5,7 +5,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { TNL::Matrices::SparseMatrix< double, Device > matrix ( 5, 5, { { 0, 0, 1 }, @@ -49,7 +49,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrixView.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrixView.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -58,10 +58,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cu new file mode 120000 index 000000000..6d1c7c6c8 --- /dev/null +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cu @@ -0,0 +1 @@ +SparseMatrixViewExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu deleted file mode 120000 index f244c8372..000000000 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -SparseMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt index 2cd34daf1..7d331938a 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/CMakeLists.txt @@ -10,8 +10,8 @@ set( COMMON_EXAMPLES # TridiagonalMatrixExample_setElement TridiagonalMatrixExample_addElement TridiagonalMatrixExample_getElement - TridiagonalMatrixExample_rowsReduction - TridiagonalMatrixExample_allRowsReduction + TridiagonalMatrixExample_reduceRows + TridiagonalMatrixExample_reduceAllRows TridiagonalMatrixExample_forElements TridiagonalMatrixExample_forAllElements TridiagonalMatrixExample_forRows @@ -23,8 +23,8 @@ set( COMMON_EXAMPLES TridiagonalMatrixViewExample_setElement TridiagonalMatrixViewExample_addElement TridiagonalMatrixViewExample_getElement - TridiagonalMatrixViewExample_rowsReduction - TridiagonalMatrixViewExample_allRowsReduction + TridiagonalMatrixViewExample_reduceRows + TridiagonalMatrixViewExample_reduceAllRows TridiagonalMatrixViewExample_forElements TridiagonalMatrixViewExample_forAllElements TridiagonalMatrixViewExample_forRows diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu deleted file mode 120000 index 795c4febf..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cpp similarity index 86% rename from Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cpp index 043dcc82e..65acc3c47 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cpp @@ -5,24 +5,24 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 / 1 3 . . . \ -> { 0, 1, 3 } * | 2 1 3 . . | -> { 2, 1, 3 } * | . 2 1 3 . | -> { 2, 1, 3 } * | . . 2 1 3 | -> { 2, 1, 3 } - * \ . . . 2 1 / 0 -> { 2, 1, 0 } - * + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * */ TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { { 0, 1, 3 }, // matrix elements - { 2, 1, 3 }, - { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, { 2, 1, 3 }, { 2, 1, 3 } } ); @@ -60,7 +60,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -69,10 +69,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cu new file mode 120000 index 000000000..e8001f966 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cpp similarity index 85% rename from Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cpp index aae0bd4e3..c985e12b7 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cpp @@ -5,24 +5,24 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 / 1 3 . . . \ -> { 0, 1, 3 } * | 2 1 3 . . | -> { 2, 1, 3 } * | . 2 1 3 . | -> { 2, 1, 3 } * | . . 2 1 3 | -> { 2, 1, 3 } - * \ . . . 2 1 / 0 -> { 2, 1, 0 } - * + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * */ TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { { 0, 1, 3 }, // matrix elements - { 2, 1, 3 }, - { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, { 2, 1, 3 }, { 2, 1, 3 } } ); @@ -60,7 +60,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -69,10 +69,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cu new file mode 120000 index 000000000..7b5585d88 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu deleted file mode 120000 index 69a58007c..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu deleted file mode 120000 index 7b330650f..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixViewExample_allRowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cpp similarity index 86% rename from Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp rename to Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cpp index bacb98bee..bef803781 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cpp @@ -5,24 +5,24 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 / 1 3 . . . \ -> { 0, 1, 3 } * | 2 1 3 . . | -> { 2, 1, 3 } * | . 2 1 3 . | -> { 2, 1, 3 } * | . . 2 1 3 | -> { 2, 1, 3 } - * \ . . . 2 1 / 0 -> { 2, 1, 0 } - * + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * */ TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { { 0, 1, 3 }, // matrix elements - { 2, 1, 3 }, - { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, { 2, 1, 3 }, { 2, 1, 3 } } ); auto view = matrix.getView(); @@ -61,7 +61,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - view.allRowsReduction( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + view.reduceAllRows( fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -70,10 +70,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cu new file mode 120000 index 000000000..6fc41327c --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_reduceAllRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cpp similarity index 86% rename from Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp rename to Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cpp index fa345292c..89068d48a 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cpp @@ -5,24 +5,24 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): - * + * * 0 / 1 3 . . . \ -> { 0, 1, 3 } * | 2 1 3 . . | -> { 2, 1, 3 } * | . 2 1 3 . | -> { 2, 1, 3 } * | . . 2 1 3 | -> { 2, 1, 3 } - * \ . . . 2 1 / 0 -> { 2, 1, 0 } - * + * \ . . . 2 1 / 0 -> { 2, 1, 0 } + * */ TNL::Matrices::TridiagonalMatrix< double, Device > matrix ( 5, // number of matrix columns { { 0, 1, 3 }, // matrix elements - { 2, 1, 3 }, - { 2, 1, 3 }, + { 2, 1, 3 }, + { 2, 1, 3 }, { 2, 1, 3 }, { 2, 1, 3 } } ); auto view = matrix.getView(); @@ -61,7 +61,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - view.rowsReduction( 0, view.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + view.reduceRows( 0, view.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "The matrix reads as: " << std::endl << matrix << std::endl; std::cout << "Max. elements in rows are: " << rowMax << std::endl; @@ -70,10 +70,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cu new file mode 120000 index 000000000..13f298463 --- /dev/null +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cu @@ -0,0 +1 @@ +TridiagonalMatrixViewExample_reduceRows.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu deleted file mode 120000 index f749c1ef4..000000000 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cu +++ /dev/null @@ -1 +0,0 @@ -TridiagonalMatrixViewExample_rowsReduction.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/CMakeLists.txt b/Documentation/Tutorials/Matrices/CMakeLists.txt index 7e3b2b210..8b56bd510 100644 --- a/Documentation/Tutorials/Matrices/CMakeLists.txt +++ b/Documentation/Tutorials/Matrices/CMakeLists.txt @@ -19,15 +19,15 @@ IF( BUILD_CUDA ) # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forRows.out # OUTPUT DenseMatrixExample_forRows.out ) - CUDA_ADD_EXECUTABLE( DenseMatrixExample_rowsReduction_vectorProduct DenseMatrixExample_rowsReduction_vectorProduct.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_rowsReduction_vectorProduct > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_rowsReduction_vectorProduct.out - OUTPUT DenseMatrixExample_rowsReduction_vectorProduct.out ) + CUDA_ADD_EXECUTABLE( DenseMatrixExample_reduceRows_vectorProduct DenseMatrixExample_reduceRows_vectorProduct.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_reduceRows_vectorProduct > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_reduceRows_vectorProduct.out + OUTPUT DenseMatrixExample_reduceRows_vectorProduct.out ) - CUDA_ADD_EXECUTABLE( DenseMatrixExample_rowsReduction_maxNorm DenseMatrixExample_rowsReduction_maxNorm.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_rowsReduction_maxNorm > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_rowsReduction_maxNorm.out - OUTPUT DenseMatrixExample_rowsReduction_maxNorm.out ) + CUDA_ADD_EXECUTABLE( DenseMatrixExample_reduceRows_maxNorm DenseMatrixExample_reduceRows_maxNorm.cu ) + ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_reduceRows_maxNorm > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_reduceRows_maxNorm.out + OUTPUT DenseMatrixExample_reduceRows_maxNorm.out ) CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_setElement DenseMatrixViewExample_setElement.cu ) ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_setElement > @@ -84,10 +84,10 @@ IF( BUILD_CUDA ) # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out # OUTPUT SparseMatrixExample_forRows.out ) - CUDA_ADD_EXECUTABLE( SparseMatrixExample_rowsReduction_vectorProduct SparseMatrixExample_rowsReduction_vectorProduct.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_rowsReduction_vectorProduct > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_rowsReduction_vectorProduct.out - OUTPUT SparseMatrixExample_rowsReduction_vectorProduct.out ) + CUDA_ADD_EXECUTABLE( SparseMatrixExample_reduceRows_vectorProduct SparseMatrixExample_reduceRows_vectorProduct.cu ) + ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_reduceRows_vectorProduct > + ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_reduceRows_vectorProduct.out + OUTPUT SparseMatrixExample_reduceRows_vectorProduct.out ) CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cu ) ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > @@ -126,8 +126,8 @@ ADD_CUSTOM_TARGET( TutorialsMatricesCuda ALL DEPENDS DenseMatrixExample_addElement.out DenseMatrixExample_setElement.out # DenseMatrixExample_forRows.out - DenseMatrixExample_rowsReduction_vectorProduct.out - DenseMatrixExample_rowsReduction_maxNorm.out + DenseMatrixExample_reduceRows_vectorProduct.out + DenseMatrixExample_reduceRows_maxNorm.out DenseMatrixViewExample_setElement.out DenseMatrixViewExample_data_encapsulation.out SparseMatrixExample_Constructor_init_list_2.out @@ -137,7 +137,7 @@ ADD_CUSTOM_TARGET( TutorialsMatricesCuda ALL DEPENDS SparseMatrixExample_setElements_map.out SparseMatrixExample_setElement.out # SparseMatrixExample_forRows.out - SparseMatrixExample_rowsReduction_vectorProduct.out + SparseMatrixExample_reduceRows_vectorProduct.out SparseMatrixViewExample_setElement.out SymmetricSparseMatrixExample.out BinarySparseMatrixExample.out diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cpp b/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cpp index a1837ebc7..42bfd4800 100644 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cpp +++ b/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cpp @@ -5,7 +5,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { TNL::Matrices::DenseMatrix< double, Device > matrix { { 1, 0, 0, 0, 0 }, @@ -48,7 +48,7 @@ void rowsReduction() /*** * Compute the largest values in each row. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); + matrix.reduceRows( 0, matrix.getRows(), fetch, reduce, keep, std::numeric_limits< double >::lowest() ); std::cout << "Max. elements in rows are: " << rowMax << std::endl; std::cout << "Max. matrix norm is: " << TNL::max( rowMax ) << std::endl; @@ -57,10 +57,10 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cpp b/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cpp index 1dcef95dd..27d4956a3 100644 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cpp +++ b/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cpp @@ -6,7 +6,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { TNL::Matrices::DenseMatrix< double, Device > matrix { { 1, 0, 0, 0, 0 }, @@ -50,7 +50,7 @@ void rowsReduction() /*** * Compute matrix-vector product. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, std::plus<>{}, keep, 0.0 ); + matrix.reduceRows( 0, matrix.getRows(), fetch, std::plus<>{}, keep, 0.0 ); std::cout << "The matrix reads as:" << std::endl << matrix << std::endl; std::cout << "The input vector is:" << x << std::endl; @@ -60,11 +60,11 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << std::endl; std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cpp index dd72230fe..ff0cde7d4 100644 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cpp +++ b/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cpp @@ -6,7 +6,7 @@ #include template< typename Device > -void rowsReduction() +void reduceRows() { TNL::Matrices::SparseMatrix< double, Device > matrix { 5, 5, { { 0, 0, 1 }, @@ -50,7 +50,7 @@ void rowsReduction() /*** * Compute matrix-vector product. */ - matrix.rowsReduction( 0, matrix.getRows(), fetch, std::plus<>{}, keep, 0.0 ); + matrix.reduceRows( 0, matrix.getRows(), fetch, std::plus<>{}, keep, 0.0 ); std::cout << "The matrix reads as:" << std::endl << matrix << std::endl; std::cout << "The input vector is:" << x << std::endl; @@ -60,11 +60,11 @@ void rowsReduction() int main( int argc, char* argv[] ) { std::cout << "Rows reduction on host:" << std::endl; - rowsReduction< TNL::Devices::Host >(); + reduceRows< TNL::Devices::Host >(); #ifdef HAVE_CUDA std::cout << std::endl; std::cout << "Rows reduction on CUDA device:" << std::endl; - rowsReduction< TNL::Devices::Cuda >(); + reduceRows< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Tutorials/Matrices/tutorial_Matrices.md b/Documentation/Tutorials/Matrices/tutorial_Matrices.md index 0c8aa3baa..3f62a3888 100644 --- a/Documentation/Tutorials/Matrices/tutorial_Matrices.md +++ b/Documentation/Tutorials/Matrices/tutorial_Matrices.md @@ -1183,8 +1183,8 @@ TODO: Write documentation on distributed matrices. ## Flexible reduction in matrix rows -Flexible reduction in matrix rows is a powerful tool for many different matrix operations. It is represented by the method `rowsReduction` (\ref TNL::Matrices::DenseMatrix::rowsReduction, -\ref TNL::Matrices::SparseMatrix::rowsReduction, \ref TNL::Matrices::TridiagonalMatrix::rowsReduction, \ref TNL::Matrices::MultidiagonalMatrix::rowsReduction, \ref TNL::Matrices::LambdaMatrix::rowsReduction) and similar to the method `forElements` it iterates over particular matrix rows. However, it performs *flexible paralell reduction* in addition. For example, the matrix-vector product can be seen as a reduction of products of matrix elements with the input vector in particular matrix rows. The first element of the result vector ios obtained as: +Flexible reduction in matrix rows is a powerful tool for many different matrix operations. It is represented by the method `reduceRows` (\ref TNL::Matrices::DenseMatrix::reduceRows, +\ref TNL::Matrices::SparseMatrix::reduceRows, \ref TNL::Matrices::TridiagonalMatrix::reduceRows, \ref TNL::Matrices::MultidiagonalMatrix::reduceRows, \ref TNL::Matrices::LambdaMatrix::reduceRows) and similar to the method `forElements` it iterates over particular matrix rows. However, it performs *flexible paralell reduction* in addition. For example, the matrix-vector product can be seen as a reduction of products of matrix elements with the input vector in particular matrix rows. The first element of the result vector ios obtained as: \f[ y_1 = a_{11} x_1 + a_{12} x_2 + \ldots + a_{1n} x_n = \sum_{j=1}^n a_{1j}x_j @@ -1238,7 +1238,7 @@ The meaning of the particular parameters is as follows: 1. `rowIdx` is an index of the matrix row related to given result of flexible reduction. 2. `value`is the result of the flexible reduction in given matrix row. -The method `rowsReduction` (\ref TNL::Matrices::DenseMatrix::rowsReduction, \ref TNL::Matrices::SparseMatrix::rowsReduction, \ref TNL::Matrices::TridiagonalMatrix::rowsReduction, \ref TNL::Matrices::MultidiagonalMatrix::rowsReduction, \ref TNL::Matrices::LambdaMatrix::rowsReduction) accepts the following arguments: +The method `reduceRows` (\ref TNL::Matrices::DenseMatrix::reduceRows, \ref TNL::Matrices::SparseMatrix::reduceRows, \ref TNL::Matrices::TridiagonalMatrix::reduceRows, \ref TNL::Matrices::MultidiagonalMatrix::reduceRows, \ref TNL::Matrices::LambdaMatrix::reduceRows) accepts the following arguments: 1. `begin` is the beginning of the matrix rows range on which the reduction will be performed. 2. `end` is the end of the matrix rows range on which the reduction will be performed. The last matrix row which is going to be processed has index `end-1`. @@ -1257,7 +1257,7 @@ The following example demonstrates implementation of the dense matrix-vector pro y_i = \sum_{j=0}^{columns - 1} a_{ij} x_j \text{ for } i = 0, \ldots, rows-1. \f] -\includelineno DenseMatrixExample_rowsReduction_vectorProduct.cpp +\includelineno DenseMatrixExample_reduceRows_vectorProduct.cpp We set the following lambda functions: @@ -1267,7 +1267,7 @@ We set the following lambda functions: The result looks as: -\include DenseMatrixExample_rowsReduction_vectorProduct.out +\include DenseMatrixExample_reduceRows_vectorProduct.out We will show one more example which is a computation of maximal absolute value in each matrix row. The results will be stored in a vector: @@ -1277,7 +1277,7 @@ y_i = \max_{j=1,\ldots,n} |a_{ij}|. See the following example: -\includelineno DenseMatrixExample_rowsReduction_maxNorm.cpp +\includelineno DenseMatrixExample_reduceRows_maxNorm.cpp The lambda functions rare: @@ -1287,13 +1287,13 @@ The lambda functions rare: Note, that the idempotent value for the reduction is \ref std::numeric_limits< double >::lowest. Of course, if we compute the maximum of all output vector elements, we get some kind of maximal matrix norm. The output looks as: -\include DenseMatrixExample_rowsReduction_maxNorm.out +\include DenseMatrixExample_reduceRows_maxNorm.out ### Sparse matrices example The following example demonstrates sparse matrix-vector product: -\includelineno SparseMatrixExample_rowsReduction_vectorProduct.cpp +\includelineno SparseMatrixExample_reduceRows_vectorProduct.cpp On the lines 11-16 we set the following matrix: @@ -1311,7 +1311,7 @@ On the lines 11-16 we set the following matrix: The lambda functions on the lines 39-48 are the same as in the example with the dense matrix. The result looks as follows: -\include SparseMatrixExample_rowsReduction_vectorProduct.out +\include SparseMatrixExample_reduceRows_vectorProduct.out ### Tridiagonal matrices example @@ -1331,7 +1331,7 @@ In this example, we will compute maximal absolute value in each row of the follo The source code reads as follows: -\includelineno TridiagonalMatrixExample_rowsReduction.cpp +\includelineno TridiagonalMatrixExample_reduceRows.cpp Here we first set the tridiagonal matrix (lines 10-27). Next we allocate the vector `rowMax` where we will store the results (line 32). The lambda function are: @@ -1341,7 +1341,7 @@ Here we first set the tridiagonal matrix (lines 10-27). Next we allocate the vec Note, that the idempotent value for the reduction is \ref std::numeric_limits< double >::lowest. The results looks as follows: -\include TridiagonalMatrixExample_rowsReduction.out +\include TridiagonalMatrixExample_reduceRows.out ### Multidiagonal matrices example @@ -1365,15 +1365,15 @@ We first create vector `rowMax` into which we will store the results and fetch i * `reduce` (lines 51-53) returns maximum value of the two input values `a` and `b`. * `keep` (line 58-60) stores the input `value` at the corresponding position, given by the row index `rowIdx`, in the output vector view `rowMaxView`. -Finally, we call the method `rowsReduction` (\ref TNL::Matrices::MultidiagonalMatrix::rowsReduction) with parameters telling the interval of rows to be processed (the first and second parameter), the lambda functions `fetch`, `reduce` and `keep`, and the idempotent element for the reduction operation which is the lowest number of given type (\ref std::numeric_limits< double >::lowest ). The result looks as follows: +Finally, we call the method `reduceRows` (\ref TNL::Matrices::MultidiagonalMatrix::reduceRows) with parameters telling the interval of rows to be processed (the first and second parameter), the lambda functions `fetch`, `reduce` and `keep`, and the idempotent element for the reduction operation which is the lowest number of given type (\ref std::numeric_limits< double >::lowest ). The result looks as follows: -\include MultidiagonalMatrixExample_rowsReduction.out +\include MultidiagonalMatrixExample_reduceRows.out ### Lambda matrices example The reduction of matrix rows is available for the lambda matrices as well. See the following example: -\includelineno LambdaMatrixExample_rowsReduction.cpp +\includelineno LambdaMatrixExample_reduceRows.cpp On the lines 14-21, we create the lower triangular lambda matrix which looks as follows: @@ -1397,7 +1397,7 @@ We want to compute maximal absolute value of matrix elements in each row. For th Note that the interface of the lambda functions is the same as for other matrix types. The result looks as follows: -\include LambdaMatrixExample_rowsReduction.out +\include LambdaMatrixExample_reduceRows.out ## Matrix-vector product diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index baf7c8c8c..27984123e 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -704,12 +704,12 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cpp * \par Output - * \include DenseMatrixExample_rowsReduction.out + * \include DenseMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ); + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ); /** * \brief Method for performing general reduction on matrix rows for constant instances. @@ -731,12 +731,12 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_rowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_reduceRows.cpp * \par Output - * \include DenseMatrixExample_rowsReduction.out + * \include DenseMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const; + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const; /** * \brief Method for performing general reduction on ALL matrix rows. @@ -756,12 +756,12 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cpp * \par Output - * \include DenseMatrixExample_allRowsReduction.out + * \include DenseMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on ALL matrix rows for constant instances. @@ -781,12 +781,12 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixExample_allRowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixExample_reduceAllRows.cpp * \par Output - * \include DenseMatrixExample_allRowsReduction.out + * \include DenseMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Computes product of matrix and vector. diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index c09ee52ff..24844d845 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -355,9 +355,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) { - this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); + this->view.reduceRows( begin, end, fetch, reduce, keep, zero ); } template< typename Real, @@ -368,9 +368,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { - this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); + this->view.reduceRows( begin, end, fetch, reduce, keep, zero ); } template< typename Real, @@ -381,9 +381,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, @@ -394,9 +394,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index c11666874..a13846e7a 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -421,12 +421,12 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cpp * \par Output - * \include DenseMatrixViewExample_rowsReduction.out + * \include DenseMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on matrix rows for constant instances. @@ -448,12 +448,12 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixViewExample_rowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_reduceRows.cpp * \par Output - * \include DenseMatrixViewExample_rowsReduction.out + * \include DenseMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on ALL matrix rows. @@ -473,12 +473,12 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cpp * \par Output - * \include DenseMatrixViewExample_allRowsReduction.out + * \include DenseMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on ALL matrix rows for constant instances. @@ -498,12 +498,12 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/DenseMatrix/DenseMatrixViewExample_allRowsReduction.cpp + * \include Matrices/DenseMatrix/DenseMatrixViewExample_reduceAllRows.cpp * \par Output - * \include DenseMatrixViewExample_allRowsReduction.out + * \include DenseMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index e31b54144..a91fcc617 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -142,7 +142,7 @@ getCompressedRowLengths( Vector& rowLengths ) const auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - this->allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + this->reduceAllRows( fetch, std::plus<>{}, keep, 0 ); } template< typename Real, @@ -290,7 +290,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void DenseMatrixView< Real, Device, Index, Organization >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) { auto values_view = this->values.getView(); auto fetch_ = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable -> decltype( fetch( IndexType(), IndexType(), RealType() ) ) { @@ -307,7 +307,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void DenseMatrixView< Real, Device, Index, Organization >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { const auto values_view = this->values.getConstView(); auto fetch_ = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable -> decltype( fetch( IndexType(), IndexType(), RealType() ) ) { @@ -324,9 +324,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void DenseMatrixView< Real, Device, Index, Organization >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, @@ -336,9 +336,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void DenseMatrixView< Real, Device, Index, Organization >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, diff --git a/src/TNL/Matrices/LambdaMatrix.h b/src/TNL/Matrices/LambdaMatrix.h index 2bcd412cb..bc817323d 100644 --- a/src/TNL/Matrices/LambdaMatrix.h +++ b/src/TNL/Matrices/LambdaMatrix.h @@ -391,12 +391,12 @@ class LambdaMatrix * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/LambdaMatrix/LambdaMatrixExample_rowsReduction.cpp + * \include Matrices/LambdaMatrix/LambdaMatrixExample_reduceRows.cpp * \par Output - * \include LambdaMatrixExample_rowsReduction.out + * \include LambdaMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on ALL matrix rows. @@ -416,12 +416,12 @@ class LambdaMatrix * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/LambdaMatrix/LambdaMatrixExample_allRowsReduction.cpp + * \include Matrices/LambdaMatrix/LambdaMatrixExample_reduceAllRows.cpp * \par Output - * \include LambdaMatrixExample_allRowsReduction.out + * \include LambdaMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Computes product of matrix and vector. diff --git a/src/TNL/Matrices/LambdaMatrix.hpp b/src/TNL/Matrices/LambdaMatrix.hpp index 9a9225b5f..ada86b848 100644 --- a/src/TNL/Matrices/LambdaMatrix.hpp +++ b/src/TNL/Matrices/LambdaMatrix.hpp @@ -145,7 +145,7 @@ getCompressedRowLengths( Vector& rowLengths ) const auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - this->allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + this->reduceAllRows( fetch, std::plus<>{}, keep, 0 ); } template< typename MatrixElementsLambda, @@ -251,7 +251,7 @@ vectorProduct( const InVector& inVector, }; if( ! end ) end = this->getRows(); - this->rowsReduction( begin, end, fetch, reduce, keep, 0.0 ); + this->reduceRows( begin, end, fetch, reduce, keep, 0.0 ); } template< typename MatrixElementsLambda, @@ -262,7 +262,7 @@ template< typename MatrixElementsLambda, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceRows( IndexType first, IndexType last, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { using FetchType = decltype( fetch( IndexType(), IndexType(), RealType() ) ); @@ -296,9 +296,9 @@ template< typename MatrixElementsLambda, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename MatrixElementsLambda, diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index dc56f15c4..e8fbbd75c 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -617,12 +617,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cpp * \par Output - * \include MultidiagonalMatrixExample_rowsReduction.out + * \include MultidiagonalMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on matrix rows for constant instances. @@ -644,12 +644,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_rowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceRows.cpp * \par Output - * \include MultidiagonalMatrixExample_rowsReduction.out + * \include MultidiagonalMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on all matrix rows. @@ -669,12 +669,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cpp * \par Output - * \include MultidiagonalMatrixExample_allRowsReduction.out + * \include MultidiagonalMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on all matrix rows for constant instances. @@ -694,12 +694,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_allRowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_reduceAllRows.cpp * \par Output - * \include MultidiagonalMatrixExample_allRowsReduction.out + * \include MultidiagonalMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for iteration over matrix rows for constant instances. diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 6d94c7cd6..7d65e340e 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -477,9 +477,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); + this->view.reduceRows( first, last, fetch, reduce, keep, zero ); } template< typename Real, @@ -491,9 +491,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); + this->view.reduceRows( first, last, fetch, reduce, keep, zero ); } template< typename Real, @@ -505,9 +505,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->view.reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, @@ -519,9 +519,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->view.reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index 08dc9e15c..d91db5a8a 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -379,12 +379,12 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cpp * \par Output - * \include MultidiagonalMatrixViewExample_rowsReduction.out + * \include MultidiagonalMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on matrix rows. @@ -406,12 +406,12 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_rowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceRows.cpp * \par Output - * \include MultidiagonalMatrixViewExample_rowsReduction.out + * \include MultidiagonalMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on all matrix rows for constant instances. @@ -431,12 +431,12 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cpp * \par Output - * \include MultidiagonalMatrixViewExample_allRowsReduction.out + * \include MultidiagonalMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on all matrix rows. @@ -456,12 +456,12 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_allRowsReduction.cpp + * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_reduceAllRows.cpp * \par Output - * \include MultidiagonalMatrixViewExample_allRowsReduction.out + * \include MultidiagonalMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index c21bb83bb..a75e40da0 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -147,7 +147,7 @@ getCompressedRowLengths( Vector& rowLengths ) const auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - this->allRowsReduction( fetch, reduce, keep, 0 ); + this->reduceAllRows( fetch, reduce, keep, 0 ); } template< typename Real, @@ -356,7 +356,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrixView< Real, Device, Index, Organization >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) const +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) const { using Real_ = decltype( fetch( IndexType(), IndexType(), RealType() ) ); const auto values_view = this->values.getConstView(); @@ -385,7 +385,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrixView< Real, Device, Index, Organization >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) { using Real_ = decltype( fetch( IndexType(), IndexType(), RealType() ) ); const auto values_view = this->values.getConstView(); @@ -414,9 +414,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrixView< Real, Device, Index, Organization >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); } template< typename Real, @@ -426,9 +426,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void MultidiagonalMatrixView< Real, Device, Index, Organization >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); } template< typename Real, @@ -651,9 +651,9 @@ vectorProduct( const InVector& inVector, if( end == 0 ) end = this->getRows(); if( outVectorMultiplicator == ( RealType ) 0.0 ) - this->rowsReduction( begin, end, fetch, reduction, keeper1, ( RealType ) 0.0 ); + this->reduceRows( begin, end, fetch, reduction, keeper1, ( RealType ) 0.0 ); else - this->rowsReduction( begin, end, fetch, reduction, keeper2, ( RealType ) 0.0 ); + this->reduceRows( begin, end, fetch, reduction, keeper2, ( RealType ) 0.0 ); } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 9989c694b..2bd171e46 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -620,12 +620,12 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cpp * \par Output - * \include SparseMatrixExample_rowsReduction.out + * \include SparseMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on matrix rows for constant instances. @@ -647,12 +647,12 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_rowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_reduceRows.cpp * \par Output - * \include SparseMatrixExample_rowsReduction.out + * \include SparseMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on all matrix rows. @@ -672,12 +672,12 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cpp * \par Output - * \include SparseMatrixExample_allRowsReduction.out + * \include SparseMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on all matrix rows for constant instances. @@ -697,12 +697,12 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixExample_allRowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixExample_reduceAllRows.cpp * \par Output - * \include SparseMatrixExample_allRowsReduction.out + * \include SparseMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for parallel iteration over matrix elements of given rows for constant instances. diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index beab59549..6ff89ad93 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -539,9 +539,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) { - this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); + this->view.reduceRows( begin, end, fetch, reduce, keep, zero ); } template< typename Real, @@ -555,9 +555,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { - this->view.rowsReduction( begin, end, fetch, reduce, keep, zero ); + this->view.reduceRows( begin, end, fetch, reduce, keep, zero ); } template< typename Real, @@ -571,9 +571,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, @@ -587,9 +587,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index 1e0bb66a5..f35d1e616 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -411,12 +411,12 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cpp * \par Output - * \include SparseMatrixViewExample_rowsReduction.out + * \include SparseMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on matrix rows for constant instances. @@ -438,12 +438,12 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixViewExample_rowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixViewExample_reduceRows.cpp * \par Output - * \include SparseMatrixViewExample_rowsReduction.out + * \include SparseMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on all matrix rows. @@ -463,12 +463,12 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cpp * \par Output - * \include SparseMatrixViewExample_allRowsReduction.out + * \include SparseMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on all matrix rows for constant instances. @@ -488,12 +488,12 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/SparseMatrix/SparseMatrixViewExample_allRowsReduction.cpp + * \include Matrices/SparseMatrix/SparseMatrixViewExample_reduceAllRows.cpp * \par Output - * \include SparseMatrixViewExample_allRowsReduction.out + * \include SparseMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index c69165828..ab103fca0 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -134,7 +134,7 @@ getCompressedRowLengths( Vector& rowLengths ) const auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - this->allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + this->reduceAllRows( fetch, std::plus<>{}, keep, 0 ); } template< typename Real, @@ -157,7 +157,7 @@ getRowCapacities( Vector& rowLengths ) const auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - this->allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + this->reduceAllRows( fetch, std::plus<>{}, keep, 0 ); } template< typename Real, @@ -504,7 +504,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) { auto columns_view = this->columnIndexes.getView(); auto values_view = this->values.getView(); @@ -532,7 +532,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchValue > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -rowsReduction( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const +reduceRows( IndexType begin, IndexType end, Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchValue& zero ) const { const auto columns_view = this->columnIndexes.getConstView(); const auto values_view = this->values.getConstView(); @@ -561,9 +561,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, @@ -575,9 +575,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -allRowsReduction( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, const Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h index 66c9540ff..8573a805f 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.h +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -509,12 +509,12 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cpp * \par Output - * \include TridiagonalMatrixExample_rowsReduction.out + * \include TridiagonalMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on matrix rows of constant matrix instances. @@ -536,12 +536,12 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceRows.cpp * \par Output - * \include TridiagonalMatrixExample_rowsReduction.out + * \include TridiagonalMatrixExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType begin, IndexType end, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType begin, IndexType end, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on all matrix rows. @@ -563,12 +563,12 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cpp * \par Output - * \include TridiagonalMatrixExample_allRowsReduction.out + * \include TridiagonalMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on all matrix rows of constant matrix instances. @@ -590,12 +590,12 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_allRowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_reduceAllRows.cpp * \par Output - * \include TridiagonalMatrixExample_allRowsReduction.out + * \include TridiagonalMatrixExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for iteration over matrix rows for constant instances. diff --git a/src/TNL/Matrices/TridiagonalMatrix.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp index 17b0c6501..c6378dfb3 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -348,9 +348,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); + this->view.reduceRows( first, last, fetch, reduce, keep, zero ); } template< typename Real, @@ -361,9 +361,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->view.rowsReduction( first, last, fetch, reduce, keep, zero ); + this->view.reduceRows( first, last, fetch, reduce, keep, zero ); } template< typename Real, @@ -374,9 +374,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->view.reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, @@ -387,9 +387,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->view.rowsReduction( 0, this->getRows(), fetch, reduce, keep, zero ); + this->view.reduceRows( 0, this->getRows(), fetch, reduce, keep, zero ); } template< typename Real, diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index 509ba240e..3135a488f 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -366,12 +366,12 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cpp * \par Output - * \include TridiagonalMatrixViewExample_rowsReduction.out + * \include TridiagonalMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on matrix rows. @@ -393,12 +393,12 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_rowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceRows.cpp * \par Output - * \include TridiagonalMatrixViewExample_rowsReduction.out + * \include TridiagonalMatrixViewExample_reduceRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for performing general reduction on all matrix rows for constant instances. @@ -418,12 +418,12 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cpp * \par Output - * \include TridiagonalMatrixViewExample_allRowsReduction.out + * \include TridiagonalMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const; /** * \brief Method for performing general reduction on all matrix rows. @@ -443,12 +443,12 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param zero is zero of given reduction operation also known as idempotent element. * * \par Example - * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_allRowsReduction.cpp + * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_reduceAllRows.cpp * \par Output - * \include TridiagonalMatrixViewExample_allRowsReduction.out + * \include TridiagonalMatrixViewExample_reduceAllRows.out */ template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > - void allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); + void reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ); /** * \brief Method for iteration over all matrix rows for constant instances. diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index cdb20d703..b4316e69c 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -118,7 +118,7 @@ getCompressedRowLengths( Vector& rowLengths ) const auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - this->allRowsReduction( fetch, reduce, keep, 0 ); + this->reduceAllRows( fetch, reduce, keep, 0 ); } template< typename Real, @@ -279,7 +279,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrixView< Real, Device, Index, Organization >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) const +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) const { using Real_ = decltype( fetch( IndexType(), IndexType(), RealType() ) ); const auto values_view = this->values.getConstView(); @@ -323,7 +323,7 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrixView< Real, Device, Index, Organization >:: -rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) +reduceRows( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero_ ) { using Real_ = decltype( fetch( IndexType(), IndexType(), RealType() ) ); auto values_view = this->values.getConstView(); @@ -367,9 +367,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrixView< Real, Device, Index, Organization >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const { - this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); } template< typename Real, @@ -379,9 +379,9 @@ template< typename Real, template< typename Fetch, typename Reduce, typename Keep, typename FetchReal > void TridiagonalMatrixView< Real, Device, Index, Organization >:: -allRowsReduction( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) +reduceAllRows( Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) { - this->rowsReduction( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); + this->reduceRows( 0, this->indexer.getNonemptyRowsCount(), fetch, reduce, keep, zero ); } template< typename Real, @@ -621,9 +621,9 @@ vectorProduct( const InVector& inVector, if( end == 0 ) end = this->getRows(); if( matrixMultiplicator == 1.0 && outVectorMultiplicator == 0.0 ) - this->rowsReduction( begin, end, fetch, reduction, keeper1, ( RealType ) 0.0 ); + this->reduceRows( begin, end, fetch, reduction, keeper1, ( RealType ) 0.0 ); else - this->rowsReduction( begin, end, fetch, reduction, keeper2, ( RealType ) 0.0 ); + this->reduceRows( begin, end, fetch, reduction, keeper2, ( RealType ) 0.0 ); } template< typename Real, diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp b/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp index 36ea3bc81..ca839a02f 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest.hpp @@ -878,7 +878,7 @@ void test_VectorProduct() } template< typename Matrix > -void test_RowsReduction() +void test_reduceRows() { using RealType = typename Matrix::RealType; using ComputeRealType = typename Matrix::ComputeRealType; @@ -955,7 +955,7 @@ void test_RowsReduction() auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - m.allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + m.reduceAllRows( fetch, std::plus<>{}, keep, 0 ); EXPECT_EQ( rowsCapacities, rowLengths ); m.getCompressedRowLengths( rowLengths ); EXPECT_EQ( rowsCapacities, rowLengths ); @@ -970,7 +970,7 @@ void test_RowsReduction() auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowSums_view[ rowIdx ] = value; }; - m.allRowsReduction( max_fetch, std::plus<>{}, max_keep, 0 ); + m.reduceAllRows( max_fetch, std::plus<>{}, max_keep, 0 ); const auto maxNorm = TNL::max( rowSums ); EXPECT_EQ( maxNorm, 8 ) ; // 29+30+31+32+33+34+35+36 } diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h b/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h index 5a4e98915..8eaf6660b 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest_CSR.h @@ -89,11 +89,11 @@ TYPED_TEST( BinaryMatrixTest_CSR, vectorProductTest ) test_VectorProduct< CSRMatrixType >(); } -TYPED_TEST( BinaryMatrixTest_CSR, rowsReduction ) +TYPED_TEST( BinaryMatrixTest_CSR, reduceRows ) { using CSRMatrixType = typename TestFixture::CSRMatrixType; - test_RowsReduction< CSRMatrixType >(); + test_reduceRows< CSRMatrixType >(); } TYPED_TEST( BinaryMatrixTest_CSR, saveAndLoadTest ) diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h b/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h index b903edeaa..69fc4c737 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest_Ellpack.h @@ -100,11 +100,11 @@ TYPED_TEST( BinaryMatrixTest_Ellpack, vectorProductTest ) test_VectorProduct< EllpackMatrixType >(); } -TYPED_TEST( BinaryMatrixTest_Ellpack, rowsReduction ) +TYPED_TEST( BinaryMatrixTest_Ellpack, reduceRows ) { using EllpackMatrixType = typename TestFixture::EllpackMatrixType; - test_RowsReduction< EllpackMatrixType >(); + test_reduceRows< EllpackMatrixType >(); } TYPED_TEST( BinaryMatrixTest_Ellpack, saveAndLoadTest ) diff --git a/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h b/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h index 673b3b49b..b547ee866 100644 --- a/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h +++ b/src/UnitTests/Matrices/BinarySparseMatrixTest_SlicedEllpack.h @@ -100,11 +100,11 @@ TYPED_TEST( BinaryMatrixTest_SlicedEllpack, vectorProductTest ) test_VectorProduct< SlicedEllpackMatrixType >(); } -TYPED_TEST( BinaryMatrixTest_SlicedEllpack, rowsReduction ) +TYPED_TEST( BinaryMatrixTest_SlicedEllpack, reduceRows ) { using SlicedEllpackMatrixType = typename TestFixture::SlicedEllpackMatrixType; - test_RowsReduction< SlicedEllpackMatrixType >(); + test_reduceRows< SlicedEllpackMatrixType >(); } TYPED_TEST( BinaryMatrixTest_SlicedEllpack, saveAndLoadTest ) diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h index 61ab14ba5..4767b1bdf 100644 --- a/src/UnitTests/Matrices/DenseMatrixTest.h +++ b/src/UnitTests/Matrices/DenseMatrixTest.h @@ -836,7 +836,7 @@ void test_ForRows() } template< typename Matrix > -void test_RowsReduction() +void test_reduceRows() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -911,7 +911,7 @@ void test_RowsReduction() auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - m.allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + m.reduceAllRows( fetch, std::plus<>{}, keep, 0 ); EXPECT_EQ( rowsCapacities, rowLengths ); m.getCompressedRowLengths( rowLengths ); EXPECT_EQ( rowsCapacities, rowLengths ); @@ -926,7 +926,7 @@ void test_RowsReduction() auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowSums_view[ rowIdx ] = value; }; - m.allRowsReduction( max_fetch, std::plus<>{}, max_keep, 0 ); + m.reduceAllRows( max_fetch, std::plus<>{}, max_keep, 0 ); const RealType maxNorm = TNL::max( rowSums ); EXPECT_EQ( maxNorm, 260 ) ; // 29+30+31+32+33+34+35+36 } diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.h b/src/UnitTests/Matrices/LambdaMatrixTest.h index 25d2be160..00a8fad01 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.h +++ b/src/UnitTests/Matrices/LambdaMatrixTest.h @@ -104,11 +104,11 @@ TYPED_TEST( LambdaMatrixTest, vectorProductTest ) test_VectorProduct< LambdaMatrixParametersType >(); } -TYPED_TEST( LambdaMatrixTest, rowsReduction ) +TYPED_TEST( LambdaMatrixTest, reduceRows ) { using LambdaMatrixParametersType = typename TestFixture::LambdaMatrixType; - test_RowsReduction< LambdaMatrixParametersType >(); + test_reduceRows< LambdaMatrixParametersType >(); } #endif diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index af6375126..c3891aed5 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -259,7 +259,7 @@ void test_VectorProduct() } template< typename Matrix > -void test_RowsReduction() +void test_reduceRows() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -300,7 +300,7 @@ void test_RowsReduction() auto keep = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable { vView[ row ] = value; }; - A.allRowsReduction( fetch, reduce, keep, 0.0 ); + A.reduceAllRows( fetch, reduce, keep, 0.0 ); EXPECT_EQ( v.getElement( 0 ), 1.0 ); EXPECT_EQ( v.getElement( 1 ), 0.0 ); diff --git a/src/UnitTests/Matrices/Legacy/Legacy_SparseMatrixTest.hpp b/src/UnitTests/Matrices/Legacy/Legacy_SparseMatrixTest.hpp index ada0a79ec..68b404799 100644 --- a/src/UnitTests/Matrices/Legacy/Legacy_SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/Legacy/Legacy_SparseMatrixTest.hpp @@ -1466,7 +1466,7 @@ void test_VectorProductCSRAdaptive() } template< typename Matrix > -void test_RowsReduction() +void test_reduceRows() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -1547,7 +1547,7 @@ void test_RowsReduction() auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - m.allRowsReduction( fetch, reduce, keep, 0 ); + m.reduceAllRows( fetch, reduce, keep, 0 ); EXPECT_EQ( rowsCapacities, rowLengths ); m.getCompressedRowLengths( rowLengths ); EXPECT_EQ( rowsCapacities, rowLengths ); @@ -1565,7 +1565,7 @@ void test_RowsReduction() auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowSums_view[ rowIdx ] = value; }; - m.allRowsReduction( max_fetch, max_reduce, max_keep, 0 ); + m.reduceAllRows( max_fetch, max_reduce, max_keep, 0 ); const RealType maxNorm = TNL::max( rowSums ); EXPECT_EQ( maxNorm, 260 ) ; // 29+30+31+32+33+34+35+36 } diff --git a/src/UnitTests/Matrices/SparseMatrixTest.h b/src/UnitTests/Matrices/SparseMatrixTest.h index f26012975..1ae0fda8a 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.h +++ b/src/UnitTests/Matrices/SparseMatrixTest.h @@ -109,11 +109,11 @@ TYPED_TEST( MatrixTest, forRows ) test_ForRows< MatrixType >(); } -TYPED_TEST( MatrixTest, rowsReduction ) +TYPED_TEST( MatrixTest, reduceRows ) { using MatrixType = typename TestFixture::MatrixType; - test_RowsReduction< MatrixType >(); + test_reduceRows< MatrixType >(); } TYPED_TEST( MatrixTest, saveAndLoadTest ) diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 300a1f91e..3c73456cb 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1458,7 +1458,7 @@ void test_ForRows() } template< typename Matrix > -void test_RowsReduction() +void test_reduceRows() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -1526,7 +1526,7 @@ void test_RowsReduction() auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] = value; }; - m.allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + m.reduceAllRows( fetch, std::plus<>{}, keep, 0 ); EXPECT_EQ( rowsCapacities, rowLengths ); m.getCompressedRowLengths( rowLengths ); EXPECT_EQ( rowsCapacities, rowLengths ); @@ -1541,7 +1541,7 @@ void test_RowsReduction() auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowSums_view[ rowIdx ] = value; }; - m.allRowsReduction( max_fetch, std::plus<>{}, max_keep, 0 ); + m.reduceAllRows( max_fetch, std::plus<>{}, max_keep, 0 ); const RealType maxNorm = TNL::max( rowSums ); EXPECT_EQ( maxNorm, 260 ) ; // 29+30+31+32+33+34+35+36 } diff --git a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h index d871579b3..e8de36e30 100644 --- a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h +++ b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.h @@ -88,11 +88,11 @@ TYPED_TEST( MatrixTest, vectorProductTest ) test_VectorProduct< MatrixType >(); } -TYPED_TEST( MatrixTest, rowsReduction ) +TYPED_TEST( MatrixTest, reduceRows ) { using MatrixType = typename TestFixture::MatrixType; - test_RowsReduction< MatrixType >(); + test_reduceRows< MatrixType >(); } TYPED_TEST( MatrixTest, saveAndLoadTest ) diff --git a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp index 01815e439..f430fdffa 100644 --- a/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SymmetricSparseMatrixTest.hpp @@ -858,7 +858,7 @@ void test_VectorProduct() } template< typename Matrix > -void test_RowsReduction() +void test_reduceRows() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; @@ -905,7 +905,7 @@ void test_RowsReduction() auto keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowLengths_view[ rowIdx ] += value; }; - m_5.allRowsReduction( fetch, std::plus<>{}, keep, 0 ); + m_5.reduceAllRows( fetch, std::plus<>{}, keep, 0 ); EXPECT_EQ( rowLengths_true, rowLengths ); m_5.getCompressedRowLengths( rowLengths ); @@ -925,7 +925,7 @@ void test_RowsReduction() auto max_keep = [=] __cuda_callable__ ( const IndexType rowIdx, const IndexType value ) mutable { rowSums_view[ rowIdx ] = value; }; - m_5.allRowsReduction( max_fetch, max_reduce, max_keep, 0 ); + m_5.reduceAllRows( max_fetch, max_reduce, max_keep, 0 ); const RealType maxNorm = TNL::max( rowSums ); EXPECT_EQ( maxNorm, 260 ) ; // 29+30+31+32+33+34+35+36*/ } -- GitLab From 9b92b2e93aa8bd7ebfc387a5134be81cc358ef37 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Fri, 19 Mar 2021 22:44:42 +0100 Subject: [PATCH 44/54] Fixed tutorials examples names for reduceRows. --- ...ion_maxNorm.cpp => DenseMatrixExample_reduceRows_maxNorm.cpp} | 0 .../Tutorials/Matrices/DenseMatrixExample_reduceRows_maxNorm.cu | 1 + ...oduct.cpp => DenseMatrixExample_reduceRows_vectorProduct.cpp} | 0 .../Matrices/DenseMatrixExample_reduceRows_vectorProduct.cu | 1 + .../Matrices/DenseMatrixExample_rowsReduction_maxNorm.cu | 1 - .../Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cu | 1 - ...duct.cpp => SparseMatrixExample_reduceRows_vectorProduct.cpp} | 0 .../Matrices/SparseMatrixExample_reduceRows_vectorProduct.cu | 1 + .../Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cu | 1 - .../Matrices/TridiagonalMatrixExample_rowsReduction.cpp | 1 - 10 files changed, 3 insertions(+), 4 deletions(-) rename Documentation/Tutorials/Matrices/{DenseMatrixExample_rowsReduction_maxNorm.cpp => DenseMatrixExample_reduceRows_maxNorm.cpp} (100%) create mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_maxNorm.cu rename Documentation/Tutorials/Matrices/{DenseMatrixExample_rowsReduction_vectorProduct.cpp => DenseMatrixExample_reduceRows_vectorProduct.cpp} (100%) create mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_vectorProduct.cu delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cu delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cu rename Documentation/Tutorials/Matrices/{SparseMatrixExample_rowsReduction_vectorProduct.cpp => SparseMatrixExample_reduceRows_vectorProduct.cpp} (100%) create mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_reduceRows_vectorProduct.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cu delete mode 120000 Documentation/Tutorials/Matrices/TridiagonalMatrixExample_rowsReduction.cpp diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cpp b/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_maxNorm.cpp similarity index 100% rename from Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cpp rename to Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_maxNorm.cpp diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_maxNorm.cu b/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_maxNorm.cu new file mode 120000 index 000000000..d58eacc45 --- /dev/null +++ b/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_maxNorm.cu @@ -0,0 +1 @@ +DenseMatrixExample_reduceRows_maxNorm.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cpp b/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_vectorProduct.cpp similarity index 100% rename from Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cpp rename to Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_vectorProduct.cpp diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_vectorProduct.cu b/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_vectorProduct.cu new file mode 120000 index 000000000..7ba31dc3b --- /dev/null +++ b/Documentation/Tutorials/Matrices/DenseMatrixExample_reduceRows_vectorProduct.cu @@ -0,0 +1 @@ +DenseMatrixExample_reduceRows_vectorProduct.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cu b/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cu deleted file mode 120000 index 04b5e78e1..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_maxNorm.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_rowsReduction_maxNorm.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cu b/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cu deleted file mode 120000 index 36e05a773..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_rowsReduction_vectorProduct.cu +++ /dev/null @@ -1 +0,0 @@ -DenseMatrixExample_rowsReduction_vectorProduct.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_reduceRows_vectorProduct.cpp similarity index 100% rename from Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cpp rename to Documentation/Tutorials/Matrices/SparseMatrixExample_reduceRows_vectorProduct.cpp diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_reduceRows_vectorProduct.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_reduceRows_vectorProduct.cu new file mode 120000 index 000000000..f2c6d541f --- /dev/null +++ b/Documentation/Tutorials/Matrices/SparseMatrixExample_reduceRows_vectorProduct.cu @@ -0,0 +1 @@ +SparseMatrixExample_reduceRows_vectorProduct.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cu deleted file mode 120000 index 1be7a26d8..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_rowsReduction_vectorProduct.cu +++ /dev/null @@ -1 +0,0 @@ -SparseMatrixExample_rowsReduction_vectorProduct.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_rowsReduction.cpp b/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_rowsReduction.cpp deleted file mode 120000 index 5a8b79027..000000000 --- a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_rowsReduction.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_rowsReduction.cpp \ No newline at end of file -- GitLab From 3b1025f155d30e5bb7cab2069b107881bf33f4e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sat, 20 Mar 2021 10:40:18 +0100 Subject: [PATCH 45/54] Fixed memory leak in DenseMatrixViewExample_data_encapsulation example. --- .../DenseMatrixExample_Constructor_init_list.cpp | 1 - .../DenseMatrixExample_Constructor_init_list.cu | 1 - .../Matrices/DenseMatrixExample_addElement.cpp | 1 - .../Matrices/DenseMatrixExample_addElement.cu | 1 - .../Matrices/DenseMatrixExample_setElement.cpp | 1 - .../Matrices/DenseMatrixExample_setElement.cu | 1 - .../DenseMatrixViewExample_data_encapsulation.cpp | 11 +++++++++++ .../Matrices/DenseMatrixViewExample_setElement.cpp | 1 - .../Matrices/DenseMatrixViewExample_setElement.cu | 1 - .../MultidiagonalMatrixExample_Constructor.cpp | 1 - ...idiagonalMatrixExample_Constructor_init_list_1.cpp | 1 - .../SparseMatrixExample_Constructor_init_list_2.cpp | 1 - .../SparseMatrixExample_Constructor_init_list_2.cu | 1 - ...MatrixExample_Constructor_rowCapacities_vector.cpp | 1 - ...eMatrixExample_Constructor_rowCapacities_vector.cu | 1 - .../SparseMatrixExample_Constructor_std_map.cpp | 1 - .../SparseMatrixExample_Constructor_std_map.cu | 1 - .../Matrices/SparseMatrixExample_addElement.cpp | 1 - .../Matrices/SparseMatrixExample_addElement.cu | 1 - .../Matrices/SparseMatrixExample_setElement.cpp | 1 - .../Matrices/SparseMatrixExample_setElement.cu | 1 - .../Matrices/SparseMatrixExample_setElements.cpp | 1 - .../Matrices/SparseMatrixExample_setElements.cu | 1 - .../Matrices/SparseMatrixExample_setElements_map.cpp | 1 - .../Matrices/SparseMatrixExample_setElements_map.cu | 1 - .../Matrices/SparseMatrixExample_setRowCapacities.cpp | 1 - .../Matrices/SparseMatrixExample_setRowCapacities.cu | 1 - .../Matrices/SparseMatrixViewExample_setElement.cpp | 1 - .../Matrices/SparseMatrixViewExample_setElement.cu | 1 - ...idiagonalMatrixExample_Constructor_init_list_1.cpp | 1 - .../Matrices/TridiagonalMatrixExample_setElement.cpp | 1 - .../Matrices/TridiagonalMatrixExample_setElements.cpp | 1 - .../Matrices/TridiagonalMatrixViewExample_getRow.cpp | 1 - .../TridiagonalMatrixViewExample_setElement.cpp | 1 - 34 files changed, 11 insertions(+), 33 deletions(-) delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cpp delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cu delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cpp delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cu delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cpp delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cu delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cpp delete mode 120000 Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cu delete mode 120000 Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor.cpp delete mode 120000 Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor_init_list_1.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cu delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cpp delete mode 120000 Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cu delete mode 120000 Documentation/Tutorials/Matrices/TridiagonalMatrixExample_Constructor_init_list_1.cpp delete mode 120000 Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElement.cpp delete mode 120000 Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElements.cpp delete mode 120000 Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_getRow.cpp delete mode 120000 Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_setElement.cpp diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cpp b/Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cpp deleted file mode 120000 index faa270f15..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cu b/Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cu deleted file mode 120000 index e633e76a9..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_Constructor_init_list.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixExample_Constructor_init_list.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cpp b/Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cpp deleted file mode 120000 index c471b0ce3..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cu b/Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cu deleted file mode 120000 index 67dd6dced..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_addElement.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixExample_addElement.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cpp b/Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cpp deleted file mode 120000 index cb68721bb..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cu b/Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cu deleted file mode 120000 index 79539e197..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixExample_setElement.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixExample_setElement.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cpp b/Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cpp index 99cf67583..79fb9b3d9 100644 --- a/Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cpp +++ b/Documentation/Tutorials/Matrices/DenseMatrixViewExample_data_encapsulation.cpp @@ -49,6 +49,17 @@ void encapsulation() std::cout << "Dense matrix view after elements manipulation:" << std::endl; std::cout << matrix << std::endl; + + /*** + * Do not forget to free allocated memory :) + */ + delete[] host_data; + if( std::is_same< Device, TNL::Devices::Host >::value ) + delete[] data; +#ifdef HAVE_CUDA + else if( std::is_same< Device, TNL::Devices::Cuda >::value ) + cudaFree( data ); +#endif } int main( int argc, char* argv[] ) diff --git a/Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cpp b/Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cpp deleted file mode 120000 index a3832e2e8..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cu b/Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cu deleted file mode 120000 index 9d1266dd3..000000000 --- a/Documentation/Tutorials/Matrices/DenseMatrixViewExample_setElement.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/DenseMatrix/DenseMatrixViewExample_setElement.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor.cpp b/Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor.cpp deleted file mode 120000 index da7690427..000000000 --- a/Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor_init_list_1.cpp b/Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor_init_list_1.cpp deleted file mode 120000 index 1e5ca52b0..000000000 --- a/Documentation/Tutorials/Matrices/MultidiagonalMatrixExample_Constructor_init_list_1.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_Constructor_init_list_1.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cpp deleted file mode 120000 index 9d23bbb1c..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cu deleted file mode 120000 index 759f1a1ca..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_init_list_2.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_init_list_2.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cpp deleted file mode 120000 index ddeed9a7b..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_rowCapacities_vector.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cu deleted file mode 120000 index 5957448c6..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_rowCapacities_vector.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_rowCapacities_vector.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cpp deleted file mode 120000 index dcc4ec9ae..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cu deleted file mode 120000 index 75a75befb..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_Constructor_std_map.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_Constructor_std_map.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cpp deleted file mode 120000 index 215dde5df..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cu deleted file mode 120000 index c2425241f..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_addElement.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_addElement.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cpp deleted file mode 120000 index 1507393de..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cu deleted file mode 120000 index 2f13c04ed..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElement.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setElement.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cpp deleted file mode 120000 index 0f5e5d1dd..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cu deleted file mode 120000 index 120be6659..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cpp deleted file mode 120000 index 5206fcc2e..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cu deleted file mode 120000 index 9c5f7c0f5..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setElements_map.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setElements_map.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cpp b/Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cpp deleted file mode 120000 index 973b2f3a8..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cu b/Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cu deleted file mode 120000 index ef674e0f0..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixExample_setRowCapacities.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixExample_setRowCapacities.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cpp b/Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cpp deleted file mode 120000 index 0b861369e..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cu b/Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cu deleted file mode 120000 index 9a6e8304d..000000000 --- a/Documentation/Tutorials/Matrices/SparseMatrixViewExample_setElement.cu +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/SparseMatrix/SparseMatrixViewExample_setElement.cu \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_Constructor_init_list_1.cpp b/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_Constructor_init_list_1.cpp deleted file mode 120000 index f074fa48b..000000000 --- a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_Constructor_init_list_1.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_Constructor_init_list_1.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElement.cpp b/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElement.cpp deleted file mode 120000 index aa3443952..000000000 --- a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElement.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElements.cpp b/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElements.cpp deleted file mode 120000 index 6a1a2e1ef..000000000 --- a/Documentation/Tutorials/Matrices/TridiagonalMatrixExample_setElements.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_setElements.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_getRow.cpp b/Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_getRow.cpp deleted file mode 120000 index 960c717f4..000000000 --- a/Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_getRow.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_setElement.cpp b/Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_setElement.cpp deleted file mode 120000 index 59094634e..000000000 --- a/Documentation/Tutorials/Matrices/TridiagonalMatrixViewExample_setElement.cpp +++ /dev/null @@ -1 +0,0 @@ -../../Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_setElement.cpp \ No newline at end of file -- GitLab From 462f67069ab71da34402bc28806625611d5021f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sat, 20 Mar 2021 10:41:24 +0100 Subject: [PATCH 46/54] Refactoring of CMakeLists in matrix tutorials. --- .../Tutorials/Matrices/CMakeLists.txt | 188 ++++-------------- 1 file changed, 42 insertions(+), 146 deletions(-) diff --git a/Documentation/Tutorials/Matrices/CMakeLists.txt b/Documentation/Tutorials/Matrices/CMakeLists.txt index 8b56bd510..70b3475d8 100644 --- a/Documentation/Tutorials/Matrices/CMakeLists.txt +++ b/Documentation/Tutorials/Matrices/CMakeLists.txt @@ -1,152 +1,48 @@ -IF( BUILD_CUDA ) - CUDA_ADD_EXECUTABLE( DenseMatrixExample_Constructor_init_list DenseMatrixExample_Constructor_init_list.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_Constructor_init_list > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_Constructor_init_list.out - OUTPUT DenseMatrixExample_Constructor_init_list.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_addElement DenseMatrixExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_addElement.out - OUTPUT DenseMatrixExample_addElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_setElement DenseMatrixExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_setElement.out - OUTPUT DenseMatrixExample_setElement.out ) - - #CUDA_ADD_EXECUTABLE( DenseMatrixExample_forRows DenseMatrixExample_forRows.cu ) - #ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_forRows > - # ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_forRows.out - # OUTPUT DenseMatrixExample_forRows.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_reduceRows_vectorProduct DenseMatrixExample_reduceRows_vectorProduct.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_reduceRows_vectorProduct > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_reduceRows_vectorProduct.out - OUTPUT DenseMatrixExample_reduceRows_vectorProduct.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixExample_reduceRows_maxNorm DenseMatrixExample_reduceRows_maxNorm.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixExample_reduceRows_maxNorm > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixExample_reduceRows_maxNorm.out - OUTPUT DenseMatrixExample_reduceRows_maxNorm.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_setElement DenseMatrixViewExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_setElement.out OUTPUT - DenseMatrixViewExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( DenseMatrixViewExample_data_encapsulation DenseMatrixViewExample_data_encapsulation.cu ) - ADD_CUSTOM_COMMAND( COMMAND DenseMatrixViewExample_data_encapsulation > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/DenseMatrixViewExample_data_encapsulation.out OUTPUT - DenseMatrixViewExample_data_encapsulation.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_init_list_2 SparseMatrixExample_Constructor_init_list_2.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_init_list_2 > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_init_list_2.out - OUTPUT SparseMatrixExample_Constructor_init_list_2.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setRowCapacities SparseMatrixExample_setRowCapacities.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setRowCapacities > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setRowCapacities.out - OUTPUT SparseMatrixExample_setRowCapacities.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_rowCapacities_vector SparseMatrixExample_Constructor_rowCapacities_vector.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_rowCapacities_vector > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_rowCapacities_vector.out - OUTPUT SparseMatrixExample_Constructor_rowCapacities_vector.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_Constructor_std_map SparseMatrixExample_Constructor_std_map.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_Constructor_std_map > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_Constructor_std_map.out - OUTPUT SparseMatrixExample_Constructor_std_map.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements SparseMatrixExample_setElements.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements.out - OUTPUT SparseMatrixExample_setElements.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElements_map SparseMatrixExample_setElements_map.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElements_map > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElements_map.out - OUTPUT SparseMatrixExample_setElements_map.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_setElement SparseMatrixExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_setElement.out - OUTPUT SparseMatrixExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_addElement SparseMatrixExample_addElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_addElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_addElement.out - OUTPUT SparseMatrixExample_addElement.out ) - -# CUDA_ADD_EXECUTABLE( SparseMatrixExample_forRows SparseMatrixExample_forRows.cu ) -# ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_forRows > -# ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_forRows.out -# OUTPUT SparseMatrixExample_forRows.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixExample_reduceRows_vectorProduct SparseMatrixExample_reduceRows_vectorProduct.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixExample_reduceRows_vectorProduct > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixExample_reduceRows_vectorProduct.out - OUTPUT SparseMatrixExample_reduceRows_vectorProduct.out ) - - CUDA_ADD_EXECUTABLE( SparseMatrixViewExample_setElement SparseMatrixViewExample_setElement.cu ) - ADD_CUSTOM_COMMAND( COMMAND SparseMatrixViewExample_setElement > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SparseMatrixViewExample_setElement.out - OUTPUT SparseMatrixViewExample_setElement.out ) - - CUDA_ADD_EXECUTABLE( SymmetricSparseMatrixExample SymmetricSparseMatrixExample.cu ) - ADD_CUSTOM_COMMAND( COMMAND SymmetricSparseMatrixExample > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/SymmetricSparseMatrixExample.out - OUTPUT SymmetricSparseMatrixExample.out ) - - CUDA_ADD_EXECUTABLE( BinarySparseMatrixExample BinarySparseMatrixExample.cu ) - ADD_CUSTOM_COMMAND( COMMAND BinarySparseMatrixExample > - ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/BinarySparseMatrixExample.out - OUTPUT BinarySparseMatrixExample.out ) - +set( COMMON_EXAMPLES + BinarySparseMatrixExample + DenseMatrixExample_reduceRows_maxNorm + DenseMatrixExample_reduceRows_vectorProduct + DenseMatrixViewExample_data_encapsulation + SparseMatrixExample_reduceRows_vectorProduct + SymmetricSparseMatrixExample +) - #### - # THe following examples/benchmarks run for very long time - CUDA_ADD_EXECUTABLE( DenseMatrixSetup_Benchmark_cuda DenseMatrixSetup_Benchmark.cu ) - CUDA_ADD_EXECUTABLE( SparseMatrixSetup_Benchmark_cuda SparseMatrixSetup_Benchmark.cu ) - CUDA_ADD_EXECUTABLE( MultidiagonalMatrixSetup_Benchmark_cuda MultidiagonalMatrixSetup_Benchmark.cu ) +#### +# The following examples/benchmarks run for very long time +# We just build them and do not run automatically. +set( LONG_EXAMPLES + DenseMatrixSetup_Benchmark + MultidiagonalMatrixSetup_Benchmark + SparseMatrixSetup_Benchmark +) -ELSE() +if( BUILD_CUDA ) + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) + endforeach() + foreach( target IN ITEMS ${LONG_EXAMPLES} ) + cuda_add_executable( ${target}-cuda ${target}.cu OPTIONS ) + #add_custom_command( COMMAND ${target}-cuda > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + #set( CUDA_OUTPUTS ${CUDA_OUTPUTS} ${target}.out ) + endforeach() +else() + foreach( target IN ITEMS ${COMMON_EXAMPLES} ) + add_executable( ${target} ${target}.cpp ) + add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) + endforeach() + foreach( target IN ITEMS ${LONG_EXAMPLES} ) + add_executable( ${target} ${target}.cpp ) + #add_custom_command( COMMAND ${target} > ${TNL_DOCUMENTATION_OUTPUT_SNIPPETS_PATH}/${target}.out OUTPUT ${target}.out ) + #set( HOST_OUTPUTS ${HOST_OUTPUTS} ${target}.out ) + endforeach() +endif() - #### - # THe following examples/benchmarks run for very long time - ADD_EXECUTABLE( DenseMatrixSetup_Benchmark DenseMatrixSetup_Benchmark.cpp ) - ADD_EXECUTABLE( SparseMatrixSetup_Benchmark SparseMatrixSetup_Benchmark.cpp ) - ADD_EXECUTABLE( MultidiagonalMatrixSetup_Benchmark MultidiagonalMatrixSetup_Benchmark.cpp ) -ENDIF() IF( BUILD_CUDA ) -ADD_CUSTOM_TARGET( TutorialsMatricesCuda ALL DEPENDS - DenseMatrixExample_Constructor_init_list.out - DenseMatrixExample_addElement.out - DenseMatrixExample_setElement.out -# DenseMatrixExample_forRows.out - DenseMatrixExample_reduceRows_vectorProduct.out - DenseMatrixExample_reduceRows_maxNorm.out - DenseMatrixViewExample_setElement.out - DenseMatrixViewExample_data_encapsulation.out - SparseMatrixExample_Constructor_init_list_2.out - SparseMatrixExample_setRowCapacities.out - SparseMatrixExample_Constructor_std_map.out - SparseMatrixExample_setElements.out - SparseMatrixExample_setElements_map.out - SparseMatrixExample_setElement.out -# SparseMatrixExample_forRows.out - SparseMatrixExample_reduceRows_vectorProduct.out - SparseMatrixViewExample_setElement.out - SymmetricSparseMatrixExample.out - BinarySparseMatrixExample.out - ) + ADD_CUSTOM_TARGET( RunTutorialsMatricesExamples-cuda ALL DEPENDS ${CUDA_OUTPUTS} ) ELSE() -ADD_CUSTOM_TARGET( TutorialsMatrices ALL DEPENDS -) -ENDIF() -# -#ADD_CUSTOM_TARGET( TutorialsPointers ALL DEPENDS -# UniquePointerHostExample.out -#) \ No newline at end of file + ADD_CUSTOM_TARGET( RunTutorialsMatricesExamples ALL DEPENDS ${HOST_OUTPUTS} ) +ENDIF() \ No newline at end of file -- GitLab From 74aa95aa79ff1ea552dca747c0432c1edf191d07 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sat, 20 Mar 2021 16:23:27 +0100 Subject: [PATCH 47/54] Fixing examples on forRows method in matrices. --- .../DenseMatrixExample_forRows.cpp | 30 +++--- .../DenseMatrixViewExample_forRows.cpp | 30 +++--- .../Matrices/LambdaMatrix/CMakeLists.txt | 1 + .../LambdaMatrixExample_forRows.cpp | 92 +++++++++++-------- .../LambdaMatrixExample_getRow.cpp | 90 ++++++++++++++++++ .../LambdaMatrixExample_getRow.cu | 76 +-------------- .../MultidiagonalMatrixExample_forRows.cpp | 37 +++----- .../MultidiagonalMatrixExample_getRow.cpp | 4 +- ...MultidiagonalMatrixViewExample_forRows.cpp | 37 +++----- .../SparseMatrixExample_forRows.cpp | 40 +++++--- .../SparseMatrixViewExample_forRows.cpp | 40 +++++--- .../TridiagonalMatrixExample_forRows.cpp | 44 +++------ .../TridiagonalMatrixViewExample_forRows.cpp | 48 +++------- .../Tutorials/Matrices/tutorial_Matrices.md | 4 +- src/TNL/Matrices/DenseMatrixRowView.h | 8 ++ src/TNL/Matrices/DenseMatrixRowView.hpp | 9 ++ src/TNL/Matrices/DenseMatrixView.hpp | 2 +- src/TNL/Matrices/LambdaMatrix.h | 14 +-- src/TNL/Matrices/LambdaMatrix.hpp | 12 +-- src/TNL/Matrices/MatrixWriter.hpp | 2 +- src/TNL/Matrices/MultidiagonalMatrix.h | 24 ++--- src/TNL/Matrices/MultidiagonalMatrix.hpp | 4 +- src/TNL/Matrices/MultidiagonalMatrixView.h | 24 ++--- src/TNL/Matrices/MultidiagonalMatrixView.hpp | 8 +- src/TNL/Matrices/SparseMatrix.h | 24 ++--- src/TNL/Matrices/SparseMatrix.hpp | 4 +- src/TNL/Matrices/SparseMatrixView.h | 24 ++--- src/TNL/Matrices/SparseMatrixView.hpp | 12 +-- src/TNL/Matrices/TridiagonalMatrix.h | 24 ++--- src/TNL/Matrices/TridiagonalMatrix.hpp | 4 +- src/TNL/Matrices/TridiagonalMatrixView.h | 24 ++--- src/TNL/Matrices/TridiagonalMatrixView.hpp | 8 +- src/UnitTests/Matrices/LambdaMatrixTest.h | 4 +- src/UnitTests/Matrices/LambdaMatrixTest.hpp | 2 +- 34 files changed, 414 insertions(+), 396 deletions(-) create mode 100644 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp mode change 100644 => 120000 Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp index 0ddaa4d6c..932802de9 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp @@ -1,37 +1,35 @@ #include -#include +#include +#include #include #include template< typename Device > void forRowsExample() { - using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; - using RowViewType = typename MatrixType::RowViewType; - MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; + using RowView = typename MatrixType::RowView; + MatrixType matrix( 5, 5 ); - auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { - for( int localIdx = 0; - localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements - localIdx++ ) // than we requested. These padding elements are processed here as well. - // and so we cannot use row.getSize() - { - row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); - row.setColumnIndex( localIdx, localIdx ); - } + auto f = [=] __cuda_callable__ ( RowView& row ) mutable { + const int& rowIdx = row.getRowIndex(); + row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); }; - matrix.forAllRows( f ); + /*** + * Set the matrix elements. + */ + matrix.forAllRows( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Creating matrix on host: " << std::endl; + std::cout << "Getting matrix rows on host: " << std::endl; forRowsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; + std::cout << "Getting matrix rows on CUDA device: " << std::endl; forRowsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp index 9ec36d7ea..255d59096 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp @@ -1,38 +1,36 @@ #include -#include +#include +#include #include #include template< typename Device > void forRowsExample() { - using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; - using RowViewType = typename MatrixType::RowViewType; - MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; + using RowView = typename MatrixType::RowView; + MatrixType matrix( 5, 5 ); auto view = matrix.getView(); - auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { - for( int localIdx = 0; - localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements - localIdx++ ) // than we requested. These padding elements are processed here as well. - // and so we cannot use row.getSize() - { - row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); - row.setColumnIndex( localIdx, localIdx ); - } + auto f = [=] __cuda_callable__ ( RowView& row ) mutable { + const int& rowIdx = row.getRowIndex(); + row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); }; - view.forAllRows( f ); + /*** + * Set the matrix elements. + */ + view.forAllRows( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Creating matrix on host: " << std::endl; + std::cout << "Getting matrix rows on host: " << std::endl; forRowsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; + std::cout << "Getting matrix rows on CUDA device: " << std::endl; forRowsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt index 94cf64959..f16a4e697 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt +++ b/Documentation/Examples/Matrices/LambdaMatrix/CMakeLists.txt @@ -4,6 +4,7 @@ set( COMMON_EXAMPLES LambdaMatrixExample_getNonzeroElementsCount LambdaMatrixExample_Laplace LambdaMatrixExample_Laplace_2 + LambdaMatrixExample_getRow LambdaMatrixExample_reduceRows LambdaMatrixExample_reduceAllRows LambdaMatrixExample_forElements diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp index eaba26dcf..a4ef60f96 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp @@ -8,55 +8,73 @@ template< typename Device > void forRowsExample() { - /** - * Prepare lambda matrix of the following form: - * - * / 1 0 0 0 0 \ - * | -2 1 -2 0 0 | - * | 0 -2 1 -2 0 | - * | 0 0 -2 1 -2 | - * | 0 0 0 -2 1 | - * \ 0 0 0 0 1 / + /*** + * Set matrix representing approximation of the Laplace operator on regular + * grid using the finite difference method. */ - - int size = 5; - auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int { - if( rowIdx == 0 || rowIdx == size - 1 ) - return 1; - return 3; + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int + { + const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid + const int gridColumn = rowIdx % gridSize; + if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node + gridColumn == 0 || gridColumn == gridSize - 1 ) + return 1; + return 5; }; - - auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value ) { - if( rowIdx == 0 || rowIdx == size -1 ) - { - columnIdx = rowIdx; - value = 1.0; - } - else - { - columnIdx = rowIdx + localIdx - 1; - value = ( columnIdx == rowIdx ) ? -2.0 : 1.0; - } + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value) { + const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid + const int gridColumn = rowIdx % gridSize; + if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node + gridColumn == 0 || gridColumn == gridSize - 1 ) + { + columnIdx = rowIdx; // diagonal element .... + value = 1.0; // ... is set to 1 + } + else // interior grid node + { + switch( localIdx ) // set diagonal element to 4 + { // and the others to -1 + case 0: + columnIdx = rowIdx - gridSize; + value = 1; + break; + case 1: + columnIdx = rowIdx - 1; + value = 1; + break; + case 2: + columnIdx = rowIdx; + value = -4; + break; + case 3: + columnIdx = rowIdx + 1; + value = 1; + break; + case 4: + columnIdx = rowIdx + gridSize; + value = 1; + break; + } + } }; + auto matrix = TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( + matrixSize, matrixSize, matrixElements, rowLengths ); + using MatrixType = decltype( matrix ); - using MatrixType = decltype( TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( matrixElements, rowLengths ) ); - MatrixType matrix( size, size, matrixElements, rowLengths ); - - /** - * Use the `forRows` method to copy the matrix elements to a dense matrix. - */ - TNL::Matrices::DenseMatrix< double, Device, int > denseMatrix( size, size ); + TNL::Matrices::DenseMatrix< double, Device > denseMatrix( matrixSize, matrixSize ); denseMatrix.setValue( 0.0 ); auto dense_view = denseMatrix.getView(); - auto f = [=] __cuda_callable__ ( const typename MatrixType::RowViewType& row ) mutable { + auto f = [=] __cuda_callable__ ( const typename MatrixType::RowView& row ) mutable { auto dense_row = dense_view.getRow( row.getRowIndex() ); for( int localIdx = 0; localIdx < row.getSize(); localIdx++ ) dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); }; matrix.forAllRows( f ); - std::cout << "Lambda matrix looks as:" << std::endl << matrix << std::endl; - std::cout << "Dense matrix looks as:" << std::endl << denseMatrix << std::endl; + std::cout << "Laplace operator lambda matrix: " << std::endl << matrix << std::endl; + std::cout << "Laplace operator dense matrix: " << std::endl << denseMatrix << std::endl; } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp new file mode 100644 index 000000000..54e4e47e8 --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp @@ -0,0 +1,90 @@ +#include +#include +#include +#include +#include + + +template< typename Device > +void getRowExample() +{ + /*** + * Set matrix representing approximation of the Laplace operator on regular + * grid using the finite difference method. + */ + const int gridSize( 4 ); + const int matrixSize = gridSize * gridSize; + auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int + { + const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid + const int gridColumn = rowIdx % gridSize; + if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node + gridColumn == 0 || gridColumn == gridSize - 1 ) + return 1; + return 5; + }; + auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value) { + const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid + const int gridColumn = rowIdx % gridSize; + if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node + gridColumn == 0 || gridColumn == gridSize - 1 ) + { + columnIdx = rowIdx; // diagonal element .... + value = 1.0; // ... is set to 1 + } + else // interior grid node + { + switch( localIdx ) // set diagonal element to 4 + { // and the others to -1 + case 0: + columnIdx = rowIdx - gridSize; + value = 1; + break; + case 1: + columnIdx = rowIdx - 1; + value = 1; + break; + case 2: + columnIdx = rowIdx; + value = -4; + break; + case 3: + columnIdx = rowIdx + 1; + value = 1; + break; + case 4: + columnIdx = rowIdx + gridSize; + value = 1; + break; + } + } + }; + auto matrix = TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( + matrixSize, matrixSize, matrixElements, rowLengths ); + using MatrixType = decltype( matrix ); + + TNL::Matrices::DenseMatrix< double, Device > denseMatrix( matrixSize, matrixSize ); + denseMatrix.setValue( 0.0 ); + auto dense_view = denseMatrix.getView(); + auto f = [=] __cuda_callable__ ( const int& rowIdx ) mutable { + auto row = matrix.getRow( rowIdx ); + auto dense_row = dense_view.getRow( rowIdx ); + for( int localIdx = 0; localIdx < row.getSize(); localIdx++ ) + dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); + }; + TNL::Algorithms::ParallelFor< Device >::exec( 0, matrixSize, f ); + + std::cout << "Laplace operator lambda matrix: " << std::endl << matrix << std::endl; + std::cout << "Laplace operator dense matrix: " << std::endl << denseMatrix << std::endl; +} + +int main( int argc, char* argv[] ) +{ + std::cout << "Running example on CPU ... " << std::endl; + getRowExample< TNL::Devices::Host >(); + +#ifdef HAVE_CUDA + std::cout << "Running example on CUDA GPU ... " << std::endl; + getRowExample< TNL::Devices::Cuda >(); +#endif +} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu deleted file mode 100644 index 7f757e6c8..000000000 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu +++ /dev/null @@ -1,75 +0,0 @@ -#include -#include -#include -#include - - -template< typename Device > -void laplaceOperatorMatrix() -{ - /*** - * Set matrix representing approximation of the Laplace operator on regular - * grid using the finite difference method. - */ - const int gridSize( 4 ); - const int matrixSize = gridSize * gridSize; - auto rowLengths = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx ) -> int - { - const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid - const int gridColumn = rowIdx % gridSize; - if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node - gridColumn == 0 || gridColumn == gridSize - 1 ) - return 1; - return 5; - }; - auto matrixElements = [=] __cuda_callable__ ( const int rows, const int columns, const int rowIdx, const int localIdx, int& columnIdx, double& value) { - const int gridRow = rowIdx / gridSize; // coordinates in the numerical grid - const int gridColumn = rowIdx % gridSize; - if( gridRow == 0 || gridRow == gridSize - 1 || // boundary grid node - gridColumn == 0 || gridColumn == gridSize - 1 ) - { - columnIdx = rowIdx; // diagonal element .... - value = 1.0; // ... is set to 1 - } - else // interior grid node - { - switch( localIdx ) // set diagonal element to 4 - { // and the others to -1 - case 0: - columnIdx = rowIdx - gridSize; - value = 1; - break; - case 1: - columnIdx = rowIdx - 1; - value = 1; - break; - case 2: - columnIdx = rowIdx; - value = -4; - break; - case 3: - columnIdx = rowIdx + 1; - value = 1; - break; - case 4: - columnIdx = rowIdx + gridSize; - value = 1; - break; - } - } - }; - auto matrix = TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( - matrixSize, matrixSize, matrixElements, rowLengths ); - std::cout << "Laplace operator matrix: " << std::endl << matrix << std::endl; -} - -int main( int argc, char* argv[] ) -{ - std::cout << "Running example on CPU ... " << std::endl; - forRowsExample< TNL::Devices::Host >(); - -#ifdef HAVE_CUDA - std::cout << "Running example on CUDA GPU ... " << std::endl; - forRowsExample< TNL::Devices::Cuda >(); -#endif -} diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu new file mode 120000 index 000000000..1794dfc1f --- /dev/null +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cu @@ -0,0 +1 @@ +LambdaMatrixExample_getRow.cpp \ No newline at end of file diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp index d1f0d625e..8629f7128 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp @@ -11,11 +11,11 @@ void forRowsExample() * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 / 1 -2 . . . \ -> { 0, 0, 1 } - * | -2 1 -2 . . | -> { 0, 2, 1 } - * | . -2 1 -2. . | -> { 3, 2, 1 } - * | . . -2 1 -2 | -> { 3, 2, 1 } - * \ . . . -2 1 / -> { 3, 2, 1 } + * 0 / 2 . . . . \ -> { 0, 0, 1 } + * | -1 2 -1 . . | -> { 0, 2, 1 } + * | . -1 2 -1. . | -> { 3, 2, 1 } + * | . . -1 2 -1 | -> { 3, 2, 1 } + * \ . . . . 2 / -> { 3, 2, 1 } * * The diagonals offsets are { -1, 0, 1 }. */ @@ -23,29 +23,16 @@ void forRowsExample() MatrixType matrix( size, // number of matrix rows size, // number of matrix columns - { -2, -1, 0 } ); // matrix diagonals offsets + { -1, -0, 1 } ); // matrix diagonals offsets - auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { - /*** - * 'forElements' method iterates only over matrix elements lying on given subdiagonals - * and so we do not need to check anything. The element value can be expressed - * by the 'localIdx' variable, see the following figure: - * - * 0 1 2 <- localIdx values - * ---------- - * 0 / 1 -2 . . . \ -> { 0, 1, -2 } - * | -2 1 -2 . . | -> { -2, 1, -2 } - * | . -2 1 -2. . | -> { -2, 1, -2 } - * | . . -2 1 -2 | -> { -2, 1, -2 } - * \ . . . -2 1 / -> { -2, 1, 0 } - * - */ + auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) { const int& rowIdx = row.getRowIndex(); - row.setElement( 1, 1.0 ); if( rowIdx > 0 ) - row.setElement( 0, -2.0 ); - if( rowIdx < size - 1 ) - row.setElement( 2, -2.0 ); + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < size - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); + }; matrix.forAllRows( f ); std::cout << matrix << std::endl; diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp index 856848bd3..2ba2a6be5 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp @@ -19,8 +19,8 @@ void getRowExample() diagonalsOffsets ); auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { - //auto row = matrix->getRow( rowIdx ); - // For some reason the previous line of code is not accepted by nvcc 10.1 + //auto row = matrix->getRow( rowIdx ); + // For some reason the previous line of code is not accepted by nvcc 10.1 // so we replace it with the following two lines. auto ref = matrix.modifyData(); auto row = ref.getRow( rowIdx ); diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp index 37e5f2724..8fcb6c360 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp @@ -11,43 +11,28 @@ void forRowsExample() * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 / 1 -2 . . . \ -> { 0, 0, 1 } - * | -2 1 -2 . . | -> { 0, 2, 1 } - * | . -2 1 -2. . | -> { 3, 2, 1 } - * | . . -2 1 -2 | -> { 3, 2, 1 } - * \ . . . -2 1 / -> { 3, 2, 1 } + * 0 / 2 . . . . \ -> { 0, 0, 1 } + * | -1 2 -1 . . | -> { 0, 2, 1 } + * | . -1 2 -1. . | -> { 3, 2, 1 } + * | . . -1 2 -1 | -> { 3, 2, 1 } + * \ . . . . 2 / -> { 3, 2, 1 } * * The diagonals offsets are { -1, 0, 1 }. */ - const int size = 5; MatrixType matrix( size, // number of matrix rows size, // number of matrix columns - { -2, -1, 0 } ); // matrix diagonals offsets + { -1, 0, 1 } ); // matrix diagonals offsets auto view = matrix.getView(); - auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { - /*** - * 'forElements' method iterates only over matrix elements lying on given subdiagonals - * and so we do not need to check anything. The element value can be expressed - * by the 'localIdx' variable, see the following figure: - * - * 0 1 2 <- localIdx values - * ---------- - * 0 / 1 -2 . . . \ -> { 0, 1, -2 } - * | -2 1 -2 . . | -> { -2, 1, -2 } - * | . -2 1 -2. . | -> { -2, 1, -2 } - * | . . -2 1 -2 | -> { -2, 1, -2 } - * \ . . . -2 1 / -> { -2, 1, 0 } - * - */ + auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) { const int& rowIdx = row.getRowIndex(); - row.setElement( 1, 1.0 ); if( rowIdx > 0 ) - row.setElement( 0, -2.0 ); - if( rowIdx < size - 1 ) - row.setElement( 2, -2.0 ); + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < size - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); }; view.forAllRows( f ); std::cout << matrix << std::endl; diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp index 0ddaa4d6c..217222112 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,32 +7,47 @@ template< typename Device > void forRowsExample() { + /*** + * Set the following matrix (dots represent zero matrix elements): + * + * / 2 . . . . \ + * | -1 2 -1 . . | + * | . -1 2 -1. . | + * | . . -1 2 -1 | + * \ . . . . 2 / + */ + const int size( 5 ); using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; - using RowViewType = typename MatrixType::RowViewType; - MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + MatrixType matrix( { 1, 3, 3, 3, 1 }, size ); - auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { - for( int localIdx = 0; - localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements - localIdx++ ) // than we requested. These padding elements are processed here as well. - // and so we cannot use row.getSize() + auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) mutable { + const int rowIdx = row.getRowIndex(); + if( rowIdx == 0 ) + row.setElement( 0, rowIdx, 2.0 ); // diagonal element + else if( rowIdx == size - 1 ) + row.setElement( 0, rowIdx, 2.0 ); // diagonal element + else { - row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); - row.setColumnIndex( localIdx, localIdx ); + row.setElement( 0, rowIdx - 1, -1.0 ); // elements below the diagonal + row.setElement( 1, rowIdx, 2.0 ); // diagonal element + row.setElement( 2, rowIdx + 1, -1.0 ); // elements above the diagonal } }; - matrix.forAllRows( f ); + /*** + * Set the matrix elements. + */ + matrix.forAllRows( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Creating matrix on host: " << std::endl; + std::cout << "Getting matrix rows on host: " << std::endl; forRowsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; + std::cout << "Getting matrix rows on CUDA device: " << std::endl; forRowsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp index bb4145a54..3f82fe161 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp @@ -1,4 +1,5 @@ #include +#include #include #include #include @@ -6,33 +7,48 @@ template< typename Device > void forRowsExample() { + /*** + * Set the following matrix (dots represent zero matrix elements): + * + * / 2 . . . . \ + * | -1 2 -1 . . | + * | . -1 2 -1. . | + * | . . -1 2 -1 | + * \ . . . . 2 / + */ using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; - using RowViewType = typename MatrixType::ViewType::RowViewType; - MatrixType matrix( { 1, 2, 3, 4, 5, }, 5 ); + const int size( 5 ); + MatrixType matrix( { 1, 3, 3, 3, 1 }, size ); auto view = matrix.getView(); - auto f = [] __cuda_callable__ ( RowViewType& row ) mutable { - for( int localIdx = 0; - localIdx <= row.getRowIndex(); // This is important, some matrix formats may allocate more matrix elements - localIdx++ ) // than we requested. These padding elements are processed here as well. - // and so we cannot use row.getSize() + auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) mutable { + const int rowIdx = row.getRowIndex(); + if( rowIdx == 0 ) + row.setElement( 0, rowIdx, 2.0 ); // diagonal element + else if( rowIdx == size - 1 ) + row.setElement( 0, rowIdx, 2.0 ); // diagonal element + else { - row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); - row.setColumnIndex( localIdx, localIdx ); + row.setElement( 0, rowIdx - 1, -1.0 ); // elements below the diagonal + row.setElement( 1, rowIdx, 2.0 ); // diagonal element + row.setElement( 2, rowIdx + 1, -1.0 ); // elements above the diagonal } }; - view.forAllRows( f ); + /*** + * Set the matrix elements. + */ + matrix.forAllRows( f ); std::cout << matrix << std::endl; } int main( int argc, char* argv[] ) { - std::cout << "Creating matrix on host: " << std::endl; + std::cout << "Getting matrix rows on host: " << std::endl; forRowsExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - std::cout << "Creating matrix on CUDA device: " << std::endl; + std::cout << "Getting matrix rows on CUDA device: " << std::endl; forRowsExample< TNL::Devices::Cuda >(); #endif } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp index d1f0d625e..b2f02547f 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -1,51 +1,35 @@ #include -#include +#include #include #include template< typename Device > void forRowsExample() { - using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 / 1 -2 . . . \ -> { 0, 0, 1 } - * | -2 1 -2 . . | -> { 0, 2, 1 } - * | . -2 1 -2. . | -> { 3, 2, 1 } - * | . . -2 1 -2 | -> { 3, 2, 1 } - * \ . . . -2 1 / -> { 3, 2, 1 } + * 0 / 2 . . . . \ -> { 0, 0, 1 } + * | -1 2 -1 . . | -> { 0, 2, 1 } + * | . -1 2 -1. . | -> { 3, 2, 1 } + * | . . -1 2 -1 | -> { 3, 2, 1 } + * \ . . . . 2 / -> { 3, 2, 1 } * * The diagonals offsets are { -1, 0, 1 }. */ const int size = 5; - MatrixType matrix( - size, // number of matrix rows - size, // number of matrix columns - { -2, -1, 0 } ); // matrix diagonals offsets + MatrixType matrix( size, size ); - auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { - /*** - * 'forElements' method iterates only over matrix elements lying on given subdiagonals - * and so we do not need to check anything. The element value can be expressed - * by the 'localIdx' variable, see the following figure: - * - * 0 1 2 <- localIdx values - * ---------- - * 0 / 1 -2 . . . \ -> { 0, 1, -2 } - * | -2 1 -2 . . | -> { -2, 1, -2 } - * | . -2 1 -2. . | -> { -2, 1, -2 } - * | . . -2 1 -2 | -> { -2, 1, -2 } - * \ . . . -2 1 / -> { -2, 1, 0 } - * - */ + auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) { const int& rowIdx = row.getRowIndex(); - row.setElement( 1, 1.0 ); if( rowIdx > 0 ) - row.setElement( 0, -2.0 ); - if( rowIdx < size - 1 ) - row.setElement( 2, -2.0 ); + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < size - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); + }; matrix.forAllRows( f ); std::cout << matrix << std::endl; diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp index 37e5f2724..b2f02547f 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -1,55 +1,37 @@ #include -#include +#include #include #include template< typename Device > void forRowsExample() { - using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 / 1 -2 . . . \ -> { 0, 0, 1 } - * | -2 1 -2 . . | -> { 0, 2, 1 } - * | . -2 1 -2. . | -> { 3, 2, 1 } - * | . . -2 1 -2 | -> { 3, 2, 1 } - * \ . . . -2 1 / -> { 3, 2, 1 } + * 0 / 2 . . . . \ -> { 0, 0, 1 } + * | -1 2 -1 . . | -> { 0, 2, 1 } + * | . -1 2 -1. . | -> { 3, 2, 1 } + * | . . -1 2 -1 | -> { 3, 2, 1 } + * \ . . . . 2 / -> { 3, 2, 1 } * * The diagonals offsets are { -1, 0, 1 }. */ - const int size = 5; - MatrixType matrix( - size, // number of matrix rows - size, // number of matrix columns - { -2, -1, 0 } ); // matrix diagonals offsets - auto view = matrix.getView(); + MatrixType matrix( size, size ); - auto f = [=] __cuda_callable__ ( typename MatrixType::RowViewType& row ) { - /*** - * 'forElements' method iterates only over matrix elements lying on given subdiagonals - * and so we do not need to check anything. The element value can be expressed - * by the 'localIdx' variable, see the following figure: - * - * 0 1 2 <- localIdx values - * ---------- - * 0 / 1 -2 . . . \ -> { 0, 1, -2 } - * | -2 1 -2 . . | -> { -2, 1, -2 } - * | . -2 1 -2. . | -> { -2, 1, -2 } - * | . . -2 1 -2 | -> { -2, 1, -2 } - * \ . . . -2 1 / -> { -2, 1, 0 } - * - */ + auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) { const int& rowIdx = row.getRowIndex(); - row.setElement( 1, 1.0 ); if( rowIdx > 0 ) - row.setElement( 0, -2.0 ); - if( rowIdx < size - 1 ) - row.setElement( 2, -2.0 ); + row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 1, 2.0 ); // elements on the diagonal + if( rowIdx < size - 1 ) // elements above the diagonal + row.setElement( 2, -1.0 ); + }; - view.forAllRows( f ); + matrix.forAllRows( f ); std::cout << matrix << std::endl; } diff --git a/Documentation/Tutorials/Matrices/tutorial_Matrices.md b/Documentation/Tutorials/Matrices/tutorial_Matrices.md index 3f62a3888..b02d06481 100644 --- a/Documentation/Tutorials/Matrices/tutorial_Matrices.md +++ b/Documentation/Tutorials/Matrices/tutorial_Matrices.md @@ -156,7 +156,7 @@ There are several ways how to create a new matrix: 4. **Methods `setElement` and `addElement` called on the host and copy matrix on GPU** setting particular matrix elements by the methods `setElement` and `addElement` when the matrix is allocated on GPU can be time consuming for large matrices. Setting up the matrix on CPU using the same methods and copying it on GPU at once when the setup is finished can be significantly more efficient. A drawback is that we need to allocate temporarily whole matrix on CPU. 5. **Methods `setElement` and `addElement` called from native device** allow to do efficient matrix elements setup even on devices (GPUs). In this case, the methods must be called from a GPU kernel or a lambda function combined with the parallel for (\ref TNL::Algorithms::ParallelFor). The user get very good performance even when manipulating matrix allocated on GPU. On the other hand, only data structures allocated on GPUs can be accessed from the kernel or lambda function. The matrix can be accessed in the GPU kernel or lambda function by means of [matrix view](#matrix_view) or the shared pointer (\ref TNL::Pointers::SharedPointer). 6. **Method `getRow` combined with `ParallelFor`** is very similar to the previous one. The difference is that we first fetch helper object called *matrix row* which is linked to particular matrix row. Using methods of this object, one may change the matrix elements in given matrix row. An advantage is that the access to the matrix row is resolved only once for all elements in the row. In some more sophisticated sparse matrix formats, this can be nontrivial operation and this approach may slightly improve the performance. Another advantage for sparse matrices is that we access the matrix elements based on their *local index* ('localIdx', see [Indexing of nonzero matrix elements in sparse matrices](indexing_of_nonzero_matrix_elements_in_sparse_matrices)) in the row which is something like a rank of the nonzero element in the row. This is more efficient than addressing the matrix elements by the column indexes which requires searching in the matrix row. So this may significantly improve the performance of setup of sparse matrices. When it comes to dense matrices, there should not be great difference in performance compared to use of the methods `setElement` and `getElement`. Note that when the method is called from a GPU kernel or a lambda function, only data structures allocated on GPU can be accessed and the matrix must be made accessible by the means of matrix view. -7. **Method `forElements`** this approach is very similar to the previous one but it avoids using `ParallelFor` and necessity of passing the matrix to GPU kernels by matrix view or shared pointers. +7. **Methods `forRows` and `forElements`** this approach is very similar to the previous one but it avoids using `ParallelFor` and necessity of passing the matrix to GPU kernels by matrix view or shared pointers. The following table shows pros and cons of particular methods: @@ -177,7 +177,7 @@ The following table shows pros and cons of particular methods: | | | | | Requires writing GPU kernel or lambda function. | | | | | | Allows accessing only data allocated on the same device/memory space. | | | | | | Use of matrix local indexes can be less intuitive. | -| **forElements** | ***** | ** | Best efficiency for sparse matrices. | Requires setting of row capacities. | +| **forRows**, **forElements** | ***** | ** | Best efficiency for sparse matrices. | Requires setting of row capacities. | | | | | Avoid use of matrix view or shared pointer in kernels/lambda function.| Requires writing GPU kernel or lambda function. | | | | | | Allows accessing only data allocated on the same device/memory space. | | | | | | Use of matrix local indexes is less intuitive. | diff --git a/src/TNL/Matrices/DenseMatrixRowView.h b/src/TNL/Matrices/DenseMatrixRowView.h index 49774949b..ce8074bce 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.h +++ b/src/TNL/Matrices/DenseMatrixRowView.h @@ -75,6 +75,14 @@ class DenseMatrixRowView __cuda_callable__ IndexType getSize() const; + /** + * \brief Returns the matrix row index. + * + * \return matrix row index. + */ + __cuda_callable__ + const IndexType& getRowIndex() const; + /** * \brief Returns constants reference to an element with given column index. * diff --git a/src/TNL/Matrices/DenseMatrixRowView.hpp b/src/TNL/Matrices/DenseMatrixRowView.hpp index 1c7af4adf..81d4aeaa6 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.hpp +++ b/src/TNL/Matrices/DenseMatrixRowView.hpp @@ -34,6 +34,15 @@ getSize() const -> IndexType return segmentView.getSize(); } +template< typename SegmentView, + typename ValuesView > +__cuda_callable__ auto +DenseMatrixRowView< SegmentView, ValuesView >:: +getRowIndex() const -> const IndexType& +{ + return segmentView.getSegmentIndex(); +} + template< typename SegmentView, typename ValuesView > __cuda_callable__ auto diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index a91fcc617..f3f7f80d3 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -409,7 +409,7 @@ forRows( IndexType begin, IndexType end, Function&& function ) auto values_view = this->values.getView(); using SegmentViewType = typename SegmentsViewType::SegmentViewType; auto f = [=] __cuda_callable__ ( SegmentViewType& segmentView ) mutable { - auto rowView = RowViewType( segmentView, values_view ); + auto rowView = RowView( segmentView, values_view ); function( rowView ); }; this->segments.forSegments( begin, end, f ); diff --git a/src/TNL/Matrices/LambdaMatrix.h b/src/TNL/Matrices/LambdaMatrix.h index bc817323d..511942f01 100644 --- a/src/TNL/Matrices/LambdaMatrix.h +++ b/src/TNL/Matrices/LambdaMatrix.h @@ -88,12 +88,12 @@ class LambdaMatrix /** * \brief Type of Lambda matrix row view. */ - using RowViewType = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType >; + using RowView = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType >; /** * \brief Type of constant Lambda matrix row view. */ - using ConstRowViewType = RowViewType; + using ConstRowView = RowView; static constexpr bool isSymmetric() { return false; }; static constexpr bool isBinary() { return false; }; @@ -242,7 +242,7 @@ class LambdaMatrix * See \ref LambdaMatrixRowView. */ __cuda_callable__ - const RowViewType getRow( const IndexType& rowIdx ) const; + const RowView getRow( const IndexType& rowIdx ) const; /** * \brief Returns value of matrix element at position given by its row and column index. @@ -306,10 +306,10 @@ class LambdaMatrix * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::LambdaMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::LambdaMatrix::RowView. * * \par Example * \include Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp @@ -330,10 +330,10 @@ class LambdaMatrix * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::LambdaMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::LambdaMatrix::RowView. * * \par Example * \include Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp diff --git a/src/TNL/Matrices/LambdaMatrix.hpp b/src/TNL/Matrices/LambdaMatrix.hpp index ada86b848..f2cdb7574 100644 --- a/src/TNL/Matrices/LambdaMatrix.hpp +++ b/src/TNL/Matrices/LambdaMatrix.hpp @@ -205,13 +205,13 @@ template< typename MatrixElementsLambda, __cuda_callable__ auto LambdaMatrix< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Device, Index >:: -getRow( const IndexType& rowIdx ) const -> const RowViewType +getRow( const IndexType& rowIdx ) const -> const RowView { - return RowViewType( this->getMatrixElementsLambda(), - this->getCompressedRowLengthsLambda(), - this->getRows(), - this->getColumns(), - rowIdx ); + return RowView( this->getMatrixElementsLambda(), + this->getCompressedRowLengthsLambda(), + this->getRows(), + this->getColumns(), + rowIdx ); } template< typename MatrixElementsLambda, diff --git a/src/TNL/Matrices/MatrixWriter.hpp b/src/TNL/Matrices/MatrixWriter.hpp index 98be21107..77dc5c250 100644 --- a/src/TNL/Matrices/MatrixWriter.hpp +++ b/src/TNL/Matrices/MatrixWriter.hpp @@ -152,7 +152,7 @@ writeMtx( std::ostream& str, str << std::setw( 9 ) << matrix.getRows() << " " << std::setw( 9 ) << matrix.getColumns() << " " << std::setw( 12 ) << matrix.getNonzeroElementsCount() << std::endl; std::ostream* str_ptr = &str; auto cout_ptr = &std::cout; - auto f = [=] __cuda_callable__ ( const typename Matrix::ConstRowViewType& row ) mutable { + auto f = [=] __cuda_callable__ ( const typename Matrix::ConstRowView& row ) mutable { auto rowIdx = row.getRowIndex(); for( IndexType localIdx = 0; localIdx < row.getSize(); localIdx++ ) { diff --git a/src/TNL/Matrices/MultidiagonalMatrix.h b/src/TNL/Matrices/MultidiagonalMatrix.h index e8fbbd75c..e29796a1e 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.h +++ b/src/TNL/Matrices/MultidiagonalMatrix.h @@ -134,12 +134,12 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Type for accessing matrix rows. */ - using RowViewType = typename ViewType::RowViewType; + using RowView = typename ViewType::RowView; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowViewType = typename ViewType::ConstViewType; + using ConstRowView = typename ViewType::ConstViewType; /** * \brief Helper type for getting self type or its modifications. @@ -492,7 +492,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - RowViewType getRow( const IndexType& rowIdx ); + RowView getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -509,7 +509,7 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - const ConstRowViewType getRow( const IndexType& rowIdx ) const; + const ConstRowView getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -814,10 +814,10 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp @@ -840,10 +840,10 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp @@ -864,10 +864,10 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp @@ -888,10 +888,10 @@ class MultidiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrix::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp diff --git a/src/TNL/Matrices/MultidiagonalMatrix.hpp b/src/TNL/Matrices/MultidiagonalMatrix.hpp index 7d65e340e..7e6ac450f 100644 --- a/src/TNL/Matrices/MultidiagonalMatrix.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrix.hpp @@ -404,7 +404,7 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) const -> const ConstRowViewType +getRow( const IndexType& rowIdx ) const -> const ConstRowView { return this->view.getRow( rowIdx ); } @@ -418,7 +418,7 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrix< Real, Device, Index, Organization, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) -> RowViewType +getRow( const IndexType& rowIdx ) -> RowView { return this->view.getRow( rowIdx ); } diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index d91db5a8a..22e7a9de0 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -76,12 +76,12 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing matrix rows. */ - using RowViewType = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; + using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowViewType = typename RowViewType::ConstViewType; + using ConstRowView = typename RowView::ConstViewType; /** * \brief Helper type for getting self type or its modifications. @@ -256,7 +256,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - RowViewType getRow( const IndexType& rowIdx ); + RowView getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -273,7 +273,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref MultidiagonalMatrixRowView. */ __cuda_callable__ - const ConstRowViewType getRow( const IndexType& rowIdx ) const; + const ConstRowView getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -576,10 +576,10 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp @@ -602,10 +602,10 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp @@ -626,10 +626,10 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp @@ -650,10 +650,10 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::MultidiagonalMatrixView::RowView. * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_forRows.cpp diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.hpp b/src/TNL/Matrices/MultidiagonalMatrixView.hpp index a75e40da0..7dadde222 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixView.hpp @@ -229,9 +229,9 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) const -> const ConstRowViewType +getRow( const IndexType& rowIdx ) const -> const ConstRowView { - return ConstRowViewType( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); + return ConstRowView( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); } template< typename Real, @@ -241,9 +241,9 @@ template< typename Real, __cuda_callable__ auto MultidiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) -> RowViewType +getRow( const IndexType& rowIdx ) -> RowView { - return RowViewType( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); + return RowView( rowIdx, this->diagonalsOffsets.getView(), this->values.getView(), this->indexer ); } template< typename Real, diff --git a/src/TNL/Matrices/SparseMatrix.h b/src/TNL/Matrices/SparseMatrix.h index 2bd171e46..b9b7dceae 100644 --- a/src/TNL/Matrices/SparseMatrix.h +++ b/src/TNL/Matrices/SparseMatrix.h @@ -151,12 +151,12 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Type for accessing matrix rows. */ - using RowViewType = typename ViewType::RowViewType; + using RowView = typename ViewType::RowView; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowViewType = typename ViewType::ConstRowViewType; + using ConstRowView = typename ViewType::ConstRowView; /** * \brief Helper type for getting self type or its modifications. @@ -502,7 +502,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref SparseMatrixRowView. */ __cuda_callable__ - const ConstRowViewType getRow( const IndexType& rowIdx ) const; + const ConstRowView getRow( const IndexType& rowIdx ) const; /** * \brief Non-constant getter of simple structure for accessing given matrix row. @@ -519,7 +519,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref SparseMatrixRowView. */ __cuda_callable__ - RowViewType getRow( const IndexType& rowIdx ); + RowView getRow( const IndexType& rowIdx ); /** * \brief Sets element at given \e row and \e column to given \e value. @@ -803,10 +803,10 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -829,10 +829,10 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -853,10 +853,10 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -877,10 +877,10 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrix::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp diff --git a/src/TNL/Matrices/SparseMatrix.hpp b/src/TNL/Matrices/SparseMatrix.hpp index 6ff89ad93..6f701a3ea 100644 --- a/src/TNL/Matrices/SparseMatrix.hpp +++ b/src/TNL/Matrices/SparseMatrix.hpp @@ -434,7 +434,7 @@ template< typename Real, typename IndexAllocator > __cuda_callable__ auto SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) const -> const ConstRowViewType +getRow( const IndexType& rowIdx ) const -> const ConstRowView { return this->view.getRow( rowIdx ); } @@ -449,7 +449,7 @@ template< typename Real, typename IndexAllocator > __cuda_callable__ auto SparseMatrix< Real, Device, Index, MatrixType, Segments, ComputeReal, RealAllocator, IndexAllocator >:: -getRow( const IndexType& rowIdx ) -> RowViewType +getRow( const IndexType& rowIdx ) -> RowView { return this->view.getRow( rowIdx ); } diff --git a/src/TNL/Matrices/SparseMatrixView.h b/src/TNL/Matrices/SparseMatrixView.h index f35d1e616..8651ad1c3 100644 --- a/src/TNL/Matrices/SparseMatrixView.h +++ b/src/TNL/Matrices/SparseMatrixView.h @@ -136,12 +136,12 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing matrix rows. */ - using RowViewType = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; + using RowView = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ValuesViewType, ColumnsIndexesViewType, isBinary() >; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowViewType = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary() >;; + using ConstRowView = SparseMatrixRowView< typename SegmentsViewType::SegmentViewType, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary() >;; /** * \brief Helper type for getting self type or its modifications. @@ -294,7 +294,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * See \ref SparseMatrixRowView. */ __cuda_callable__ - ConstRowViewType getRow( const IndexType& rowIdx ) const; + ConstRowView getRow( const IndexType& rowIdx ) const; /** * \brief Non-constant getter of simple structure for accessing given matrix row. @@ -311,7 +311,7 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * See \ref SparseMatrixRowView. */ __cuda_callable__ - RowViewType getRow( const IndexType& rowIdx ); + RowView getRow( const IndexType& rowIdx ); /** * \brief Sets element at given \e row and \e column to given \e value. @@ -585,10 +585,10 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp @@ -611,10 +611,10 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp @@ -635,10 +635,10 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp @@ -659,10 +659,10 @@ class SparseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::SparseMatrixView::RowView. * * \par Example * \include Matrices/SparseMatrix/SparseMatrixViewExample_forRows.cpp diff --git a/src/TNL/Matrices/SparseMatrixView.hpp b/src/TNL/Matrices/SparseMatrixView.hpp index ab103fca0..593c100a9 100644 --- a/src/TNL/Matrices/SparseMatrixView.hpp +++ b/src/TNL/Matrices/SparseMatrixView.hpp @@ -223,10 +223,10 @@ template< typename Real, typename ComputeReal > __cuda_callable__ auto SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -getRow( const IndexType& rowIdx ) const -> ConstRowViewType +getRow( const IndexType& rowIdx ) const -> ConstRowView { TNL_ASSERT_LT( rowIdx, this->getRows(), "Row index is larger than number of matrix rows." ); - return ConstRowViewType( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); + return ConstRowView( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); } template< typename Real, @@ -237,10 +237,10 @@ template< typename Real, typename ComputeReal > __cuda_callable__ auto SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView, ComputeReal >:: -getRow( const IndexType& rowIdx ) -> RowViewType +getRow( const IndexType& rowIdx ) -> RowView { TNL_ASSERT_LT( rowIdx, this->getRows(), "Row index is larger than number of matrix rows." ); - return RowViewType( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); + return RowView( this->segments.getSegmentView( rowIdx ), this->values, this->columnIndexes ); } template< typename Real, @@ -673,7 +673,7 @@ forRows( IndexType begin, IndexType end, Function&& function ) auto values_view = this->values.getView(); using SegmentViewType = typename SegmentsViewType::SegmentViewType; auto f = [=] __cuda_callable__ ( SegmentViewType& segmentView ) mutable { - auto rowView = RowViewType( segmentView, values_view, columns_view ); + auto rowView = RowView( segmentView, values_view, columns_view ); function( rowView ); }; this->segments.forSegments( begin, end, f ); @@ -694,7 +694,7 @@ forRows( IndexType begin, IndexType end, Function&& function ) const const auto values_view = this->values.getConstView(); using SegmentViewType = typename SegmentsViewType::SegmentViewType; auto f = [=] __cuda_callable__ ( const SegmentViewType& segmentView ) mutable { - const auto rowView = ConstRowViewType( segmentView, values_view, columns_view ); + const auto rowView = ConstRowView( segmentView, values_view, columns_view ); function( rowView ); }; this->segments.forSegments( begin, end, f ); diff --git a/src/TNL/Matrices/TridiagonalMatrix.h b/src/TNL/Matrices/TridiagonalMatrix.h index 8573a805f..c970ff9b7 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.h +++ b/src/TNL/Matrices/TridiagonalMatrix.h @@ -117,12 +117,12 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > /** * \brief Type for accessing matrix rows. */ - using RowViewType = typename ViewType::RowViewType; + using RowView = typename ViewType::RowView; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowViewType = typename ViewType::ConstRowViewType; + using ConstRowView = typename ViewType::ConstRowView; /** * \brief Helper type for getting self type or its modifications. @@ -388,7 +388,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - RowViewType getRow( const IndexType& rowIdx ); + RowView getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -405,7 +405,7 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - const ConstRowViewType getRow( const IndexType& rowIdx ) const; + const ConstRowView getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -698,10 +698,10 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -724,10 +724,10 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -748,10 +748,10 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -772,10 +772,10 @@ class TridiagonalMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrix::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp diff --git a/src/TNL/Matrices/TridiagonalMatrix.hpp b/src/TNL/Matrices/TridiagonalMatrix.hpp index c6378dfb3..87a508a9c 100644 --- a/src/TNL/Matrices/TridiagonalMatrix.hpp +++ b/src/TNL/Matrices/TridiagonalMatrix.hpp @@ -283,7 +283,7 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getRow( const IndexType& rowIdx ) const -> const ConstRowViewType +getRow( const IndexType& rowIdx ) const -> const ConstRowView { return this->view.getRow( rowIdx ); } @@ -296,7 +296,7 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrix< Real, Device, Index, Organization, RealAllocator >:: -getRow( const IndexType& rowIdx ) -> RowViewType +getRow( const IndexType& rowIdx ) -> RowView { return this->view.getRow( rowIdx ); } diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index 3135a488f..a078804f5 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -74,12 +74,12 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing matrix rows. */ - using RowViewType = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; + using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; /** * \brief Type for accessing constant matrix rows. */ - using ConstRowViewType = typename RowViewType::ConstViewType; + using ConstRowView = typename RowView::ConstViewType; /** * \brief Helper type for getting self type or its modifications. @@ -243,7 +243,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - RowViewType getRow( const IndexType& rowIdx ); + RowView getRow( const IndexType& rowIdx ); /** * \brief Constant getter of simple structure for accessing given matrix row. @@ -260,7 +260,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * See \ref TridiagonalMatrixRowView. */ __cuda_callable__ - const ConstRowViewType getRow( const IndexType& rowIdx ) const; + const ConstRowView getRow( const IndexType& rowIdx ) const; /** * \brief Set all matrix elements to given value. @@ -539,10 +539,10 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -565,10 +565,10 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -589,10 +589,10 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) mutable { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) mutable { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -613,10 +613,10 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called for each row. * * ``` - * auto function = [] __cuda_callable__ ( RowViewType& row ) { ... }; + * auto function = [] __cuda_callable__ ( RowView& row ) { ... }; * ``` * - * \e RowViewType represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowViewType. + * \e RowView represents matrix row - see \ref TNL::Matrices::TridiagonalMatrixView::RowView. * * \par Example * \include Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp diff --git a/src/TNL/Matrices/TridiagonalMatrixView.hpp b/src/TNL/Matrices/TridiagonalMatrixView.hpp index b4316e69c..3aa633776 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixView.hpp @@ -183,9 +183,9 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) const -> const ConstRowViewType +getRow( const IndexType& rowIdx ) const -> const ConstRowView { - return ConstRowViewType( rowIdx, this->values.getView(), this->indexer ); + return ConstRowView( rowIdx, this->values.getView(), this->indexer ); } template< typename Real, @@ -195,9 +195,9 @@ template< typename Real, __cuda_callable__ auto TridiagonalMatrixView< Real, Device, Index, Organization >:: -getRow( const IndexType& rowIdx ) -> RowViewType +getRow( const IndexType& rowIdx ) -> RowView { - return RowViewType( rowIdx, this->values.getView(), this->indexer ); + return RowView( rowIdx, this->values.getView(), this->indexer ); } template< typename Real, diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.h b/src/UnitTests/Matrices/LambdaMatrixTest.h index 00a8fad01..0cdfb37f3 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.h +++ b/src/UnitTests/Matrices/LambdaMatrixTest.h @@ -89,11 +89,11 @@ TYPED_TEST( LambdaMatrixTest, getElementTest ) test_GetElement< LambdaMatrixParametersType >(); } -TYPED_TEST( LambdaMatrixTest, getRowTest ) +TYPED_TEST( LambdaMatrixTest, forRowsTest ) { using LambdaMatrixParametersType = typename TestFixture::LambdaMatrixType; - test_GetRow< LambdaMatrixParametersType >(); + test_ForRows< LambdaMatrixParametersType >(); } diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index c3891aed5..aff13d0c2 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -163,7 +163,7 @@ void test_GetElement() } template< typename Matrix > -void test_GetRow() +void test_ForRows() { using RealType = typename Matrix::RealType; using DeviceType = typename Matrix::DeviceType; -- GitLab From db592750d2db8e3a60546a1275d5c9155b3e3f25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sat, 20 Mar 2021 18:01:31 +0100 Subject: [PATCH 48/54] Fixes after renaming RowViewType -> RowView. --- src/TNL/Matrices/DistributedMatrix.h | 4 ++-- .../Meshes/MeshDetails/initializer/SubentitySeedsCreator.h | 4 ++-- src/UnitTests/Matrices/DenseMatrixTest.h | 4 ++-- src/UnitTests/Matrices/LambdaMatrixTest.hpp | 2 +- src/UnitTests/Matrices/MultidiagonalMatrixTest.h | 2 +- src/UnitTests/Matrices/SparseMatrixTest.hpp | 4 ++-- src/UnitTests/Matrices/TridiagonalMatrixTest.h | 2 +- 7 files changed, 11 insertions(+), 11 deletions(-) diff --git a/src/TNL/Matrices/DistributedMatrix.h b/src/TNL/Matrices/DistributedMatrix.h index 778e1a9c3..2deed3abf 100644 --- a/src/TNL/Matrices/DistributedMatrix.h +++ b/src/TNL/Matrices/DistributedMatrix.h @@ -36,8 +36,8 @@ public: using RowsCapacitiesType = Containers::DistributedVector< IndexType, DeviceType, IndexType >; - using MatrixRow = typename Matrix::RowViewType; - using ConstMatrixRow = typename Matrix::ConstRowViewType; + using MatrixRow = typename Matrix::RowView; + using ConstMatrixRow = typename Matrix::ConstRowView; template< typename _Real = RealType, typename _Device = DeviceType, diff --git a/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h b/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h index f504a8561..1e2edb332 100644 --- a/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h +++ b/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h @@ -31,7 +31,7 @@ class SubentitySeedsCreator using LocalIndexType = typename MeshTraitsType::LocalIndexType; using EntityTraitsType = typename MeshTraitsType::template EntityTraits< EntityDimensionTag::value >; using EntityTopology = typename EntityTraitsType::EntityTopology; - using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowViewType; + using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowView; using SubentityTraits = typename MeshTraitsType::template SubentityTraits< EntityTopology, SubentityDimensionTag::value >; using SubentityTopology = typename SubentityTraits::SubentityTopology; @@ -82,7 +82,7 @@ class SubentitySeedsCreator< MeshConfig, EntityDimensionTag, DimensionTag< 0 > > using LocalIndexType = typename MeshTraitsType::LocalIndexType; using EntityTraitsType = typename MeshTraitsType::template EntityTraits< EntityDimensionTag::value >; using EntityTopology = typename EntityTraitsType::EntityTopology; - using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowViewType; + using SubvertexAccessorType = typename MeshTraitsType::SubentityMatrixType::RowView; using SubentityTraits = typename MeshTraitsType::template SubentityTraits< EntityTopology, 0 >; using SubentityTopology = typename SubentityTraits::SubentityTopology; diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h index 4767b1bdf..2ae7ffaf8 100644 --- a/src/UnitTests/Matrices/DenseMatrixTest.h +++ b/src/UnitTests/Matrices/DenseMatrixTest.h @@ -816,8 +816,8 @@ void test_ForRows() const IndexType rows = 8; Matrix m( rows, cols ); - using RowViewType = typename Matrix::RowViewType; - m.forAllRows( [] __cuda_callable__ ( RowViewType& row ) mutable { + using RowView = typename Matrix::RowView; + m.forAllRows( [] __cuda_callable__ ( RowView& row ) mutable { for( IndexType localIdx = 0; localIdx <= row.getRowIndex(); localIdx++ ) { row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index aff13d0c2..4b2402463 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -206,7 +206,7 @@ void test_ForRows() TNL::Matrices::DenseMatrix< RealType, DeviceType, IndexType > denseMatrix( size, size ); denseMatrix.setValue( 0.0 ); auto dense_view = denseMatrix.getView(); - auto f = [=] __cuda_callable__ ( const typename MatrixType::RowViewType& row ) mutable { + auto f = [=] __cuda_callable__ ( const typename MatrixType::RowView& row ) mutable { auto dense_row = dense_view.getRow( row.getRowIndex() ); for( IndexType localIdx = 0; localIdx < row.getSize(); localIdx++ ) dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index 0dadfe158..7bc09cdc2 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -798,7 +798,7 @@ void test_ForRows() const IndexType size( 5 ); Matrix m( size, size, { -1, 0, 1 } ); - auto f = [=] __cuda_callable__ ( typename Matrix::RowViewType& row ) mutable { + auto f = [=] __cuda_callable__ ( typename Matrix::RowView& row ) mutable { const IndexType rowIdx = row.getRowIndex(); if( rowIdx > 0 ) row.setElement( 0, -2.0 ); diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 3c73456cb..0b07dc508 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1438,8 +1438,8 @@ void test_ForRows() const IndexType rows = 8; Matrix m( { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, cols ); - using RowViewType = typename Matrix::RowViewType; - m.forAllRows( [] __cuda_callable__ ( RowViewType& row ) mutable { + using RowView = typename Matrix::RowView; + m.forAllRows( [] __cuda_callable__ ( RowView& row ) mutable { for( IndexType localIdx = 0; localIdx <= row.getRowIndex(); localIdx++ ) { row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); diff --git a/src/UnitTests/Matrices/TridiagonalMatrixTest.h b/src/UnitTests/Matrices/TridiagonalMatrixTest.h index 3244c33e6..47d8b91df 100644 --- a/src/UnitTests/Matrices/TridiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/TridiagonalMatrixTest.h @@ -749,7 +749,7 @@ void test_forRows() const IndexType size( 5 ); Matrix m( size, size ); - auto f = [=] __cuda_callable__ ( typename Matrix::RowViewType& row ) mutable { + auto f = [=] __cuda_callable__ ( typename Matrix::RowView& row ) mutable { const IndexType rowIdx = row.getRowIndex(); if( rowIdx > 0 ) row.setElement( 0, -2.0 ); -- GitLab From 06a34e5cd578bf358bb72e15d67eccb32b894dfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sun, 21 Mar 2021 10:30:47 +0100 Subject: [PATCH 49/54] Added tutorials for forRows method. --- .../MultidiagonalMatrixExample_getRow.cpp | 2 +- .../SparseMatrixViewExample_getRow.cpp | 23 +++++- .../TridiagonalMatrixExample_forRows.cpp | 1 - .../TridiagonalMatrixViewExample_forRows.cpp | 1 - .../Tutorials/Matrices/tutorial_Matrices.md | 81 +++++++++++++++++-- 5 files changed, 98 insertions(+), 10 deletions(-) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp index 2ba2a6be5..24c4d574c 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp @@ -52,7 +52,7 @@ int main( int argc, char* argv[] ) getRowExample< TNL::Devices::Host >(); #ifdef HAVE_CUDA - // It seems that nvcc 10.1 does not handle lambda functions properly. + // It seems that nvcc 10.1 does not handle lambda functions properly. // It is hard to make nvcc to compile this example and it does not work // properly. We will try it with later version of CUDA. //std::cout << "Getting matrix rows on CUDA device: " << std::endl; diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp index 569fabb6a..275de124e 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp @@ -8,12 +8,31 @@ template< typename Device > void getRowExample() { - TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 1, 1, 1, 1 }, 5 ); + /*** + * Set the following matrix (dots represent zero matrix elements): + * + * / 2 . . . . \ + * | -1 2 -1 . . | + * | . -1 2 -1. . | + * | . . -1 2 -1 | + * \ . . . . 2 / + */ + const int size = 5; + TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 3, 3, 3, 1 }, size ); auto view = matrix.getView(); auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { auto row = view.getRow( rowIdx ); - row.setElement( 0, rowIdx, 10 * ( rowIdx + 1 ) ); + if( rowIdx == 0 ) + row.setElement( 0, rowIdx, 2.0 ); // diagonal element + else if( rowIdx == size - 1 ) + row.setElement( 0, rowIdx, 2.0 ); // diagonal element + else + { + row.setElement( 0, rowIdx - 1, -1.0 ); // elements below the diagonal + row.setElement( 1, rowIdx, 2.0 ); // diagonal element + row.setElement( 2, rowIdx + 1, -1.0 ); // elements above the diagonal + } }; /*** diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp index b2f02547f..5607fac63 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -29,7 +29,6 @@ void forRowsExample() row.setElement( 1, 2.0 ); // elements on the diagonal if( rowIdx < size - 1 ) // elements above the diagonal row.setElement( 2, -1.0 ); - }; matrix.forAllRows( f ); std::cout << matrix << std::endl; diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp index b2f02547f..5607fac63 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_forRows.cpp @@ -29,7 +29,6 @@ void forRowsExample() row.setElement( 1, 2.0 ); // elements on the diagonal if( rowIdx < size - 1 ) // elements above the diagonal row.setElement( 2, -1.0 ); - }; matrix.forAllRows( f ); std::cout << matrix << std::endl; diff --git a/Documentation/Tutorials/Matrices/tutorial_Matrices.md b/Documentation/Tutorials/Matrices/tutorial_Matrices.md index b02d06481..ae21857dd 100644 --- a/Documentation/Tutorials/Matrices/tutorial_Matrices.md +++ b/Documentation/Tutorials/Matrices/tutorial_Matrices.md @@ -395,7 +395,23 @@ Here we show an example: \includelineno DenseMatrixViewExample_getRow.cpp -Here we create the matrix on the line 10 and get the matrix view on the line 16. Next we use `ParallelFor` (\ref TNL::Algorithms::ParallelFor) (line 26) to iterate over the matrix rows and the lambda function `f` (lines 18-21) for each of them. In the lambda function, we first fetch the matrix row by means of the merhod `getRow` (\ref TNL::Matrices::DenseMatrixView::getRow) and next we set the matrix elements by using the method `setElement` of the matrix row (\ref TNL::Matrices::DenseMatrixRowView::setElement). For the compatibility with the sparse matrices, use the variant of `setElement` with the parameter `localIdx`. It has no effect here, it is only for compatibility of the interface. +Here we create the matrix on the line 10 and get the matrix view on the line 16. Next we use `ParallelFor` (\ref TNL::Algorithms::ParallelFor) (line 26) to iterate over the matrix rows and the lambda function `f` (lines 18-21) for each of them. In the lambda function, we first fetch the matrix row by means of the method `getRow` (\ref TNL::Matrices::DenseMatrixView::getRow) and next we set the matrix elements by using the method `setElement` of the matrix row (\ref TNL::Matrices::DenseMatrixRowView::setElement). For the compatibility with the sparse matrices, use the variant of `setElement` with the parameter `localIdx`. It has no effect here, it is only for compatibility of the interface. + +The result looks as follows: + +\include DenseMatrixViewExample_getRow.out + +#### Method `forRows` + +This method iterates in parallel over all matrix rows. In fact, it combines \ref TNL::Algorithms::ParallelFor and \ref TNL::Matrices:::DenseMatrix::getRow method in one. See the following example. It is even a bit simpler compared to the previous one: + +\includelineno DenseMatrixViewExample_forRows.cpp + +The lambda function, which is called for each matrix row (lines 15-17), have to accept parameter `row` with type `RowView`. This type is defined inside each TNL matrix and in the case of the dense matrix, it is \ref TNL::Matrices::DenseMatrixRowView. We use the method \ref TNL::Matrices::DenseMatrixRowView::getRowIndex to get the index of the matrix row being currently processed and method \ref TNL::Matrices::DenseMatrixRowView::setElement which sets the value of the element with given column index (the first parameter). + +The result looks as follows: + +\include DenseMatrixViewExample_forRows.out #### Method `forElements` @@ -418,7 +434,7 @@ The result looks as follows: [Sparse matrices](https://en.wikipedia.org/wiki/Sparse_matrix) are extremely important in a lot of numerical algorithms. They are used at situations when we need to operate with matrices having majority of the matrix elements equal to zero. In this case, only the non-zero matrix elements are stored with possibly some *padding zeros* used for memory alignment. This is necessary mainly on GPUs. See the [Overview of matrix types](#overview_of_matrix_types) for the differences in memory requirements. -Major disadvantage of sparse matrices is that there are a lot of different formats for their storage in memory. Though [CSR (Compressed Sparse Row)](https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)) format is the most popular of all, especially for GPUs, there are many other formats. Often their performance differ significantly for various matrices. So it is a good idea to test several sparse matrix formats if you want to get the best performance. In TNL, there is one templated class \ref TNL::Matrices::SparseMatrix representing general sparse matrices. The change of underlying matrix format can be done just by changing one template parameter. The list of the template paramaters is as follows: +Major disadvantage of sparse matrices is that there are a lot of different formats for their storage in memory. Though [CSR (Compressed Sparse Row)](https://en.wikipedia.org/wiki/Sparse_matrix#Compressed_sparse_row_(CSR,_CRS_or_Yale_format)) format is the most popular of all, especially for GPUs, there are many other formats. Often their performance differ significantly for various matrices. So it is a good idea to test several sparse matrix formats if you want to get the best performance. In TNL, there is one templated class \ref TNL::Matrices::SparseMatrix representing general sparse matrices. The change of underlying matrix format can be done just by changing one template parameter. The list of the template parameters is as follows: * `Real` is type if the matrix elements. It is `double` by default. * `Device` is a device where the matrix is allocated. Currently it can be either \ref TNL::Devices::Host for CPU or \ref TNL::Devices::Cuda for CUDA supporting GPUs. It is \ref TNL::Devices::Host by default. @@ -477,7 +493,6 @@ The result of both examples looks as follows: \include SparseMatrixExample_Constructor_init_list_1.out - #### Initializer list Small matrices can be initialized by a constructor with an [initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list). We assume having the following sparse matrix @@ -558,7 +573,7 @@ More efficient method, especially for GPUs, is to combine `getRow` (\ref TNL::Ma \includelineno SparseMatrixViewExample_getRow.cpp -On the line 11, we create small matrix having five rows (number of rows is given by the size of the [initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list) ) and columns (number of columns is given by the second parameter) and we set each row capacity to one (particular elements of the initializer list). On the line 22, we call `ParallelFor` (\ref TNL::Algorithms::ParallelFor) to iterate over all matrix elements. Each row is processed by the lambda function `f` (lines 14-17). In the lambda function, we first fetch a sparse matrix row (\ref TNL::Matrices::SparseMatrixRowView) which serves for accessing particular matrix rows. This object has a method `setElement` (\ref TNL::Matrices::SparseMatrixRowView::setElement) accepting three parameters: +On the line 21, we create small matrix having five rows (number of rows is given by the size of the [initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list) ) and columns (number of columns is given by the second parameter) and we set each row capacity to one or three (particular elements of the initializer list). On the line 41, we call `ParallelFor` (\ref TNL::Algorithms::ParallelFor) to iterate over all matrix rows. Each row is processed by the lambda function `f` (lines 24-36). In the lambda function, we first fetch a sparse matrix row (\ref TNL::Matrices::SparseMatrixRowView) (line 25) which serves for accessing particular matrix elements in the matrix row. This object has a method `setElement` (\ref TNL::Matrices::SparseMatrixRowView::setElement) accepting three parameters: 1. `localIdx` is a rank of the nonzero element in given matrix row. 2. `columnIdx` is the new column index of the matrix element. @@ -568,6 +583,22 @@ The result looks as follows: \include SparseMatrixViewExample_getRow.out +#### Method `forRows` + +The method `forRows` (\ref TNL::Matrices::SparseMatrix::forRows) calls the method `getRow` (\ref TNL::Matrices::SparseMatrix::getRow) in parallel. See the following example which has the same effect as the previous one but it is slightly simpler: + +\includelineno SparseMatrixExample_forRows.cpp + +The differences are: + +1. We do not need to get the matrix view as we did in the previous example. +2. We call the method `forAllRows` (\ref TNL::Matrices::SparseMatrix::forAllRows) instead of `ParallelFor` (\ref TNL::Algorithms::ParallelFor) which is simpler since we do not have to state the device type explicitly. The method `forAllRows` calls the method `forRows` for all matrix rows so we do not have to state explicitly the interval of matrix rows neither. +3. The lambda function `f` (lines 23-35) accepts one parameter `row` of the type `RowView` (\ref TNL::Matrices::SparseMatrix::RowView which is \ref TNL::Matrices::SparseMatrixRowView) instead of the index of the matrix row. Therefore we do not need to call the method `getRow` (\ref TNL::Matrices::SparseMatrix::getRow). On the other hand, we need the method `geRowIndex` (\ref TNL::Matrices::SparseMatrixRowView::getRowIndex) to get the index of the matrix row (line 24). + +The rest of the lambda function remains the same. The result looks as follows: + +\include SparseMatrixExample_forRows.out + #### Method `forElements` Finally, another efficient way of setting the nonzero matrix elements, is use of the method `forElements` (\ref TNL::Matrices::SparseMatrix::forElements). It requires indexes of the range of rows (`begin` and `end`) to be processed and a lambda function `function` which is called for each nonzero element. The lambda function provides the following data: @@ -818,7 +849,7 @@ The result looks as follows: #### Method `getRow` - A bit different way how to do the same is the use of tridiagonal matrix view and the method `getRow` (\ref TNL::Matrices::TridiagonalMatrixView::getRow) as the following example demonstrates: + A bit different way of setting up the matrix, is the use of tridiagonal matrix view and the method `getRow` (\ref TNL::Matrices::TridiagonalMatrixView::getRow) as the following example demonstrates: \includelineno TridiagonalMatrixViewExample_getRow.cpp @@ -828,6 +859,22 @@ The result looks as follows: \include TridiagonalMatrixViewExample_getRow.out +### Method `forRows` + +As in the case of other matrix types, the method `forRows` (\ref TNL::Matrices::TridiagonalMatrix::forRows) calls the method `getRow` (\ref TNL::Matrices::TridiagonalMatrix::getRow) in parallel. It is demonstrated by the following example which we may directly compare with the previous one: + +\includelineno TridiagonalMatrixExample_forRows.cpp + +The differences are: + +1. We do not need to get the matrix view as we did in the previous example. +2. We call the method `forAllRows` (\ref TNL::Matrices::TridiagonalMatrix::forAllRows) (line 33) instead of `ParallelFor` (\ref TNL::Algorithms::ParallelFor) which is simpler since we do not have to state the device type explicitly. The method `forAllRows` calls the method `forRows` for all matrix rows so we do not have to state explicitly the interval of matrix rows neither. +3. The lambda function `f` (lines 25-31) accepts one parameter `row` of the type `RowView` (\ref TNL::Matrices::TridiagonalMatrix::RowView which is \ref TNL::Matrices::TridiagonalMatrixRowView) instead of the index of the matrix row. Therefore we do not need to call the method `getRow` (\ref TNL::Matrices::TridiagonalMatrix::getRow). On the other hand, we need the method `geRowIndex` (\ref TNL::Matrices::TridiagonalMatrixRowView::getRowIndex) to get the index of the matrix row (line 24). + +The rest of the lambda function remains the same. The result looks as follows: + +\include TridiagonalMatrixExample_forRows.out + #### Method `forElements` Finally, even a bit more simple way of matrix elements manipulation with the method `forElements` (\ref TNL::Matrices::TridiagonalMatrix::forElements) is demonstrated in the following example: @@ -1084,6 +1131,18 @@ We use `ParallelFor2D` (\ref TNL::Algorithms::ParallelFor2D) to iterate over all \include MultidiagonalMatrixExample_Constructor.out +### Method `forRows` + +As in the case of other matrix types, the method `forRows` (\ref TNL::Matrices::MultidiagonalMatrix::forRows) calls the method `getRow` (\ref TNL::Matrices::MultidiagonalMatrix::getRow) in parallel. It is demonstrated by the following example: + +\includelineno MultidiagonalMatrixExample_forRows.cpp + + We call the method `forAllRows` (\ref TNL::Matrices::MultidiagonalMatrix::forAllRows) (line 36) instead of `ParallelFor` (\ref TNL::Algorithms::ParallelFor) which is simpler since we do not have to state the device type explicitly. The method `forAllRows` calls the method `forRows` for all matrix rows so we do not have to state explicitly the interval of matrix rows neither. The lambda function `f` (lines 28-35) accepts one parameter `row` of the type `RowView` (\ref TNL::Matrices::MultidiagonalMatrix::RowView which is \ref TNL::Matrices::MultidiagonalMatrixRowView). At the beginning of the lambda function, we call the method `geRowIndex` (\ref TNL::Matrices::MultidiagonalMatrixRowView::getRowIndex) to get the index of the matrix row (line 29). + +The result looks as follows: + +\include MultidiagonalMatrixExample_forRows.out + #### Method `forElements` Similar and even a bit simpler way of setting the matrix elements is offered by the method `forElements` (\ref TNL::Matrices::MultidiagonalMatrix::forElements, \ref TNL::Matrices::MultidiagonalMatrixView::forElements) as demonstrated in the following example: @@ -1153,6 +1212,18 @@ The result looks as follows: \include LambdaMatrixExample_Constructor.out +#### Method `forRows` + +Method `forRows` (\ref TNL::Matrices::LambdaMatrix::forRows, \ref TNL::Matrices::LambdaMatrix::forAllRows) iterates in parallel over all matrix rows. In the case of lambda matrices, it cannot be used for changing the matrix elements since they cannot be changed. In the following example, we show how to use this method to copy the matrix elements values to the dense matrix: + +\includelineno LambdaMatrixExample_forRows.cpp + +We start with the lambda functions (lines 17-61) defining the elements of the lambda matrix. Next, we create the lambda matrix `matrix` (lines 62-64) and the dense matrix `denseMatrix` (lines 66-67) together with the dense matrix view (line 68). The lambda function `f` (lines 69-73) serves for copying matrix elements from the lambda matrix to the dense matrix. The process of matrix elements copying is started by calling the method `forAllRows` (\ref TNL::Matrices::LambdaMatrix::forRows, \ref TNL::Matrices::LambdaMatrix::forAllRows) (line 74). + +Note, however, that use of `forElements` method (\ref TNL::Matrices::LambdaMatrix::forElements) would be more convenient. The result looks as follows: + +\include LambdaMatrixExample_forRows.out + #### Method `forElements` The lambda matrix has the same interface as other matrix types except of the method `getRow`. The following example demonstrates the use of the method `forElements` (\ref TNL::Matrices::LambdaMatrix::forElements) to copy the lambda matrix into the dense matrix: -- GitLab From 7c00255ab5ca50543c6415930156dce04be9c04e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sun, 21 Mar 2021 17:42:06 +0100 Subject: [PATCH 50/54] Added iterator for sparse matrix row. --- src/TNL/Matrices/SparseMatrixElement.h | 69 +++++++++++++ src/TNL/Matrices/SparseMatrixRowView.h | 20 +++- src/TNL/Matrices/SparseMatrixRowView.hpp | 45 +++++++++ .../Matrices/SparseMatrixRowViewIterator.h | 98 +++++++++++++++++++ .../Matrices/SparseMatrixRowViewIterator.hpp | 95 ++++++++++++++++++ src/UnitTests/Matrices/SparseMatrixTest.hpp | 25 +++++ 6 files changed, 349 insertions(+), 3 deletions(-) create mode 100644 src/TNL/Matrices/SparseMatrixElement.h create mode 100644 src/TNL/Matrices/SparseMatrixRowViewIterator.h create mode 100644 src/TNL/Matrices/SparseMatrixRowViewIterator.hpp diff --git a/src/TNL/Matrices/SparseMatrixElement.h b/src/TNL/Matrices/SparseMatrixElement.h new file mode 100644 index 000000000..485fb919b --- /dev/null +++ b/src/TNL/Matrices/SparseMatrixElement.h @@ -0,0 +1,69 @@ +/*************************************************************************** + SparseMatrixElement.h - description + ------------------- + begin : Mar 21, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +#include + +namespace TNL { +namespace Matrices { + + +template< typename Real, + typename Index, + bool isBinary_ = false > +class SparseMatrixElement +{ + public: + + using RealType = Real; + + using IndexType = Index; + + __cuda_callable__ + SparseMatrixElement( RealType& value, + const IndexType& rowIdx, + IndexType& columnIdx, + const IndexType& localIdx ) + : value_( value ), rowIdx( rowIdx ), columnIdx( columnIdx ), localIdx( localIdx ) {}; + + __cuda_callable__ + RealType& value() { return value_; }; + + __cuda_callable__ + const RealType& value() const { return value_; }; + + __cuda_callable__ + const IndexType& rowIndex() const { return rowIdx; }; + + __cuda_callable__ + IndexType& columnIndex() { return columnIdx; }; + + __cuda_callable__ + const IndexType& columnIndex() const { return columnIdx; }; + + __cuda_callable__ + const IndexType& localIndex() const { return localIdx; }; + + protected: + + RealType& value_; + + const IndexType& rowIdx; + + IndexType& columnIdx; + + const IndexType& localIdx; +}; + + } // namespace Matrices +} // namespace TNL diff --git a/src/TNL/Matrices/SparseMatrixRowView.h b/src/TNL/Matrices/SparseMatrixRowView.h index 5a3148af4..bfdb74838 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.h +++ b/src/TNL/Matrices/SparseMatrixRowView.h @@ -13,6 +13,7 @@ #include #include +#include namespace TNL { namespace Matrices { @@ -83,13 +84,14 @@ class SparseMatrixRowView /** * \brief Type of sparse matrix row view. */ - using RowViewType = SparseMatrixRowView< SegmentView, ValuesViewType, ColumnsIndexesViewType, isBinary_ >; - + using RowView = SparseMatrixRowView< SegmentView, ValuesViewType, ColumnsIndexesViewType, isBinary_ >; /** * \brief Type of constant sparse matrix row view. */ - using ConstRowViewType = SparseMatrixRowView< SegmentView, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary_ >; + using ConstView = SparseMatrixRowView< SegmentView, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary_ >; + + using IteratorType = SparseMatrixRowViewIterator< RowView >; /** * \brief Tells whether the parent matrix is a binary matrix. @@ -212,6 +214,18 @@ class SparseMatrixRowView __cuda_callable__ bool operator==( const SparseMatrixRowView< _SegmentView, _ValuesView, _ColumnsIndexesView, _isBinary >& other ) const; + __cuda_callable__ + IteratorType begin(); + + __cuda_callable__ + IteratorType end(); + + __cuda_callable__ + const IteratorType cbegin() const; + + __cuda_callable__ + const IteratorType cend() const; + protected: diff --git a/src/TNL/Matrices/SparseMatrixRowView.hpp b/src/TNL/Matrices/SparseMatrixRowView.hpp index 5a9fd475e..82ae9b870 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.hpp +++ b/src/TNL/Matrices/SparseMatrixRowView.hpp @@ -181,6 +181,50 @@ operator==( const SparseMatrixRowView< _SegmentView, _ValuesView, _ColumnsIndexe return true; } +template< typename SegmentView, + typename ValuesView, + typename ColumnsIndexesView, + bool isBinary_ > +__cuda_callable__ auto +SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >:: +begin() -> IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename SegmentView, + typename ValuesView, + typename ColumnsIndexesView, + bool isBinary_ > +__cuda_callable__ auto +SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >:: +end() -> IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + +template< typename SegmentView, + typename ValuesView, + typename ColumnsIndexesView, + bool isBinary_ > +__cuda_callable__ auto +SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >:: +cbegin() const -> const IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename SegmentView, + typename ValuesView, + typename ColumnsIndexesView, + bool isBinary_ > +__cuda_callable__ auto +SparseMatrixRowView< SegmentView, ValuesView, ColumnsIndexesView, isBinary_ >:: +cend() const -> const IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + template< typename SegmentView, typename ValuesView, typename ColumnsIndexesView, @@ -197,5 +241,6 @@ std::ostream& operator<<( std::ostream& str, const SparseMatrixRowView< SegmentV return str; } + } // namespace Matrices } // namespace TNL diff --git a/src/TNL/Matrices/SparseMatrixRowViewIterator.h b/src/TNL/Matrices/SparseMatrixRowViewIterator.h new file mode 100644 index 000000000..d34be5e26 --- /dev/null +++ b/src/TNL/Matrices/SparseMatrixRowViewIterator.h @@ -0,0 +1,98 @@ + /*************************************************************************** + SparseMatrixRowView.h - description + ------------------- + begin : Dec 28, 2019 + copyright : (C) 2019 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +#include +#include + +namespace TNL { +namespace Matrices { + +template< typename RowView > +class SparseMatrixRowViewIterator +{ + + public: + + /** + * \brief Type of SparseMatrixRowView + */ + using RowViewType = RowView; + + /** + * \brief The type of matrix elements. + */ + using RealType = typename RowViewType::RealType; + + /** + * \brief The type used for matrix elements indexing. + */ + using IndexType = typename RowViewType::IndexType; + + /** + * \brief The type of related matrix element. + */ + using MatrixElementType = SparseMatrixElement< RealType, IndexType >; + + /** + * \brief Tells whether the parent matrix is a binary matrix. + * @return `true` if the matrix is binary. + */ + static constexpr bool isBinary() { return RowViewType::isBinary(); }; + + __cuda_callable__ + SparseMatrixRowViewIterator( RowViewType& rowView, + const IndexType& localIdx ); + + /** + * \brief Comparison of two matrix row iterators. + * + * \param other is another matrix row iterator. + * \return \e true if both iterators points at the same point of the same matrix, \e false otherwise. + */ + __cuda_callable__ + bool operator==( const SparseMatrixRowViewIterator& other ) const; + + /** + * \brief Comparison of two matrix row iterators. + * + * \param other is another matrix row iterator. + * \return \e false if both iterators points at the same point of the same matrix, \e true otherwise. + */ + __cuda_callable__ + bool operator!=( const SparseMatrixRowViewIterator& other ) const; + + __cuda_callable__ + SparseMatrixRowViewIterator& operator++(); + + __cuda_callable__ + SparseMatrixRowViewIterator& operator--(); + + __cuda_callable__ + MatrixElementType operator*(); + + __cuda_callable__ + const MatrixElementType operator*() const; + + protected: + + RowViewType& rowView; + + IndexType localIdx = 0; +}; + + + } // namespace Matrices +} // namespace TNL + +#include diff --git a/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp b/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp new file mode 100644 index 000000000..b6e60f22b --- /dev/null +++ b/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp @@ -0,0 +1,95 @@ +/*************************************************************************** + SparseMatrixRowView.hpp - description + ------------------- + begin : Dec 28, 2019 + copyright : (C) 2019 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include +#include + +namespace TNL { +namespace Matrices { + +template< typename RowView > +__cuda_callable__ +SparseMatrixRowViewIterator< RowView >:: +SparseMatrixRowViewIterator( RowViewType& rowView, + const IndexType& localIdx ) +: rowView( rowView ), localIdx( localIdx ) +{ +} + +template< typename RowView > +__cuda_callable__ bool +SparseMatrixRowViewIterator< RowView >:: +operator==( const SparseMatrixRowViewIterator& other ) const +{ + if( &this->rowView == &other.rowView && + localIdx == other.localIdx ) + return true; + return false; +} + +template< typename RowView > +__cuda_callable__ bool +SparseMatrixRowViewIterator< RowView >:: +operator!=( const SparseMatrixRowViewIterator& other ) const +{ + return ! ( other == *this ); +} + +template< typename RowView > +__cuda_callable__ +SparseMatrixRowViewIterator< RowView >& +SparseMatrixRowViewIterator< RowView >:: +operator++() +{ + if( localIdx < rowView.getSize() ) + localIdx ++; + return *this; +} + +template< typename RowView > +__cuda_callable__ +SparseMatrixRowViewIterator< RowView >& +SparseMatrixRowViewIterator< RowView >:: +operator--() +{ + if( localIdx > 0 ) + localIdx --; + return *this; +} + +template< typename RowView > +__cuda_callable__ auto +SparseMatrixRowViewIterator< RowView >:: +operator*() -> MatrixElementType +{ + return MatrixElementType( + this->rowView.getValue( this->localIdx ), + this->rowView.getRowIndex(), + this->rowView.getColumnIndex( this->localIdx ), + this->localIdx ); +} + +template< typename RowView > +__cuda_callable__ auto +SparseMatrixRowViewIterator< RowView >:: +operator*() const -> const MatrixElementType +{ + return MatrixElementType( + this->rowView.getValue( this->localIdx ), + this->rowView.getRowIndex( this->localIdx ), + this->rowView.getColumnIndex( this->localIdx ), + this->localIdx ); +} + + + } // namespace Matrices +} // namespace TNL diff --git a/src/UnitTests/Matrices/SparseMatrixTest.hpp b/src/UnitTests/Matrices/SparseMatrixTest.hpp index 0b07dc508..f906adfbf 100644 --- a/src/UnitTests/Matrices/SparseMatrixTest.hpp +++ b/src/UnitTests/Matrices/SparseMatrixTest.hpp @@ -1437,6 +1437,8 @@ void test_ForRows() const IndexType cols = 8; const IndexType rows = 8; + ///// + // Test without iterator Matrix m( { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, cols ); using RowView = typename Matrix::RowView; m.forAllRows( [] __cuda_callable__ ( RowView& row ) mutable { @@ -1447,6 +1449,29 @@ void test_ForRows() } } ); + for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) + for( IndexType colIdx = 0; colIdx < cols; colIdx++ ) + { + if( colIdx <= rowIdx ) + EXPECT_EQ( m.getElement( rowIdx, colIdx ), rowIdx - colIdx + 1.0 ); + else + EXPECT_EQ( m.getElement( rowIdx, colIdx ), 0.0 ); + } + + //// + // Test with iterator + m.getValues() = 0.0; + m.forAllRows( [] __cuda_callable__ ( RowView& row ) mutable { + for( auto element : row ) + { + if( element.localIndex() <= element.rowIndex() ) + { + element.value() = element.rowIndex() - element.localIndex() + 1.0; + element.columnIndex() = element.localIndex(); + } + } + } ); + for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) for( IndexType colIdx = 0; colIdx < cols; colIdx++ ) { -- GitLab From 228a552b3fc5f3b20052b001a73f8d7ee1ad1dae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Mon, 22 Mar 2021 18:15:37 +0100 Subject: [PATCH 51/54] Added iterator to Dense, Lambda, Multidiagonal and Tridiagonal matrix row. --- src/TNL/Matrices/DenseMatrix.h | 16 +-- src/TNL/Matrices/DenseMatrix.hpp | 16 +-- src/TNL/Matrices/DenseMatrixElement.h | 63 ++++++++++++ src/TNL/Matrices/DenseMatrixRowView.h | 80 ++++++++++++++- src/TNL/Matrices/DenseMatrixRowView.hpp | 55 ++++++++++- src/TNL/Matrices/DenseMatrixView.h | 16 +-- src/TNL/Matrices/DenseMatrixView.hpp | 16 +-- src/TNL/Matrices/LambdaMatrixElement.h | 65 ++++++++++++ src/TNL/Matrices/LambdaMatrixRowView.h | 50 +++++++++- src/TNL/Matrices/LambdaMatrixRowView.hpp | 44 +++++++++ .../Matrices/LambdaMatrixRowViewIterator.h | 98 +++++++++++++++++++ .../Matrices/LambdaMatrixRowViewIterator.hpp | 95 ++++++++++++++++++ src/TNL/Matrices/MultidiagonalMatrixElement.h | 68 +++++++++++++ src/TNL/Matrices/MultidiagonalMatrixRowView.h | 59 ++++++++++- .../Matrices/MultidiagonalMatrixRowView.hpp | 38 ++++++- src/TNL/Matrices/MultidiagonalMatrixView.h | 2 +- src/TNL/Matrices/SparseMatrixRowView.h | 29 +++++- .../Matrices/SparseMatrixRowViewIterator.h | 8 +- .../Matrices/SparseMatrixRowViewIterator.hpp | 6 +- src/TNL/Matrices/TridiagonalMatrixRowView.h | 53 +++++++++- src/TNL/Matrices/TridiagonalMatrixRowView.hpp | 38 ++++++- src/TNL/Matrices/TridiagonalMatrixView.h | 2 +- src/UnitTests/Matrices/DenseMatrixTest.h | 44 +++++++-- src/UnitTests/Matrices/LambdaMatrixTest.hpp | 18 +++- .../Matrices/MultidiagonalMatrixTest.h | 33 +++++++ .../Matrices/TridiagonalMatrixTest.h | 34 +++++++ 26 files changed, 980 insertions(+), 66 deletions(-) create mode 100644 src/TNL/Matrices/DenseMatrixElement.h create mode 100644 src/TNL/Matrices/LambdaMatrixElement.h create mode 100644 src/TNL/Matrices/LambdaMatrixRowViewIterator.h create mode 100644 src/TNL/Matrices/LambdaMatrixRowViewIterator.hpp create mode 100644 src/TNL/Matrices/MultidiagonalMatrixElement.h diff --git a/src/TNL/Matrices/DenseMatrix.h b/src/TNL/Matrices/DenseMatrix.h index 27984123e..d5444fada 100644 --- a/src/TNL/Matrices/DenseMatrix.h +++ b/src/TNL/Matrices/DenseMatrix.h @@ -472,7 +472,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forRows.out */ template< typename Function > - void forElements( IndexType begin, IndexType end, Function& function ) const; + void forElements( IndexType begin, IndexType end, Function&& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. @@ -494,7 +494,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forRows.out */ template< typename Function > - void forElements( IndexType begin, IndexType end, Function& function ); + void forElements( IndexType begin, IndexType end, Function&& function ); /** * \brief This method calls \e forElements for all matrix rows (for constant instances). @@ -510,7 +510,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forAllRows.out */ template< typename Function > - void forAllElements( Function& function ) const; + void forAllElements( Function&& function ) const; /** * \brief This method calls \e forElements for all matrix rows. @@ -526,7 +526,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \include DenseMatrixExample_forAllRows.out */ template< typename Function > - void forAllElements( Function& function ); + void forAllElements( Function&& function ); /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). @@ -643,7 +643,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForRows( IndexType begin, IndexType end, Function& function ) const; + void sequentialForRows( IndexType begin, IndexType end, Function&& function ) const; /** * \brief Method for sequential iteration over all matrix rows for non-constant instances. @@ -660,7 +660,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForRows( IndexType begin, IndexType end, Function& function ); + void sequentialForRows( IndexType begin, IndexType end, Function&& function ); /** * \brief This method calls \e sequentialForRows for all matrix rows (for constant instances). @@ -671,7 +671,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForAllRows( Function& function ) const; + void sequentialForAllRows( Function&& function ) const; /** * \brief This method calls \e sequentialForRows for all matrix rows. @@ -682,7 +682,7 @@ class DenseMatrix : public Matrix< Real, Device, Index, RealAllocator > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForAllRows( Function& function ); + void sequentialForAllRows( Function&& function ); /** * \brief Method for performing general reduction on matrix rows. diff --git a/src/TNL/Matrices/DenseMatrix.hpp b/src/TNL/Matrices/DenseMatrix.hpp index 24844d845..d7d4ad82f 100644 --- a/src/TNL/Matrices/DenseMatrix.hpp +++ b/src/TNL/Matrices/DenseMatrix.hpp @@ -407,7 +407,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -forElements( IndexType begin, IndexType end, Function& function ) const +forElements( IndexType begin, IndexType end, Function&& function ) const { this->view.forElements( begin, end, function ); } @@ -420,7 +420,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -forElements( IndexType first, IndexType last, Function& function ) +forElements( IndexType first, IndexType last, Function&& function ) { this->view.forElements( first, last, function ); } @@ -433,7 +433,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -forAllElements( Function& function ) const +forAllElements( Function&& function ) const { this->forElements( 0, this->getRows(), function ); } @@ -446,7 +446,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -forAllElements( Function& function ) +forAllElements( Function&& function ) { this->forElements( 0, this->getRows(), function ); } @@ -511,7 +511,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -sequentialForRows( IndexType begin, IndexType end, Function& function ) const +sequentialForRows( IndexType begin, IndexType end, Function&& function ) const { this->view.sequentialForRows( begin, end, function ); } @@ -524,7 +524,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -sequentialForRows( IndexType first, IndexType last, Function& function ) +sequentialForRows( IndexType first, IndexType last, Function&& function ) { this->view.sequentialForRows( first, last, function ); } @@ -537,7 +537,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -sequentialForAllRows( Function& function ) const +sequentialForAllRows( Function&& function ) const { this->sequentialForRows( 0, this->getRows(), function ); } @@ -550,7 +550,7 @@ template< typename Real, template< typename Function > void DenseMatrix< Real, Device, Index, Organization, RealAllocator >:: -sequentialForAllRows( Function& function ) +sequentialForAllRows( Function&& function ) { this->sequentialForRows( 0, this->getRows(), function ); } diff --git a/src/TNL/Matrices/DenseMatrixElement.h b/src/TNL/Matrices/DenseMatrixElement.h new file mode 100644 index 000000000..c2c1c9c34 --- /dev/null +++ b/src/TNL/Matrices/DenseMatrixElement.h @@ -0,0 +1,63 @@ +/*************************************************************************** + DenseMatrixElement.h - description + ------------------- + begin : Mar 22, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +#include + +namespace TNL { +namespace Matrices { + + +template< typename Real, + typename Index > +class DenseMatrixElement +{ + public: + + using RealType = Real; + + using IndexType = Index; + + __cuda_callable__ + DenseMatrixElement( RealType& value, + const IndexType& rowIdx, + const IndexType& columnIdx, + const IndexType& localIdx ) // localIdx is here only for compatibility with SparseMatrixElement + : value_( value ), rowIdx( rowIdx ), columnIdx( columnIdx ) {}; + + __cuda_callable__ + RealType& value() { return value_; }; + + __cuda_callable__ + const RealType& value() const { return value_; }; + + __cuda_callable__ + const IndexType& rowIndex() const { return rowIdx; }; + + __cuda_callable__ + const IndexType& columnIndex() const { return columnIdx; }; + + __cuda_callable__ + const IndexType& localIndex() const { return columnIdx; }; + + protected: + + RealType& value_; + + const IndexType& rowIdx; + + const IndexType& columnIdx; +}; + + } // namespace Matrices +} // namespace TNL diff --git a/src/TNL/Matrices/DenseMatrixRowView.h b/src/TNL/Matrices/DenseMatrixRowView.h index ce8074bce..113f8aa74 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.h +++ b/src/TNL/Matrices/DenseMatrixRowView.h @@ -10,6 +10,10 @@ #pragma once +#include +#include +#include + namespace TNL { namespace Matrices { @@ -57,6 +61,31 @@ class DenseMatrixRowView */ using ValuesViewType = ValuesView; + /** + * \brief Type of constant container view used for storing the matrix elements values. + */ + using ConstValuesViewType = typename ValuesViewType::ConstViewType; + + /** + * \brief Type of dense matrix row view. + */ + using RowView = DenseMatrixRowView< SegmentView, ValuesViewType >; + + /** + * \brief Type of constant sparse matrix row view. + */ + using ConstView = DenseMatrixRowView< SegmentView, ConstValuesViewType >; + + /** + * \brief The type of related matrix element. + */ + using MatrixElementType = DenseMatrixElement< RealType, IndexType >; + + /** + * \brief Type of iterator for the matrix row. + */ + using IteratorType = SparseMatrixRowViewIterator< RowView >; + /** * \brief Constructor with \e segmentView and \e values * @@ -91,7 +120,7 @@ class DenseMatrixRowView * \return constant reference to the matrix element. */ __cuda_callable__ - const RealType& getElement( const IndexType column ) const; + const RealType& getValue( const IndexType column ) const; /** * \brief Returns non-constants reference to an element with given column index. @@ -101,7 +130,17 @@ class DenseMatrixRowView * \return non-constant reference to the matrix element. */ __cuda_callable__ - RealType& getElement( const IndexType column ); + RealType& getValue( const IndexType column ); + + /** + * \brief This method is only for compatibility with sparse matrix row. + * + * \param localIdx is the rank of the matrix element in given row. + * + * \return the value of \ref localIdx as column index. + */ + __cuda_callable__ + IndexType getColumnIndex( const IndexType localIdx ) const; /** * \brief Sets value of matrix element with given column index @@ -110,8 +149,8 @@ class DenseMatrixRowView * \param value is a value the matrix element will be set to. */ __cuda_callable__ - void setElement( const IndexType column, - const RealType& value ); + void setValue( const IndexType column, + const RealType& value ); /** * \brief Sets value of matrix element with given column index @@ -126,6 +165,39 @@ class DenseMatrixRowView void setElement( const IndexType localIdx, const IndexType column, const RealType& value ); + + /** + * \brief Returns iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + IteratorType begin(); + + /** + * \brief Returns iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + IteratorType end(); + + /** + * \brief Returns constant iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + const IteratorType cbegin() const; + + /** + * \brief Returns constant iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + const IteratorType cend() const; + protected: SegmentViewType segmentView; diff --git a/src/TNL/Matrices/DenseMatrixRowView.hpp b/src/TNL/Matrices/DenseMatrixRowView.hpp index 81d4aeaa6..6c322cf71 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.hpp +++ b/src/TNL/Matrices/DenseMatrixRowView.hpp @@ -47,7 +47,7 @@ template< typename SegmentView, typename ValuesView > __cuda_callable__ auto DenseMatrixRowView< SegmentView, ValuesView >:: -getElement( const IndexType column ) const -> const RealType& +getValue( const IndexType column ) const -> const RealType& { TNL_ASSERT_LT( column, this->getSize(), "Column index exceeds matrix row size." ); return values[ segmentView.getGlobalIndex( column ) ]; @@ -57,18 +57,29 @@ template< typename SegmentView, typename ValuesView > __cuda_callable__ auto DenseMatrixRowView< SegmentView, ValuesView >:: -getElement( const IndexType column ) -> RealType& +getValue( const IndexType column ) -> RealType& { TNL_ASSERT_LT( column, this->getSize(), "Column index exceeds matrix row size." ); return values[ segmentView.getGlobalIndex( column ) ]; } +template< typename SegmentView, + typename ValuesView > +__cuda_callable__ auto +DenseMatrixRowView< SegmentView, ValuesView >:: +getColumnIndex( const IndexType localIdx ) const -> IndexType +{ + TNL_ASSERT_LT( localIdx, this->getSize(), "Column index exceeds matrix row size." ); + return localIdx; +} + + template< typename SegmentView, typename ValuesView > __cuda_callable__ void DenseMatrixRowView< SegmentView, ValuesView >:: -setElement( const IndexType column, - const RealType& value ) +setValue( const IndexType column, + const RealType& value ) { TNL_ASSERT_LT( column, this->getSize(), "Column index exceeds matrix row size." ); const IndexType globalIdx = segmentView.getGlobalIndex( column ); @@ -88,5 +99,41 @@ setElement( const IndexType localIdx, values[ globalIdx ] = value; } +template< typename SegmentView, + typename ValuesView > +__cuda_callable__ auto +DenseMatrixRowView< SegmentView, ValuesView >:: +begin() -> IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename SegmentView, + typename ValuesView > +__cuda_callable__ auto +DenseMatrixRowView< SegmentView, ValuesView >:: +end() -> IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + +template< typename SegmentView, + typename ValuesView > +__cuda_callable__ auto +DenseMatrixRowView< SegmentView, ValuesView >:: +cbegin() const -> const IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename SegmentView, + typename ValuesView > +__cuda_callable__ auto +DenseMatrixRowView< SegmentView, ValuesView >:: +cend() const -> const IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + } // namespace Matrices } // namespace TNL diff --git a/src/TNL/Matrices/DenseMatrixView.h b/src/TNL/Matrices/DenseMatrixView.h index a13846e7a..89ace2d06 100644 --- a/src/TNL/Matrices/DenseMatrixView.h +++ b/src/TNL/Matrices/DenseMatrixView.h @@ -525,7 +525,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_forRows.out */ template< typename Function > - void forElements( IndexType begin, IndexType end, Function& function ) const; + void forElements( IndexType begin, IndexType end, Function&& function ) const; /** * \brief Method for iteration over all matrix rows for non-constant instances. @@ -547,7 +547,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_forRows.out */ template< typename Function > - void forElements( IndexType begin, IndexType end, Function& function ); + void forElements( IndexType begin, IndexType end, Function&& function ); /** * \brief This method calls \e forElements for all matrix rows. @@ -563,7 +563,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixViewExample_forAllRows.out */ template< typename Function > - void forAllElements( Function& function ) const; + void forAllElements( Function&& function ) const; /** * \brief This method calls \e forElements for all matrix rows. @@ -579,7 +579,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \include DenseMatrixExample_forAllRows.out */ template< typename Function > - void forAllElements( Function& function ); + void forAllElements( Function&& function ); /** * \brief Method for parallel iteration over matrix rows from interval [ \e begin, \e end). @@ -696,7 +696,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForRows( IndexType begin, IndexType end, Function& function ) const; + void sequentialForRows( IndexType begin, IndexType end, Function&& function ) const; /** * \brief Method for sequential iteration over all matrix rows for non-constant instances. @@ -713,7 +713,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForRows( IndexType begin, IndexType end, Function& function ); + void sequentialForRows( IndexType begin, IndexType end, Function&& function ); /** * \brief This method calls \e sequentialForRows for all matrix rows (for constant instances). @@ -724,7 +724,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForAllRows( Function& function ) const; + void sequentialForAllRows( Function&& function ) const; /** * \brief This method calls \e sequentialForRows for all matrix rows. @@ -735,7 +735,7 @@ class DenseMatrixView : public MatrixView< Real, Device, Index > * \param function is an instance of the lambda function to be called in each row. */ template< typename Function > - void sequentialForAllRows( Function& function ); + void sequentialForAllRows( Function&& function ); /** * \brief Computes product of matrix and vector. diff --git a/src/TNL/Matrices/DenseMatrixView.hpp b/src/TNL/Matrices/DenseMatrixView.hpp index f3f7f80d3..0bf262aa0 100644 --- a/src/TNL/Matrices/DenseMatrixView.hpp +++ b/src/TNL/Matrices/DenseMatrixView.hpp @@ -348,7 +348,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forElements( IndexType begin, IndexType end, Function& function ) const +forElements( IndexType begin, IndexType end, Function&& function ) const { const auto values_view = this->values.getConstView(); auto f = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable { @@ -364,7 +364,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forElements( IndexType begin, IndexType end, Function& function ) +forElements( IndexType begin, IndexType end, Function&& function ) { auto values_view = this->values.getView(); auto f = [=] __cuda_callable__ ( IndexType rowIdx, IndexType columnIdx, IndexType globalIdx, bool& compute ) mutable { @@ -380,7 +380,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forAllElements( Function& function ) const +forAllElements( Function&& function ) const { this->forElements( 0, this->getRows(), function ); } @@ -392,7 +392,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -forAllElements( Function& function ) +forAllElements( Function&& function ) { this->forElements( 0, this->getRows(), function ); } @@ -464,7 +464,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -sequentialForRows( IndexType begin, IndexType end, Function& function ) const +sequentialForRows( IndexType begin, IndexType end, Function&& function ) const { for( IndexType row = begin; row < end; row ++ ) this->forRows( row, row + 1, function ); @@ -477,7 +477,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -sequentialForRows( IndexType begin, IndexType end, Function& function ) +sequentialForRows( IndexType begin, IndexType end, Function&& function ) { for( IndexType row = begin; row < end; row ++ ) this->forRows( row, row + 1, function ); @@ -490,7 +490,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -sequentialForAllRows( Function& function ) const +sequentialForAllRows( Function&& function ) const { this->sequentialForRows( 0, this->getRows(), function ); } @@ -502,7 +502,7 @@ template< typename Real, template< typename Function > void DenseMatrixView< Real, Device, Index, Organization >:: -sequentialForAllRows( Function& function ) +sequentialForAllRows( Function&& function ) { this->sequentialForRows( 0, this->getRows(), function ); } diff --git a/src/TNL/Matrices/LambdaMatrixElement.h b/src/TNL/Matrices/LambdaMatrixElement.h new file mode 100644 index 000000000..57ba698f3 --- /dev/null +++ b/src/TNL/Matrices/LambdaMatrixElement.h @@ -0,0 +1,65 @@ +/*************************************************************************** + LambdaMatrixElement.h - description + ------------------- + begin : Mar 22, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +#include + +namespace TNL { +namespace Matrices { + + +template< typename Real, + typename Index > +class LambdaMatrixElement +{ + public: + + using RealType = Real; + + using IndexType = Index; + + __cuda_callable__ + LambdaMatrixElement( const RealType& value, + const IndexType& rowIdx, + const IndexType& columnIdx, + const IndexType& localIdx ) + : value_( value ), rowIdx( rowIdx ), columnIdx( columnIdx ), localIdx( localIdx ) {}; + + __cuda_callable__ + LambdaMatrixElement( const LambdaMatrixElement& el ) = default; + + __cuda_callable__ + const RealType& value() const { return value_; }; + + __cuda_callable__ + const IndexType& rowIndex() const { return rowIdx; }; + + __cuda_callable__ + const IndexType& columnIndex() const { return columnIdx; }; + + __cuda_callable__ + const IndexType& localIndex() const { return localIdx; }; + + protected: + + const RealType value_; + + const IndexType& rowIdx; + + const IndexType columnIdx; + + const IndexType& localIdx; +}; + + } // namespace Matrices +} // namespace TNL diff --git a/src/TNL/Matrices/LambdaMatrixRowView.h b/src/TNL/Matrices/LambdaMatrixRowView.h index e989b9c02..b2e7bfaf8 100644 --- a/src/TNL/Matrices/LambdaMatrixRowView.h +++ b/src/TNL/Matrices/LambdaMatrixRowView.h @@ -13,6 +13,9 @@ #include #include +#include +#include + namespace TNL { namespace Matrices { @@ -80,12 +83,22 @@ class LambdaMatrixRowView /** * \brief Type of Lambda matrix row view. */ - using RowViewType = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType >; + using RowView = LambdaMatrixRowView< MatrixElementsLambdaType, CompressedRowLengthsLambdaType, RealType, IndexType >; /** * \brief Type of constant Lambda matrix row view. */ - using ConstRowViewType = RowViewType; + using ConstRowView = RowView; + + /** + * \brief The type of related matrix element. + */ + using MatrixElementType = LambdaMatrixElement< RealType, IndexType >; + + /** + * \brief Type of iterator for the matrix row. + */ + using IteratorType = LambdaMatrixRowViewIterator< RowView >; /** * \brief Constructor with related lambda functions, matrix dimensions and row index. @@ -154,6 +167,39 @@ class LambdaMatrixRowView __cuda_callable__ bool operator==( const LambdaMatrixRowView< MatrixElementsLambda_, CompressedRowLengthsLambda_, Real_, Index_ >& other ) const; + /** + * \brief Returns non-constant iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + const IteratorType begin() const; + + /** + * \brief Returns non-constant iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + const IteratorType end() const; + + /** + * \brief Returns constant iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + const IteratorType cbegin() const; + + /** + * \brief Returns constant iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + const IteratorType cend() const; + + protected: const MatrixElementsLambda& matrixElementsLambda; diff --git a/src/TNL/Matrices/LambdaMatrixRowView.hpp b/src/TNL/Matrices/LambdaMatrixRowView.hpp index fce6076cc..2aec69b94 100644 --- a/src/TNL/Matrices/LambdaMatrixRowView.hpp +++ b/src/TNL/Matrices/LambdaMatrixRowView.hpp @@ -118,6 +118,50 @@ operator==( const LambdaMatrixRowView< MatrixElementsLambda_, CompressedRowLengt return true; } +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +begin() const -> const IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +end() const -> const IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +cbegin() const -> const IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename MatrixElementsLambda, + typename CompressedRowLengthsLambda, + typename Real, + typename Index > +__cuda_callable__ auto +LambdaMatrixRowView< MatrixElementsLambda, CompressedRowLengthsLambda, Real, Index >:: +cend() const -> const IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + template< typename MatrixElementsLambda, typename CompressedRowLengthsLambda, typename Real, diff --git a/src/TNL/Matrices/LambdaMatrixRowViewIterator.h b/src/TNL/Matrices/LambdaMatrixRowViewIterator.h new file mode 100644 index 000000000..67b7253e8 --- /dev/null +++ b/src/TNL/Matrices/LambdaMatrixRowViewIterator.h @@ -0,0 +1,98 @@ + /*************************************************************************** + LambdaMatrixRowView.h - description + ------------------- + begin : Mar 21, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +#include +#include + +namespace TNL { +namespace Matrices { + +template< typename RowView > +class LambdaMatrixRowViewIterator +{ + + public: + + /** + * \brief Type of LambdaMatrixRowView + */ + using RowViewType = RowView; + + /** + * \brief The type of matrix elements. + */ + using RealType = typename RowViewType::RealType; + + /** + * \brief The type used for matrix elements indexing. + */ + using IndexType = typename RowViewType::IndexType; + + /** + * \brief The type of related matrix element. + */ + using MatrixElementType = typename RowView::MatrixElementType; + + /** + * \brief Tells whether the parent matrix is a binary matrix. + * @return `true` if the matrix is binary. + */ + static constexpr bool isBinary() { return RowViewType::isBinary(); }; + + __cuda_callable__ + LambdaMatrixRowViewIterator( const RowViewType& rowView, + const IndexType& localIdx ); + + /** + * \brief Comparison of two matrix row iterators. + * + * \param other is another matrix row iterator. + * \return \e true if both iterators points at the same point of the same matrix, \e false otherwise. + */ + __cuda_callable__ + bool operator==( const LambdaMatrixRowViewIterator& other ) const; + + /** + * \brief Comparison of two matrix row iterators. + * + * \param other is another matrix row iterator. + * \return \e false if both iterators points at the same point of the same matrix, \e true otherwise. + */ + __cuda_callable__ + bool operator!=( const LambdaMatrixRowViewIterator& other ) const; + + __cuda_callable__ + LambdaMatrixRowViewIterator& operator++(); + + __cuda_callable__ + LambdaMatrixRowViewIterator& operator--(); + + __cuda_callable__ + MatrixElementType operator*(); + + __cuda_callable__ + const MatrixElementType operator*() const; + + protected: + + const RowViewType& rowView; + + IndexType localIdx = 0; +}; + + + } // namespace Matrices +} // namespace TNL + +#include diff --git a/src/TNL/Matrices/LambdaMatrixRowViewIterator.hpp b/src/TNL/Matrices/LambdaMatrixRowViewIterator.hpp new file mode 100644 index 000000000..13ff5efca --- /dev/null +++ b/src/TNL/Matrices/LambdaMatrixRowViewIterator.hpp @@ -0,0 +1,95 @@ +/*************************************************************************** + LambdaMatrixRowView.hpp - description + ------------------- + begin : Mar 21, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include +#include + +namespace TNL { +namespace Matrices { + +template< typename RowView > +__cuda_callable__ +LambdaMatrixRowViewIterator< RowView >:: +LambdaMatrixRowViewIterator( const RowViewType& rowView, + const IndexType& localIdx ) +: rowView( rowView ), localIdx( localIdx ) +{ +} + +template< typename RowView > +__cuda_callable__ bool +LambdaMatrixRowViewIterator< RowView >:: +operator==( const LambdaMatrixRowViewIterator& other ) const +{ + if( &this->rowView == &other.rowView && + localIdx == other.localIdx ) + return true; + return false; +} + +template< typename RowView > +__cuda_callable__ bool +LambdaMatrixRowViewIterator< RowView >:: +operator!=( const LambdaMatrixRowViewIterator& other ) const +{ + return ! ( other == *this ); +} + +template< typename RowView > +__cuda_callable__ +LambdaMatrixRowViewIterator< RowView >& +LambdaMatrixRowViewIterator< RowView >:: +operator++() +{ + if( localIdx < rowView.getSize() ) + localIdx ++; + return *this; +} + +template< typename RowView > +__cuda_callable__ +LambdaMatrixRowViewIterator< RowView >& +LambdaMatrixRowViewIterator< RowView >:: +operator--() +{ + if( localIdx > 0 ) + localIdx --; + return *this; +} + +template< typename RowView > +__cuda_callable__ auto +LambdaMatrixRowViewIterator< RowView >:: +operator*() -> MatrixElementType +{ + return MatrixElementType( + this->rowView.getValue( this->localIdx ), + this->rowView.getRowIndex(), + this->rowView.getColumnIndex( this->localIdx ), + this->localIdx ); +} + +template< typename RowView > +__cuda_callable__ auto +LambdaMatrixRowViewIterator< RowView >:: +operator*() const -> const MatrixElementType +{ + return MatrixElementType( + this->rowView.getValue( this->localIdx ), + this->rowView.getRowIndex(), + this->rowView.getColumnIndex( this->localIdx ), + this->localIdx ); +} + + + } // namespace Matrices +} // namespace TNL diff --git a/src/TNL/Matrices/MultidiagonalMatrixElement.h b/src/TNL/Matrices/MultidiagonalMatrixElement.h new file mode 100644 index 000000000..3672526ea --- /dev/null +++ b/src/TNL/Matrices/MultidiagonalMatrixElement.h @@ -0,0 +1,68 @@ +/*************************************************************************** + MultidiagonalMatrixElement.h - description + ------------------- + begin : Mar 22, 2021 + copyright : (C) 2021 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +#include + +namespace TNL { +namespace Matrices { + + +template< typename Real, + typename Index > +class MultidiagonalMatrixElement +{ + public: + + using RealType = Real; + + using IndexType = Index; + + __cuda_callable__ + MultidiagonalMatrixElement( RealType& value, + const IndexType& rowIdx, + const IndexType& columnIdx, + const IndexType& localIdx ) + : value_( value ), rowIdx( rowIdx ), columnIdx( columnIdx ), localIdx( localIdx ) {}; + + __cuda_callable__ + RealType& value() { return value_; }; + + __cuda_callable__ + const RealType& value() const { return value_; }; + + __cuda_callable__ + const IndexType& rowIndex() const { return rowIdx; }; + + __cuda_callable__ + IndexType& columnIndex() { return columnIdx; }; + + __cuda_callable__ + const IndexType& columnIndex() const { return columnIdx; }; + + __cuda_callable__ + const IndexType& localIndex() const { return localIdx; }; + + protected: + + RealType& value_; + + const IndexType& rowIdx; + + IndexType columnIdx; + + const IndexType& localIdx; +}; + + } // namespace Matrices +} // namespace TNL diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.h b/src/TNL/Matrices/MultidiagonalMatrixRowView.h index 88b976aa3..bd01f53a7 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.h @@ -10,6 +10,9 @@ #pragma once +#include +#include + namespace TNL { namespace Matrices { @@ -27,7 +30,7 @@ namespace Matrices { * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_getRow.cpp * \par Output - * \include MultidiagonalatrixExample_getRow.out + * \include MultidiagonalMatrixExample_getRow.out * * \par Example * \include Matrices/MultidiagonalMatrix/MultidiagonalMatrixViewExample_getRow.cpp @@ -76,17 +79,32 @@ class MultidiagonalMatrixRowView /** * \brief Type of constant container view used for storing the column indexes of the matrix elements. */ - using ConstDiagonalsOffsetsViewType = typename DiagonalsOffsetsView::ConstViewType; + using ConstDiagonalsOffsetsView = typename DiagonalsOffsetsView::ConstViewType; /** * \brief Type of constant indexer view. */ - using ConstIndexerViewType = typename Indexer::ConstType; + using ConstIndexerViewType = typename IndexerType::ConstType; + + /** + * \brief Type of constant sparse matrix row view. + */ + using RowView = MultidiagonalMatrixRowView< ValuesViewType, IndexerType, DiagonalsOffsetsView >; /** * \brief Type of constant sparse matrix row view. */ - using ConstViewType = MultidiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType, ConstDiagonalsOffsetsViewType >; + using ConstRowView = MultidiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType, ConstDiagonalsOffsetsView >; + + /** + * \brief The type of related matrix element. + */ + using MatrixElementType = MultidiagonalMatrixElement< RealType, IndexType >; + + /** + * \brief Type of iterator for the matrix row. + */ + using IteratorType = SparseMatrixRowViewIterator< RowView >; /** * \brief Constructor with all necessary data. @@ -157,6 +175,39 @@ class MultidiagonalMatrixRowView __cuda_callable__ void setElement( const IndexType localIdx, const RealType& value ); + + /** + * \brief Returns iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + IteratorType begin(); + + /** + * \brief Returns iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + IteratorType end(); + + /** + * \brief Returns constant iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + const IteratorType cbegin() const; + + /** + * \brief Returns constant iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + const IteratorType cend() const; + protected: IndexType rowIdx; diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp b/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp index f63a64b44..0f43c6ad8 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.hpp @@ -74,7 +74,7 @@ getValue( const IndexType localIdx ) -> RealType& template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > __cuda_callable__ -void +void MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: setElement( const IndexType localIdx, const RealType& value ) @@ -82,5 +82,41 @@ setElement( const IndexType localIdx, this->values[ indexer.getGlobalIndex( rowIdx, localIdx ) ] = value; } +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > +__cuda_callable__ +auto +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: +begin() -> IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > +__cuda_callable__ +auto +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: +end() -> IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > +__cuda_callable__ +auto +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: +cbegin() const -> const IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename ValuesView, typename Indexer, typename DiagonalsOffsetsView > +__cuda_callable__ +auto +MultidiagonalMatrixRowView< ValuesView, Indexer, DiagonalsOffsetsView >:: +cend() const -> const IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + } // namespace Matrices } // namespace TNL diff --git a/src/TNL/Matrices/MultidiagonalMatrixView.h b/src/TNL/Matrices/MultidiagonalMatrixView.h index 22e7a9de0..bc3de664b 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixView.h @@ -81,7 +81,7 @@ class MultidiagonalMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing constant matrix rows. */ - using ConstRowView = typename RowView::ConstViewType; + using ConstRowView = typename RowView::ConstRowView; /** * \brief Helper type for getting self type or its modifications. diff --git a/src/TNL/Matrices/SparseMatrixRowView.h b/src/TNL/Matrices/SparseMatrixRowView.h index bfdb74838..338ac390c 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.h +++ b/src/TNL/Matrices/SparseMatrixRowView.h @@ -91,6 +91,14 @@ class SparseMatrixRowView */ using ConstView = SparseMatrixRowView< SegmentView, ConstValuesViewType, ConstColumnsIndexesViewType, isBinary_ >; + /** + * \brief The type of related matrix element. + */ + using MatrixElementType = SparseMatrixElement< RealType, IndexType >; + + /** + * \brief Type of iterator for the matrix row. + */ using IteratorType = SparseMatrixRowViewIterator< RowView >; /** @@ -214,19 +222,38 @@ class SparseMatrixRowView __cuda_callable__ bool operator==( const SparseMatrixRowView< _SegmentView, _ValuesView, _ColumnsIndexesView, _isBinary >& other ) const; + /** + * \brief Returns iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ __cuda_callable__ IteratorType begin(); + /** + * \brief Returns iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ __cuda_callable__ IteratorType end(); + /** + * \brief Returns constant iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ __cuda_callable__ const IteratorType cbegin() const; + /** + * \brief Returns constant iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ __cuda_callable__ const IteratorType cend() const; - protected: SegmentViewType segmentView; diff --git a/src/TNL/Matrices/SparseMatrixRowViewIterator.h b/src/TNL/Matrices/SparseMatrixRowViewIterator.h index d34be5e26..dc7ef5069 100644 --- a/src/TNL/Matrices/SparseMatrixRowViewIterator.h +++ b/src/TNL/Matrices/SparseMatrixRowViewIterator.h @@ -1,8 +1,8 @@ /*************************************************************************** - SparseMatrixRowView.h - description + SparseMatrixRowViewIterator.h - description ------------------- - begin : Dec 28, 2019 - copyright : (C) 2019 by Tomas Oberhuber + begin : Mar 20, 2021 + copyright : (C) 2021 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ @@ -42,7 +42,7 @@ class SparseMatrixRowViewIterator /** * \brief The type of related matrix element. */ - using MatrixElementType = SparseMatrixElement< RealType, IndexType >; + using MatrixElementType = typename RowView::MatrixElementType; /** * \brief Tells whether the parent matrix is a binary matrix. diff --git a/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp b/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp index b6e60f22b..313e2fbf1 100644 --- a/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp +++ b/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp @@ -1,8 +1,8 @@ /*************************************************************************** SparseMatrixRowView.hpp - description ------------------- - begin : Dec 28, 2019 - copyright : (C) 2019 by Tomas Oberhuber + begin : Mar 20, 2021 + copyright : (C) 2021 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ @@ -85,7 +85,7 @@ operator*() const -> const MatrixElementType { return MatrixElementType( this->rowView.getValue( this->localIdx ), - this->rowView.getRowIndex( this->localIdx ), + this->rowView.getRowIndex(), this->rowView.getColumnIndex( this->localIdx ), this->localIdx ); } diff --git a/src/TNL/Matrices/TridiagonalMatrixRowView.h b/src/TNL/Matrices/TridiagonalMatrixRowView.h index 06d80cd28..347e591d7 100644 --- a/src/TNL/Matrices/TridiagonalMatrixRowView.h +++ b/src/TNL/Matrices/TridiagonalMatrixRowView.h @@ -10,6 +10,9 @@ #pragma once +#include +#include + namespace TNL { namespace Matrices { @@ -72,7 +75,22 @@ class TridiagonalMatrixRowView /** * \brief Type of constant sparse matrix row view. */ - using ConstViewType = TridiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType >; + using RowView = TridiagonalMatrixRowView< ValuesViewType, IndexerType >; + + /** + * \brief Type of constant sparse matrix row view. + */ + using ConstRowView = TridiagonalMatrixRowView< ConstValuesViewType, ConstIndexerViewType >; + + /** + * \brief The type of related matrix element. + */ + using MatrixElementType = MultidiagonalMatrixElement< RealType, IndexType >; + + /** + * \brief Type of iterator for the matrix row. + */ + using IteratorType = SparseMatrixRowViewIterator< RowView >; /** * \brief Constructor with all necessary data. @@ -141,6 +159,39 @@ class TridiagonalMatrixRowView __cuda_callable__ void setElement( const IndexType localIdx, const RealType& value ); + + /** + * \brief Returns iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + IteratorType begin(); + + /** + * \brief Returns iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + IteratorType end(); + + /** + * \brief Returns constant iterator pointing at the beginning of the matrix row. + * + * \return iterator pointing at the beginning. + */ + __cuda_callable__ + const IteratorType cbegin() const; + + /** + * \brief Returns constant iterator pointing at the end of the matrix row. + * + * \return iterator pointing at the end. + */ + __cuda_callable__ + const IteratorType cend() const; + protected: IndexType rowIdx; diff --git a/src/TNL/Matrices/TridiagonalMatrixRowView.hpp b/src/TNL/Matrices/TridiagonalMatrixRowView.hpp index 7b502416c..de4983d86 100644 --- a/src/TNL/Matrices/TridiagonalMatrixRowView.hpp +++ b/src/TNL/Matrices/TridiagonalMatrixRowView.hpp @@ -72,7 +72,7 @@ getValue( const IndexType localIdx ) -> RealType& template< typename ValuesView, typename Indexer > __cuda_callable__ -void +void TridiagonalMatrixRowView< ValuesView, Indexer >:: setElement( const IndexType localIdx, const RealType& value ) @@ -80,5 +80,41 @@ setElement( const IndexType localIdx, this->values[ indexer.getGlobalIndex( rowIdx, localIdx ) ] = value; } +template< typename ValuesView, typename Indexer > +__cuda_callable__ +auto +TridiagonalMatrixRowView< ValuesView, Indexer >:: +begin() -> IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename ValuesView, typename Indexer > +__cuda_callable__ +auto +TridiagonalMatrixRowView< ValuesView, Indexer >:: +end() -> IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + +template< typename ValuesView, typename Indexer > +__cuda_callable__ +auto +TridiagonalMatrixRowView< ValuesView, Indexer >:: +cbegin() const -> const IteratorType +{ + return IteratorType( *this, 0 ); +} + +template< typename ValuesView, typename Indexer > +__cuda_callable__ +auto +TridiagonalMatrixRowView< ValuesView, Indexer >:: +cend() const -> const IteratorType +{ + return IteratorType( *this, this->getSize() ); +} + } // namespace Matrices } // namespace TNL diff --git a/src/TNL/Matrices/TridiagonalMatrixView.h b/src/TNL/Matrices/TridiagonalMatrixView.h index a078804f5..be2926934 100644 --- a/src/TNL/Matrices/TridiagonalMatrixView.h +++ b/src/TNL/Matrices/TridiagonalMatrixView.h @@ -79,7 +79,7 @@ class TridiagonalMatrixView : public MatrixView< Real, Device, Index > /** * \brief Type for accessing constant matrix rows. */ - using ConstRowView = typename RowView::ConstViewType; + using ConstRowView = typename RowView::ConstRowView; /** * \brief Helper type for getting self type or its modifications. diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h index 2ae7ffaf8..5efd3fa21 100644 --- a/src/UnitTests/Matrices/DenseMatrixTest.h +++ b/src/UnitTests/Matrices/DenseMatrixTest.h @@ -608,7 +608,7 @@ void test_SetRow() { 2, 3, 4, 5, 6 } }; auto row = matrix_view.getRow( rowIdx ); for( IndexType i = 0; i < 5; i++ ) - row.setElement( columnIndexes[ rowIdx ][ i ], values[ rowIdx ][ i ] ); + row.setValue( columnIndexes[ rowIdx ][ i ], values[ rowIdx ][ i ] ); }; TNL::Algorithms::ParallelFor< DeviceType >::exec( 0, 3, f ); @@ -725,7 +725,7 @@ void test_AddRow() auto row = matrix_view.getRow( rowIdx ); for( IndexType i = 0; i < 5; i++ ) { - RealType& val = row.getElement( i ); + RealType& val = row.getValue( i ); val = rowIdx * val + values[ rowIdx ][ i ]; } }; @@ -793,9 +793,8 @@ void test_ForElements() const IndexType rows = 8; Matrix m( rows, cols ); - m.forAllElements( [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, IndexType& columnIdx, RealType& value, bool compute ) mutable { + m.forAllElements( [] __cuda_callable__ ( IndexType rowIdx, IndexType localIdx, const IndexType& columnIdx, RealType& value, bool compute ) mutable { value = rowIdx + 1.0; - columnIdx = localIdx; } ); for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) @@ -815,14 +814,31 @@ void test_ForRows() const IndexType cols = 8; const IndexType rows = 8; + ///// + // Test without iterator Matrix m( rows, cols ); using RowView = typename Matrix::RowView; m.forAllRows( [] __cuda_callable__ ( RowView& row ) mutable { for( IndexType localIdx = 0; localIdx <= row.getRowIndex(); localIdx++ ) - { row.setValue( localIdx, row.getRowIndex() - localIdx + 1.0 ); - row.setColumnIndex( localIdx, localIdx ); + } ); + + for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) + for( IndexType colIdx = 0; colIdx < cols; colIdx++ ) + { + if( colIdx <= rowIdx ) + EXPECT_EQ( m.getElement( rowIdx, colIdx ), rowIdx - colIdx + 1.0 ); + else + EXPECT_EQ( m.getElement( rowIdx, colIdx ), 0.0 ); } + + ///// + // Test without iterator + m.getValues() = 0.0; + m.forAllRows( [] __cuda_callable__ ( RowView& row ) mutable { + for( auto element : row ) + if( element.columnIndex() <= element.rowIndex() ) + element.value() = element.rowIndex() - element.columnIndex() + 1.0; } ); for( IndexType rowIdx = 0; rowIdx < rows; rowIdx++ ) @@ -833,6 +849,8 @@ void test_ForRows() else EXPECT_EQ( m.getElement( rowIdx, colIdx ), 0.0 ); } + + } template< typename Matrix > @@ -1585,6 +1603,20 @@ TYPED_TEST( MatrixTest, addRowTest ) test_AddRow< MatrixType >(); } +TYPED_TEST( MatrixTest, forElementsTest ) +{ + using MatrixType = typename TestFixture::MatrixType; + + test_ForElements< MatrixType >(); +} + +TYPED_TEST( MatrixTest, forRowsTest ) +{ + using MatrixType = typename TestFixture::MatrixType; + + test_ForRows< MatrixType >(); +} + TYPED_TEST( MatrixTest, vectorProductTest ) { using MatrixType = typename TestFixture::MatrixType; diff --git a/src/UnitTests/Matrices/LambdaMatrixTest.hpp b/src/UnitTests/Matrices/LambdaMatrixTest.hpp index 4b2402463..ae435059d 100644 --- a/src/UnitTests/Matrices/LambdaMatrixTest.hpp +++ b/src/UnitTests/Matrices/LambdaMatrixTest.hpp @@ -203,13 +203,15 @@ void test_ForRows() MatrixType m( size, size, matrixElements, rowLengths ); + //// + // Test without iterator TNL::Matrices::DenseMatrix< RealType, DeviceType, IndexType > denseMatrix( size, size ); denseMatrix.setValue( 0.0 ); auto dense_view = denseMatrix.getView(); auto f = [=] __cuda_callable__ ( const typename MatrixType::RowView& row ) mutable { auto dense_row = dense_view.getRow( row.getRowIndex() ); for( IndexType localIdx = 0; localIdx < row.getSize(); localIdx++ ) - dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); + dense_row.setValue( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); }; m.forAllRows( f ); @@ -217,6 +219,20 @@ void test_ForRows() for( IndexType column = 0; column < size; column++ ) EXPECT_EQ( m.getElement( row, column ), denseMatrix.getElement( row, column ) ); + //// + // Test with iterator + denseMatrix.getValues() = 0.0; + auto f_iter = [=] __cuda_callable__ ( const typename MatrixType::RowView& row ) mutable { + auto dense_row = dense_view.getRow( row.getRowIndex() ); + for( const auto element : row ) + dense_row.setValue( element.columnIndex(), element.value() ); + }; + m.forAllRows( f_iter ); + + for( IndexType row = 0; row < size; row++ ) + for( IndexType column = 0; column < size; column++ ) + EXPECT_EQ( m.getElement( row, column ), denseMatrix.getElement( row, column ) ); + } template< typename Matrix > diff --git a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h index 7bc09cdc2..0f2a4a632 100644 --- a/src/UnitTests/Matrices/MultidiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/MultidiagonalMatrixTest.h @@ -798,6 +798,8 @@ void test_ForRows() const IndexType size( 5 ); Matrix m( size, size, { -1, 0, 1 } ); + ///// + // Test without iterator auto f = [=] __cuda_callable__ ( typename Matrix::RowView& row ) mutable { const IndexType rowIdx = row.getRowIndex(); if( rowIdx > 0 ) @@ -821,6 +823,37 @@ void test_ForRows() else EXPECT_EQ( m.getElement( row, column ), 0.0 ); } + + ///// + // Test with iterator + m.getValues() = 0.0; + auto f_iter = [=] __cuda_callable__ ( typename Matrix::RowView& row ) mutable { + for( auto element : row ) + { + if( element.rowIndex() > 0 && element.localIndex() == 0 ) + element.value() = -2.0; + if( element.localIndex() == 1 ) + element.value() = 1.0; + if( element.rowIndex() < size - 1 && element.localIndex() == 2 ) + element.value() = -2.0; + } + }; + m.forAllRows( f_iter ); + + for( IndexType row = 0; row < size; row++ ) + for( IndexType column = 0; column < size; column++ ) + { + const IndexType diff = row - column; + if( diff == 0 ) + EXPECT_EQ( m.getElement( row, column ), 1.0 ); + else if( diff == 1 && row > 0 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else if( diff == -1 && row < size - 1 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else + EXPECT_EQ( m.getElement( row, column ), 0.0 ); + } + } template< typename Matrix > diff --git a/src/UnitTests/Matrices/TridiagonalMatrixTest.h b/src/UnitTests/Matrices/TridiagonalMatrixTest.h index 47d8b91df..32cf143ad 100644 --- a/src/UnitTests/Matrices/TridiagonalMatrixTest.h +++ b/src/UnitTests/Matrices/TridiagonalMatrixTest.h @@ -749,6 +749,9 @@ void test_forRows() const IndexType size( 5 ); Matrix m( size, size ); + ///// + // Test without iterator + // auto f = [=] __cuda_callable__ ( typename Matrix::RowView& row ) mutable { const IndexType rowIdx = row.getRowIndex(); if( rowIdx > 0 ) @@ -759,6 +762,37 @@ void test_forRows() }; m.forAllRows( f ); + for( IndexType row = 0; row < size; row++ ) + for( IndexType column = 0; column < size; column++ ) + { + const IndexType diff = row - column; + if( diff == 0 ) + EXPECT_EQ( m.getElement( row, column ), 1.0 ); + else if( diff == 1 && row > 0 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else if( diff == -1 && row < size - 1 ) + EXPECT_EQ( m.getElement( row, column ), -2.0 ); + else + EXPECT_EQ( m.getElement( row, column ), 0.0 ); + } + + ///// + // Test with iterator + // + m.getValues() = 0.0; + auto f_iter = [=] __cuda_callable__ ( typename Matrix::RowView& row ) mutable { + for( auto element : row ) + { + if( element.rowIndex() > 0 && element.localIndex() == 0 ) + element.value() = -2.0; + if( element.localIndex() == 1 ) + element.value() = 1.0; + if( element.rowIndex() < size - 1 && element.localIndex() == 2 ) + element.value() = -2.0; + } + }; + m.forAllRows( f_iter ); + for( IndexType row = 0; row < size; row++ ) for( IndexType column = 0; column < size; column++ ) { -- GitLab From ba0fc548e9980fb99fa36e7ba2d71227e2bbeb10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 23 Mar 2021 15:54:49 +0100 Subject: [PATCH 52/54] Renaming SparseMatrixRowViewIterator to MatrixRowViewIterator and fixing DenseMatrixElement. --- src/TNL/Matrices/DenseMatrixElement.h | 2 +- src/TNL/Matrices/DenseMatrixRowView.h | 4 ++-- ...ViewIterator.h => MatrixRowViewIterator.h} | 16 ++++++------- ...Iterator.hpp => MatrixRowViewIterator.hpp} | 24 +++++++++---------- src/TNL/Matrices/MultidiagonalMatrixRowView.h | 4 ++-- src/TNL/Matrices/SparseMatrixRowView.h | 4 ++-- src/TNL/Matrices/TridiagonalMatrixRowView.h | 4 ++-- src/UnitTests/Matrices/DenseMatrixTest.h | 2 -- 8 files changed, 29 insertions(+), 31 deletions(-) rename src/TNL/Matrices/{SparseMatrixRowViewIterator.h => MatrixRowViewIterator.h} (83%) rename src/TNL/Matrices/{SparseMatrixRowViewIterator.hpp => MatrixRowViewIterator.hpp} (77%) diff --git a/src/TNL/Matrices/DenseMatrixElement.h b/src/TNL/Matrices/DenseMatrixElement.h index c2c1c9c34..e35235fd9 100644 --- a/src/TNL/Matrices/DenseMatrixElement.h +++ b/src/TNL/Matrices/DenseMatrixElement.h @@ -56,7 +56,7 @@ class DenseMatrixElement const IndexType& rowIdx; - const IndexType& columnIdx; + const IndexType columnIdx; }; } // namespace Matrices diff --git a/src/TNL/Matrices/DenseMatrixRowView.h b/src/TNL/Matrices/DenseMatrixRowView.h index 113f8aa74..e280b20b4 100644 --- a/src/TNL/Matrices/DenseMatrixRowView.h +++ b/src/TNL/Matrices/DenseMatrixRowView.h @@ -11,7 +11,7 @@ #pragma once #include -#include +#include #include namespace TNL { @@ -84,7 +84,7 @@ class DenseMatrixRowView /** * \brief Type of iterator for the matrix row. */ - using IteratorType = SparseMatrixRowViewIterator< RowView >; + using IteratorType = MatrixRowViewIterator< RowView >; /** * \brief Constructor with \e segmentView and \e values diff --git a/src/TNL/Matrices/SparseMatrixRowViewIterator.h b/src/TNL/Matrices/MatrixRowViewIterator.h similarity index 83% rename from src/TNL/Matrices/SparseMatrixRowViewIterator.h rename to src/TNL/Matrices/MatrixRowViewIterator.h index dc7ef5069..cf99bea29 100644 --- a/src/TNL/Matrices/SparseMatrixRowViewIterator.h +++ b/src/TNL/Matrices/MatrixRowViewIterator.h @@ -1,5 +1,5 @@ /*************************************************************************** - SparseMatrixRowViewIterator.h - description + MatrixRowViewIterator.h - description ------------------- begin : Mar 20, 2021 copyright : (C) 2021 by Tomas Oberhuber @@ -19,7 +19,7 @@ namespace TNL { namespace Matrices { template< typename RowView > -class SparseMatrixRowViewIterator +class MatrixRowViewIterator { public: @@ -51,7 +51,7 @@ class SparseMatrixRowViewIterator static constexpr bool isBinary() { return RowViewType::isBinary(); }; __cuda_callable__ - SparseMatrixRowViewIterator( RowViewType& rowView, + MatrixRowViewIterator( RowViewType& rowView, const IndexType& localIdx ); /** @@ -61,7 +61,7 @@ class SparseMatrixRowViewIterator * \return \e true if both iterators points at the same point of the same matrix, \e false otherwise. */ __cuda_callable__ - bool operator==( const SparseMatrixRowViewIterator& other ) const; + bool operator==( const MatrixRowViewIterator& other ) const; /** * \brief Comparison of two matrix row iterators. @@ -70,13 +70,13 @@ class SparseMatrixRowViewIterator * \return \e false if both iterators points at the same point of the same matrix, \e true otherwise. */ __cuda_callable__ - bool operator!=( const SparseMatrixRowViewIterator& other ) const; + bool operator!=( const MatrixRowViewIterator& other ) const; __cuda_callable__ - SparseMatrixRowViewIterator& operator++(); + MatrixRowViewIterator& operator++(); __cuda_callable__ - SparseMatrixRowViewIterator& operator--(); + MatrixRowViewIterator& operator--(); __cuda_callable__ MatrixElementType operator*(); @@ -95,4 +95,4 @@ class SparseMatrixRowViewIterator } // namespace Matrices } // namespace TNL -#include +#include diff --git a/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp b/src/TNL/Matrices/MatrixRowViewIterator.hpp similarity index 77% rename from src/TNL/Matrices/SparseMatrixRowViewIterator.hpp rename to src/TNL/Matrices/MatrixRowViewIterator.hpp index 313e2fbf1..7b233e47b 100644 --- a/src/TNL/Matrices/SparseMatrixRowViewIterator.hpp +++ b/src/TNL/Matrices/MatrixRowViewIterator.hpp @@ -18,8 +18,8 @@ namespace Matrices { template< typename RowView > __cuda_callable__ -SparseMatrixRowViewIterator< RowView >:: -SparseMatrixRowViewIterator( RowViewType& rowView, +MatrixRowViewIterator< RowView >:: +MatrixRowViewIterator( RowViewType& rowView, const IndexType& localIdx ) : rowView( rowView ), localIdx( localIdx ) { @@ -27,8 +27,8 @@ SparseMatrixRowViewIterator( RowViewType& rowView, template< typename RowView > __cuda_callable__ bool -SparseMatrixRowViewIterator< RowView >:: -operator==( const SparseMatrixRowViewIterator& other ) const +MatrixRowViewIterator< RowView >:: +operator==( const MatrixRowViewIterator& other ) const { if( &this->rowView == &other.rowView && localIdx == other.localIdx ) @@ -38,16 +38,16 @@ operator==( const SparseMatrixRowViewIterator& other ) const template< typename RowView > __cuda_callable__ bool -SparseMatrixRowViewIterator< RowView >:: -operator!=( const SparseMatrixRowViewIterator& other ) const +MatrixRowViewIterator< RowView >:: +operator!=( const MatrixRowViewIterator& other ) const { return ! ( other == *this ); } template< typename RowView > __cuda_callable__ -SparseMatrixRowViewIterator< RowView >& -SparseMatrixRowViewIterator< RowView >:: +MatrixRowViewIterator< RowView >& +MatrixRowViewIterator< RowView >:: operator++() { if( localIdx < rowView.getSize() ) @@ -57,8 +57,8 @@ operator++() template< typename RowView > __cuda_callable__ -SparseMatrixRowViewIterator< RowView >& -SparseMatrixRowViewIterator< RowView >:: +MatrixRowViewIterator< RowView >& +MatrixRowViewIterator< RowView >:: operator--() { if( localIdx > 0 ) @@ -68,7 +68,7 @@ operator--() template< typename RowView > __cuda_callable__ auto -SparseMatrixRowViewIterator< RowView >:: +MatrixRowViewIterator< RowView >:: operator*() -> MatrixElementType { return MatrixElementType( @@ -80,7 +80,7 @@ operator*() -> MatrixElementType template< typename RowView > __cuda_callable__ auto -SparseMatrixRowViewIterator< RowView >:: +MatrixRowViewIterator< RowView >:: operator*() const -> const MatrixElementType { return MatrixElementType( diff --git a/src/TNL/Matrices/MultidiagonalMatrixRowView.h b/src/TNL/Matrices/MultidiagonalMatrixRowView.h index bd01f53a7..dcfcfe55c 100644 --- a/src/TNL/Matrices/MultidiagonalMatrixRowView.h +++ b/src/TNL/Matrices/MultidiagonalMatrixRowView.h @@ -11,7 +11,7 @@ #pragma once #include -#include +#include namespace TNL { namespace Matrices { @@ -104,7 +104,7 @@ class MultidiagonalMatrixRowView /** * \brief Type of iterator for the matrix row. */ - using IteratorType = SparseMatrixRowViewIterator< RowView >; + using IteratorType = MatrixRowViewIterator< RowView >; /** * \brief Constructor with all necessary data. diff --git a/src/TNL/Matrices/SparseMatrixRowView.h b/src/TNL/Matrices/SparseMatrixRowView.h index 338ac390c..4976a420e 100644 --- a/src/TNL/Matrices/SparseMatrixRowView.h +++ b/src/TNL/Matrices/SparseMatrixRowView.h @@ -13,7 +13,7 @@ #include #include -#include +#include namespace TNL { namespace Matrices { @@ -99,7 +99,7 @@ class SparseMatrixRowView /** * \brief Type of iterator for the matrix row. */ - using IteratorType = SparseMatrixRowViewIterator< RowView >; + using IteratorType = MatrixRowViewIterator< RowView >; /** * \brief Tells whether the parent matrix is a binary matrix. diff --git a/src/TNL/Matrices/TridiagonalMatrixRowView.h b/src/TNL/Matrices/TridiagonalMatrixRowView.h index 347e591d7..dd60f9073 100644 --- a/src/TNL/Matrices/TridiagonalMatrixRowView.h +++ b/src/TNL/Matrices/TridiagonalMatrixRowView.h @@ -10,7 +10,7 @@ #pragma once -#include +#include #include namespace TNL { @@ -90,7 +90,7 @@ class TridiagonalMatrixRowView /** * \brief Type of iterator for the matrix row. */ - using IteratorType = SparseMatrixRowViewIterator< RowView >; + using IteratorType = MatrixRowViewIterator< RowView >; /** * \brief Constructor with all necessary data. diff --git a/src/UnitTests/Matrices/DenseMatrixTest.h b/src/UnitTests/Matrices/DenseMatrixTest.h index 5efd3fa21..c6dfa3842 100644 --- a/src/UnitTests/Matrices/DenseMatrixTest.h +++ b/src/UnitTests/Matrices/DenseMatrixTest.h @@ -36,8 +36,6 @@ static const char* TEST_FILE_NAME = "test_DenseMatrixTest.tnl"; void test_GetSerializationType() { using namespace TNL::Algorithms::Segments; - std::cerr << TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() << std::endl; - std::cerr << TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, ColumnMajorOrder >::getSerializationType() << std::endl; EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, RowMajorOrder >" ) ); EXPECT_EQ( ( TNL::Matrices::DenseMatrix< int, TNL::Devices::Host, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< int, [any_device], int, RowMajorOrder >" ) ); EXPECT_EQ( ( TNL::Matrices::DenseMatrix< float, TNL::Devices::Cuda, int, RowMajorOrder >::getSerializationType() ), TNL::String( "Matrices::DenseMatrix< float, [any_device], int, RowMajorOrder >" ) ); -- GitLab From 47f892d653d69595a0164efe974bd8315151b2fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 23 Mar 2021 18:58:36 +0100 Subject: [PATCH 53/54] Fix after renaming DenseMatrixRowView::setElement to DenseMatrixRowView::setValue. --- .../Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp | 2 +- .../Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp | 2 +- .../Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp | 2 +- .../Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp | 2 +- .../Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp | 2 +- .../Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp | 2 +- .../Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp | 2 +- .../Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp | 2 +- Documentation/Tutorials/Matrices/DenseMatrixSetup_Benchmark.cpp | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp index 932802de9..3a1a87c47 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp @@ -13,7 +13,7 @@ void forRowsExample() auto f = [=] __cuda_callable__ ( RowView& row ) mutable { const int& rowIdx = row.getRowIndex(); - row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); + row.setValue( rowIdx, 10 * ( rowIdx + 1 ) ); }; /*** diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp index c61a1c822..2e05b1678 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getConstRow.cpp @@ -23,7 +23,7 @@ void getRowExample() */ auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { auto row = matrix->getRow( rowIdx ); - return row.getElement( rowIdx ); + return row.getValue( rowIdx ); }; /*** diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp index 0cf1e1d76..615289066 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_getRow.cpp @@ -13,7 +13,7 @@ void getRowExample() auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { auto row = matrix->getRow( rowIdx ); - row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); + row.setValue( rowIdx, 10 * ( rowIdx + 1 ) ); }; /*** diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp index 255d59096..05c8022d8 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp @@ -14,7 +14,7 @@ void forRowsExample() auto f = [=] __cuda_callable__ ( RowView& row ) mutable { const int& rowIdx = row.getRowIndex(); - row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); + row.setValue( rowIdx, 10 * ( rowIdx + 1 ) ); }; /*** diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp index a0b998024..5fc1195ec 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getConstRow.cpp @@ -26,7 +26,7 @@ void getRowExample() */ auto fetch = [=] __cuda_callable__ ( int rowIdx ) mutable -> double { auto row = matrixView.getRow( rowIdx ); - return row.getElement( rowIdx ); + return row.getValue( rowIdx ); }; int trace = TNL::Algorithms::Reduction< Device >::reduce( 0, matrix.getRows(), fetch, std::plus<>{}, 0 ); diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp index 8142599c1..9c78e9cc9 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp @@ -17,7 +17,7 @@ void getRowExample() auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { auto row = matrixView.getRow( rowIdx ); - row.setElement( rowIdx, 10 * ( rowIdx + 1 ) ); + row.setValue( rowIdx, 10 * ( rowIdx + 1 ) ); }; /*** diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp index a4ef60f96..1430ff413 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp @@ -69,7 +69,7 @@ void forRowsExample() auto f = [=] __cuda_callable__ ( const typename MatrixType::RowView& row ) mutable { auto dense_row = dense_view.getRow( row.getRowIndex() ); for( int localIdx = 0; localIdx < row.getSize(); localIdx++ ) - dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); + dense_row.setValue( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); }; matrix.forAllRows( f ); diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp index 54e4e47e8..e5e369089 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_getRow.cpp @@ -70,7 +70,7 @@ void getRowExample() auto row = matrix.getRow( rowIdx ); auto dense_row = dense_view.getRow( rowIdx ); for( int localIdx = 0; localIdx < row.getSize(); localIdx++ ) - dense_row.setElement( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); + dense_row.setValue( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); }; TNL::Algorithms::ParallelFor< Device >::exec( 0, matrixSize, f ); diff --git a/Documentation/Tutorials/Matrices/DenseMatrixSetup_Benchmark.cpp b/Documentation/Tutorials/Matrices/DenseMatrixSetup_Benchmark.cpp index 9b346d7be..7696e9d0d 100644 --- a/Documentation/Tutorials/Matrices/DenseMatrixSetup_Benchmark.cpp +++ b/Documentation/Tutorials/Matrices/DenseMatrixSetup_Benchmark.cpp @@ -52,7 +52,7 @@ void getRow( const int matrixSize, Matrix& matrix ) auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { auto row = matrixView.getRow( rowIdx ); for( int i = 0; i < matrixSize; i++ ) - row.setElement( i, rowIdx + i ); + row.setValue( i, rowIdx + i ); }; TNL::Algorithms::ParallelFor< typename Matrix::DeviceType >::exec( 0, matrixSize, f ); } -- GitLab From 4f594e307491a447e0f9642d548bd17f4cebd511 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Sun, 28 Mar 2021 14:17:12 +0200 Subject: [PATCH 54/54] Update of matrix forRows examples. --- .../DenseMatrixExample_forRows.cpp | 25 ++++++++++-- .../DenseMatrixViewExample_forElements.cpp | 2 +- .../DenseMatrixViewExample_forRows.cpp | 25 ++++++++++-- .../DenseMatrixViewExample_getRow.cpp | 9 ++++- .../LambdaMatrixExample_forRows.cpp | 17 ++++++++- .../MultidiagonalMatrixExample_forRows.cpp | 23 +++++++++-- .../SparseMatrixExample_forRows.cpp | 29 ++++++++++---- .../SparseMatrixViewExample_getRow.cpp | 17 ++++----- .../TridiagonalMatrixExample_forRows.cpp | 18 ++++++++- .../TridiagonalMatrixViewExample_getRow.cpp | 16 +++++++- .../Tutorials/Matrices/tutorial_Matrices.md | 38 +++++++++++++------ 11 files changed, 170 insertions(+), 49 deletions(-) diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp index 3a1a87c47..ea8c88484 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixExample_forRows.cpp @@ -9,17 +9,34 @@ void forRowsExample() { using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; using RowView = typename MatrixType::RowView; - MatrixType matrix( 5, 5 ); + const int size = 5; + MatrixType matrix( size, size ); + /*** + * Set the matrix elements. + */ auto f = [=] __cuda_callable__ ( RowView& row ) mutable { const int& rowIdx = row.getRowIndex(); - row.setValue( rowIdx, 10 * ( rowIdx + 1 ) ); + if( rowIdx > 0 ) + row.setValue( rowIdx - 1, -1.0 ); + row.setValue( rowIdx, rowIdx + 1.0 ); + if( rowIdx < size - 1 ) + row.setValue( rowIdx + 1, -1.0 ); }; + matrix.forAllRows( f ); + std::cout << matrix << std::endl; /*** - * Set the matrix elements. + * Now divide each matrix row by its largest element with use of iterators. */ - matrix.forAllRows( f ); + matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable { + double largest = std::numeric_limits< double >::lowest(); + for( auto element : row ) + largest = TNL::max( largest, element.value() ); + for( auto element : row ) + element.value() /= largest; + } ); + std::cout << "Divide each matrix row by its largest element... " << std::endl; std::cout << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forElements.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forElements.cpp index cdc9fac58..6a980d23c 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forElements.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forElements.cpp @@ -10,7 +10,7 @@ void forElementsExample() auto matrixView = matrix.getView(); auto f = [=] __cuda_callable__ ( int rowIdx, int columnIdx, int globalIdx, double& value, bool& compute ) { - if( rowIdx < columnIdx ) + if( columnIdx > rowIdx ) compute = false; else value = rowIdx + columnIdx; diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp index 05c8022d8..06d6c8553 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_forRows.cpp @@ -9,18 +9,35 @@ void forRowsExample() { using MatrixType = TNL::Matrices::DenseMatrix< double, Device >; using RowView = typename MatrixType::RowView; - MatrixType matrix( 5, 5 ); + const int size = 5; + MatrixType matrix( size, size ); auto view = matrix.getView(); + /*** + * Set the matrix elements. + */ auto f = [=] __cuda_callable__ ( RowView& row ) mutable { const int& rowIdx = row.getRowIndex(); - row.setValue( rowIdx, 10 * ( rowIdx + 1 ) ); + if( rowIdx > 0 ) + row.setValue( rowIdx - 1, -1.0 ); + row.setValue( rowIdx, rowIdx + 1.0 ); + if( rowIdx < size - 1 ) + row.setValue( rowIdx + 1, -1.0 ); }; + view.forAllRows( f ); + std::cout << matrix << std::endl; /*** - * Set the matrix elements. + * Now divide each matrix row by its largest element - with the use of iterators. */ - view.forAllRows( f ); + view.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable { + double largest = std::numeric_limits< double >::lowest(); + for( auto element : row ) + largest = TNL::max( largest, element.value() ); + for( auto element : row ) + element.value() /= largest; + } ); + std::cout << "Divide each matrix row by its largest element... " << std::endl; std::cout << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp index 9c78e9cc9..0afd84217 100644 --- a/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/DenseMatrix/DenseMatrixViewExample_getRow.cpp @@ -7,7 +7,8 @@ template< typename Device > void getRowExample() { - TNL::Matrices::DenseMatrix< double, Device > matrix( 5, 5 ); + const int size = 5; + TNL::Matrices::DenseMatrix< double, Device > matrix( size, size ); /*** * Create dense matrix view which can be captured by the following lambda @@ -17,7 +18,11 @@ void getRowExample() auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { auto row = matrixView.getRow( rowIdx ); - row.setValue( rowIdx, 10 * ( rowIdx + 1 ) ); + if( rowIdx > 0 ) + row.setValue( rowIdx - 1, -1.0 ); + row.setValue( rowIdx, rowIdx + 1.0 ); + if( rowIdx < size - 1 ) + row.setValue( rowIdx + 1, -1.0 ); }; /*** diff --git a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp index 1430ff413..f8d826710 100644 --- a/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/LambdaMatrix/LambdaMatrixExample_forRows.cpp @@ -62,11 +62,12 @@ void forRowsExample() auto matrix = TNL::Matrices::LambdaMatrixFactory< double, Device, int >::create( matrixSize, matrixSize, matrixElements, rowLengths ); using MatrixType = decltype( matrix ); + using RowView = typename MatrixType::RowView; TNL::Matrices::DenseMatrix< double, Device > denseMatrix( matrixSize, matrixSize ); denseMatrix.setValue( 0.0 ); auto dense_view = denseMatrix.getView(); - auto f = [=] __cuda_callable__ ( const typename MatrixType::RowView& row ) mutable { + auto f = [=] __cuda_callable__ ( const RowView& row ) mutable { auto dense_row = dense_view.getRow( row.getRowIndex() ); for( int localIdx = 0; localIdx < row.getSize(); localIdx++ ) dense_row.setValue( row.getColumnIndex( localIdx ), row.getValue( localIdx ) ); @@ -75,6 +76,20 @@ void forRowsExample() std::cout << "Laplace operator lambda matrix: " << std::endl << matrix << std::endl; std::cout << "Laplace operator dense matrix: " << std::endl << denseMatrix << std::endl; + + /*** + * Compute sum of elements in each row and store it into a vector. + */ + TNL::Containers::Vector< double, Device > sum_vector( matrixSize ); + auto sum_view = sum_vector.getView(); + matrix.forAllRows( [=] __cuda_callable__ ( const RowView& row ) mutable { + double sum( 0.0 ); + for( auto element : row ) + sum += TNL::abs( element.value() ); + sum_view[ row.getRowIndex() ] = sum; + } ); + + std::cout << "Sums in matrix rows = " << sum_vector << std::endl; } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp index 8629f7128..9ff377445 100644 --- a/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/MultidiagonalMatrix/MultidiagonalMatrixExample_forRows.cpp @@ -7,15 +7,16 @@ template< typename Device > void forRowsExample() { using MatrixType = TNL::Matrices::MultidiagonalMatrix< double, Device >; + using RowView = typename MatrixType::RowView; /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): * - * 0 / 2 . . . . \ -> { 0, 0, 1 } + * 0 / 2 -1. . . . \ -> { 0, 0, 1 } * | -1 2 -1 . . | -> { 0, 2, 1 } * | . -1 2 -1. . | -> { 3, 2, 1 } * | . . -1 2 -1 | -> { 3, 2, 1 } - * \ . . . . 2 / -> { 3, 2, 1 } + * \ . . . -1. 2 / -> { 3, 2, 1 } * * The diagonals offsets are { -1, 0, 1 }. */ @@ -25,17 +26,31 @@ void forRowsExample() size, // number of matrix columns { -1, -0, 1 } ); // matrix diagonals offsets - auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) { + auto f = [=] __cuda_callable__ ( RowView& row ) { const int& rowIdx = row.getRowIndex(); if( rowIdx > 0 ) row.setElement( 0, -1.0 ); // elements below the diagonal row.setElement( 1, 2.0 ); // elements on the diagonal - if( rowIdx < size - 1 ) // elements above the diagonal + if( rowIdx < size - 1 ) // elements above the diagonal row.setElement( 2, -1.0 ); }; matrix.forAllRows( f ); std::cout << matrix << std::endl; + + /*** + * Compute sum of elements in each row and store it into a vector. + */ + TNL::Containers::Vector< double, Device > sum_vector( size ); + auto sum_view = sum_vector.getView(); + matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable { + double sum( 0.0 ); + for( auto element : row ) + sum += TNL::abs( element.value() ); + sum_view[ row.getRowIndex() ] = sum; + } ); + + std::cout << "Sums in matrix rows = " << sum_vector << std::endl; } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp index 217222112..b1201e59b 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixExample_forRows.cpp @@ -11,16 +11,20 @@ void forRowsExample() * Set the following matrix (dots represent zero matrix elements): * * / 2 . . . . \ - * | -1 2 -1 . . | - * | . -1 2 -1. . | - * | . . -1 2 -1 | + * | 1 2 1 . . | + * | . 1 2 1. . | + * | . . 1 2 1 | * \ . . . . 2 / */ const int size( 5 ); using MatrixType = TNL::Matrices::SparseMatrix< double, Device >; MatrixType matrix( { 1, 3, 3, 3, 1 }, size ); + using RowView = typename MatrixType::RowView; - auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) mutable { + /*** + * Set the matrix elements. + */ + auto f = [=] __cuda_callable__ ( RowView& row ) mutable { const int rowIdx = row.getRowIndex(); if( rowIdx == 0 ) row.setElement( 0, rowIdx, 2.0 ); // diagonal element @@ -28,16 +32,25 @@ void forRowsExample() row.setElement( 0, rowIdx, 2.0 ); // diagonal element else { - row.setElement( 0, rowIdx - 1, -1.0 ); // elements below the diagonal + row.setElement( 0, rowIdx - 1, 1.0 ); // elements below the diagonal row.setElement( 1, rowIdx, 2.0 ); // diagonal element - row.setElement( 2, rowIdx + 1, -1.0 ); // elements above the diagonal + row.setElement( 2, rowIdx + 1, 1.0 ); // elements above the diagonal } }; + matrix.forAllRows( f ); + std::cout << matrix << std::endl; /*** - * Set the matrix elements. + * Divide each matrix row by a sum of all elements in the row - with use of iterators. */ - matrix.forAllRows( f ); + matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable { + double sum( 0.0 ); + for( auto element : row ) + sum += element.value(); + for( auto element: row ) + element.value() /= sum; + } ); + std::cout << "Divide each matrix row by a sum of all elements in the row ... " << std::endl; std::cout << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp index 275de124e..3741b09f0 100644 --- a/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp @@ -12,15 +12,18 @@ void getRowExample() * Set the following matrix (dots represent zero matrix elements): * * / 2 . . . . \ - * | -1 2 -1 . . | - * | . -1 2 -1. . | - * | . . -1 2 -1 | + * | 1 2 1 . . | + * | . 1 2 1. . | + * | . . 1 2 1 | * \ . . . . 2 / */ const int size = 5; TNL::Matrices::SparseMatrix< double, Device > matrix( { 1, 3, 3, 3, 1 }, size ); auto view = matrix.getView(); + /*** + * Set the matrix elements. + */ auto f = [=] __cuda_callable__ ( int rowIdx ) mutable { auto row = view.getRow( rowIdx ); if( rowIdx == 0 ) @@ -29,15 +32,11 @@ void getRowExample() row.setElement( 0, rowIdx, 2.0 ); // diagonal element else { - row.setElement( 0, rowIdx - 1, -1.0 ); // elements below the diagonal + row.setElement( 0, rowIdx - 1, 1.0 ); // elements below the diagonal row.setElement( 1, rowIdx, 2.0 ); // diagonal element - row.setElement( 2, rowIdx + 1, -1.0 ); // elements above the diagonal + row.setElement( 2, rowIdx + 1, 1.0 ); // elements above the diagonal } }; - - /*** - * Set the matrix elements. - */ TNL::Algorithms::ParallelFor< Device >::exec( 0, matrix.getRows(), f ); std::cout << matrix << std::endl; } diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp index 5607fac63..9ae4f3264 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixExample_forRows.cpp @@ -7,6 +7,7 @@ template< typename Device > void forRowsExample() { using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; + using RowView = typename MatrixType::RowView; /*** * Set the following matrix (dots represent zero matrix elements and zeros are * padding zeros for memory alignment): @@ -17,12 +18,11 @@ void forRowsExample() * | . . -1 2 -1 | -> { 3, 2, 1 } * \ . . . . 2 / -> { 3, 2, 1 } * - * The diagonals offsets are { -1, 0, 1 }. */ const int size = 5; MatrixType matrix( size, size ); - auto f = [=] __cuda_callable__ ( typename MatrixType::RowView& row ) { + auto f = [=] __cuda_callable__ ( RowView& row ) { const int& rowIdx = row.getRowIndex(); if( rowIdx > 0 ) row.setElement( 0, -1.0 ); // elements below the diagonal @@ -32,6 +32,20 @@ void forRowsExample() }; matrix.forAllRows( f ); std::cout << matrix << std::endl; + + /*** + * Compute sum of elements in each row and store it into a vector. + */ + TNL::Containers::Vector< double, Device > sum_vector( size ); + auto sum_view = sum_vector.getView(); + matrix.forAllRows( [=] __cuda_callable__ ( RowView& row ) mutable { + double sum( 0.0 ); + for( auto element : row ) + sum += TNL::abs( element.value() ); + sum_view[ row.getRowIndex() ] = sum; + } ); + + std::cout << "Sums in matrix rows = " << sum_vector << std::endl; } int main( int argc, char* argv[] ) diff --git a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp index 641149e05..8f8fe0b69 100644 --- a/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp +++ b/Documentation/Examples/Matrices/TridiagonalMatrix/TridiagonalMatrixViewExample_getRow.cpp @@ -7,11 +7,23 @@ template< typename Device > void getRowExample() { + /*** + * Set the following matrix (dots represent zero matrix elements and zeros are + * padding zeros for memory alignment): + * + * 0 / 2 -1 . . . \ -> { 0, 0, 1 } + * | -1 2 -1 . . | -> { 0, 2, 1 } + * | . -1 2 -1. . | -> { 3, 2, 1 } + * | . . -1 2 -1 | -> { 3, 2, 1 } + * \ . . . -1 2 / -> { 3, 2, 1 } + * + */ + const int matrixSize( 5 ); using MatrixType = TNL::Matrices::TridiagonalMatrix< double, Device >; MatrixType matrix( matrixSize, // number of matrix rows - matrixSize // number of matrix columns + matrixSize // number of matrix columns ); auto view = matrix.getView(); @@ -19,7 +31,7 @@ void getRowExample() auto row = view.getRow( rowIdx ); if( rowIdx > 0 ) - row.setElement( 0, -1.0 ); // elements below the diagonal + row.setElement( 0, -1.0 ); // elements below the diagonal row.setElement( 1, 2.0 ); // elements on the diagonal if( rowIdx < matrixSize - 1 ) // elements above the diagonal row.setElement( 2, -1.0 ); diff --git a/Documentation/Tutorials/Matrices/tutorial_Matrices.md b/Documentation/Tutorials/Matrices/tutorial_Matrices.md index ae21857dd..5c60bece9 100644 --- a/Documentation/Tutorials/Matrices/tutorial_Matrices.md +++ b/Documentation/Tutorials/Matrices/tutorial_Matrices.md @@ -395,7 +395,7 @@ Here we show an example: \includelineno DenseMatrixViewExample_getRow.cpp -Here we create the matrix on the line 10 and get the matrix view on the line 16. Next we use `ParallelFor` (\ref TNL::Algorithms::ParallelFor) (line 26) to iterate over the matrix rows and the lambda function `f` (lines 18-21) for each of them. In the lambda function, we first fetch the matrix row by means of the method `getRow` (\ref TNL::Matrices::DenseMatrixView::getRow) and next we set the matrix elements by using the method `setElement` of the matrix row (\ref TNL::Matrices::DenseMatrixRowView::setElement). For the compatibility with the sparse matrices, use the variant of `setElement` with the parameter `localIdx`. It has no effect here, it is only for compatibility of the interface. +Here we create the matrix on the line 10 and get the matrix view on the line 16. Next we use `ParallelFor` (\ref TNL::Algorithms::ParallelFor) (line 31) to iterate over the matrix rows and call the lambda function `f` (lines 19-26) for each of them. In the lambda function, we first fetch the matrix row by means of the method `getRow` (\ref TNL::Matrices::DenseMatrixView::getRow) and next we set the matrix elements by using the method `setElement` of the matrix row (\ref TNL::Matrices::DenseMatrixRowView::setElement). For the compatibility with the sparse matrices, use the variant of `setElement` with the parameter `localIdx`. It has no effect here, it is only for compatibility of the interface. The result looks as follows: @@ -405,13 +405,15 @@ The result looks as follows: This method iterates in parallel over all matrix rows. In fact, it combines \ref TNL::Algorithms::ParallelFor and \ref TNL::Matrices:::DenseMatrix::getRow method in one. See the following example. It is even a bit simpler compared to the previous one: -\includelineno DenseMatrixViewExample_forRows.cpp +\includelineno DenseMatrixExample_forRows.cpp -The lambda function, which is called for each matrix row (lines 15-17), have to accept parameter `row` with type `RowView`. This type is defined inside each TNL matrix and in the case of the dense matrix, it is \ref TNL::Matrices::DenseMatrixRowView. We use the method \ref TNL::Matrices::DenseMatrixRowView::getRowIndex to get the index of the matrix row being currently processed and method \ref TNL::Matrices::DenseMatrixRowView::setElement which sets the value of the element with given column index (the first parameter). +The lambda function `f`, which is called for each matrix row (lines 18-25), have to accept parameter `row` with type `RowView`. This type is defined inside each TNL matrix and in the case of the dense matrix, it is \ref TNL::Matrices::DenseMatrixRowView. We use the method \ref TNL::Matrices::DenseMatrixRowView::getRowIndex to get the index of the matrix row being currently processed and method \ref TNL::Matrices::DenseMatrixRowView::setElement which sets the value of the element with given column index (the first parameter). + +Next, on the lines 32-38, we call another lambda function which firstly find the largest element in each row (lines 33-35) and then it divides the matrix row by its value (lines 36-37). The result looks as follows: -\include DenseMatrixViewExample_forRows.out +\include DenseMatrixExample_forRows.out #### Method `forElements` @@ -571,7 +573,7 @@ The result looks as follows: More efficient method, especially for GPUs, is to combine `getRow` (\ref TNL::Matrices::SparseMatrix::getRow) method with `ParallelFor` (\ref TNL::Algorithms::ParallelFor) and lambda function as the following example demonstrates: -\includelineno SparseMatrixViewExample_getRow.cpp +\includelineno SparseMatrixExample_getRow.cpp On the line 21, we create small matrix having five rows (number of rows is given by the size of the [initializer list](https://en.cppreference.com/w/cpp/utility/initializer_list) ) and columns (number of columns is given by the second parameter) and we set each row capacity to one or three (particular elements of the initializer list). On the line 41, we call `ParallelFor` (\ref TNL::Algorithms::ParallelFor) to iterate over all matrix rows. Each row is processed by the lambda function `f` (lines 24-36). In the lambda function, we first fetch a sparse matrix row (\ref TNL::Matrices::SparseMatrixRowView) (line 25) which serves for accessing particular matrix elements in the matrix row. This object has a method `setElement` (\ref TNL::Matrices::SparseMatrixRowView::setElement) accepting three parameters: @@ -581,7 +583,7 @@ On the line 21, we create small matrix having five rows (number of rows is given The result looks as follows: -\include SparseMatrixViewExample_getRow.out +\include SparseMatrixExample_getRow.out #### Method `forRows` @@ -593,9 +595,11 @@ The differences are: 1. We do not need to get the matrix view as we did in the previous example. 2. We call the method `forAllRows` (\ref TNL::Matrices::SparseMatrix::forAllRows) instead of `ParallelFor` (\ref TNL::Algorithms::ParallelFor) which is simpler since we do not have to state the device type explicitly. The method `forAllRows` calls the method `forRows` for all matrix rows so we do not have to state explicitly the interval of matrix rows neither. -3. The lambda function `f` (lines 23-35) accepts one parameter `row` of the type `RowView` (\ref TNL::Matrices::SparseMatrix::RowView which is \ref TNL::Matrices::SparseMatrixRowView) instead of the index of the matrix row. Therefore we do not need to call the method `getRow` (\ref TNL::Matrices::SparseMatrix::getRow). On the other hand, we need the method `geRowIndex` (\ref TNL::Matrices::SparseMatrixRowView::getRowIndex) to get the index of the matrix row (line 24). +3. The lambda function `f` (lines 27-39) accepts one parameter `row` of the type `RowView` (\ref TNL::Matrices::SparseMatrix::RowView which is \ref TNL::Matrices::SparseMatrixRowView) instead of the index of the matrix row. Therefore we do not need to call the method `getRow` (\ref TNL::Matrices::SparseMatrix::getRow). On the other hand, we need the method `geRowIndex` (\ref TNL::Matrices::SparseMatrixRowView::getRowIndex) to get the index of the matrix row (line 28). + +On the lines 46-52, we call a lambda function which computes sum of all elements in a row (lines 47-49) and it divides the row by the `sum` then (lines 50-51). -The rest of the lambda function remains the same. The result looks as follows: + The result looks as follows: \include SparseMatrixExample_forRows.out @@ -853,7 +857,7 @@ The result looks as follows: \includelineno TridiagonalMatrixViewExample_getRow.cpp -We create a matrix with the same size (line 10-15). Next, we fetch the tridiagonal matrix view (ef TNL::Matrices::TridiagonalMatrixView ,line 16) which we use in the lambda function for matrix elements modification (lines 18-26). Inside the lambda function, we first get a matrix row by calling the method `getRow` (\ref TNL::Matrices::TridiagonalMatrixView::getRow) using which we can access the matrix elements (lines 21-25). We would like to stress that the method `setElement` addresses the matrix elements with the `localIdx` parameter which is a rank of the nonzero element in the matrix row - see [Indexing of nonzero matrix elements in sparse matrices](#indexing-of-nonzero-matrix-elements-in-sparse-matrices). The lambda function is called by the `ParallelFor` (\ref TNL::Algorithms::ParallelFor). +We create a matrix with the same size (line 22-27). Next, we fetch the tridiagonal matrix view (ef TNL::Matrices::TridiagonalMatrixView ,line 28) which we use in the lambda function for matrix elements modification (lines 30-38). Inside the lambda function, we first get a matrix row by calling the method `getRow` (\ref TNL::Matrices::TridiagonalMatrixView::getRow) using which we can access the matrix elements (lines 33-37). We would like to stress that the method `setElement` addresses the matrix elements with the `localIdx` parameter which is a rank of the nonzero element in the matrix row - see [Indexing of nonzero matrix elements in sparse matrices](#indexing-of-nonzero-matrix-elements-in-sparse-matrices). The lambda function is called by the `ParallelFor` (\ref TNL::Algorithms::ParallelFor). The result looks as follows: @@ -871,7 +875,9 @@ The differences are: 2. We call the method `forAllRows` (\ref TNL::Matrices::TridiagonalMatrix::forAllRows) (line 33) instead of `ParallelFor` (\ref TNL::Algorithms::ParallelFor) which is simpler since we do not have to state the device type explicitly. The method `forAllRows` calls the method `forRows` for all matrix rows so we do not have to state explicitly the interval of matrix rows neither. 3. The lambda function `f` (lines 25-31) accepts one parameter `row` of the type `RowView` (\ref TNL::Matrices::TridiagonalMatrix::RowView which is \ref TNL::Matrices::TridiagonalMatrixRowView) instead of the index of the matrix row. Therefore we do not need to call the method `getRow` (\ref TNL::Matrices::TridiagonalMatrix::getRow). On the other hand, we need the method `geRowIndex` (\ref TNL::Matrices::TridiagonalMatrixRowView::getRowIndex) to get the index of the matrix row (line 24). -The rest of the lambda function remains the same. The result looks as follows: +Next, we compute sum of absolute values of matrix elements in each row and store it in a vector (lines 39-46). Firstly we create the vector `sum_vector` for storing the sums (line 39) and get a vector view `sum_view` to get access to the vector from a lambda function. On the lines 41-46, we call lambda function for each matrix row which iterates over all matrix elements and sum their absolute values. Finally we store the result to the output vector (line 45). + +The result looks as follows: \include TridiagonalMatrixExample_forRows.out @@ -1139,6 +1145,8 @@ As in the case of other matrix types, the method `forRows` (\ref TNL::Matrices:: We call the method `forAllRows` (\ref TNL::Matrices::MultidiagonalMatrix::forAllRows) (line 36) instead of `ParallelFor` (\ref TNL::Algorithms::ParallelFor) which is simpler since we do not have to state the device type explicitly. The method `forAllRows` calls the method `forRows` for all matrix rows so we do not have to state explicitly the interval of matrix rows neither. The lambda function `f` (lines 28-35) accepts one parameter `row` of the type `RowView` (\ref TNL::Matrices::MultidiagonalMatrix::RowView which is \ref TNL::Matrices::MultidiagonalMatrixRowView). At the beginning of the lambda function, we call the method `geRowIndex` (\ref TNL::Matrices::MultidiagonalMatrixRowView::getRowIndex) to get the index of the matrix row (line 29). +Next, we compute sum of absolute values of matrix elements in each row and store it in a vector (lines 39-46). Firstly we create the vector `sum_vector` for storing the sums (line 39) and get a vector view `sum_view` to get access to the vector from a lambda function. On the lines 41-46, we call lambda function for each matrix row which iterates over all matrix elements and sum their absolute values. Finally we store the result to the output vector (line 45). + The result looks as follows: \include MultidiagonalMatrixExample_forRows.out @@ -1218,9 +1226,15 @@ Method `forRows` (\ref TNL::Matrices::LambdaMatrix::forRows, \ref TNL::Matrices: \includelineno LambdaMatrixExample_forRows.cpp -We start with the lambda functions (lines 17-61) defining the elements of the lambda matrix. Next, we create the lambda matrix `matrix` (lines 62-64) and the dense matrix `denseMatrix` (lines 66-67) together with the dense matrix view (line 68). The lambda function `f` (lines 69-73) serves for copying matrix elements from the lambda matrix to the dense matrix. The process of matrix elements copying is started by calling the method `forAllRows` (\ref TNL::Matrices::LambdaMatrix::forRows, \ref TNL::Matrices::LambdaMatrix::forAllRows) (line 74). +We start with the lambda functions (lines 17-61) defining the elements of the lambda matrix. Next, we create the lambda matrix `matrix` (lines 62-64) and the dense matrix `denseMatrix` (lines 67-68) together with the dense matrix view (line 69). The lambda function `f` (lines 70-74) serves for copying matrix elements from the lambda matrix to the dense matrix. The process of matrix elements copying is started by calling the method `forAllRows` (\ref TNL::Matrices::LambdaMatrix::forRows, \ref TNL::Matrices::LambdaMatrix::forAllRows) (line 75). -Note, however, that use of `forElements` method (\ref TNL::Matrices::LambdaMatrix::forElements) would be more convenient. The result looks as follows: +Note, however, that use of `forElements` method (\ref TNL::Matrices::LambdaMatrix::forElements) would be more convenient. + +Next, we compute sum of absolute values of matrix elements in each row and store it in a vector (lines 83-90). Firstly we create the vector `sum_vector` for storing the sums (line 83) and get a vector view `sum_view` to get access to the vector from a lambda function. On the lines 85-90, we call lambda function for each matrix row which iterates over all matrix elements and sum their absolute values. Finally we store the result to the output vector (line 92). + + + +The result looks as follows: \include LambdaMatrixExample_forRows.out -- GitLab