diff --git a/src/Benchmarks/HeatEquation/tnlTestNeighbourGridEntityGetter2D_impl.h b/src/Benchmarks/HeatEquation/tnlTestNeighbourGridEntityGetter2D_impl.h index 23d35fdae1038c6ec82e65fd69febfb1b0c6779a..13c7848decb4f967dd04630a03e62bbb49249efa 100644 --- a/src/Benchmarks/HeatEquation/tnlTestNeighbourGridEntityGetter2D_impl.h +++ b/src/Benchmarks/HeatEquation/tnlTestNeighbourGridEntityGetter2D_impl.h @@ -134,9 +134,9 @@ class tnlTestNeighborGridEntityGetter< void refresh( const GridType& grid, const IndexType& entityIndex ) { #ifndef HAVE_CUDA // TODO: fix this to work with CUDA - StaticFor< IndexType, -stencilSize, 0, StencilYRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, 1, stencilSize + 1, StencilYRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, -stencilSize, stencilSize + 1, StencilXRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, 0, StencilYRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, 1, stencilSize + 1, StencilYRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, stencilSize + 1, StencilXRefresher >::exec( *this, entityIndex ); #endif }; diff --git a/src/Examples/flow-sw/PhysicalVariablesGetter.h b/src/Examples/flow-sw/PhysicalVariablesGetter.h index 2af0a02b820db183fa32669dcecfe63f7f006990..d7eeb7618657c34485b3e4842ea04e12d3ee725c 100644 --- a/src/Examples/flow-sw/PhysicalVariablesGetter.h +++ b/src/Examples/flow-sw/PhysicalVariablesGetter.h @@ -79,7 +79,7 @@ class PhysicalVariablesGetter { const RealType e = energy.template getData< DeviceType >()( meshEntity ); const RealType rho = density.template getData< DeviceType >()( meshEntity ); - const RealType momentumNorm = momentum.template getData< DeviceType >().getVector( meshEntity ).lpNorm( 2.0 ); + const RealType momentumNorm = lpNorm( momentum.template getData< DeviceType >().getVector( meshEntity ), 2.0 ); if( rho == 0.0 ) return 0; else diff --git a/src/Examples/flow-vl/PhysicalVariablesGetter.h b/src/Examples/flow-vl/PhysicalVariablesGetter.h index 2af0a02b820db183fa32669dcecfe63f7f006990..d7eeb7618657c34485b3e4842ea04e12d3ee725c 100644 --- a/src/Examples/flow-vl/PhysicalVariablesGetter.h +++ b/src/Examples/flow-vl/PhysicalVariablesGetter.h @@ -79,7 +79,7 @@ class PhysicalVariablesGetter { const RealType e = energy.template getData< DeviceType >()( meshEntity ); const RealType rho = density.template getData< DeviceType >()( meshEntity ); - const RealType momentumNorm = momentum.template getData< DeviceType >().getVector( meshEntity ).lpNorm( 2.0 ); + const RealType momentumNorm = lpNorm( momentum.template getData< DeviceType >().getVector( meshEntity ), 2.0 ); if( rho == 0.0 ) return 0; else diff --git a/src/Examples/flow/PhysicalVariablesGetter.h b/src/Examples/flow/PhysicalVariablesGetter.h index 2af0a02b820db183fa32669dcecfe63f7f006990..d7eeb7618657c34485b3e4842ea04e12d3ee725c 100644 --- a/src/Examples/flow/PhysicalVariablesGetter.h +++ b/src/Examples/flow/PhysicalVariablesGetter.h @@ -79,7 +79,7 @@ class PhysicalVariablesGetter { const RealType e = energy.template getData< DeviceType >()( meshEntity ); const RealType rho = density.template getData< DeviceType >()( meshEntity ); - const RealType momentumNorm = momentum.template getData< DeviceType >().getVector( meshEntity ).lpNorm( 2.0 ); + const RealType momentumNorm = lpNorm( momentum.template getData< DeviceType >().getVector( meshEntity ), 2.0 ); if( rho == 0.0 ) return 0; else diff --git a/src/Examples/inviscid-flow-sw/PhysicalVariablesGetter.h b/src/Examples/inviscid-flow-sw/PhysicalVariablesGetter.h index 2af0a02b820db183fa32669dcecfe63f7f006990..d7eeb7618657c34485b3e4842ea04e12d3ee725c 100644 --- a/src/Examples/inviscid-flow-sw/PhysicalVariablesGetter.h +++ b/src/Examples/inviscid-flow-sw/PhysicalVariablesGetter.h @@ -79,7 +79,7 @@ class PhysicalVariablesGetter { const RealType e = energy.template getData< DeviceType >()( meshEntity ); const RealType rho = density.template getData< DeviceType >()( meshEntity ); - const RealType momentumNorm = momentum.template getData< DeviceType >().getVector( meshEntity ).lpNorm( 2.0 ); + const RealType momentumNorm = lpNorm( momentum.template getData< DeviceType >().getVector( meshEntity ), 2.0 ); if( rho == 0.0 ) return 0; else diff --git a/src/Examples/inviscid-flow-vl/PhysicalVariablesGetter.h b/src/Examples/inviscid-flow-vl/PhysicalVariablesGetter.h index 2af0a02b820db183fa32669dcecfe63f7f006990..d7eeb7618657c34485b3e4842ea04e12d3ee725c 100644 --- a/src/Examples/inviscid-flow-vl/PhysicalVariablesGetter.h +++ b/src/Examples/inviscid-flow-vl/PhysicalVariablesGetter.h @@ -79,7 +79,7 @@ class PhysicalVariablesGetter { const RealType e = energy.template getData< DeviceType >()( meshEntity ); const RealType rho = density.template getData< DeviceType >()( meshEntity ); - const RealType momentumNorm = momentum.template getData< DeviceType >().getVector( meshEntity ).lpNorm( 2.0 ); + const RealType momentumNorm = lpNorm( momentum.template getData< DeviceType >().getVector( meshEntity ), 2.0 ); if( rho == 0.0 ) return 0; else diff --git a/src/Examples/inviscid-flow/PhysicalVariablesGetter.h b/src/Examples/inviscid-flow/PhysicalVariablesGetter.h index a391d2d6477327d2a7058354212a9c85431173c8..a0a7f672d2ee2a14f9357343c7ba8a08d45b63d3 100644 --- a/src/Examples/inviscid-flow/PhysicalVariablesGetter.h +++ b/src/Examples/inviscid-flow/PhysicalVariablesGetter.h @@ -79,7 +79,7 @@ class PhysicalVariablesGetter { const RealType e = energy.template getData< DeviceType >()( meshEntity ); const RealType rho = density.template getData< DeviceType >()( meshEntity ); - const RealType momentumNorm = momentum.template getData< DeviceType >().getVector( meshEntity ).lpNorm( 2.0 ); + const RealType momentumNorm = lpNorm( momentum.template getData< DeviceType >().getVector( meshEntity ), 2.0 ); if( rho == 0.0 ) return 0; else diff --git a/src/TNL/Containers/StaticArray.h b/src/TNL/Containers/StaticArray.h index dea551a12c968c7de0ac2373ead879a60e22bedb..09923613ab75959022ce6f356b6a169e469e9b06 100644 --- a/src/TNL/Containers/StaticArray.h +++ b/src/TNL/Containers/StaticArray.h @@ -55,7 +55,7 @@ class StaticArray // reference: https://stackoverflow.com/q/4610503 template< typename _unused = void > __cuda_callable__ - inline StaticArray( const Value v[ Size ] ); + StaticArray( const Value v[ Size ] ); /** * \brief Constructor that sets all array components to value \e v. @@ -75,6 +75,25 @@ class StaticArray inline StaticArray( const std::initializer_list< Value > &elems ); + /** + * \brief Constructor that sets components of arrays with Size = 2. + * + * \param v1 Value of the first array component. + * \param v2 Value of the second array component. + */ + __cuda_callable__ + inline StaticArray( const Value& v1, const Value& v2 ); + + /** + * \brief Constructor that sets components of arrays with Size = 3. + * + * \param v1 Value of the first array component. + * \param v2 Value of the second array component. + * \param v3 Value of the third array component. + */ + __cuda_callable__ + inline StaticArray( const Value& v1, const Value& v2, const Value& v3 ); + /** * \brief Gets type of this array. */ @@ -109,6 +128,30 @@ class StaticArray __cuda_callable__ inline Value& operator[]( int i ); + /** \brief Returns the first coordinate.*/ + __cuda_callable__ + inline Value& x(); + + /** \brief Returns the first coordinate.*/ + __cuda_callable__ + inline const Value& x() const; + + /** \brief Returns the second coordinate for arrays with Size >= 2.*/ + __cuda_callable__ + inline Value& y(); + + /** \brief Returns the second coordinate for arrays with Size >= 2.*/ + __cuda_callable__ + inline const Value& y() const; + + /** \brief Returns the third coordinate for arrays with Size >= 3..*/ + __cuda_callable__ + inline Value& z(); + + /** \brief Returns the third coordinate for arrays with Size >= 3..*/ + __cuda_callable__ + inline const Value& z() const; + /** * \brief Assigns another static \e array to this array, replacing its current contents. */ @@ -180,379 +223,6 @@ class StaticArray Value data[ Size ]; }; -/** - * \brief Specific static array with the size of 1. Works like the class StaticArray. - */ -template< typename Value > -class StaticArray< 1, Value > -{ - protected: - enum { Size = 1 }; - - public: - using ValueType = Value; - using IndexType = int; - - /** - * \brief Gets size of this array. - */ - __cuda_callable__ - static constexpr int getSize(); - - /** \brief See StaticArray::StaticArray().*/ - __cuda_callable__ - inline StaticArray(); - - /** \brief See StaticArray::StaticArray(const Value v[Size]).*/ - // Note: the template avoids ambiguity of overloaded functions with literal 0 and pointer - // reference: https://stackoverflow.com/q/4610503 - template< typename _unused = void > - __cuda_callable__ - inline StaticArray( const Value v[ Size ] ); - - /** \brief See StaticArray::StaticArray(const Value& v).*/ - __cuda_callable__ - inline StaticArray( const Value& v ); - - /** \brief See StaticArray::StaticArray( const StaticArray< Size, Value >& v ).*/ - __cuda_callable__ - inline StaticArray( const StaticArray< Size, Value >& v ); - - inline StaticArray( const std::initializer_list< Value > &elems ); - - /** \brief See StaticArray::getType().*/ - static String getType(); - - /** \brief See StaticArray::getData().*/ - __cuda_callable__ - inline Value* getData(); - - /** \brief See StaticArray::getData() const.*/ - __cuda_callable__ - inline const Value* getData() const; - - /** \brief See StaticArray::operator[]( int i ) const.*/ - __cuda_callable__ - inline const Value& operator[]( int i ) const; - - /** \brief See StaticArray::operator[]( int i ).*/ - __cuda_callable__ - inline Value& operator[]( int i ); - - /** \brief Returns the first coordinate - the first element of this static array.*/ - __cuda_callable__ - inline Value& x(); - - /** \brief Returns the first coordinate - the first element of this static array.*/ - __cuda_callable__ - inline const Value& x() const; - - /** \brief Similar to StaticArray::operator = ( const StaticArray< Size, Value >& array ) only with Size equal to 1.*/ - __cuda_callable__ - inline StaticArray< 1, Value >& operator = ( const StaticArray< 1, Value >& array ); - - /** \brief See StaticArray::operator = (const Array& array).*/ - template< typename Array > - __cuda_callable__ - inline StaticArray< 1, Value >& operator = ( const Array& array ); - - /** \brief See StaticArray::operator == (const Array& array) const.*/ - template< typename Array > - __cuda_callable__ - inline bool operator == ( const Array& array ) const; - - /** \brief See StaticArray::operator != (const Array& array) const.*/ - template< typename Array > - __cuda_callable__ - inline bool operator != ( const Array& array ) const; - - template< typename OtherValue > - __cuda_callable__ - operator StaticArray< 1, OtherValue >() const; - - /** \brief See StaticArray::setValue().*/ - __cuda_callable__ - inline - void setValue( const ValueType& val ); - - /** \brief See StaticArray::save().*/ - bool save( File& file ) const; - - /** \brief See StaticArray::load().*/ - bool load( File& file); - - /** \brief See StaticArray::sort().*/ - void sort(); - - /** \brief See StaticArray::write().*/ - std::ostream& write( std::ostream& str, const char* separator = " " ) const; - - protected: - Value data[ Size ]; -}; - -/** - * \brief Specific static array with the size of 2. Works like the class StaticArray. - */ -template< typename Value > -class StaticArray< 2, Value > -{ - protected: - enum { Size = 2 }; - - public: - using ValueType = Value; - using IndexType = int; - - /** - * \brief Gets size of this array. - */ - __cuda_callable__ - static constexpr int getSize(); - - /** \brief See StaticArray::StaticArray().*/ - __cuda_callable__ - inline StaticArray(); - - /** \brief See StaticArray::StaticArray(const Value v[Size]).*/ - // Note: the template avoids ambiguity of overloaded functions with literal 0 and pointer - // reference: https://stackoverflow.com/q/4610503 - template< typename _unused = void > - __cuda_callable__ - inline StaticArray( const Value v[ Size ] ); - - /** \brief See StaticArray::StaticArray(const Value& v).*/ - __cuda_callable__ - inline StaticArray( const Value& v ); - - /** - * \brief Constructor that sets the two static array components to value \e v1 and \e v2. - * - * \param v1 Reference to the value of first array/vector component. - * \param v2 Reference to the value of second array/vector component. - */ - __cuda_callable__ - inline StaticArray( const Value& v1, const Value& v2 ); - - /** \brief See StaticArray::StaticArray( const StaticArray< Size, Value >& v ).*/ - __cuda_callable__ - inline StaticArray( const StaticArray< Size, Value >& v ); - - inline StaticArray( const std::initializer_list< Value > &elems ); - - /** \brief See StaticArray::getType().*/ - static String getType(); - - /** \brief See StaticArray::getData().*/ - __cuda_callable__ - inline Value* getData(); - - /** \brief See StaticArray::getData() const.*/ - __cuda_callable__ - inline const Value* getData() const; - - /** \brief See StaticArray::operator[]( int i ) const.*/ - __cuda_callable__ - inline const Value& operator[]( int i ) const; - - /** \brief See StaticArray::operator[]( int i ).*/ - __cuda_callable__ - inline Value& operator[]( int i ); - - /** \brief Returns the first coordinate - the first element of this static array.*/ - __cuda_callable__ - inline Value& x(); - - /** \brief Returns the first coordinate - the first element of this static array.*/ - __cuda_callable__ - inline const Value& x() const; - - /** \brief Returns the second coordinate - the second element of this static array.*/ - __cuda_callable__ - inline Value& y(); - - /** \brief Returns the second coordinate - the second element of this static array.*/ - __cuda_callable__ - inline const Value& y() const; - - /** \brief Similar to StaticArray::operator = ( const StaticArray< Size, Value >& array ) only with Size equal to 2.*/ - __cuda_callable__ - inline StaticArray< 2, Value >& operator = ( const StaticArray< 2, Value >& array ); - - /** \brief See StaticArray::operator = (const Array& array).*/ - template< typename Array > - __cuda_callable__ - inline StaticArray< 2, Value >& operator = ( const Array& array ); - - /** \brief See StaticArray::operator == (const Array& array) const.*/ - template< typename Array > - __cuda_callable__ - inline bool operator == ( const Array& array ) const; - - /** \brief See StaticArray::operator != (const Array& array) const.*/ - template< typename Array > - __cuda_callable__ - inline bool operator != ( const Array& array ) const; - - template< typename OtherValue > - __cuda_callable__ - operator StaticArray< 2, OtherValue >() const; - - /** \brief See StaticArray::setValue().*/ - __cuda_callable__ - inline void setValue( const ValueType& val ); - - /** \brief See StaticArray::save().*/ - bool save( File& file ) const; - - /** \brief See StaticArray::load().*/ - bool load( File& file); - - /** \brief See StaticArray::sort().*/ - void sort(); - - /** \brief See StaticArray::write().*/ - std::ostream& write( std::ostream& str, const char* separator = " " ) const; - - protected: - Value data[ Size ]; -}; - -/** - * \brief Specific static array with the size of 3. Works like the class StaticArray. - */ -template< typename Value > -class StaticArray< 3, Value > -{ - protected: - enum { Size = 3 }; - public: - using ValueType = Value; - using IndexType = int; - - /** - * \brief Gets size of this array. - */ - __cuda_callable__ - static constexpr int getSize(); - - /** \brief See StaticArray::StaticArray().*/ - __cuda_callable__ - inline StaticArray(); - - /** \brief See StaticArray::StaticArray(const Value v[ size ]).*/ - // Note: the template avoids ambiguity of overloaded functions with literal 0 and pointer - // reference: https://stackoverflow.com/q/4610503 - template< typename _unused = void > - __cuda_callable__ - inline StaticArray( const Value v[ Size ] ); - - /** \brief See StaticArray::StaticArray(const Value& v).*/ - __cuda_callable__ - inline StaticArray( const Value& v ); - - /** - * \brief Constructor that sets the three array components to value \e v1 \e v2 and \e v3. - * - * \param v1 Reference to the value of first array/vector component. - * \param v2 Reference to the value of second array/vector component. - * \param v3 Reference to the value of third array/vector component. - */ - __cuda_callable__ - inline StaticArray( const Value& v1, const Value& v2, const Value& v3 ); - - /** \brief See StaticArray::StaticArray( const StaticArray< Size, Value >& v ).*/ - __cuda_callable__ - inline StaticArray( const StaticArray< Size, Value >& v ); - - StaticArray( const std::initializer_list< Value > &elems ); - - /** \brief See StaticArray::getType().*/ - static String getType(); - - /** \brief See StaticArray::getData().*/ - __cuda_callable__ - inline Value* getData(); - - /** \brief See StaticArray::getData() const.*/ - __cuda_callable__ - inline const Value* getData() const; - - /** \brief See StaticArray::operator[]( int i ) const.*/ - __cuda_callable__ - inline const Value& operator[]( int i ) const; - - /** \brief See StaticArray::operator[]( int i ).*/ - __cuda_callable__ - inline Value& operator[]( int i ); - - /** \brief Returns the first coordinate - the first element of this static array.*/ - __cuda_callable__ - inline Value& x(); - - /** \brief Returns the first coordinate - the first element of this static array.*/ - __cuda_callable__ - inline const Value& x() const; - - /** \brief Returns the second coordinate - the second element of this static array.*/ - __cuda_callable__ - inline Value& y(); - - /** \brief Returns the second coordinate - the second element of this static array.*/ - __cuda_callable__ - inline const Value& y() const; - - /** \brief Returns the third coordinate - the third element of this static array.*/ - __cuda_callable__ - inline Value& z(); - - /** \brief Returns the third coordinate - the third element of this static array.*/ - __cuda_callable__ - inline const Value& z() const; - - /** \brief Similar to StaticArray::operator = ( const StaticArray< Size, Value >& array ) only with Size equal to 3.*/ - __cuda_callable__ - inline StaticArray< 3, Value >& operator = ( const StaticArray< 3, Value >& array ); - - /** \brief See StaticArray::operator = (const Array& array).*/ - template< typename Array > - __cuda_callable__ - inline StaticArray< 3, Value >& operator = ( const Array& array ); - - /** \brief See StaticArray::operator == (const Array& array) const.*/ - template< typename Array > - __cuda_callable__ - inline bool operator == ( const Array& array ) const; - - /** \brief See StaticArray::operator != (const Array& array) const.*/ - template< typename Array > - __cuda_callable__ - inline bool operator != ( const Array& array ) const; - - template< typename OtherValue > - __cuda_callable__ - operator StaticArray< 3, OtherValue >() const; - - /** \brief See StaticArray::setValue().*/ - __cuda_callable__ - inline void setValue( const ValueType& val ); - - /** \brief See StaticArray::save().*/ - bool save( File& file ) const; - - /** \brief See StaticArray::load().*/ - bool load( File& file); - - /** \brief See StaticArray::sort().*/ - void sort(); - - /** \brief See StaticArray::write().*/ - std::ostream& write( std::ostream& str, const char* separator = " " ) const; - - protected: - Value data[ Size ]; -}; - template< int Size, typename Value > std::ostream& operator << ( std::ostream& str, const StaticArray< Size, Value >& a ); @@ -566,7 +236,4 @@ struct IsStatic< Containers::StaticArray< Size, Value_ > > } // namespace TNL -#include -#include -#include -#include +#include diff --git a/src/TNL/Containers/StaticArray.hpp b/src/TNL/Containers/StaticArray.hpp new file mode 100644 index 0000000000000000000000000000000000000000..582670e3818065b272588fb67abe912d24bef52f --- /dev/null +++ b/src/TNL/Containers/StaticArray.hpp @@ -0,0 +1,336 @@ +/*************************************************************************** + StaticArray_impl.h - description + ------------------- + begin : Feb 10, 2014 + copyright : (C) 2014 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include +#include +#include +#include + +namespace TNL { +namespace Containers { + +namespace Detail { + +//// +// Lambdas used together with StaticFor for static loop unrolling in the +// implementation of the StaticArray +template< typename LeftValue, typename RightValue = LeftValue > +auto assignArrayLambda = [] __cuda_callable__ ( int i, LeftValue* data, const RightValue* v ) { data[ i ] = v[ i ]; }; + +template< typename LeftValue, typename RightValue = LeftValue > +auto assignValueLambda = [] __cuda_callable__ ( int i, LeftValue* data, const RightValue v ) { data[ i ] = v; }; + +//// +// StaticArrayComparator does static loop unrolling of array comparison +template< int Size, typename LeftValue, typename RightValue, int Index > +struct StaticArrayComparator +{ + __cuda_callable__ + static bool EQ( const StaticArray< Size, LeftValue >& left, + const StaticArray< Size, RightValue >& right ) + { + if( left[ Index ] == right[ Index ] ) + return StaticArrayComparator< Size, LeftValue, RightValue, Index + 1 >::EQ( left, right ); + return false; + } +}; + +template< int Size, typename LeftValue, typename RightValue > +struct StaticArrayComparator< Size, LeftValue, RightValue, Size > +{ + __cuda_callable__ + static bool EQ( const StaticArray< Size, LeftValue >& left, + const StaticArray< Size, RightValue >& right ) + { + return true; + } +}; + +//// +// Static array sort does static loop unrolling of array sort. +// It performs static variant of bubble sort as follows: +// +// for( int k = Size - 1; k > 0; k--) +// for( int i = 0; i < k; i++ ) +// if( data[ i ] > data[ i+1 ] ) +// swap( data[ i ], data[ i+1 ] ); +template< int k, int i, typename Value > +struct StaticArraySort +{ + __cuda_callable__ + static void exec( Value* data ) { + if( data[ i ] > data[ i + 1 ] ) + swap( data[ i ], data[ i+1 ] ); + StaticArraySort< k, i + 1, Value >::exec( data ); + } +}; + +template< int k, typename Value > +struct StaticArraySort< k, k, Value > +{ + __cuda_callable__ + static void exec( Value* data ) { + StaticArraySort< k - 1, 0, Value >::exec( data ); + } +}; + +template< typename Value > +struct StaticArraySort< 0, 0, Value > +{ + __cuda_callable__ + static void exec( Value* data ) {} +}; + +} //namespace Detail + + +template< int Size, typename Value > +__cuda_callable__ +constexpr int StaticArray< Size, Value >::getSize() +{ + return Size; +} + +template< int Size, typename Value > +__cuda_callable__ +inline StaticArray< Size, Value >::StaticArray() +{ +}; + + +template< int Size, typename Value > + template< typename _unused > +__cuda_callable__ +StaticArray< Size, Value >::StaticArray( const Value v[ Size ] ) +{ + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value >, data, v ); +} + +template< int Size, typename Value > +__cuda_callable__ +inline StaticArray< Size, Value >::StaticArray( const Value& v ) +{ + StaticFor< 0, Size >::exec( Detail::assignValueLambda< Value >, data, v ); +} + +template< int Size, typename Value > +__cuda_callable__ +inline StaticArray< Size, Value >::StaticArray( const StaticArray< Size, Value >& v ) +{ + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value >, data, v.getData() ); +} + +template< int Size, typename Value > +StaticArray< Size, Value >::StaticArray( const std::initializer_list< Value > &elems) +{ + auto it = elems.begin(); + for( int i = 0; i < getSize(); i++ ) + data[ i ] = *it++; +} + +template< int Size, typename Value > + __cuda_callable__ +StaticArray< Size, Value >::StaticArray( const Value& v1, const Value& v2 ) +{ + static_assert( Size == 2, "This constructor can be called only for arrays with Size = 2." ); + data[ 0 ] = v1; + data[ 1 ] = v2; +} + +template< int Size, typename Value > + __cuda_callable__ +StaticArray< Size, Value >::StaticArray( const Value& v1, const Value& v2, const Value& v3 ) +{ + static_assert( Size == 3, "This constructor can be called only for arrays with Size = 3." ); + data[ 0 ] = v1; + data[ 1 ] = v2; + data[ 2 ] = v3; +} + +template< int Size, typename Value > +String StaticArray< Size, Value >::getType() +{ + return String( "Containers::StaticArray< " ) + + convertToString( Size ) + + String( ", " ) + + TNL::getType< Value >() + + String( " >" ); +} + +template< int Size, typename Value > +__cuda_callable__ +inline Value* StaticArray< Size, Value >::getData() +{ + return data; +} + +template< int Size, typename Value > +__cuda_callable__ +inline const Value* StaticArray< Size, Value >::getData() const +{ + return data; +} + +template< int Size, typename Value > +__cuda_callable__ +inline const Value& StaticArray< Size, Value >::operator[]( int i ) const +{ + TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); + TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); + return data[ i ]; +} + +template< int Size, typename Value > +__cuda_callable__ +inline Value& StaticArray< Size, Value >::operator[]( int i ) +{ + TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); + TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); + return data[ i ]; +} +template< int Size, typename Value > +__cuda_callable__ +inline Value& StaticArray< Size, Value >::x() +{ + return data[ 0 ]; +} + +template< int Size, typename Value > +__cuda_callable__ +inline const Value& StaticArray< Size, Value >::x() const +{ + return data[ 0 ]; +} + +template< int Size, typename Value > +__cuda_callable__ +inline Value& StaticArray< Size, Value >::y() +{ + static_assert( Size > 1, "Cannot call StaticArray< Size, Value >::y() for arrays with Size < 2." ); + return data[ 1 ]; +} + +template< int Size, typename Value > +__cuda_callable__ +inline const Value& StaticArray< Size, Value >::y() const +{ + static_assert( Size > 1, "Cannot call StaticArray< Size, Value >::y() for arrays with Size < 2." ); + return data[ 1 ]; +} + +template< int Size, typename Value > +__cuda_callable__ +inline Value& StaticArray< Size, Value >::z() +{ + static_assert( Size > 1, "Cannot call StaticArray< Size, Value >::z() for arrays with Size < 3." ); + return data[ 2 ]; +} + +template< int Size, typename Value > +__cuda_callable__ +inline const Value& StaticArray< Size, Value >::z() const +{ + static_assert( Size > 1, "Cannot call StaticArray< Size, Value >::z() for arrays with Size < 3." ); + return data[ 2 ]; +} + +template< int Size, typename Value > +__cuda_callable__ +inline StaticArray< Size, Value >& StaticArray< Size, Value >::operator = ( const StaticArray< Size, Value >& array ) +{ + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value >, data, array.getData() ); + return *this; +} + +template< int Size, typename Value > + template< typename Array > +__cuda_callable__ +inline StaticArray< Size, Value >& StaticArray< Size, Value >::operator = ( const Array& array ) +{ + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value, typename Array::ValueType >, data, array.getData() ); + return *this; +} + +template< int Size, typename Value > + template< typename Array > +__cuda_callable__ +inline bool StaticArray< Size, Value >::operator == ( const Array& array ) const +{ + return Detail::StaticArrayComparator< Size, Value, typename Array::ValueType, 0 >::EQ( *this, array ); +} + +template< int Size, typename Value > + template< typename Array > +__cuda_callable__ +inline bool StaticArray< Size, Value >::operator != ( const Array& array ) const +{ + return ! this->operator == ( array ); +} + +template< int Size, typename Value > + template< typename OtherValue > +__cuda_callable__ +StaticArray< Size, Value >:: +operator StaticArray< Size, OtherValue >() const +{ + StaticArray< Size, OtherValue > aux; + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< OtherValue, Value >, aux.getData(), data ); + return aux; +} + +template< int Size, typename Value > +__cuda_callable__ +inline void StaticArray< Size, Value >::setValue( const ValueType& val ) +{ + StaticFor< 0, Size >::exec( Detail::assignValueLambda< Value >, data, val ); +} + +template< int Size, typename Value > +bool StaticArray< Size, Value >::save( File& file ) const +{ + file.save< Value, Value, Devices::Host >( data, Size ); + return true; +} + +template< int Size, typename Value > +bool StaticArray< Size, Value >::load( File& file) +{ + file.load< Value, Value, Devices::Host >( data, Size ); + return true; +} + +template< int Size, typename Value > +void StaticArray< Size, Value >::sort() +{ + Detail::StaticArraySort< Size - 1, 0, Value >::exec( data ); +} + +template< int Size, typename Value > +std::ostream& StaticArray< Size, Value >::write( std::ostream& str, const char* separator ) const +{ + for( int i = 0; i < Size - 1; i++ ) + str << data[ i ] << separator; + str << data[ Size - 1 ]; + return str; +} + +template< int Size, typename Value > +std::ostream& operator << ( std::ostream& str, const StaticArray< Size, Value >& a ) +{ + str << "[ "; + a.write( str, ", " ); + str << " ]"; + return str; +}; + +} // namespace Containers +} // namespace TNL diff --git a/src/TNL/Containers/StaticArray1D_impl.h b/src/TNL/Containers/StaticArray1D_impl.h deleted file mode 100644 index 2a0a2718f8144b224926c1f61388cbaebbac575f..0000000000000000000000000000000000000000 --- a/src/TNL/Containers/StaticArray1D_impl.h +++ /dev/null @@ -1,197 +0,0 @@ -/*************************************************************************** - StaticArray1D_impl.h - description - ------------------- - begin : Feb 10, 2014 - copyright : (C) 2014 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include - -namespace TNL { -namespace Containers { - -template< typename Value > -__cuda_callable__ -constexpr int StaticArray< 1, Value >::getSize() -{ - return Size; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 1, Value >::StaticArray() -{ -} - -template< typename Value > - template< typename _unused > -__cuda_callable__ -inline StaticArray< 1, Value >::StaticArray( const Value v[ Size ] ) -{ - data[ 0 ] = v[ 0 ]; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 1, Value >::StaticArray( const Value& v ) -{ - data[ 0 ] = v; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 1, Value >::StaticArray( const StaticArray< Size, Value >& v ) -{ - data[ 0 ] = v[ 0 ]; -} - -template< typename Value > -StaticArray< 1, Value >::StaticArray( const std::initializer_list< Value > &elems) -{ - auto it = elems.begin(); - for( int i = 0; i < getSize(); i++ ) - data[ i ] = *it++; -} - -template< typename Value > -String StaticArray< 1, Value >::getType() -{ - return String( "Containers::StaticArray< " ) + - convertToString( Size ) + - String( ", " ) + - TNL::getType< Value >() + - String( " >" ); -} - -template< typename Value > -__cuda_callable__ -inline Value* StaticArray< 1, Value >::getData() -{ - return data; -} - -template< typename Value > -__cuda_callable__ -inline const Value* StaticArray< 1, Value >::getData() const -{ - return data; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 1, Value >::operator[]( int i ) const -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 1, Value >::operator[]( int i ) -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 1, Value >::x() -{ - return data[ 0 ]; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 1, Value >::x() const -{ - return data[ 0 ]; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 1, Value >& StaticArray< 1, Value >::operator = ( const StaticArray< 1, Value >& array ) -{ - data[ 0 ] = array[ 0 ]; - return *this; -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -inline StaticArray< 1, Value >& StaticArray< 1, Value >::operator = ( const Array& array ) -{ - data[ 0 ] = array[ 0 ]; - return *this; -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -inline bool StaticArray< 1, Value >::operator == ( const Array& array ) const -{ - return( ( int ) Size == ( int ) Array::getSize() && data[ 0 ] == array[ 0 ] ); -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -inline bool StaticArray< 1, Value >::operator != ( const Array& array ) const -{ - return ! this->operator == ( array ); -} - -template< typename Value > - template< typename OtherValue > -__cuda_callable__ -StaticArray< 1, Value >:: -operator StaticArray< 1, OtherValue >() const -{ - StaticArray< 1, OtherValue > aux; - aux[ 0 ] = data[ 0 ]; - return aux; -} - - -template< typename Value > -__cuda_callable__ -inline void StaticArray< 1, Value >::setValue( const ValueType& val ) -{ - data[ 0 ] = val; -} - -template< typename Value > -bool StaticArray< 1, Value >::save( File& file ) const -{ - file.save< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< typename Value > -bool StaticArray< 1, Value >::load( File& file) -{ - file.load< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< typename Value > -void StaticArray< 1, Value >::sort() -{ -} - -template< typename Value > -std::ostream& StaticArray< 1, Value >::write( std::ostream& str, const char* separator ) const -{ - str << data[ 0 ]; - return str; -} - -} // namespace Containers -} // namespace TNL diff --git a/src/TNL/Containers/StaticArray2D_impl.h b/src/TNL/Containers/StaticArray2D_impl.h deleted file mode 100644 index 2968bee4a7016a8ea3f4ddb51f05f75ca32334f4..0000000000000000000000000000000000000000 --- a/src/TNL/Containers/StaticArray2D_impl.h +++ /dev/null @@ -1,229 +0,0 @@ -/*************************************************************************** - StaticArray2D_impl.h - description - ------------------- - begin : Feb 10, 2014 - copyright : (C) 2014 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include -#include - -namespace TNL { -namespace Containers { - -template< typename Value > -__cuda_callable__ -constexpr int StaticArray< 2, Value >::getSize() -{ - return Size; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 2, Value >::StaticArray() -{ -} - -template< typename Value > - template< typename _unused > -__cuda_callable__ -inline StaticArray< 2, Value >::StaticArray( const Value v[ Size ] ) -{ - data[ 0 ] = v[ 0 ]; - data[ 1 ] = v[ 1 ]; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 2, Value >::StaticArray( const Value& v ) -{ - data[ 0 ] = v; - data[ 1 ] = v; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 2, Value >::StaticArray( const Value& v1, const Value& v2 ) -{ - data[ 0 ] = v1; - data[ 1 ] = v2; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 2, Value >::StaticArray( const StaticArray< Size, Value >& v ) -{ - data[ 0 ] = v[ 0 ]; - data[ 1 ] = v[ 1 ]; -} - -template< typename Value > -StaticArray< 2, Value >::StaticArray( const std::initializer_list< Value > &elems) -{ - auto it = elems.begin(); - for( int i = 0; i < getSize(); i++ ) - data[ i ] = *it++; -} - -template< typename Value > -String StaticArray< 2, Value >::getType() -{ - return String( "Containers::StaticArray< " ) + - convertToString( Size ) + - String( ", " ) + - TNL::getType< Value >() + - String( " >" ); -} - -template< typename Value > -__cuda_callable__ -inline Value* StaticArray< 2, Value >::getData() -{ - return data; -} - -template< typename Value > -__cuda_callable__ -inline const Value* StaticArray< 2, Value >::getData() const -{ - return data; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 2, Value >::operator[]( int i ) const -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 2, Value >::operator[]( int i ) -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 2, Value >::x() -{ - return data[ 0 ]; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 2, Value >::x() const -{ - return data[ 0 ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 2, Value >::y() -{ - return data[ 1 ]; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 2, Value >::y() const -{ - return data[ 1 ]; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 2, Value >& StaticArray< 2, Value >::operator = ( const StaticArray< 2, Value >& array ) -{ - data[ 0 ] = array[ 0 ]; - data[ 1 ] = array[ 1 ]; - return *this; -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -inline StaticArray< 2, Value >& StaticArray< 2, Value >::operator = ( const Array& array ) -{ - data[ 0 ] = array[ 0 ]; - data[ 1 ] = array[ 1 ]; - return *this; -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -inline bool StaticArray< 2, Value >::operator == ( const Array& array ) const -{ - return( ( int ) Size == ( int ) Array::getSize() && - data[ 0 ] == array[ 0 ] && - data[ 1 ] == array[ 1 ] ); -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -inline bool StaticArray< 2, Value >::operator != ( const Array& array ) const -{ - return ! this->operator == ( array ); -} - -template< typename Value > - template< typename OtherValue > -__cuda_callable__ -StaticArray< 2, Value >:: -operator StaticArray< 2, OtherValue >() const -{ - StaticArray< 2, OtherValue > aux; - aux[ 0 ] = data[ 0 ]; - aux[ 1 ] = data[ 1 ]; - return aux; -} - -template< typename Value > -__cuda_callable__ -inline void StaticArray< 2, Value >::setValue( const ValueType& val ) -{ - data[ 1 ] = data[ 0 ] = val; -} - -template< typename Value > -bool StaticArray< 2, Value >::save( File& file ) const -{ - file.save< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< typename Value > -bool StaticArray< 2, Value >::load( File& file) -{ - file.load< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< typename Value > -void StaticArray< 2, Value >::sort() -{ - if( data[ 0 ] > data[ 1 ] ) - swap( data[ 0 ], data[ 1 ] ); -} - -template< typename Value > -std::ostream& StaticArray< 2, Value >::write( std::ostream& str, const char* separator ) const -{ - str << data[ 0 ] << separator << data[ 1 ]; - return str; -} - -} // namespace Containers -} // namespace TNL diff --git a/src/TNL/Containers/StaticArray3D_impl.h b/src/TNL/Containers/StaticArray3D_impl.h deleted file mode 100644 index bc00ccc8e658c68e0349bf7139ae30abbc5abb26..0000000000000000000000000000000000000000 --- a/src/TNL/Containers/StaticArray3D_impl.h +++ /dev/null @@ -1,257 +0,0 @@ -/*************************************************************************** - StaticArray3D_impl.h - description - ------------------- - begin : Feb 10, 2014 - copyright : (C) 2014 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include -#include - -namespace TNL { -namespace Containers { - -template< typename Value > -__cuda_callable__ -constexpr int StaticArray< 3, Value >::getSize() -{ - return Size; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 3, Value >::StaticArray() -{ -} - -template< typename Value > - template< typename _unused > -__cuda_callable__ -inline StaticArray< 3, Value >::StaticArray( const Value v[ Size ] ) -{ - data[ 0 ] = v[ 0 ]; - data[ 1 ] = v[ 1 ]; - data[ 2 ] = v[ 2 ]; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 3, Value >::StaticArray( const Value& v ) -{ - data[ 0 ] = v; - data[ 1 ] = v; - data[ 2 ] = v; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 3, Value >::StaticArray( const Value& v1, const Value& v2, const Value& v3 ) -{ - data[ 0 ] = v1; - data[ 1 ] = v2; - data[ 2 ] = v3; -} - -template< typename Value > -__cuda_callable__ -inline StaticArray< 3, Value >::StaticArray( const StaticArray< Size, Value >& v ) -{ - data[ 0 ] = v[ 0 ]; - data[ 1 ] = v[ 1 ]; - data[ 2 ] = v[ 2 ]; -} - -template< typename Value > -StaticArray< 3, Value >::StaticArray( const std::initializer_list< Value > &elems) -{ - auto it = elems.begin(); - for( int i = 0; i < getSize(); i++ ) - data[ i ] = *it++; -} - -template< typename Value > -String StaticArray< 3, Value >::getType() -{ - return String( "Containers::StaticArray< " ) + - convertToString( Size ) + - String( ", " ) + - TNL::getType< Value >() + - String( " >" ); -} - -template< typename Value > -__cuda_callable__ -inline Value* StaticArray< 3, Value >::getData() -{ - return data; -} - -template< typename Value > -__cuda_callable__ -inline const Value* StaticArray< 3, Value >::getData() const -{ - return data; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 3, Value >::operator[]( int i ) const -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 3, Value >::operator[]( int i ) -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 3, Value >::x() -{ - return data[ 0 ]; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 3, Value >::x() const -{ - return data[ 0 ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 3, Value >::y() -{ - return data[ 1 ]; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 3, Value >::y() const -{ - return data[ 1 ]; -} - -template< typename Value > -__cuda_callable__ -inline Value& StaticArray< 3, Value >::z() -{ - return data[ 2 ]; -} - -template< typename Value > -__cuda_callable__ -inline const Value& StaticArray< 3, Value >::z() const -{ - return data[ 2 ]; -} -template< typename Value > -__cuda_callable__ -StaticArray< 3, Value >& StaticArray< 3, Value >::operator = ( const StaticArray< 3, Value >& array ) -{ - data[ 0 ] = array[ 0 ]; - data[ 1 ] = array[ 1 ]; - data[ 2 ] = array[ 2 ]; - return *this; -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -StaticArray< 3, Value >& StaticArray< 3, Value >::operator = ( const Array& array ) -{ - data[ 0 ] = array[ 0 ]; - data[ 1 ] = array[ 1 ]; - data[ 2 ] = array[ 2 ]; - return *this; -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -bool StaticArray< 3, Value >::operator == ( const Array& array ) const -{ - return( ( int ) Size == ( int ) Array::getSize() && - data[ 0 ] == array[ 0 ] && - data[ 1 ] == array[ 1 ] && - data[ 2 ] == array[ 2 ] ); -} - -template< typename Value > - template< typename Array > -__cuda_callable__ -bool StaticArray< 3, Value >::operator != ( const Array& array ) const -{ - return ! this->operator == ( array ); -} - -template< typename Value > - template< typename OtherValue > -__cuda_callable__ -StaticArray< 3, Value >:: -operator StaticArray< 3, OtherValue >() const -{ - StaticArray< 3, OtherValue > aux; - aux[ 0 ] = data[ 0 ]; - aux[ 1 ] = data[ 1 ]; - aux[ 2 ] = data[ 2 ]; - return aux; -} - -template< typename Value > -__cuda_callable__ -void StaticArray< 3, Value >::setValue( const ValueType& val ) -{ - data[ 2 ] = data[ 1 ] = data[ 0 ] = val; -} - -template< typename Value > -bool StaticArray< 3, Value >::save( File& file ) const -{ - file.save< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< typename Value > -bool StaticArray< 3, Value >::load( File& file) -{ - file.load< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< typename Value > -void StaticArray< 3, Value >::sort() -{ - /**** - * Bubble sort on three elements - */ - if( data[ 0 ] > data[ 1 ] ) - swap( data[ 0 ], data[ 1 ] ); - if( data[ 1 ] > data[ 2 ] ) - swap( data[ 1 ], data[2 ] ); - if( data[ 0 ] > data[ 1 ] ) - swap( data[ 0 ], data[ 1 ] ); -} - -template< typename Value > -std::ostream& StaticArray< 3, Value >::write( std::ostream& str, const char* separator ) const -{ - str << data[ 0 ] << separator << data[ 1 ] << separator << data[ 2 ]; - return str; -} - -} // namespace Containers -} // namespace TNL diff --git a/src/TNL/Containers/StaticArray_impl.h b/src/TNL/Containers/StaticArray_impl.h deleted file mode 100644 index da513b841d4e1c1e78b44bbb3ec96f364387b77e..0000000000000000000000000000000000000000 --- a/src/TNL/Containers/StaticArray_impl.h +++ /dev/null @@ -1,215 +0,0 @@ -/*************************************************************************** - StaticArray_impl.h - description - ------------------- - begin : Feb 10, 2014 - copyright : (C) 2014 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include -#include - -namespace TNL { -namespace Containers { - -template< int Size, typename Value > -__cuda_callable__ -constexpr int StaticArray< Size, Value >::getSize() -{ - return Size; -} - -template< int Size, typename Value > -__cuda_callable__ -inline StaticArray< Size, Value >::StaticArray() -{ -}; - -template< int Size, typename Value > - template< typename _unused > -__cuda_callable__ -inline StaticArray< Size, Value >::StaticArray( const Value v[ Size ] ) -{ - for( int i = 0; i < Size; i++ ) - data[ i ] = v[ i ]; -} - -template< int Size, typename Value > -__cuda_callable__ -inline StaticArray< Size, Value >::StaticArray( const Value& v ) -{ - for( int i = 0; i < Size; i++ ) - data[ i ] = v; -} - -template< int Size, typename Value > -__cuda_callable__ -inline StaticArray< Size, Value >::StaticArray( const StaticArray< Size, Value >& v ) -{ - for( int i = 0; i < Size; i++ ) - data[ i ] = v[ i ]; -} - -template< int Size, typename Value > -StaticArray< Size, Value >::StaticArray( const std::initializer_list< Value > &elems) -{ - auto it = elems.begin(); - for( int i = 0; i < getSize(); i++ ) - data[ i ] = *it++; -} - -template< int Size, typename Value > -String StaticArray< Size, Value >::getType() -{ - return String( "Containers::StaticArray< " ) + - convertToString( Size ) + - String( ", " ) + - TNL::getType< Value >() + - String( " >" ); -} - -template< int Size, typename Value > -__cuda_callable__ -inline Value* StaticArray< Size, Value >::getData() -{ - return data; -} - -template< int Size, typename Value > -__cuda_callable__ -inline const Value* StaticArray< Size, Value >::getData() const -{ - return data; -} - -template< int Size, typename Value > -__cuda_callable__ -inline const Value& StaticArray< Size, Value >::operator[]( int i ) const -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< int Size, typename Value > -__cuda_callable__ -inline Value& StaticArray< Size, Value >::operator[]( int i ) -{ - TNL_ASSERT_GE( i, 0, "Element index must be non-negative." ); - TNL_ASSERT_LT( i, Size, "Element index is out of bounds." ); - return data[ i ]; -} - -template< int Size, typename Value > -__cuda_callable__ -inline StaticArray< Size, Value >& StaticArray< Size, Value >::operator = ( const StaticArray< Size, Value >& array ) -{ - for( int i = 0; i < Size; i++ ) - data[ i ] = array[ i ]; - return *this; -} - -template< int Size, typename Value > - template< typename Array > -__cuda_callable__ -inline StaticArray< Size, Value >& StaticArray< Size, Value >::operator = ( const Array& array ) -{ - for( int i = 0; i < Size; i++ ) - data[ i ] = array[ i ]; - return *this; -} - -template< int Size, typename Value > - template< typename Array > -__cuda_callable__ -inline bool StaticArray< Size, Value >::operator == ( const Array& array ) const -{ - if( ( int ) Size != ( int ) Array::getSize() ) - return false; - for( int i = 0; i < Size; i++ ) - if( data[ i ] != array[ i ] ) - return false; - return true; -} - -template< int Size, typename Value > - template< typename Array > -__cuda_callable__ -inline bool StaticArray< Size, Value >::operator != ( const Array& array ) const -{ - return ! this->operator == ( array ); -} - -template< int Size, typename Value > - template< typename OtherValue > -__cuda_callable__ -StaticArray< Size, Value >:: -operator StaticArray< Size, OtherValue >() const -{ - StaticArray< Size, OtherValue > aux; - for( int i = 0; i < Size; i++ ) - aux[ i ] = data[ i ]; - return aux; -} - -template< int Size, typename Value > -__cuda_callable__ -inline void StaticArray< Size, Value >::setValue( const ValueType& val ) -{ - for( int i = 0; i < Size; i++ ) - data[ i ] = val; -} - -template< int Size, typename Value > -bool StaticArray< Size, Value >::save( File& file ) const -{ - file.save< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< int Size, typename Value > -bool StaticArray< Size, Value >::load( File& file) -{ - file.load< Value, Value, Devices::Host >( data, Size ); - return true; -} - -template< int Size, typename Value > -void StaticArray< Size, Value >::sort() -{ - /**** - * We assume that the array data is small and so - * may sort it with the bubble sort. - */ - for( int k = Size - 1; k > 0; k--) - for( int i = 0; i < k; i++ ) - if( data[ i ] > data[ i+1 ] ) - swap( data[ i ], data[ i+1 ] ); -} - -template< int Size, typename Value > -std::ostream& StaticArray< Size, Value >::write( std::ostream& str, const char* separator ) const -{ - for( int i = 0; i < Size - 1; i++ ) - str << data[ i ] << separator; - str << data[ Size - 1 ]; - return str; -} - - -template< int Size, typename Value > -std::ostream& operator << ( std::ostream& str, const StaticArray< Size, Value >& a ) -{ - str << "[ "; - a.write( str, ", " ); - str << " ]"; - return str; -}; - -} // namespace Containers -} // namespace TNL diff --git a/src/TNL/Containers/StaticVector.h b/src/TNL/Containers/StaticVector.h index 132c64cbfb631f9b1f42dbdfabc3cafda09397fc..11d2575e0c5175473bfd95e061a7f2fdcc88fd72 100644 --- a/src/TNL/Containers/StaticVector.h +++ b/src/TNL/Containers/StaticVector.h @@ -76,6 +76,26 @@ class StaticVector : public StaticArray< Size, Real > StaticVector( const std::initializer_list< Real > &elems ); + /** + * \brief Constructor that sets components of arrays with Size = 2. + * + * \param v1 Real of the first array component. + * \param v2 Real of the second array component. + */ + __cuda_callable__ + inline StaticVector( const Real& v1, const Real& v2 ); + + /** + * \brief Constructor that sets components of arrays with Size = 3. + * + * \param v1 Real of the first array component. + * \param v2 Real of the second array component. + * \param v3 Real of the third array component. + */ + __cuda_callable__ + inline StaticVector( const Real& v1, const Real& v2, const Real& v3 ); + + template< typename T1, typename T2, template< typename, typename > class Operation > @@ -152,314 +172,12 @@ class StaticVector : public StaticArray< Size, Real > template< typename OtherReal > __cuda_callable__ operator StaticVector< Size, OtherReal >() const; - - /** - * \brief Returns static vector with all elements in absolute value. - */ - __cuda_callable__ - StaticVector abs() const; - - /** - * \brief Returns the length of this vector in p-dimensional vector space. - * - * \param p Number specifying the dimension of vector space. - */ - __cuda_callable__ - Real lpNorm( const Real& p ) const; -}; - -/** - * \brief Specific static vector with the size of 1. Works like the class StaticVector. - */ -template< typename Real > -class StaticVector< 1, Real > : public StaticArray< 1, Real > -{ - public: - using RealType = Real; - using IndexType = int; - using ThisType = StaticVector< 1, Real >; - - constexpr static int size = 1; - - using StaticArray< 1, Real >::getSize; - //using StaticArray< 1, Real >::operator ==; - //using StaticArray< 1, Real >::operator !=; - - - - /** \brief See StaticVector::StaticVector().*/ - __cuda_callable__ - StaticVector(); - - /** \brief See StaticVector::StaticVector(const Real v[Size]).*/ - // Note: the template avoids ambiguity of overloaded functions with literal 0 and pointer - // reference: https://stackoverflow.com/q/4610503 - template< typename _unused = void > - __cuda_callable__ - StaticVector( const Real v[ 1 ] ); - - /** \brief See StaticVector::StaticVector( const Real& v ).*/ - __cuda_callable__ - StaticVector( const Real& v ); - - /** \brief See StaticVector::StaticVector( const StaticVector< Size, Real >& v ).*/ - __cuda_callable__ - StaticVector( const StaticVector< 1, Real >& v ); - - StaticVector( const std::initializer_list< Real > &elems ); - - template< typename T1, - typename T2, - template< typename, typename > class Operation > - __cuda_callable__ - StaticVector( const Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation >& op ); - - template< typename T, - template< typename > class Operation > - __cuda_callable__ - StaticVector( const Expressions::StaticUnaryExpressionTemplate< T, Operation >& op ); - - bool setup( const Config::ParameterContainer& parameters, - const String& prefix = "" ); - - /** \brief See StaticVector::getType().*/ - static String getType(); - - template< typename StaticVectorOperationType > - StaticVector& operator = ( const StaticVectorOperationType& vo ); - - /** \brief See StaticVector::operator += ( const StaticVector& v ).*/ - __cuda_callable__ - StaticVector& operator += ( const StaticVector& v ); - - /** \brief See StaticVector::operator -= ( const StaticVector& v ).*/ - __cuda_callable__ - StaticVector& operator -= ( const StaticVector& v ); - - /** \brief See StaticVector::operator *= ( const Real& c ).*/ - __cuda_callable__ - StaticVector& operator *= ( const Real& c ); - - /** \brief See StaticVector::operator /= ( const Real& c ).*/ - __cuda_callable__ - StaticVector& operator /= ( const Real& c ); - - template< typename OtherReal > - __cuda_callable__ - operator StaticVector< 1, OtherReal >() const; - - /** \brief See StaticVector::abs() const.*/ - __cuda_callable__ - StaticVector abs() const; - - /** \brief See StaticVector::lpNorm( const Real& p ) const.*/ - __cuda_callable__ - Real lpNorm( const Real& p ) const; -}; - -/** - * \brief Specific static vector with the size of 2. Works like the class StaticVector. - */ -template< typename Real > -class StaticVector< 2, Real > : public StaticArray< 2, Real > -{ - public: - using RealType = Real; - using IndexType = int; - using ThisType = StaticVector< 2, Real >; - - constexpr static int size = 2; - - using StaticArray< 2, Real >::getSize; - //using StaticArray< 2, Real >::operator ==; - //using StaticArray< 2, Real >::operator !=; - - - /** \brief See StaticVector::StaticVector().*/ - __cuda_callable__ - StaticVector(); - - /** \brief See StaticVector::StaticVector(const Real v[Size]).*/ - // Note: the template avoids ambiguity of overloaded functions with literal 0 and pointer - // reference: https://stackoverflow.com/q/4610503 - template< typename _unused = void > - __cuda_callable__ - StaticVector( const Real v[ 2 ] ); - - /** \brief See StaticVector::StaticVector( const Real& v ).*/ - __cuda_callable__ - StaticVector( const Real& v ); - - /** - * \brief Constructor that sets the two static vector components to value \e v1 and \e v2. - * - * \param v1 Reference to the value of first vector component. - * \param v2 Reference to the value of second vector component. - */ - __cuda_callable__ - StaticVector( const Real& v1, const Real& v2 ); - - /** \brief See StaticVector::StaticVector( const StaticVector< Size, Real >& v ).*/ - __cuda_callable__ - StaticVector( const StaticVector< 2, Real >& v ); - - StaticVector( const std::initializer_list< Real > &elems ); - - template< typename T1, - typename T2, - template< typename, typename > class Operation > - __cuda_callable__ - StaticVector( const Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation >& op ); - - template< typename T, - template< typename > class Operation > - __cuda_callable__ - StaticVector( const Expressions::StaticUnaryExpressionTemplate< T, Operation >& op ); - - bool setup( const Config::ParameterContainer& parameters, - const String& prefix = "" ); - - /** \brief See StaticVector::getType().*/ - static String getType(); - - template< typename StaticVectorOperationType > - StaticVector& operator = ( const StaticVectorOperationType& vo ); - - /** \brief See StaticVector::operator += ( const StaticVector& v ).*/ - __cuda_callable__ - StaticVector& operator += ( const StaticVector& v ); - - /** \brief See StaticVector::operator -= ( const StaticVector& v ).*/ - __cuda_callable__ - StaticVector& operator -= ( const StaticVector& v ); - - /** \brief See StaticVector::operator *= ( const Real& c ).*/ - __cuda_callable__ - StaticVector& operator *= ( const Real& c ); - - /** \brief See StaticVector::operator /= ( const Real& c ).*/ - __cuda_callable__ - StaticVector& operator /= ( const Real& c ); - - template< typename OtherReal > - __cuda_callable__ - operator StaticVector< 2, OtherReal >() const; - - /** \brief See StaticVector::abs() const.*/ - __cuda_callable__ - StaticVector abs() const; - - /** \brief See StaticVector::lpNorm( const Real& p ) const.*/ - __cuda_callable__ - Real lpNorm( const Real& p ) const; -}; - -/** - * \brief Specific static vector with the size of 3. Works like the class StaticVector. - */ -template< typename Real > -class StaticVector< 3, Real > : public StaticArray< 3, Real > -{ - public: - using RealType = Real; - using IndexType = int; - using ThisType = StaticVector< 3, Real >; - - constexpr static int size = 3; - - using StaticArray< 3, Real >::getSize; - //using StaticArray< 3, Real >::operator ==; - //using StaticArray< 3, Real >::operator !=; - - - /** \brief See StaticVector::StaticVector().*/ - __cuda_callable__ - StaticVector(); - - /** \brief See StaticVector::StaticVector(const Real v[Size]).*/ - // Note: the template avoids ambiguity of overloaded functions with literal 0 and pointer - // reference: https://stackoverflow.com/q/4610503 - template< typename _unused = void > - __cuda_callable__ - StaticVector( const Real v[ 3 ] ); - - /** \brief See StaticVector::StaticVector( const Real& v ).*/ - __cuda_callable__ - StaticVector( const Real& v ); - - /** - * \brief Constructor that sets the three static vector components to value \e v1 \e v2 and \e v3. - * - * \param v1 Reference to the value of first vector component. - * \param v2 Reference to the value of second vector component. - * \param v3 Reference to the value of third vector component. - */ - __cuda_callable__ - StaticVector( const Real& v1, const Real& v2, const Real& v3 ); - - /** \brief See StaticVector::StaticVector( const StaticVector< Size, Real >& v ).*/ - __cuda_callable__ - StaticVector( const StaticVector< 3, Real >& v ); - - StaticVector( const std::initializer_list< Real > &elems ); - - template< typename T1, - typename T2, - template< typename, typename > class Operation > - __cuda_callable__ - StaticVector( const Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation >& op ); - - template< typename T, - template< typename > class Operation > - __cuda_callable__ - StaticVector( const Expressions::StaticUnaryExpressionTemplate< T, Operation >& op ); - - bool setup( const Config::ParameterContainer& parameters, - const String& prefix = "" ); - - /** \brief See StaticVector::getType().*/ - static String getType(); - - template< typename StaticVectorOperationType > - StaticVector& operator = ( const StaticVectorOperationType& vo ); - - /** \brief See StaticVector::operator += ( const StaticVector& v ).*/ - __cuda_callable__ - StaticVector& operator += ( const StaticVector& v ); - - /** \brief See StaticVector::operator -= ( const StaticVector& v ).*/ - __cuda_callable__ - StaticVector& operator -= ( const StaticVector& v ); - - /** \brief See StaticVector::operator *= ( const Real& c ).*/ - __cuda_callable__ - StaticVector& operator *= ( const Real& c ); - - /** \brief See StaticVector::operator /= ( const Real& c ).*/ - __cuda_callable__ - StaticVector& operator /= ( const Real& c ); - - template< typename OtherReal > - __cuda_callable__ - operator StaticVector< 3, OtherReal >() const; - - /** \brief See StaticVector::abs() const.*/ - __cuda_callable__ - StaticVector abs() const; - - /** \brief See StaticVector::lpNorm( const Real& p ) const.*/ - __cuda_callable__ - Real lpNorm( const Real& p ) const; - }; } // namespace Containers } // namespace TNL -#include -#include -#include -#include +#include namespace TNL { @@ -592,4 +310,4 @@ struct IsStatic< Containers::StaticVector< Size, Real > > } // namespace TNL #include -#include \ No newline at end of file +#include diff --git a/src/TNL/Containers/StaticVector_impl.h b/src/TNL/Containers/StaticVector.hpp similarity index 72% rename from src/TNL/Containers/StaticVector_impl.h rename to src/TNL/Containers/StaticVector.hpp index aa281b4c4e43a79399f7b27093ff611fa6a36256..78e7e21a8d1a5c4e3391614d8dba05e02e396770 100644 --- a/src/TNL/Containers/StaticVector_impl.h +++ b/src/TNL/Containers/StaticVector.hpp @@ -17,6 +17,22 @@ namespace TNL { namespace Containers { +namespace Detail { + +//// +// Lambdas used together with StaticFor for static loop unrolling in the +// implementation of the StaticVector +template< typename LeftReal, typename RightReal = LeftReal > +auto addVectorLambda = [] __cuda_callable__ ( int i, LeftReal* data, const RightReal* v ) { data[ i ] += v[ i ]; }; + +template< typename LeftReal, typename RightReal = LeftReal > +auto subtractVectorLambda = [] __cuda_callable__ ( int i, LeftReal* data, const RightReal* v ) { data[ i ] -= v[ i ]; }; + +template< typename LeftReal, typename RightReal = LeftReal > +auto scalarMultiplicationLambda = [] __cuda_callable__ ( int i, LeftReal* data, const RightReal v ) { data[ i ] *= v; }; + +} //namespace Detail + template< int Size, typename Real > __cuda_callable__ StaticVector< Size, Real >::StaticVector() @@ -51,6 +67,20 @@ StaticVector< Size, Real >::StaticVector( const std::initializer_list< Real > &e { } +template< int Size, typename Real > + __cuda_callable__ +StaticVector< Size, Real >::StaticVector( const Real& v1, const Real& v2 ) +: StaticArray< Size, Real >( v1, v2 ) +{ +} + +template< int Size, typename Real > + __cuda_callable__ +StaticVector< Size, Real >::StaticVector( const Real& v1, const Real& v2, const Real& v3 ) +: StaticArray< Size, Real >( v1, v2, v3 ) +{ +} + template< int Size, typename Real > template< typename T1, typename T2, @@ -76,8 +106,12 @@ StaticVector< Size, Real >::setup( const Config::ParameterContainer& parameters, const String& prefix ) { for( int i = 0; i < Size; i++ ) - if( ! parameters.template getParameter< double >( prefix + convertToString( i ), this->data[ i ] ) ) + { + double aux; + if( ! parameters.template getParameter< double >( prefix + convertToString( i ), aux ) ) return false; + this->data[ i ] = aux; + } return true; } @@ -104,8 +138,7 @@ template< int Size, typename Real > __cuda_callable__ StaticVector< Size, Real >& StaticVector< Size, Real >::operator += ( const StaticVector& v ) { - for( int i = 0; i < Size; i++ ) - this->data[ i ] += v[ i ]; + StaticFor< 0, Size >::exec( Detail::addVectorLambda< Real >, this->getData(), v.getData() ); return *this; } @@ -113,8 +146,7 @@ template< int Size, typename Real > __cuda_callable__ StaticVector< Size, Real >& StaticVector< Size, Real >::operator -= ( const StaticVector& v ) { - for( int i = 0; i < Size; i++ ) - this->data[ i ] -= v[ i ]; + StaticFor< 0, Size >::exec( Detail::subtractVectorLambda< Real >, this->getData(), v.getData() ); return *this; } @@ -122,8 +154,7 @@ template< int Size, typename Real > __cuda_callable__ StaticVector< Size, Real >& StaticVector< Size, Real >::operator *= ( const Real& c ) { - for( int i = 0; i < Size; i++ ) - this->data[ i ] *= c; + StaticFor< 0, Size >::exec( Detail::scalarMultiplicationLambda< Real >, this->getData(), c ); return *this; } @@ -131,9 +162,7 @@ template< int Size, typename Real > __cuda_callable__ StaticVector< Size, Real >& StaticVector< Size, Real >::operator /= ( const Real& c ) { - const RealType d = 1.0 / c; - for( int i = 0; i < Size; i++ ) - this->data[ i ] *= d; + StaticFor< 0, Size >::exec( Detail::scalarMultiplicationLambda< Real >, this->getData(), 1.0 / c ); return *this; } @@ -144,46 +173,9 @@ StaticVector< Size, Real >:: operator StaticVector< Size, OtherReal >() const { StaticVector< Size, OtherReal > aux; - for( int i = 0; i < Size; i++ ) - aux[ i ] = this->data[ i ]; + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< OtherReal, Real >, aux.getData(), this->getData() ); return aux; } -template< int Size, typename Real > -__cuda_callable__ -StaticVector< Size, Real > -StaticVector< Size, Real >::abs() const -{ - StaticVector< Size, Real > v; - for( int i = 0; i < Size; i++ ) - v.data[ i ] = TNL::abs( this->data[ i ] ); - return v; -} - -template< int Size, typename Real > -__cuda_callable__ -Real -StaticVector< Size, Real >::lpNorm( const Real& p ) const -{ - if( p == 1.0 ) - { - Real aux = TNL::abs( this->data[ 0 ] ); - for( int i = 1; i < Size; i++ ) - aux += TNL::abs( this->data[ i ] ); - return aux; - } - if( p == 2.0 ) - { - Real aux = this->data[ 0 ] * this->data[ 0 ]; - for( int i = 1; i < Size; i++ ) - aux += this->data[ i ] * this->data[ i ]; - return TNL::sqrt( aux ); - } - Real aux = TNL::pow( TNL::abs( this->data[ 0 ] ), p ); - for( int i = 1; i < Size; i++ ) - aux += TNL::pow( TNL::abs( this->data[ i ] ), p ); - return TNL::pow( aux, 1.0 / p ); -} - } // namespace Containers } // namespace TNL diff --git a/src/TNL/Containers/StaticVector1D_impl.h b/src/TNL/Containers/StaticVector1D_impl.h deleted file mode 100644 index 5e2fdcaba47f71b14decd16539db06fcc63b28b2..0000000000000000000000000000000000000000 --- a/src/TNL/Containers/StaticVector1D_impl.h +++ /dev/null @@ -1,161 +0,0 @@ -/*************************************************************************** - StaticVector1D_impl.h - description - ------------------- - begin : Feb 10, 2014 - copyright : (C) 2014 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include -#include - -namespace TNL { -namespace Containers { - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real >::StaticVector() -{ -} - -template< typename Real > - template< typename _unused > -__cuda_callable__ -StaticVector< 1, Real >::StaticVector( const Real v[ 1 ] ) -: StaticArray< 1, Real >( v ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real >::StaticVector( const Real& v ) -: StaticArray< 1, Real >( v ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real >::StaticVector( const StaticVector< 1, Real >& v ) -: StaticArray< 1, Real >( v ) -{ -} - -template< typename Real > -StaticVector< 1, Real >::StaticVector( const std::initializer_list< Real > &elems ) -: StaticArray< 1, Real >( elems ) -{ -} - -template< typename Real > - template< typename T1, - typename T2, - template< typename, typename > class Operation > -__cuda_callable__ -StaticVector< 1, Real >::StaticVector( const Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation >& op ) -{ - Algorithms::VectorAssignment< StaticVector< 1, Real >, Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation > >::assignStatic( *this, op ); -}; - -template< typename Real > - template< typename T, - template< typename > class Operation > -__cuda_callable__ -StaticVector< 1, Real >::StaticVector( const Expressions::StaticUnaryExpressionTemplate< T, Operation >& op ) -{ - Algorithms::VectorAssignment< StaticVector< 1, Real >, Expressions::StaticUnaryExpressionTemplate< T, Operation > >::assignStatic( *this, op ); -}; - -template< typename Real > -bool -StaticVector< 1, Real >::setup( const Config::ParameterContainer& parameters, - const String& prefix ) -{ - this->data[ 0 ] = parameters.getParameter< double >( prefix + "0" ); - return true; -} - -template< typename Real > -String StaticVector< 1, Real >::getType() -{ - return String( "Containers::StaticVector< " ) + - convertToString( 1 ) + - String( ", " ) + - TNL::getType< Real >() + - String( " >" ); -} - -template< typename Real > - template< typename RHS > -StaticVector< 1, Real >& -StaticVector< 1, Real >::operator =( const RHS& rhs ) -{ - Algorithms::VectorAssignment< StaticVector< 1, Real >, RHS >::assignStatic( *this, rhs ); - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real >& StaticVector< 1, Real >::operator += ( const StaticVector& v ) -{ - this->data[ 0 ] += v[ 0 ]; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real >& StaticVector< 1, Real >::operator -= ( const StaticVector& v ) -{ - this->data[ 0 ] -= v[ 0 ]; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real >& StaticVector< 1, Real >::operator *= ( const Real& c ) -{ - this->data[ 0 ] *= c; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real >& StaticVector< 1, Real >::operator /= ( const Real& c ) -{ - this->data[ 0 ] /= c; - return *this; -} - -template< typename Real > - template< typename OtherReal > -__cuda_callable__ -StaticVector< 1, Real >:: -operator StaticVector< 1, OtherReal >() const -{ - StaticVector< 1, OtherReal > aux; - aux[ 0 ] = this->data[ 0 ]; - return aux; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 1, Real > -StaticVector< 1, Real >::abs() const -{ - return StaticVector< 1, Real >( TNL::abs( this->data[ 0 ] ) ); -} - -template< typename Real > -__cuda_callable__ -Real -StaticVector< 1, Real >::lpNorm( const Real& p ) const -{ - return TNL::abs( this->data[ 0 ] ); -} - -} // namespace Containers -} // namespace TNL diff --git a/src/TNL/Containers/StaticVector2D_impl.h b/src/TNL/Containers/StaticVector2D_impl.h deleted file mode 100644 index b1606b6540f752815731dc02803bde6bf0fb7909..0000000000000000000000000000000000000000 --- a/src/TNL/Containers/StaticVector2D_impl.h +++ /dev/null @@ -1,181 +0,0 @@ -/*************************************************************************** - StaticVector2D_impl.h - description - ------------------- - begin : Feb 10, 2014 - copyright : (C) 2014 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include - -namespace TNL { -namespace Containers { - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >::StaticVector() -{ -} - -template< typename Real > - template< typename _unused > -__cuda_callable__ -StaticVector< 2, Real >::StaticVector( const Real v[ 2 ] ) -: StaticArray< 2, Real >( v ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >::StaticVector( const Real& v ) -: StaticArray< 2, Real >( v ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >::StaticVector( const Real& v1, const Real& v2 ) -: StaticArray< 2, Real >( v1, v2 ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >::StaticVector( const StaticVector< 2, Real >& v ) -: StaticArray< 2, Real >( v ) -{ -} - -template< typename Real > -StaticVector< 2, Real >::StaticVector( const std::initializer_list< Real > &elems ) -: StaticArray< 2, Real >( elems ) -{ -} - -template< typename Real > - template< typename T1, - typename T2, - template< typename, typename > class Operation > -__cuda_callable__ -StaticVector< 2, Real >::StaticVector( const Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation >& op ) -{ - Algorithms::VectorAssignment< StaticVector< 2, Real >, Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation > >::assignStatic( *this, op ); -}; - -template< typename Real > - template< typename T, - template< typename > class Operation > -__cuda_callable__ -StaticVector< 2, Real >::StaticVector( const Expressions::StaticUnaryExpressionTemplate< T, Operation >& op ) -{ - Algorithms::VectorAssignment< StaticVector< 2, Real >, Expressions::StaticUnaryExpressionTemplate< T, Operation > >::assignStatic( *this, op ); -}; - -template< typename Real > -bool -StaticVector< 2, Real >::setup( const Config::ParameterContainer& parameters, - const String& prefix ) -{ - this->data[ 0 ] = parameters.getParameter< double >( prefix + "0" ); - this->data[ 1 ] = parameters.getParameter< double >( prefix + "1" ); - return true; -} - -template< typename Real > -String StaticVector< 2, Real >::getType() -{ - return String( "Containers::StaticVector< " ) + - convertToString( 2 ) + - String( ", " ) + - TNL::getType< Real >() + - String( " >" ); -} - -template< typename Real > - template< typename RHS > -StaticVector< 2, Real >& -StaticVector< 2, Real >::operator =( const RHS& rhs ) -{ - Algorithms::VectorAssignment< StaticVector< 2, Real >, RHS >::assignStatic( *this, rhs ); - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >& StaticVector< 2, Real >::operator += ( const StaticVector& v ) -{ - this->data[ 0 ] += v[ 0 ]; - this->data[ 1 ] += v[ 1 ]; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >& StaticVector< 2, Real >::operator -= ( const StaticVector& v ) -{ - this->data[ 0 ] -= v[ 0 ]; - this->data[ 1 ] -= v[ 1 ]; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >& StaticVector< 2, Real >::operator *= ( const Real& c ) -{ - this->data[ 0 ] *= c; - this->data[ 1 ] *= c; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real >& StaticVector< 2, Real >::operator /= ( const Real& c ) -{ - const RealType d = 1.0 /c; - this->data[ 0 ] *= d; - this->data[ 1 ] *= d; - return *this; -} - -template< typename Real > - template< typename OtherReal > -__cuda_callable__ -StaticVector< 2, Real >:: -operator StaticVector< 2, OtherReal >() const -{ - StaticVector< 2, OtherReal > aux; - aux[ 0 ] = this->data[ 0 ]; - aux[ 1 ] = this->data[ 1 ]; - return aux; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 2, Real > -StaticVector< 2, Real >::abs() const -{ - return StaticVector< 2, Real >( TNL::abs( this->data[ 0 ] ), - TNL::abs( this->data[ 1 ] ) ); -} - -template< typename Real > -__cuda_callable__ -Real -StaticVector< 2, Real >::lpNorm( const Real& p ) const -{ - if( p == 1.0 ) - return TNL::abs( this->data[ 0 ] ) + TNL::abs( this->data[ 1 ] ); - if( p == 2.0 ) - return TNL::sqrt( this->data[ 0 ] * this->data[ 0 ] + - this->data[ 1 ] * this->data[ 1 ] ); - return TNL::pow( TNL::pow( TNL::abs( this->data[ 0 ] ), p ) + - TNL::pow( TNL::abs( this->data[ 1 ] ), p ), 1.0 / p ); -} - -} // namespace Containers -} // namespace TNL diff --git a/src/TNL/Containers/StaticVector3D_impl.h b/src/TNL/Containers/StaticVector3D_impl.h deleted file mode 100644 index 36a89cb6958d3b83d1d41987d7c18c04b584b740..0000000000000000000000000000000000000000 --- a/src/TNL/Containers/StaticVector3D_impl.h +++ /dev/null @@ -1,193 +0,0 @@ -/*************************************************************************** - StaticVector3D_impl.h - description - ------------------- - begin : Feb 10, 2014 - copyright : (C) 2014 by Tomas Oberhuber - email : tomas.oberhuber@fjfi.cvut.cz - ***************************************************************************/ - -/* See Copyright Notice in tnl/Copyright */ - -#pragma once - -#include -#include - -namespace TNL { -namespace Containers { - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >::StaticVector() -{ -} - -template< typename Real > - template< typename _unused > -__cuda_callable__ -StaticVector< 3, Real >::StaticVector( const Real v[ 3 ] ) -: StaticArray< 3, Real >( v ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >::StaticVector( const Real& v ) -: StaticArray< 3, Real >( v ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >::StaticVector( const Real& v1, const Real& v2, const Real& v3 ) -: StaticArray< 3, Real >( v1, v2, v3 ) -{ -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >::StaticVector( const StaticVector< 3, Real >& v ) -: StaticArray< 3, Real >( v ) -{ -} - -template< typename Real > -StaticVector< 3, Real >::StaticVector( const std::initializer_list< Real > &elems ) -: StaticArray< 3, Real >( elems ) -{ -} - -template< typename Real > - template< typename T1, - typename T2, - template< typename, typename > class Operation > -__cuda_callable__ -StaticVector< 3, Real >::StaticVector( const Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation >& op ) -{ - Algorithms::VectorAssignment< StaticVector< 3, Real >, Expressions::StaticBinaryExpressionTemplate< T1, T2, Operation > >::assignStatic( *this, op ); -}; - -template< typename Real > - template< typename T, - template< typename > class Operation > -__cuda_callable__ -StaticVector< 3, Real >::StaticVector( const Expressions::StaticUnaryExpressionTemplate< T, Operation >& op ) -{ - Algorithms::VectorAssignment< StaticVector< 3, Real >, Expressions::StaticUnaryExpressionTemplate< T, Operation > >::assignStatic( *this, op ); -}; - - -template< typename Real > -bool -StaticVector< 3, Real >::setup( const Config::ParameterContainer& parameters, - const String& prefix ) -{ - this->data[ 0 ] = parameters.getParameter< double >( prefix + "0" ); - this->data[ 1 ] = parameters.getParameter< double >( prefix + "1" ); - this->data[ 2 ] = parameters.getParameter< double >( prefix + "2" ); - return true; -} - -template< typename Real > -String StaticVector< 3, Real >::getType() -{ - return String( "Containers::StaticVector< " ) + - convertToString( 3 ) + - String( ", " ) + - TNL::getType< Real >() + - String( " >" ); -} - -template< typename Real > - template< typename RHS > -StaticVector< 3, Real >& -StaticVector< 3, Real >::operator =( const RHS& rhs ) -{ - Algorithms::VectorAssignment< StaticVector< 3, Real >, RHS >::assignStatic( *this, rhs ); - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >& StaticVector< 3, Real >::operator += ( const StaticVector& v ) -{ - this->data[ 0 ] += v[ 0 ]; - this->data[ 1 ] += v[ 1 ]; - this->data[ 2 ] += v[ 2 ]; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >& StaticVector< 3, Real >::operator -= ( const StaticVector& v ) -{ - this->data[ 0 ] -= v[ 0 ]; - this->data[ 1 ] -= v[ 1 ]; - this->data[ 2 ] -= v[ 2 ]; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >& StaticVector< 3, Real >::operator *= ( const Real& c ) -{ - this->data[ 0 ] *= c; - this->data[ 1 ] *= c; - this->data[ 2 ] *= c; - return *this; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real >& StaticVector< 3, Real >::operator /= ( const Real& c ) -{ - const RealType d = 1.0 / c; - this->data[ 0 ] *= d; - this->data[ 1 ] *= d; - this->data[ 2 ] *= d; - return *this; -} - -template< typename Real > - template< typename OtherReal > -__cuda_callable__ -StaticVector< 3, Real >:: -operator StaticVector< 3, OtherReal >() const -{ - StaticVector< 3, OtherReal > aux; - aux[ 0 ] = this->data[ 0 ]; - aux[ 1 ] = this->data[ 1 ]; - aux[ 2 ] = this->data[ 2 ]; - return aux; -} - -template< typename Real > -__cuda_callable__ -StaticVector< 3, Real > -StaticVector< 3, Real >::abs() const -{ - return StaticVector< 3, Real >( TNL::abs( this->data[ 0 ] ), - TNL::abs( this->data[ 1 ] ), - TNL::abs( this->data[ 2 ] ) ); -} - -template< typename Real > -__cuda_callable__ -Real -StaticVector< 3, Real >::lpNorm( const Real& p ) const -{ - if( p == 1.0 ) - return TNL::abs( this->data[ 0 ] ) + - TNL::abs( this->data[ 1 ] ) + - TNL::abs( this->data[ 2 ] ); - if( p == 2.0 ) - return TNL::sqrt( this->data[ 0 ] * this->data[ 0 ] + - this->data[ 1 ] * this->data[ 1 ] + - this->data[ 2 ] * this->data[ 2 ] ); - return TNL::pow( TNL::pow( TNL::abs( this->data[ 0 ] ), p ) + - TNL::pow( TNL::abs( this->data[ 1 ] ), p ) + - TNL::pow( TNL::abs( this->data[ 2 ] ), p ), 1.0 / p ); -} - -} // namespace Containers -} // namespace TNL diff --git a/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter1D_impl.h b/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter1D_impl.h index 737d38c261882245338439d8e13fe25f8303f2ee..b8983feed92441a040ab8cab310afa36e8ef1b84 100644 --- a/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter1D_impl.h +++ b/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter1D_impl.h @@ -14,7 +14,7 @@ #include #include #include -#include +#include namespace TNL { namespace Meshes { @@ -176,7 +176,7 @@ class NeighborGridEntityGetter< void refresh( const GridType& grid, const IndexType& entityIndex ) { #ifndef HAVE_CUDA // TODO: fix it -- does not work with nvcc - StaticFor< IndexType, -stencilSize, stencilSize + 1, StencilRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, stencilSize + 1, StencilRefresher >::exec( *this, entityIndex ); #endif }; diff --git a/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter2D_impl.h b/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter2D_impl.h index 5332eed97a3468270316f58f94bb228fa960a3cb..5b0e48767f6b530388fd0dd92612c12b3b0fcbb8 100644 --- a/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter2D_impl.h +++ b/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter2D_impl.h @@ -199,9 +199,9 @@ class NeighborGridEntityGetter< void refresh( const GridType& grid, const IndexType& entityIndex ) { #ifndef HAVE_CUDA // TODO: fix this to work with CUDA - StaticFor< IndexType, -stencilSize, 0, StencilYRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, 1, stencilSize + 1, StencilYRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, -stencilSize, stencilSize + 1, StencilXRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, 0, StencilYRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, 1, stencilSize + 1, StencilYRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, stencilSize + 1, StencilXRefresher >::exec( *this, entityIndex ); #endif }; diff --git a/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter3D_impl.h b/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter3D_impl.h index 78955a6c0a55318f925212751b33fc408571359c..5fe5329bb3284ed578f502ad6828a84842fcc5eb 100644 --- a/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter3D_impl.h +++ b/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter3D_impl.h @@ -14,7 +14,7 @@ #include #include #include -#include +#include namespace TNL { namespace Meshes { @@ -223,11 +223,11 @@ class NeighborGridEntityGetter< void refresh( const GridType& grid, const IndexType& entityIndex ) { #ifndef HAVE_CUDA // TODO: fix this to work with CUDA - StaticFor< IndexType, -stencilSize, 0, StencilZRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, 1, stencilSize + 1, StencilZRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, -stencilSize, 0, StencilYRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, 1, stencilSize + 1, StencilYRefresher >::exec( *this, entityIndex ); - StaticFor< IndexType, -stencilSize, stencilSize + 1, StencilXRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, 0, StencilZRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, 1, stencilSize + 1, StencilZRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, 0, StencilYRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, 1, stencilSize + 1, StencilYRefresher >::exec( *this, entityIndex ); + TemplateStaticFor< IndexType, -stencilSize, stencilSize + 1, StencilXRefresher >::exec( *this, entityIndex ); #endif }; diff --git a/src/TNL/Meshes/MeshDetails/EntityStorageRebinder.h b/src/TNL/Meshes/MeshDetails/EntityStorageRebinder.h index 7dff9d43e7f4a0c621efd26f8582dfaab541df01..7630a2d64920de41dbbc3f3f2e17603d2d3aabc1 100644 --- a/src/TNL/Meshes/MeshDetails/EntityStorageRebinder.h +++ b/src/TNL/Meshes/MeshDetails/EntityStorageRebinder.h @@ -29,7 +29,7 @@ #include #include #include -#include +#include namespace TNL { namespace Meshes { @@ -144,14 +144,14 @@ public: static void exec( Mesh& mesh ) { - StaticFor< int, Dimension + 1, Mesh::getMeshDimension() + 1, Inner >::execHost( mesh ); + TemplateStaticFor< int, Dimension + 1, Mesh::getMeshDimension() + 1, Inner >::execHost( mesh ); } }; public: static void exec( Mesh& mesh ) { - StaticFor< int, 0, Mesh::getMeshDimension() + 1, OuterLoop >::execHost( mesh ); + TemplateStaticFor< int, 0, Mesh::getMeshDimension() + 1, OuterLoop >::execHost( mesh ); } }; diff --git a/src/TNL/Meshes/MeshDetails/IndexPermutationApplier.h b/src/TNL/Meshes/MeshDetails/IndexPermutationApplier.h index cdd98bc57acdfd9a7343ba7812dd1181175d8298..c98f145d0876b38c25ff7a77368de7b33f6f0656 100644 --- a/src/TNL/Meshes/MeshDetails/IndexPermutationApplier.h +++ b/src/TNL/Meshes/MeshDetails/IndexPermutationApplier.h @@ -167,16 +167,16 @@ public: entities.getData() ); // permute superentities storage - StaticFor< int, 0, Dimension, SubentitiesStorageWorker >::execHost( mesh, perm ); + TemplateStaticFor< int, 0, Dimension, SubentitiesStorageWorker >::execHost( mesh, perm ); // permute subentities storage - StaticFor< int, Dimension + 1, Mesh::getMeshDimension() + 1, SuperentitiesStorageWorker >::execHost( mesh, perm ); + TemplateStaticFor< int, Dimension + 1, Mesh::getMeshDimension() + 1, SuperentitiesStorageWorker >::execHost( mesh, perm ); // update superentity indices from the subentities - StaticFor< int, 0, Dimension, SubentitiesWorker >::execHost( mesh, iperm ); + TemplateStaticFor< int, 0, Dimension, SubentitiesWorker >::execHost( mesh, iperm ); // update subentity indices from the superentities - StaticFor< int, Dimension + 1, Mesh::getMeshDimension() + 1, SuperentitiesWorker >::execHost( mesh, iperm ); + TemplateStaticFor< int, Dimension + 1, Mesh::getMeshDimension() + 1, SuperentitiesWorker >::execHost( mesh, iperm ); } }; diff --git a/src/TNL/Meshes/MeshDetails/MeshLayers/BoundaryTags/Initializer.h b/src/TNL/Meshes/MeshDetails/MeshLayers/BoundaryTags/Initializer.h index 918f4e025529dfde0117fbe2886d413e8bebde22..21ccd0ccd4c2b82979d4d636b7ec9a039fcd4175 100644 --- a/src/TNL/Meshes/MeshDetails/MeshLayers/BoundaryTags/Initializer.h +++ b/src/TNL/Meshes/MeshDetails/MeshLayers/BoundaryTags/Initializer.h @@ -10,7 +10,7 @@ #pragma once -#include +#include #include #include #include @@ -121,8 +121,8 @@ public: public: static void exec( Mesh& mesh ) { - StaticFor< int, 0, Mesh::getMeshDimension() + 1, SetEntitiesCount >::execHost( mesh ); - StaticFor< int, 0, Mesh::getMeshDimension() + 1, ResetBoundaryTags >::execHost( mesh ); + TemplateStaticFor< int, 0, Mesh::getMeshDimension() + 1, SetEntitiesCount >::execHost( mesh ); + TemplateStaticFor< int, 0, Mesh::getMeshDimension() + 1, ResetBoundaryTags >::execHost( mesh ); auto kernel = [] __cuda_callable__ ( GlobalIndexType faceIndex, @@ -136,7 +136,7 @@ public: const GlobalIndexType cellIndex = face.template getSuperentityIndex< Mesh::getMeshDimension() >( 0 ); mesh->template setIsBoundaryEntity< Mesh::getMeshDimension() >( cellIndex, true ); // initialize all subentities - StaticFor< int, 0, Mesh::getMeshDimension() - 1, InitializeSubentities >::exec( *mesh, faceIndex, face ); + TemplateStaticFor< int, 0, Mesh::getMeshDimension() - 1, InitializeSubentities >::exec( *mesh, faceIndex, face ); } }; @@ -146,7 +146,7 @@ public: kernel, &meshPointer.template modifyData< DeviceType >() ); - StaticFor< int, 0, Mesh::getMeshDimension() + 1, UpdateBoundaryIndices >::execHost( mesh ); + TemplateStaticFor< int, 0, Mesh::getMeshDimension() + 1, UpdateBoundaryIndices >::execHost( mesh ); } }; diff --git a/src/TNL/Meshes/MeshDetails/initializer/EntityInitializer.h b/src/TNL/Meshes/MeshDetails/initializer/EntityInitializer.h index 684e60543bc5b8ab8e5702c070c18294706df910..7058691a301d569fdb8e230ba2889f4929e409c9 100644 --- a/src/TNL/Meshes/MeshDetails/initializer/EntityInitializer.h +++ b/src/TNL/Meshes/MeshDetails/initializer/EntityInitializer.h @@ -16,7 +16,7 @@ #pragma once -#include +#include #include #include diff --git a/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h b/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h index 25200c932a9918a3e74402ecc887fe2b19f8c041..8ad08bd63d3b9a6a1be186ff73baa34370da2d28 100644 --- a/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h +++ b/src/TNL/Meshes/MeshDetails/initializer/SubentitySeedsCreator.h @@ -16,7 +16,7 @@ #pragma once -#include +#include #include namespace TNL { @@ -47,7 +47,7 @@ public: static SubentitySeedArray create( const SubvertexAccessorType& subvertices ) { SubentitySeedArray subentitySeeds; - StaticFor< LocalIndexType, 0, SUBENTITIES_COUNT, CreateSubentitySeeds >::execHost( subentitySeeds, subvertices ); + TemplateStaticFor< LocalIndexType, 0, SUBENTITIES_COUNT, CreateSubentitySeeds >::execHost( subentitySeeds, subvertices ); return subentitySeeds; } @@ -61,7 +61,7 @@ private: public: static void exec( SubentitySeedArray& subentitySeeds, const SubvertexAccessorType& subvertices ) { - StaticFor< LocalIndexType, 0, SUBENTITY_VERTICES_COUNT, SetSubentitySeedVertex >::execHost( subentitySeeds[ subentityIndex ], subvertices ); + TemplateStaticFor< LocalIndexType, 0, SUBENTITY_VERTICES_COUNT, SetSubentitySeedVertex >::execHost( subentitySeeds[ subentityIndex ], subvertices ); } private: diff --git a/src/TNL/Meshes/Writers/VTKWriter_impl.h b/src/TNL/Meshes/Writers/VTKWriter_impl.h index b3f1723b7444f60aabf445466a5829256c692d9c..97d98bf6e0d3c62939794cb4fe5ba7d681d4fffa 100644 --- a/src/TNL/Meshes/Writers/VTKWriter_impl.h +++ b/src/TNL/Meshes/Writers/VTKWriter_impl.h @@ -407,10 +407,10 @@ VTKWriter< Mesh >::writeAllEntities( const Mesh& mesh, std::ostream& str ) const Index cellsListSize = __impl::getCellsListSize( mesh ); str << std::endl << "CELLS " << allEntitiesCount << " " << cellsListSize << std::endl; - StaticFor< int, 0, Mesh::getMeshDimension() + 1, EntitiesWriter >::exec( mesh, str ); + TemplateStaticFor< int, 0, Mesh::getMeshDimension() + 1, EntitiesWriter >::exec( mesh, str ); str << std::endl << "CELL_TYPES " << allEntitiesCount << std::endl; - StaticFor< int, 0, Mesh::getMeshDimension() + 1, EntityTypesWriter >::exec( mesh, str ); + TemplateStaticFor< int, 0, Mesh::getMeshDimension() + 1, EntityTypesWriter >::exec( mesh, str ); } template< typename Mesh > diff --git a/src/TNL/StaticFor.h b/src/TNL/StaticFor.h index 74cf6855c9b69fe20316880eeb2e4fd8b558148f..1539e05aa52cd25222098f10a38b2688417bd8dd 100644 --- a/src/TNL/StaticFor.h +++ b/src/TNL/StaticFor.h @@ -1,8 +1,8 @@ /*************************************************************************** StaticFor.h - description ------------------- - begin : Feb 23, 2014 - copyright : (C) 2014 by Tomas Oberhuber + begin : Jul 16, 2019 + copyright : (C) 2019 by Tomas Oberhuber email : tomas.oberhuber@fjfi.cvut.cz ***************************************************************************/ @@ -10,83 +10,29 @@ #pragma once -#include -#include - -#include +#include namespace TNL { -namespace detail { - -template< typename IndexType, - typename Begin, - typename N, - template< IndexType > class LoopBody > -struct StaticForExecutor -{ - template< typename... Args > - __cuda_callable__ - static void exec( Args&&... args ) - { - using Decrement = std::integral_constant< IndexType, N::value - 1 >; - StaticForExecutor< IndexType, Begin, Decrement, LoopBody >::exec( std::forward< Args >( args )... ); - LoopBody< Begin::value + N::value - 1 >::exec( std::forward< Args >( args )... ); - } - - template< typename... Args > - static void execHost( Args&&... args ) - { - using Decrement = std::integral_constant< IndexType, N::value - 1 >; - StaticForExecutor< IndexType, Begin, Decrement, LoopBody >::execHost( std::forward< Args >( args )... ); - LoopBody< Begin::value + N::value - 1 >::exec( std::forward< Args >( args )... ); - } -}; -template< typename IndexType, - typename Begin, - template< IndexType > class LoopBody > -struct StaticForExecutor< IndexType, - Begin, - std::integral_constant< IndexType, 0 >, - LoopBody > +template< int Begin, int End > +struct StaticFor { - template< typename... Args > - __cuda_callable__ - static void exec( Args&&... args ) - {} - - template< typename... Args > - static void execHost( Args&&... args ) - {} + template< typename Function, typename... Args > + __cuda_callable__ + static void exec( const Function& f, Args... args ) + { + static_assert( Begin < End, "Wrong index interval for StaticFor. Being must be lower than end." ); + f( Begin, args... ); + StaticFor< Begin + 1, End >::exec( f, args... ); + }; }; -} // namespace detail - -template< typename IndexType, - IndexType begin, - IndexType end, - template< IndexType > class LoopBody > -struct StaticFor +template< int End > +struct StaticFor< End, End > { - template< typename... Args > - __cuda_callable__ - static void exec( Args&&... args ) - { - detail::StaticForExecutor< IndexType, - std::integral_constant< IndexType, begin >, - std::integral_constant< IndexType, end - begin >, - LoopBody >::exec( std::forward< Args >( args )... ); - } - - // nvcc would complain if we wonted to call a host-only function from the __cuda_callable__ exec above - template< typename... Args > - static void execHost( Args&&... args ) - { - detail::StaticForExecutor< IndexType, - std::integral_constant< IndexType, begin >, - std::integral_constant< IndexType, end - begin >, - LoopBody >::execHost( std::forward< Args >( args )... ); - } + template< typename Function, typename... Args > + __cuda_callable__ + static void exec( const Function& f, Args... args ){}; }; -} // namespace TNL +} //namespace TNL diff --git a/src/TNL/TemplateStaticFor.h b/src/TNL/TemplateStaticFor.h new file mode 100644 index 0000000000000000000000000000000000000000..88ad764fd9b78d0348469a115ee1cb83ecb7993b --- /dev/null +++ b/src/TNL/TemplateStaticFor.h @@ -0,0 +1,92 @@ +/*************************************************************************** + TemplateStaticFor.h - description + ------------------- + begin : Feb 23, 2014 + copyright : (C) 2014 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include +#include + +#include + +namespace TNL { +namespace detail { + +template< typename IndexType, + typename Begin, + typename N, + template< IndexType > class LoopBody > +struct TemplateStaticForExecutor +{ + template< typename... Args > + __cuda_callable__ + static void exec( Args&&... args ) + { + using Decrement = std::integral_constant< IndexType, N::value - 1 >; + TemplateStaticForExecutor< IndexType, Begin, Decrement, LoopBody >::exec( std::forward< Args >( args )... ); + LoopBody< Begin::value + N::value - 1 >::exec( std::forward< Args >( args )... ); + } + + template< typename... Args > + static void execHost( Args&&... args ) + { + using Decrement = std::integral_constant< IndexType, N::value - 1 >; + TemplateStaticForExecutor< IndexType, Begin, Decrement, LoopBody >::execHost( std::forward< Args >( args )... ); + LoopBody< Begin::value + N::value - 1 >::exec( std::forward< Args >( args )... ); + } +}; + +template< typename IndexType, + typename Begin, + template< IndexType > class LoopBody > +struct TemplateStaticForExecutor< IndexType, + Begin, + std::integral_constant< IndexType, 0 >, + LoopBody > +{ + template< typename... Args > + __cuda_callable__ + static void exec( Args&&... args ) + {} + + template< typename... Args > + static void execHost( Args&&... args ) + {} +}; + +} // namespace detail + +template< typename IndexType, + IndexType begin, + IndexType end, + template< IndexType > class LoopBody > +struct TemplateStaticFor +{ + template< typename... Args > + __cuda_callable__ + static void exec( Args&&... args ) + { + detail::TemplateStaticForExecutor< IndexType, + std::integral_constant< IndexType, begin >, + std::integral_constant< IndexType, end - begin >, + LoopBody >::exec( std::forward< Args >( args )... ); + } + + // nvcc would complain if we wonted to call a host-only function from the __cuda_callable__ exec above + template< typename... Args > + static void execHost( Args&&... args ) + { + detail::TemplateStaticForExecutor< IndexType, + std::integral_constant< IndexType, begin >, + std::integral_constant< IndexType, end - begin >, + LoopBody >::execHost( std::forward< Args >( args )... ); + } +}; + +} // namespace TNL diff --git a/src/UnitTests/Containers/StaticVectorTest.cpp b/src/UnitTests/Containers/StaticVectorTest.cpp index 3f57237281b0df36aa17f32979b187ed2383866b..b735545b54511aadf6fb7d9fce58a2d38d1b69c3 100644 --- a/src/UnitTests/Containers/StaticVectorTest.cpp +++ b/src/UnitTests/Containers/StaticVectorTest.cpp @@ -487,9 +487,9 @@ TYPED_TEST( StaticVectorTest, lpNorm ) const RealType expectedL1norm = size; const RealType expectedL2norm = std::sqrt( size ); const RealType expectedL3norm = std::cbrt( size ); - EXPECT_EQ( v.lpNorm( 1.0 ), expectedL1norm ); - EXPECT_EQ( v.lpNorm( 2.0 ), expectedL2norm ); - EXPECT_NEAR( v.lpNorm( 3.0 ), expectedL3norm, epsilon ); + EXPECT_EQ( lpNorm( v, 1.0 ), expectedL1norm ); + EXPECT_EQ( lpNorm( v, 2.0 ), expectedL2norm ); + EXPECT_NEAR( lpNorm( v, 3.0 ), expectedL3norm, epsilon ); } TYPED_TEST( StaticVectorTest, verticalOperations )