diff --git a/src/TNL/Containers/Array.h b/src/TNL/Containers/Array.h index 90eb61a56c2d8eb210736ea0be6f982f9fd2b8e6..ea726d316c182521bc9064eee908eff366413d5e 100644 --- a/src/TNL/Containers/Array.h +++ b/src/TNL/Containers/Array.h @@ -280,6 +280,18 @@ class Array * \param end The end of the array sub-interval. The default value is 0 * which is, however, replaced with the array size. */ + ConstViewType getView( IndexType begin = 0, IndexType end = 0 ) const; + + /** + * \brief Returns a non-modifiable view of the array. + * + * If \e begin and \e end is set, view for sub-interval [ \e begin, \e end ) + * is returned. + * + * \param begin is the beginning of the sub-interval, 0 by default. + * \param end is the end of the sub-interval. Default value is 0 which is, + * however, replaced with the Array size. + */ ConstViewType getConstView( IndexType begin = 0, IndexType end = 0 ) const; /** diff --git a/src/TNL/Containers/Array.hpp b/src/TNL/Containers/Array.hpp index ceb709c60de5262e908a94f1cde3462f032fcdb2..7b4f17f2a38565cd841216ff767e6220d4536664 100644 --- a/src/TNL/Containers/Array.hpp +++ b/src/TNL/Containers/Array.hpp @@ -341,6 +341,18 @@ getView( IndexType begin, IndexType end ) return ViewType( getData() + begin, end - begin ); } +template< typename Value, + typename Device, + typename Index > +typename Array< Value, Device, Index >::ConstViewType +Array< Value, Device, Index >:: +getView( IndexType begin, IndexType end ) const +{ + if( end == 0 ) + end = getSize(); + return ConstViewType( &getData()[ begin ], end - begin ); +} + template< typename Value, typename Device, typename Index > diff --git a/src/TNL/Containers/ArrayView.h b/src/TNL/Containers/ArrayView.h index be67ed9e868c819789621a63db7da57e13e46dce..e42f910497a633d7f51b2ee16ae86b83aacbc777 100644 --- a/src/TNL/Containers/ArrayView.h +++ b/src/TNL/Containers/ArrayView.h @@ -180,6 +180,19 @@ public: * which is, however, replaced with the array size. */ __cuda_callable__ + ConstViewType getView( const IndexType begin = 0, IndexType end = 0 ) const; + + /** + * \brief Returns a non-modifiable view of the array view. + * + * If \e begin and \e end is set, view for sub-interval [ \e begin, \e end ) + * is returned. + * + * \param begin is the beginning of the sub-interval, 0 by default. + * \param end is the end of the sub-interval. Default value is 0 which is, + * however, replaced with the ArrayView size. + */ + __cuda_callable__ ConstViewType getConstView( const IndexType begin = 0, IndexType end = 0 ) const; /** diff --git a/src/TNL/Containers/ArrayView.hpp b/src/TNL/Containers/ArrayView.hpp index 872ccbaa5658c13a738127a892a7eebca2682d7b..199704a5a5a9095d56cbd8bde46eeefbfeab05b4 100644 --- a/src/TNL/Containers/ArrayView.hpp +++ b/src/TNL/Containers/ArrayView.hpp @@ -95,6 +95,18 @@ template< typename Value, __cuda_callable__ typename ArrayView< Value, Device, Index >::ConstViewType ArrayView< Value, Device, Index >:: +getView( const IndexType begin, IndexType end ) const +{ + if( end == 0 ) + end = this->getSize(); + return ConstViewType( &getData()[ begin ], end - begin ); +} + +template< typename Value, + typename Device, + typename Index > +typename ArrayView< Value, Device, Index >::ConstViewType +ArrayView< Value, Device, Index >:: getConstView( const IndexType begin, IndexType end ) const { if( end == 0 ) diff --git a/src/TNL/Containers/Expressions/VerticalOperations.h b/src/TNL/Containers/Expressions/VerticalOperations.h index 6aff760f91b9a45b8fceeffc705d98257a7dbbca..665606dbe92d23dd3997a30172f6f4b50ce563a1 100644 --- a/src/TNL/Containers/Expressions/VerticalOperations.h +++ b/src/TNL/Containers/Expressions/VerticalOperations.h @@ -241,10 +241,16 @@ auto ExpressionArgMax( const Expression& expression, typename Expression::IndexT } template< typename Expression > -auto ExpressionSum( const Expression& expression ) -> typename std::remove_reference< decltype( expression[ 0 ] ) >::type +auto ExpressionSum( const Expression& expression ) -> + typename std::conditional< + std::is_same< typename std::remove_cv< typename std::remove_reference< decltype( expression[ 0 ] ) >::type >::type, bool >::value, + typename Expression::IndexType, + typename std::remove_reference< decltype( expression[ 0 ] ) >::type + >::type { - using ResultType = typename std::remove_cv< typename std::remove_reference< decltype( expression[ 0 ] ) >::type >::type; + using ResultTypeBase = typename std::remove_cv< typename std::remove_reference< decltype( expression[ 0 ] ) >::type >::type; using IndexType = typename Expression::IndexType; + using ResultType = typename std::conditional< std::is_same< ResultTypeBase, bool >::value, IndexType, ResultTypeBase >::type; auto fetch = [=] __cuda_callable__ ( IndexType i ) { return expression[ i ]; }; auto reduction = [=] __cuda_callable__ ( ResultType& a, const ResultType& b ) { a += b; }; diff --git a/src/TNL/Containers/Vector.h b/src/TNL/Containers/Vector.h index 7da5bf47ca9ae39a304530958d71f4fcfc0cd004..6d7a62f1008f9de13be9c38277ba925a0d202738 100644 --- a/src/TNL/Containers/Vector.h +++ b/src/TNL/Containers/Vector.h @@ -74,7 +74,17 @@ public: * however, replaced with the Vector size. */ ViewType getView( IndexType begin = 0, IndexType end = 0 ); - + + /** + * \brief Returns a non-modifiable view of the vector. + * + * If \e begin and \e end is set, view for sub-interval [ \e begin, \e end ) + * is returned. + * + * \param begin is the beginning of the sub-interval, 0 by default. + * \param end is the end of the sub-interval. Default value is 0 which is, + * however, replaced with the Vector size. + */ ConstViewType getView( IndexType begin = 0, IndexType end = 0 ) const; /** @@ -258,8 +268,8 @@ public: * * \param v Reference to another vector. */ - template< typename ResultType = RealType, typename Vector > - ResultType differenceSum( const Vector& v ) const; + //template< typename ResultType = RealType, typename Vector > + //ResultType differenceSum( const Vector& v ) const; /** * \brief Returns this vector multiplied by scalar \e alpha. diff --git a/src/TNL/Containers/Vector.hpp b/src/TNL/Containers/Vector.hpp index 684afc3765a421b46c0e64268a2594c9a24686a0..ecef79651c8153b0bf548a3f959d2110884a0892 100644 --- a/src/TNL/Containers/Vector.hpp +++ b/src/TNL/Containers/Vector.hpp @@ -311,14 +311,14 @@ ResultType Vector< Real, Device, Index >::differenceLpNorm( const VectorT& v, co } -template< typename Real, +/*template< typename Real, typename Device, typename Index > template< typename ResultType, typename VectorT > ResultType Vector< Real, Device, Index >::differenceSum( const VectorT& v ) const { return Algorithms::VectorOperations< Device >::template getVectorDifferenceSum< Vector, VectorT, ResultType >( *this, v ); -} +}*/ template< typename Real, diff --git a/src/TNL/Containers/VectorExpressions.h b/src/TNL/Containers/VectorExpressions.h index 254fae7f0fe22eefe3f52873df5f963c9f708db7..379515d13f0dbcbe9cf3da53ecd1ae155931e3cf 100644 --- a/src/TNL/Containers/VectorExpressions.h +++ b/src/TNL/Containers/VectorExpressions.h @@ -16,6 +16,8 @@ #include <TNL/Containers/Expressions/Comparison.h> #include <TNL/Containers/Expressions/VerticalOperations.h> +#include "VectorView.h" + namespace TNL { //// @@ -25,24 +27,37 @@ namespace TNL { //// // Addition template< typename Real, typename Device, typename Index, typename ET > -const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Addition > +const Containers::Expressions::BinaryExpressionTemplate< + typename Containers::VectorView< Real, Device, Index >::ConstViewType, + ET, + Containers::Expressions::Addition > operator+( const Containers::Vector< Real, Device, Index >& a, const ET& b ) { - return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Addition >( a.getView(), b ); + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; + return Containers::Expressions::BinaryExpressionTemplate< ConstView, ET, Containers::Expressions::Addition >( a.getView(), b ); } template< typename ET, typename Real, typename Device, typename Index > -const Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Addition > +const Containers::Expressions::BinaryExpressionTemplate< + ET, + typename Containers::VectorView< Real, Device, Index >::ConstViewType, + Containers::Expressions::Addition > operator+( const ET& a, const Containers::Vector< Real, Device, Index >& b ) { - return Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Addition >( a, b.getView() ); + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; + return Containers::Expressions::BinaryExpressionTemplate< ET, ConstView, Containers::Expressions::Addition >( a, b.getView() ); } template< typename Real1, typename Real2, typename Device, typename Index > -const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Addition > +const Containers::Expressions::BinaryExpressionTemplate< + typename Containers::VectorView< Real1, Device, Index >::ConstViewType, + typename Containers::VectorView< Real2, Device, Index >::ConstViewType, + Containers::Expressions::Addition > operator+( const Containers::Vector< Real1, Device, Index >& a, const Containers::Vector< Real2, Device, Index >& b ) { - return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Addition >( a.getView(), b.getView() ); + using ConstView1 = typename Containers::Vector< Real1, Device, Index >::ConstViewType; + using ConstView2 = typename Containers::Vector< Real2, Device, Index >::ConstViewType; + return Containers::Expressions::BinaryExpressionTemplate< ConstView1, ConstView2, Containers::Expressions::Addition >( a.getView(), b.getView() ); } //// @@ -51,6 +66,7 @@ template< typename Real, typename Device, typename Index, typename ET > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Subtraction > operator-( const Containers::Vector< Real, Device, Index >& a, const ET& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Subtraction >( a.getView(), b ); } @@ -58,14 +74,20 @@ template< typename ET, typename Real, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Subtraction > operator-( const ET& a, const Containers::Vector< Real, Device, Index >& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Subtraction >( a, b.getView() ); } template< typename Real1, typename Real2, typename Device, typename Index > -const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Subtraction > +const Containers::Expressions::BinaryExpressionTemplate< + typename Containers::VectorView< Real1, Device, Index >::ConstViewType, + typename Containers::VectorView< Real2, Device, Index >::ConstViewType, + Containers::Expressions::Subtraction > operator-( const Containers::Vector< Real1, Device, Index >& a, const Containers::Vector< Real2, Device, Index >& b ) { - return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Subtraction >( a.getView(), b.getView() ); + using ConstView1 = typename Containers::Vector< Real1, Device, Index >::ConstViewType; + using ConstView2 = typename Containers::Vector< Real2, Device, Index >::ConstViewType; + return Containers::Expressions::BinaryExpressionTemplate< ConstView1, ConstView2, Containers::Expressions::Subtraction >( a.getView(), b.getView() ); } //// @@ -74,6 +96,7 @@ template< typename Real, typename Device, typename Index, typename ET > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Multiplication > operator*( const Containers::Vector< Real, Device, Index >& a, const ET& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Multiplication >( a.getView(), b ); } @@ -81,6 +104,7 @@ template< typename ET, typename Real, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Multiplication > operator*( const ET& a, const Containers::Vector< Real, Device, Index >& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Multiplication >( a, b.getView() ); } @@ -88,6 +112,8 @@ template< typename Real1, typename Real2, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Multiplication > operator*( const Containers::Vector< Real1, Device, Index >& a, const Containers::Vector< Real2, Device, Index >& b ) { + using ConstView1 = typename Containers::Vector< Real1, Device, Index >::ConstViewType; + using ConstView2 = typename Containers::Vector< Real2, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Multiplication >( a.getView(), b.getView() ); } @@ -97,6 +123,7 @@ template< typename Real, typename Device, typename Index, typename ET > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Division > operator/( const Containers::Vector< Real, Device, Index >& a, const ET& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Division >( a.getView(), b ); } @@ -104,6 +131,7 @@ template< typename ET, typename Real, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Division > operator/( const ET& a, const Containers::Vector< Real, Device, Index >& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Division >( a, b.getView() ); } @@ -111,6 +139,8 @@ template< typename Real1, typename Real2, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Division > operator/( const Containers::Vector< Real1, Device, Index >& a, const Containers::Vector< Real2, Device, Index >& b ) { + using ConstView1 = typename Containers::Vector< Real1, Device, Index >::ConstViewType; + using ConstView2 = typename Containers::Vector< Real2, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Division >( a.getView(), b.getView() ); } @@ -120,6 +150,7 @@ template< typename Real, typename Device, typename Index, typename ET > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Min > min( const Containers::Vector< Real, Device, Index >& a, const ET& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Min >( a.getView(), b ); } @@ -127,6 +158,7 @@ template< typename ET, typename Real, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Min > min( const ET& a, const Containers::Vector< Real, Device, Index >& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Min >( a, b.getView() ); } @@ -134,6 +166,8 @@ template< typename Real1, typename Real2, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Min > min( const Containers::Vector< Real1, Device, Index >& a, const Containers::Vector< Real2, Device, Index >& b ) { + using ConstView1 = typename Containers::Vector< Real1, Device, Index >::ConstViewType; + using ConstView2 = typename Containers::Vector< Real2, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Min >( a.getView(), b.getView() ); } @@ -143,6 +177,7 @@ template< typename Real, typename Device, typename Index, typename ET > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Max > max( const Containers::Vector< Real, Device, Index >& a, const ET& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, ET, Containers::Expressions::Max >( a.getView(), b ); } @@ -150,6 +185,7 @@ template< typename ET, typename Real, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Max > max( const ET& a, const Containers::Vector< Real, Device, Index >& b ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< ET, Containers::VectorView< Real, Device, Index >, Containers::Expressions::Max >( a, b.getView() ); } @@ -157,6 +193,8 @@ template< typename Real1, typename Real2, typename Device, typename Index > const Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Max > max( const Containers::Vector< Real1, Device, Index >& a, const Containers::Vector< Real2, Device, Index >& b ) { + using ConstView1 = typename Containers::Vector< Real1, Device, Index >::ConstViewType; + using ConstView2 = typename Containers::Vector< Real2, Device, Index >::ConstViewType; return Containers::Expressions::BinaryExpressionTemplate< Containers::VectorView< Real1, Device, Index >, Containers::VectorView< Real2, Device, Index >, Containers::Expressions::Max >( a.getView(), b.getView() ); } @@ -300,6 +338,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Minus > operator-( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Minus >( a.getView() ); } @@ -309,6 +348,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Abs > abs( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Abs >( a.getView() ); } @@ -318,6 +358,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Sin > sin( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Sin >( a.getView() ); } @@ -327,6 +368,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Cos > cos( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Cos >( a.getView() ); } @@ -336,6 +378,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Tan > tan( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Tan >( a.getView() ); } @@ -345,6 +388,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Sqrt > sqrt( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Sqrt >( a.getView() ); } @@ -354,6 +398,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Cbrt > cbrt( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Cbrt >( a.getView() ); } @@ -363,6 +408,7 @@ template< typename Real, typename Device, typename Index, typename ExpType > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Pow, ExpType > pow( const Containers::Vector< Real, Device, Index >& a, const ExpType& exp ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Pow, ExpType >( a.getView(), exp ); } @@ -372,6 +418,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Floor > floor( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Floor >( a.getView() ); } @@ -381,6 +428,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Ceil > ceil( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Ceil >( a.getView() ); } @@ -390,6 +438,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Acos > acos( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Acos >( a.getView() ); } @@ -399,6 +448,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Asin > asin( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Asin >( a.getView() ); } @@ -408,6 +458,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Atan > atan( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Atan >( a.getView() ); } @@ -417,6 +468,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Cosh > cosh( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Cosh >( a.getView() ); } @@ -426,6 +478,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Tanh > tanh( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Tanh >( a.getView() ); } @@ -435,6 +488,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Log > log( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Log >( a.getView() ); } @@ -444,6 +498,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Log10 > log10( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Log10 >( a.getView() ); } @@ -453,6 +508,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Log2 > log2( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Log2 >( a.getView() ); } @@ -462,6 +518,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Exp > exp( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Exp >( a.getView() ); } @@ -471,6 +528,7 @@ template< typename Real, typename Device, typename Index > const Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Sign > sign( const Containers::Vector< Real, Device, Index >& a ) { + using ConstView = typename Containers::Vector< Real, Device, Index >::ConstViewType; return Containers::Expressions::UnaryExpressionTemplate< Containers::VectorView< Real, Device, Index >, Containers::Expressions::Sign >( a.getView() ); } diff --git a/src/TNL/Containers/VectorView.h b/src/TNL/Containers/VectorView.h index 0d6483ac5eb29424a7935ff4ccffd2d1bd6c98bc..242ffc12078a9c3ce49eb53991a04798c795f402 100644 --- a/src/TNL/Containers/VectorView.h +++ b/src/TNL/Containers/VectorView.h @@ -95,6 +95,19 @@ public: __cuda_callable__ ConstViewType getConstView( IndexType begin = 0, IndexType end = 0 ) const; + /** + * \brief Returns a non-modifiable view of the vector view. + * + * If \e begin and \e end is set, view for sub-interval [ \e begin, \e end ) + * is returned. + * + * \param begin is the beginning of the sub-interval, 0 by default. + * \param end is the end of the sub-interval. Default value is 0 which is, + * however, replaced with the VectorView size. + */ + __cuda_callable__ + ConstViewType getConstView( const IndexType begin = 0, IndexType end = 0 ) const; + static String getType(); diff --git a/src/TNL/Containers/VectorView.hpp b/src/TNL/Containers/VectorView.hpp index 57c1cc2ec524b9e4199c82bb0a99b54cbfaf62c6..f1414f76da64874f93f92b847e56ec8e849dbc77 100644 --- a/src/TNL/Containers/VectorView.hpp +++ b/src/TNL/Containers/VectorView.hpp @@ -60,7 +60,19 @@ template< typename Real, __cuda_callable__ typename VectorView< Real, Device, Index >::ConstViewType VectorView< Real, Device, Index >:: -getConstView( IndexType begin, IndexType end ) const +getView( IndexType begin, IndexType end ) const +{ + if( end == 0 ) + end = this->getSize(); + return ConstViewType( &getData()[ begin ], end - begin );; +} + +template< typename Real, + typename Device, + typename Index > +typename VectorView< Real, Device, Index >::ConstViewType +VectorView< Real, Device, Index >:: +getConstView( const IndexType begin, IndexType end ) const { if( end == 0 ) end = this->getSize(); diff --git a/src/UnitTests/Containers/VectorTest-3.h b/src/UnitTests/Containers/VectorTest-3.h index c857c9edd5bf36a3d4c3034694e401bd3707bd02..ad79049c4213e658193b008d3cdca6fc56b13ea6 100644 --- a/src/UnitTests/Containers/VectorTest-3.h +++ b/src/UnitTests/Containers/VectorTest-3.h @@ -58,16 +58,42 @@ TYPED_TEST( VectorTest, absMax ) using VectorType = typename TestFixture::VectorType; using VectorOperations = typename TestFixture::VectorOperations; using ViewType = typename TestFixture::ViewType; - const int size = VECTOR_TEST_SIZE; - VectorType v; - v.setSize( size ); + // this test expect an even size + const int size = VECTOR_TEST_SIZE % 2 ? VECTOR_TEST_SIZE - 1 : VECTOR_TEST_SIZE; + + VectorType v( size ); ViewType v_view( v ); setNegativeLinearSequence( v ); - EXPECT_EQ( v.absMax(), size - 1 ); - EXPECT_EQ( v_view.absMax(), size - 1 ); + EXPECT_EQ( max( abs( v ) ), size - 1 ); + EXPECT_EQ( max( abs( v_view ) ),, size - 1 ); EXPECT_EQ( VectorOperations::getVectorAbsMax( v ), size - 1 ); + + VectorType u( size ), v( size ); + ViewType u_view( u ), v_view( v ); + + v.setValue( 1.0 ); + + setConstantSequence( u, 2 ); + EXPECT_EQ( sum( u - v ), size ); + EXPECT_EQ( sum( u_view - v_view ), size ); + EXPECT_EQ( VectorOperations::getVectorDifferenceSum( u, v ), size ); + + setLinearSequence( u ); + EXPECT_EQ( sum( u - v ), 0.5 * size * ( size - 1 ) - size ); + EXPECT_EQ( sum( u_view - v_view ), 0.5 * size * ( size - 1 ) - size ); + EXPECT_EQ( VectorOperations::getVectorDifferenceSum( u, v ), 0.5 * size * ( size - 1 ) - size ); + + setNegativeLinearSequence( u ); + EXPECT_EQ( sum( u - v ), - 0.5 * size * ( size - 1 ) - size ); + EXPECT_EQ( sum( u_view - v_view ), - 0.5 * size * ( size - 1 ) - size ); + EXPECT_EQ( VectorOperations::getVectorDifferenceSum( u, v ), - 0.5 * size * ( size - 1 ) - size ); + + setOscilatingSequence( u, 1.0 ); + EXPECT_EQ( sum( u - v ), - size ); + EXPECT_EQ( sum( u_view - v_view ), - size ); + EXPECT_EQ( VectorOperations::getVectorDifferenceSum( u, v ), - size ); } TYPED_TEST( VectorTest, absMin )