From 1ba3ac51ab59c34ed9f18b74b82413d9b4744dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Jul 2019 12:22:09 +0200 Subject: [PATCH 1/6] Renaming StaticFor to TemplateStaticFor. --- .../tnlTestNeighbourGridEntityGetter2D_impl.h | 6 ++--- .../{StaticArray_impl.h => StaticArray.hpp} | 0 .../NeighborGridEntityGetter1D_impl.h | 4 ++-- .../NeighborGridEntityGetter2D_impl.h | 6 ++--- .../NeighborGridEntityGetter3D_impl.h | 12 +++++----- .../MeshDetails/EntityStorageRebinder.h | 6 ++--- .../MeshDetails/IndexPermutationApplier.h | 8 +++---- .../MeshLayers/BoundaryTags/Initializer.h | 10 ++++----- .../initializer/EntityInitializer.h | 2 +- .../initializer/SubentitySeedsCreator.h | 6 ++--- src/TNL/Meshes/Writers/VTKWriter_impl.h | 4 ++-- src/TNL/{StaticFor.h => TemplateStaticFor.h} | 22 +++++++++---------- 12 files changed, 43 insertions(+), 43 deletions(-) rename src/TNL/Containers/{StaticArray_impl.h => StaticArray.hpp} (100%) rename src/TNL/{StaticFor.h => TemplateStaticFor.h} (78%) diff --git a/src/Benchmarks/HeatEquation/tnlTestNeighbourGridEntityGetter2D_impl.h b/src/Benchmarks/HeatEquation/tnlTestNeighbourGridEntityGetter2D_impl.h index 23d35fdae..13c7848de 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/TNL/Containers/StaticArray_impl.h b/src/TNL/Containers/StaticArray.hpp similarity index 100% rename from src/TNL/Containers/StaticArray_impl.h rename to src/TNL/Containers/StaticArray.hpp diff --git a/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter1D_impl.h b/src/TNL/Meshes/GridDetails/NeighborGridEntityGetter1D_impl.h index 737d38c26..b8983feed 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 5332eed97..5b0e48767 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 78955a6c0..5fe5329bb 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 7dff9d43e..7630a2d64 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 cdd98bc57..c98f145d0 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 918f4e025..21ccd0ccd 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 684e60543..7058691a3 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 25200c932..8ad08bd63 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 b3f1723b7..97d98bf6e 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/TemplateStaticFor.h similarity index 78% rename from src/TNL/StaticFor.h rename to src/TNL/TemplateStaticFor.h index 74cf6855c..88ad764fd 100644 --- a/src/TNL/StaticFor.h +++ b/src/TNL/TemplateStaticFor.h @@ -1,5 +1,5 @@ /*************************************************************************** - StaticFor.h - description + TemplateStaticFor.h - description ------------------- begin : Feb 23, 2014 copyright : (C) 2014 by Tomas Oberhuber @@ -22,14 +22,14 @@ template< typename IndexType, typename Begin, typename N, template< IndexType > class LoopBody > -struct StaticForExecutor +struct TemplateStaticForExecutor { 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 )... ); + TemplateStaticForExecutor< IndexType, Begin, Decrement, LoopBody >::exec( std::forward< Args >( args )... ); LoopBody< Begin::value + N::value - 1 >::exec( std::forward< Args >( args )... ); } @@ -37,7 +37,7 @@ struct StaticForExecutor static void execHost( Args&&... args ) { using Decrement = std::integral_constant< IndexType, N::value - 1 >; - StaticForExecutor< IndexType, Begin, Decrement, LoopBody >::execHost( std::forward< Args >( args )... ); + TemplateStaticForExecutor< IndexType, Begin, Decrement, LoopBody >::execHost( std::forward< Args >( args )... ); LoopBody< Begin::value + N::value - 1 >::exec( std::forward< Args >( args )... ); } }; @@ -45,10 +45,10 @@ struct StaticForExecutor template< typename IndexType, typename Begin, template< IndexType > class LoopBody > -struct StaticForExecutor< IndexType, - Begin, - std::integral_constant< IndexType, 0 >, - LoopBody > +struct TemplateStaticForExecutor< IndexType, + Begin, + std::integral_constant< IndexType, 0 >, + LoopBody > { template< typename... Args > __cuda_callable__ @@ -66,13 +66,13 @@ template< typename IndexType, IndexType begin, IndexType end, template< IndexType > class LoopBody > -struct StaticFor +struct TemplateStaticFor { template< typename... Args > __cuda_callable__ static void exec( Args&&... args ) { - detail::StaticForExecutor< IndexType, + detail::TemplateStaticForExecutor< IndexType, std::integral_constant< IndexType, begin >, std::integral_constant< IndexType, end - begin >, LoopBody >::exec( std::forward< Args >( args )... ); @@ -82,7 +82,7 @@ struct StaticFor template< typename... Args > static void execHost( Args&&... args ) { - detail::StaticForExecutor< IndexType, + detail::TemplateStaticForExecutor< IndexType, std::integral_constant< IndexType, begin >, std::integral_constant< IndexType, end - begin >, LoopBody >::execHost( std::forward< Args >( args )... ); -- GitLab From f5d9fd5a46527d21b952d25b6963b0b3ef8975cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Jul 2019 12:25:42 +0200 Subject: [PATCH 2/6] Added StaticFor. --- src/TNL/Containers/StaticArray.h | 421 +++-------------------------- src/TNL/Containers/StaticArray.hpp | 65 +++++ 2 files changed, 109 insertions(+), 377 deletions(-) diff --git a/src/TNL/Containers/StaticArray.h b/src/TNL/Containers/StaticArray.h index dea551a12..e0f4f39ae 100644 --- a/src/TNL/Containers/StaticArray.h +++ b/src/TNL/Containers/StaticArray.h @@ -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 index da513b841..fb53b9112 100644 --- a/src/TNL/Containers/StaticArray.hpp +++ b/src/TNL/Containers/StaticArray.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace TNL { namespace Containers { @@ -63,6 +64,25 @@ StaticArray< Size, Value >::StaticArray( const std::initializer_list< Value > &e 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() { @@ -104,6 +124,51 @@ inline Value& StaticArray< Size, Value >::operator[]( int i ) 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__ -- GitLab From 82b95fa5e670d362571ba36c75cf1fffdee0ef40 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Jul 2019 14:22:28 +0200 Subject: [PATCH 3/6] Added StaticFor. --- src/TNL/Containers/StaticArray1D_impl.h | 197 ------------------ src/TNL/Containers/StaticArray2D_impl.h | 229 --------------------- src/TNL/Containers/StaticArray3D_impl.h | 257 ------------------------ src/TNL/StaticFor.h | 38 ++++ 4 files changed, 38 insertions(+), 683 deletions(-) delete mode 100644 src/TNL/Containers/StaticArray1D_impl.h delete mode 100644 src/TNL/Containers/StaticArray2D_impl.h delete mode 100644 src/TNL/Containers/StaticArray3D_impl.h create mode 100644 src/TNL/StaticFor.h diff --git a/src/TNL/Containers/StaticArray1D_impl.h b/src/TNL/Containers/StaticArray1D_impl.h deleted file mode 100644 index 2a0a2718f..000000000 --- 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 2968bee4a..000000000 --- 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 bc00ccc8e..000000000 --- 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/StaticFor.h b/src/TNL/StaticFor.h new file mode 100644 index 000000000..1539e05aa --- /dev/null +++ b/src/TNL/StaticFor.h @@ -0,0 +1,38 @@ +/*************************************************************************** + StaticFor.h - description + ------------------- + begin : Jul 16, 2019 + copyright : (C) 2019 by Tomas Oberhuber + email : tomas.oberhuber@fjfi.cvut.cz + ***************************************************************************/ + +/* See Copyright Notice in tnl/Copyright */ + +#pragma once + +#include + +namespace TNL { + +template< int Begin, int End > +struct StaticFor +{ + 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... ); + }; +}; + +template< int End > +struct StaticFor< End, End > +{ + template< typename Function, typename... Args > + __cuda_callable__ + static void exec( const Function& f, Args... args ){}; +}; + +} //namespace TNL -- GitLab From ff8f19a6752802e94a1179727201e52a29e038c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Jul 2019 14:22:58 +0200 Subject: [PATCH 4/6] Reimplementing StaticArray using StaticFor, deleting StaticArray specialization by Size. --- src/TNL/Containers/StaticArray.h | 2 +- src/TNL/Containers/StaticArray.hpp | 118 +++++++++++++++++++++-------- 2 files changed, 88 insertions(+), 32 deletions(-) diff --git a/src/TNL/Containers/StaticArray.h b/src/TNL/Containers/StaticArray.h index e0f4f39ae..09923613a 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. diff --git a/src/TNL/Containers/StaticArray.hpp b/src/TNL/Containers/StaticArray.hpp index fb53b9112..582670e38 100644 --- a/src/TNL/Containers/StaticArray.hpp +++ b/src/TNL/Containers/StaticArray.hpp @@ -13,11 +13,86 @@ #include #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() @@ -31,29 +106,27 @@ inline StaticArray< Size, Value >::StaticArray() { }; + template< int Size, typename Value > template< typename _unused > __cuda_callable__ -inline StaticArray< Size, Value >::StaticArray( const Value v[ Size ] ) +StaticArray< Size, Value >::StaticArray( const Value v[ Size ] ) { - for( int i = 0; i < Size; i++ ) - data[ i ] = v[ i ]; + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value >, data, v ); } 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; + 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 ) { - for( int i = 0; i < Size; i++ ) - data[ i ] = v[ i ]; + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value >, data, v.getData() ); } template< int Size, typename Value > @@ -174,8 +247,7 @@ 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 ]; + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value >, data, array.getData() ); return *this; } @@ -184,8 +256,7 @@ template< int Size, typename Value > __cuda_callable__ inline StaticArray< Size, Value >& StaticArray< Size, Value >::operator = ( const Array& array ) { - for( int i = 0; i < Size; i++ ) - data[ i ] = array[ i ]; + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< Value, typename Array::ValueType >, data, array.getData() ); return *this; } @@ -194,12 +265,7 @@ template< int Size, typename Value > __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; + return Detail::StaticArrayComparator< Size, Value, typename Array::ValueType, 0 >::EQ( *this, array ); } template< int Size, typename Value > @@ -217,8 +283,7 @@ StaticArray< Size, Value >:: operator StaticArray< Size, OtherValue >() const { StaticArray< Size, OtherValue > aux; - for( int i = 0; i < Size; i++ ) - aux[ i ] = data[ i ]; + StaticFor< 0, Size >::exec( Detail::assignArrayLambda< OtherValue, Value >, aux.getData(), data ); return aux; } @@ -226,8 +291,7 @@ 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; + StaticFor< 0, Size >::exec( Detail::assignValueLambda< Value >, data, val ); } template< int Size, typename Value > @@ -247,14 +311,7 @@ bool StaticArray< Size, Value >::load( File& file) 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 ] ); + Detail::StaticArraySort< Size - 1, 0, Value >::exec( data ); } template< int Size, typename Value > @@ -266,7 +323,6 @@ std::ostream& StaticArray< Size, Value >::write( std::ostream& str, const char* return str; } - template< int Size, typename Value > std::ostream& operator << ( std::ostream& str, const StaticArray< Size, Value >& a ) { -- GitLab From f91588ef74e5759da0362308328c2c419f24b873 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Jul 2019 15:49:25 +0200 Subject: [PATCH 5/6] Reimplementing StaticVector using StaticFor. --- src/TNL/Containers/StaticVector.h | 312 ++---------------- .../{StaticVector_impl.h => StaticVector.hpp} | 63 +++- src/TNL/Containers/StaticVector1D_impl.h | 161 --------- src/TNL/Containers/StaticVector2D_impl.h | 181 ---------- src/TNL/Containers/StaticVector3D_impl.h | 193 ----------- 5 files changed, 73 insertions(+), 837 deletions(-) rename src/TNL/Containers/{StaticVector_impl.h => StaticVector.hpp} (70%) delete mode 100644 src/TNL/Containers/StaticVector1D_impl.h delete mode 100644 src/TNL/Containers/StaticVector2D_impl.h delete mode 100644 src/TNL/Containers/StaticVector3D_impl.h diff --git a/src/TNL/Containers/StaticVector.h b/src/TNL/Containers/StaticVector.h index 132c64cbf..17e9fe34f 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 > @@ -168,298 +188,10 @@ class StaticVector : public StaticArray< Size, Real > 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 +324,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 70% rename from src/TNL/Containers/StaticVector_impl.h rename to src/TNL/Containers/StaticVector.hpp index aa281b4c4..a3c3dc5b6 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,9 @@ 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 ]; + //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 +148,9 @@ 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 ]; + //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 +158,9 @@ 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; + //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 +168,10 @@ 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; + //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,8 +182,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 ]; + //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; } diff --git a/src/TNL/Containers/StaticVector1D_impl.h b/src/TNL/Containers/StaticVector1D_impl.h deleted file mode 100644 index 5e2fdcaba..000000000 --- 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 b1606b654..000000000 --- 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 36a89cb69..000000000 --- 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 -- GitLab From cc100c1b73849eab0f1fa82a3ce2c1d5346fe41e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Oberhuber?= Date: Tue, 16 Jul 2019 17:32:33 +0200 Subject: [PATCH 6/6] Deleting StaticVector::abs and StaticVector::lpNorm. --- .../flow-sw/PhysicalVariablesGetter.h | 2 +- .../flow-vl/PhysicalVariablesGetter.h | 2 +- src/Examples/flow/PhysicalVariablesGetter.h | 2 +- .../PhysicalVariablesGetter.h | 2 +- .../PhysicalVariablesGetter.h | 2 +- .../inviscid-flow/PhysicalVariablesGetter.h | 2 +- src/TNL/Containers/StaticVector.h | 14 ------ src/TNL/Containers/StaticVector.hpp | 47 ------------------- src/UnitTests/Containers/StaticVectorTest.cpp | 6 +-- 9 files changed, 9 insertions(+), 70 deletions(-) diff --git a/src/Examples/flow-sw/PhysicalVariablesGetter.h b/src/Examples/flow-sw/PhysicalVariablesGetter.h index 2af0a02b8..d7eeb7618 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 2af0a02b8..d7eeb7618 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 2af0a02b8..d7eeb7618 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 2af0a02b8..d7eeb7618 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 2af0a02b8..d7eeb7618 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 a391d2d64..a0a7f672d 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/StaticVector.h b/src/TNL/Containers/StaticVector.h index 17e9fe34f..11d2575e0 100644 --- a/src/TNL/Containers/StaticVector.h +++ b/src/TNL/Containers/StaticVector.h @@ -172,20 +172,6 @@ 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; }; } // namespace Containers diff --git a/src/TNL/Containers/StaticVector.hpp b/src/TNL/Containers/StaticVector.hpp index a3c3dc5b6..78e7e21a8 100644 --- a/src/TNL/Containers/StaticVector.hpp +++ b/src/TNL/Containers/StaticVector.hpp @@ -138,8 +138,6 @@ 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; } @@ -148,8 +146,6 @@ 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; } @@ -158,8 +154,6 @@ 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; } @@ -168,9 +162,6 @@ 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; } @@ -182,47 +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/UnitTests/Containers/StaticVectorTest.cpp b/src/UnitTests/Containers/StaticVectorTest.cpp index 3f5723728..b735545b5 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 ) -- GitLab