Loading src/TNL/Containers/StaticVector.h +1 −0 Original line number Diff line number Diff line Loading @@ -12,6 +12,7 @@ #include <TNL/Containers/StaticArray.h> #include <TNL/Config/ParameterContainer.h> #include <TNL/Experimental/ExpressionTemplates/BinaryExpressionTemplate.h> namespace TNL { namespace Containers { Loading src/TNL/Experimental/ExpressionTemplates/BinaryExpressionTemplate.h 0 → 100644 +152 −0 Original line number Diff line number Diff line /*************************************************************************** BinaryExpressionTemplate.h - description ------------------- begin : Apr 18, 2019 copyright : (C) 2019 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ #pragma once #include <TNL/Experimental/ExpressionTemplates/ExpressionTemplatesOperations.h> #include <TNL/Experimental/ExpressionTemplates/ExpressionVariableType.h> namespace TNL { namespace ExpressionTemplates { template< typename T1, typename T2, template< typename, typename > class Operation, ExpressionVariableType T1Type = ExpressionVariableTypeGetter< T1 >::value, ExpressionVariableType T2Type = ExpressionVariableTypeGetter< T2 >::value > struct BinaryExpressionTemplate { BinaryExpressionTemplate( const T1& a, const T2& b ){}; static T1 evaluate( const T1& a, const T2& b ) { return Operation< T1, T2 >::evaluate( a, b ); } }; template< typename T1, typename T2, template< typename, typename > class Operation > struct BinaryExpressionTemplate< T1, T2, Operation, VectorVariable, VectorVariable > { using RealType = typename T1::RealType; using IsExpressionTemplate = bool; BinaryExpressionTemplate( const T1& a, const T2& b ): op1( a ), op2( b ){} static BinaryExpressionTemplate evaluate( const T1& a, const T2& b ) { return BinaryExpressionTemplate( a, b ); } RealType operator[]( const int i ) const { return Operation< typename T1::RealType, typename T2::RealType >::evaluate( op1[ i ], op2[ i ] ); } int getSize() const { return op1.getSize(); } protected: const T1 &op1; const T2 &op2; }; template< typename T1, typename T2, template< typename, typename > class Operation > struct BinaryExpressionTemplate< T1, T2, Operation, VectorVariable, ArithmeticVariable > { using RealType = typename T1::RealType; using IsExpressionTemplate = bool; BinaryExpressionTemplate( const T1& a, const T2& b ): op1( a ), op2( b ){} BinaryExpressionTemplate evaluate( const T1& a, const T2& b ) { return BinaryExpressionTemplate( a, b ); } RealType operator[]( const int i ) const { return Operation< typename T1::RealType, T2 >::evaluate( op1[ i ], op2 ); } int getSize() const { return op1.getSize(); } protected: const T1 &op1; const T2 &op2; }; template< typename T1, typename T2, template< typename, typename > class Operation > struct BinaryExpressionTemplate< T1, T2, Operation, ArithmeticVariable, VectorVariable > { using RealType = typename T2::RealType; using IsExpressionTemplate = bool; BinaryExpressionTemplate( const T1& a, const T2& b ): op1( a ), op2( b ){} BinaryExpressionTemplate evaluate( const T1& a, const T2& b ) { return BinaryExpressionTemplate( a, b ); } RealType operator[]( const int i ) const { return Operation< T1, typename T2::RealType >::evaluate( op1, op2[ i ] ); } int getSize() const { return op2.getSize(); } protected: const T1& op1; const T2& op2; }; template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Addition > operator+( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Addition >( a, b ); } template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Subtraction > operator-( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Subtraction >( a, b ); } template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Multiplication > operator*( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Multiplication >( a, b ); } template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Division > operator/( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Division >( a, b ); } } //namespace ExpressionTemplates } // namespace TNL No newline at end of file src/TNL/Experimental/ExpressionTemplates/CMakeLists.txt 0 → 100644 +16 −0 Original line number Diff line number Diff line set( headers StaticVectorExpressions.h VectorExpressions.h ) IF( BUILD_CUDA ) CUDA_ADD_EXECUTABLE( tnl-expression-templates expression-templates.cu ) ELSE( BUILD_CUDA ) ADD_EXECUTABLE( tnl-expression-templates expression-templates.cpp ) ADD_EXECUTABLE( tnl-expression-templates-static expression-templates-static.cpp ) ENDIF( BUILD_CUDA ) INSTALL( TARGETS tnl-expression-templates RUNTIME DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE ) INSTALL( FILES ${headers} DESTINATION ${TNL_TARGET_INCLUDE_DIRECTORY}/Experimental/ExpressionTemplates ) src/TNL/Experimental/ExpressionTemplates/ExpressionTemplatesOperations.h 0 → 100644 +54 −0 Original line number Diff line number Diff line /*************************************************************************** ExpressionTemplatesOperations.h - description ------------------- begin : Apr 18, 2019 copyright : (C) 2019 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ #pragma once namespace TNL { namespace ExpressionTemplates { template< typename T1, typename T2 > struct Addition { static auto evaluate( const T1& a, const T2& b ) -> decltype( a + b ) { return a + b; } }; template< typename T1, typename T2 > struct Subtraction { static auto evaluate( const T1& a, const T2& b ) -> decltype( a - b ) { return a - b; } }; template< typename T1, typename T2 > struct Multiplication { static auto evaluate( const T1& a, const T2& b ) -> decltype( a * b ) { return a * b; } }; template< typename T1, typename T2 > struct Division { static auto evaluate( const T1& a, const T2& b ) -> decltype( a / b ) { return a / b; } }; } // ExpressionTemplates } // namespace TNL No newline at end of file src/TNL/Experimental/ExpressionTemplates/ExpressionVariableType.h 0 → 100644 +70 −0 Original line number Diff line number Diff line /*************************************************************************** ExpressionVariableType.h - description ------------------- begin : Apr 18, 2019 copyright : (C) 2019 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ #pragma once #include <type_traits> namespace TNL { enum ExpressionVariableType { ArithmeticVariable, VectorVariable, OtherVariable }; /** * SFINAE for checking if T has getSize method */ template< typename T > class IsExpressionTemplate { private: typedef char YesType[1]; typedef char NoType[2]; template< typename C > static YesType& test( typename C::IsExpressionTemplate ); template< typename C > static NoType& test(...); public: static constexpr bool value = ( sizeof( test< T >(0) ) == sizeof( YesType ) ); }; template< typename T > struct IsVectorType { static constexpr bool value = false; }; template< int Size, typename Real > struct IsVectorType< Containers::StaticVector< Size, Real > > { static constexpr bool value = true; }; template< typename T, bool IsArithmetic = std::is_arithmetic< T >::value, bool IsVector = IsVectorType< T >::value || IsExpressionTemplate< T >::value > struct ExpressionVariableTypeGetter { static constexpr ExpressionVariableType value = OtherVariable; }; template< typename T > struct ExpressionVariableTypeGetter< T, true, false > { static constexpr ExpressionVariableType value = ArithmeticVariable; }; template< typename T > struct ExpressionVariableTypeGetter< T, false, true > { static constexpr ExpressionVariableType value = VectorVariable; }; } //namespace TNL Loading
src/TNL/Containers/StaticVector.h +1 −0 Original line number Diff line number Diff line Loading @@ -12,6 +12,7 @@ #include <TNL/Containers/StaticArray.h> #include <TNL/Config/ParameterContainer.h> #include <TNL/Experimental/ExpressionTemplates/BinaryExpressionTemplate.h> namespace TNL { namespace Containers { Loading
src/TNL/Experimental/ExpressionTemplates/BinaryExpressionTemplate.h 0 → 100644 +152 −0 Original line number Diff line number Diff line /*************************************************************************** BinaryExpressionTemplate.h - description ------------------- begin : Apr 18, 2019 copyright : (C) 2019 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ #pragma once #include <TNL/Experimental/ExpressionTemplates/ExpressionTemplatesOperations.h> #include <TNL/Experimental/ExpressionTemplates/ExpressionVariableType.h> namespace TNL { namespace ExpressionTemplates { template< typename T1, typename T2, template< typename, typename > class Operation, ExpressionVariableType T1Type = ExpressionVariableTypeGetter< T1 >::value, ExpressionVariableType T2Type = ExpressionVariableTypeGetter< T2 >::value > struct BinaryExpressionTemplate { BinaryExpressionTemplate( const T1& a, const T2& b ){}; static T1 evaluate( const T1& a, const T2& b ) { return Operation< T1, T2 >::evaluate( a, b ); } }; template< typename T1, typename T2, template< typename, typename > class Operation > struct BinaryExpressionTemplate< T1, T2, Operation, VectorVariable, VectorVariable > { using RealType = typename T1::RealType; using IsExpressionTemplate = bool; BinaryExpressionTemplate( const T1& a, const T2& b ): op1( a ), op2( b ){} static BinaryExpressionTemplate evaluate( const T1& a, const T2& b ) { return BinaryExpressionTemplate( a, b ); } RealType operator[]( const int i ) const { return Operation< typename T1::RealType, typename T2::RealType >::evaluate( op1[ i ], op2[ i ] ); } int getSize() const { return op1.getSize(); } protected: const T1 &op1; const T2 &op2; }; template< typename T1, typename T2, template< typename, typename > class Operation > struct BinaryExpressionTemplate< T1, T2, Operation, VectorVariable, ArithmeticVariable > { using RealType = typename T1::RealType; using IsExpressionTemplate = bool; BinaryExpressionTemplate( const T1& a, const T2& b ): op1( a ), op2( b ){} BinaryExpressionTemplate evaluate( const T1& a, const T2& b ) { return BinaryExpressionTemplate( a, b ); } RealType operator[]( const int i ) const { return Operation< typename T1::RealType, T2 >::evaluate( op1[ i ], op2 ); } int getSize() const { return op1.getSize(); } protected: const T1 &op1; const T2 &op2; }; template< typename T1, typename T2, template< typename, typename > class Operation > struct BinaryExpressionTemplate< T1, T2, Operation, ArithmeticVariable, VectorVariable > { using RealType = typename T2::RealType; using IsExpressionTemplate = bool; BinaryExpressionTemplate( const T1& a, const T2& b ): op1( a ), op2( b ){} BinaryExpressionTemplate evaluate( const T1& a, const T2& b ) { return BinaryExpressionTemplate( a, b ); } RealType operator[]( const int i ) const { return Operation< T1, typename T2::RealType >::evaluate( op1, op2[ i ] ); } int getSize() const { return op2.getSize(); } protected: const T1& op1; const T2& op2; }; template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Addition > operator+( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Addition >( a, b ); } template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Subtraction > operator-( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Subtraction >( a, b ); } template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Multiplication > operator*( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Multiplication >( a, b ); } template< typename T1, typename T2 > BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Division > operator/( const T1 &a, const T2 &b ) { return BinaryExpressionTemplate< T1, T2, ExpressionTemplates::Division >( a, b ); } } //namespace ExpressionTemplates } // namespace TNL No newline at end of file
src/TNL/Experimental/ExpressionTemplates/CMakeLists.txt 0 → 100644 +16 −0 Original line number Diff line number Diff line set( headers StaticVectorExpressions.h VectorExpressions.h ) IF( BUILD_CUDA ) CUDA_ADD_EXECUTABLE( tnl-expression-templates expression-templates.cu ) ELSE( BUILD_CUDA ) ADD_EXECUTABLE( tnl-expression-templates expression-templates.cpp ) ADD_EXECUTABLE( tnl-expression-templates-static expression-templates-static.cpp ) ENDIF( BUILD_CUDA ) INSTALL( TARGETS tnl-expression-templates RUNTIME DESTINATION bin PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE ) INSTALL( FILES ${headers} DESTINATION ${TNL_TARGET_INCLUDE_DIRECTORY}/Experimental/ExpressionTemplates )
src/TNL/Experimental/ExpressionTemplates/ExpressionTemplatesOperations.h 0 → 100644 +54 −0 Original line number Diff line number Diff line /*************************************************************************** ExpressionTemplatesOperations.h - description ------------------- begin : Apr 18, 2019 copyright : (C) 2019 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ #pragma once namespace TNL { namespace ExpressionTemplates { template< typename T1, typename T2 > struct Addition { static auto evaluate( const T1& a, const T2& b ) -> decltype( a + b ) { return a + b; } }; template< typename T1, typename T2 > struct Subtraction { static auto evaluate( const T1& a, const T2& b ) -> decltype( a - b ) { return a - b; } }; template< typename T1, typename T2 > struct Multiplication { static auto evaluate( const T1& a, const T2& b ) -> decltype( a * b ) { return a * b; } }; template< typename T1, typename T2 > struct Division { static auto evaluate( const T1& a, const T2& b ) -> decltype( a / b ) { return a / b; } }; } // ExpressionTemplates } // namespace TNL No newline at end of file
src/TNL/Experimental/ExpressionTemplates/ExpressionVariableType.h 0 → 100644 +70 −0 Original line number Diff line number Diff line /*************************************************************************** ExpressionVariableType.h - description ------------------- begin : Apr 18, 2019 copyright : (C) 2019 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ /* See Copyright Notice in tnl/Copyright */ #pragma once #include <type_traits> namespace TNL { enum ExpressionVariableType { ArithmeticVariable, VectorVariable, OtherVariable }; /** * SFINAE for checking if T has getSize method */ template< typename T > class IsExpressionTemplate { private: typedef char YesType[1]; typedef char NoType[2]; template< typename C > static YesType& test( typename C::IsExpressionTemplate ); template< typename C > static NoType& test(...); public: static constexpr bool value = ( sizeof( test< T >(0) ) == sizeof( YesType ) ); }; template< typename T > struct IsVectorType { static constexpr bool value = false; }; template< int Size, typename Real > struct IsVectorType< Containers::StaticVector< Size, Real > > { static constexpr bool value = true; }; template< typename T, bool IsArithmetic = std::is_arithmetic< T >::value, bool IsVector = IsVectorType< T >::value || IsExpressionTemplate< T >::value > struct ExpressionVariableTypeGetter { static constexpr ExpressionVariableType value = OtherVariable; }; template< typename T > struct ExpressionVariableTypeGetter< T, true, false > { static constexpr ExpressionVariableType value = ArithmeticVariable; }; template< typename T > struct ExpressionVariableTypeGetter< T, false, true > { static constexpr ExpressionVariableType value = VectorVariable; }; } //namespace TNL