Commit 72759e36 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Added StaticEllpackIndexMultimap

parent c5191249
Loading
Loading
Loading
Loading
+75 −0
Original line number Diff line number Diff line
/***************************************************************************
                          StaticEllpackIndexMultimap.h  -  description
                             -------------------
    begin                : Sep 9, 2015
    copyright            : (C) 2015 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <TNL/Containers/Vector.h>
#include <TNL/Experimental/Multimaps/StaticEllpackIndexMultimapValues.h>

namespace TNL {

template< int ValuesCount,
          typename Index = int,
          typename Device = Devices::Host,
          typename LocalIndex = Index >
class StaticEllpackIndexMultimap
   : public virtual Object
{
   public:
      using DeviceType                 = Device;
      using IndexType                  = Index;
      using LocalIndexType             = LocalIndex;
      using ValuesAccessorType         = StaticEllpackIndexMultimapValues< ValuesCount, IndexType, DeviceType, LocalIndexType >;
      using ConstValuesAccessorType    = StaticEllpackIndexMultimapValues< ValuesCount, const IndexType, DeviceType, LocalIndexType >;

      StaticEllpackIndexMultimap();

      static String getType();

      String getTypeVirtual() const;

      void setKeysRange( const IndexType& keysRange );

      const IndexType getKeysRange() const;

      bool allocate();

      ValuesAccessorType getValues( const IndexType& inputIndex );

      ConstValuesAccessorType getValues( const IndexType& inputIndex ) const;

      bool operator==( const StaticEllpackIndexMultimap& other ) const;

      bool save( File& file ) const;

      bool load( File& file );

      using Object::load;

      using Object::save;

      void print( std::ostream& str ) const;

   protected:
      Containers::Vector< IndexType, DeviceType, IndexType > values;

      IndexType keysRange;
};

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
std::ostream& operator << ( std::ostream& str, const StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >& multimap );

} // namespace TNL

#include <TNL/Experimental/Multimaps/StaticEllpackIndexMultimap_impl.h>
+84 −0
Original line number Diff line number Diff line
/***************************************************************************
                          StaticEllpackIndexMultimapValues.h  -  description
                             -------------------
    begin                : Sep 10, 2015
    copyright            : (C) 2015 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <type_traits>
#include <ostream>

namespace TNL {

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
class StaticEllpackIndexMultimap;

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
class StaticEllpackIndexMultimapValues
{
   public:
      using DeviceType     = Device;
      using IndexType      = Index;
      using LocalIndexType = LocalIndex;
      using NetworkType    = StaticEllpackIndexMultimap< ValuesCount, IndexType, DeviceType, LocalIndexType >;

      StaticEllpackIndexMultimapValues();

      StaticEllpackIndexMultimapValues( StaticEllpackIndexMultimapValues&& other );

      StaticEllpackIndexMultimapValues& operator=( const StaticEllpackIndexMultimapValues& );

      StaticEllpackIndexMultimapValues& operator=( StaticEllpackIndexMultimapValues&& other );

      void bind( const StaticEllpackIndexMultimapValues& other );

      constexpr LocalIndexType getSize() const;

      constexpr LocalIndexType getAllocatedSize() const;

      void setValue( const LocalIndexType& portIndex,
                     const IndexType& value );

      IndexType getValue( const LocalIndexType& portIndex ) const;

      IndexType& operator[]( const LocalIndexType& portIndex );

      const IndexType& operator[]( const LocalIndexType& portIndex ) const;

      bool operator==( const StaticEllpackIndexMultimapValues& other ) const;

      bool operator!=( const StaticEllpackIndexMultimapValues& other ) const;

      void print( std::ostream& str ) const;

   protected:
      StaticEllpackIndexMultimapValues( IndexType* values,
                                        const IndexType& input );

      IndexType* values;

      friend StaticEllpackIndexMultimap< ValuesCount, IndexType, DeviceType, LocalIndexType >;
      friend StaticEllpackIndexMultimap< ValuesCount, typename std::remove_const< IndexType >::type, DeviceType, LocalIndexType >;
};

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
std::ostream& operator << ( std::ostream& str, const StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >& ports );

} // namespace TNL

#include <TNL/Experimental/Multimaps/StaticEllpackIndexMultimapValues_impl.h>
+239 −0
Original line number Diff line number Diff line
/***************************************************************************
                          StaticEllpackIndexMultimapValues_impl.h  -  description
                             -------------------
    begin                : Sep 10, 2015
    copyright            : (C) 2015 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include "StaticEllpackIndexMultimapValues.h"

#include <TNL/Assert.h>

namespace TNL {
 
template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
StaticEllpackIndexMultimapValues()
: values( nullptr )
{
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
StaticEllpackIndexMultimapValues( StaticEllpackIndexMultimapValues&& other )
: values( other.values )
{
   other.values = nullptr;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >&
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
operator=( const StaticEllpackIndexMultimapValues& other )
{
   if( this->values != other.values ) {
      for( LocalIndexType i = 0; i < this->getSize(); i++ )
         this->setValue( i, other[ i ] );
   }
   return *this;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >&
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
operator=( StaticEllpackIndexMultimapValues&& other )
{
   this->values = other.values;
   other.values = nullptr;
   return *this;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
void
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
bind( const StaticEllpackIndexMultimapValues& other )
{
   this->values = other.values;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
StaticEllpackIndexMultimapValues( IndexType* values,
                                  const IndexType& input )
{
   this->values = &values[ input * ValuesCount ];
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
constexpr LocalIndex
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
getSize() const
{
   return ValuesCount;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
constexpr LocalIndex
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
getAllocatedSize() const
{
   return ValuesCount;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
void
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
setValue( const LocalIndexType& portIndex,
          const IndexType& value )
{
   Assert( this->values,
              std::cerr << "This instance is not bound to any multimap." << std::endl; );
   Assert( portIndex < this->getSize(),
              std::cerr << " portIndex = " << portIndex
                        << " getSize() = " << this->getSize()
                        << std::endl );
   this->values[ portIndex ] = value;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
Index
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
getValue( const LocalIndexType& portIndex ) const
{
   Assert( this->values,
              std::cerr << "This instance is not bound to any multimap." << std::endl; );
   Assert( portIndex < this->getSize(),
              std::cerr << " portIndex = " << portIndex
                        << " getSize() = " << this->getSize()
                        << std::endl );
   return this->values[ portIndex ];
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
Index&
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
operator[]( const LocalIndexType& portIndex )
{
   Assert( this->values,
              std::cerr << "This instance is not bound to any multimap." << std::endl; );
   Assert( portIndex < this->getSize(),
              std::cerr << " portIndex = " << portIndex
                        << " getSize() = " << this->getSize()
                        << std::endl );
   return this->values[ portIndex ];
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
const Index&
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
operator[]( const LocalIndexType& portIndex ) const
{
   Assert( this->values,
              std::cerr << "This instance is not bound to any multimap." << std::endl; );
   Assert( portIndex < this->getSize(),
              std::cerr << " portIndex = " << portIndex
                        << " getSize() = " << this->getSize()
                        << std::endl );
   return this->values[ portIndex ];
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
bool
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
operator==( const StaticEllpackIndexMultimapValues& other ) const
{
   if( this->values == other.values )
      return true;
   if( ! this->values || ! other.values )
      return false;
   for( LocalIndexType i = 0; i < this->getSize(); i++ )
      if( this->operator[]( i ) != other[ i ] )
         return false;
   return true;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
bool
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
operator!=( const StaticEllpackIndexMultimapValues& other ) const
{
   return ! ( *this == other );
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
void
StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >::
print( std::ostream& str ) const
{
   str << "[ ";
   if( this->getSize() > 0 )
   {
      str << this->getValue( 0 );
      for( typename std::remove_const< Index >::type i = 1; i < this->getSize(); i++ )
         str << ", " << this->getValue( i );
   }
   str << " ]";
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
std::ostream& operator << ( std::ostream& str, const StaticEllpackIndexMultimapValues< ValuesCount, Index, Device, LocalIndex >& ports )
{
   ports.print( str );
   return str;
}

} // namespace TNL
+187 −0
Original line number Diff line number Diff line
/***************************************************************************
                          StaticEllpackIndexMultimap_impl.h  -  description
                             -------------------
    begin                : Sep 9, 2015
    copyright            : (C) 2015 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once 

#include <TNL/Experimental/Multimaps/StaticEllpackIndexMultimap.h>

namespace TNL {

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
StaticEllpackIndexMultimap()
: keysRange( 0 )
{
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
String
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
getType()
{
   return String( "StaticEllpackIndexMultimap< ") +
          String( TNL::getType< Index >() ) +
          String( ", " ) +
          Device :: getDeviceType() +
          String( ", " ) +
          String( TNL::getType< LocalIndexType >() ) +
          String( " >" );
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
String
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
getTypeVirtual() const
{
   return this->getType();
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
void
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
setKeysRange( const IndexType& keysRange )
{
   Assert( keysRange >= 0, );
   this->keysRange = keysRange;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
const Index
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
getKeysRange() const
{
   return this->keysRange;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
bool
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
allocate()
{
   return this->values.setSize( this->keysRange * ValuesCount );
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
typename StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::ValuesAccessorType
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
getValues( const IndexType& inputIndex )
{
   Assert( inputIndex < this->getKeysRange(), );
   return ValuesAccessorType( this->values.getData(), inputIndex );
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
typename StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::ConstValuesAccessorType
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
getValues( const IndexType& inputIndex ) const
{
   Assert( inputIndex < this->getKeysRange(), );
   return ConstValuesAccessorType( this->values.getData(), inputIndex );
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
bool
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
operator==( const StaticEllpackIndexMultimap& other ) const
{
   return ( this->keysRange == other.keysRange && this->values == other.values );
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
bool
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
save( File& file ) const
{
   if( ! Object::save( file ) )
      return false;
   if( ! file.write( &this->keysRange ) )
      return false;
   if( ! this->values.save( file ) )
      return false;
   return true;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
bool
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
load( File& file )
{
   if( ! Object::load( file ) )
      return false;
   if( ! file.read( &this->keysRange ) )
      return false;
   if( ! this->values.load( file ) )
      return false;
   return true;
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
void
StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >::
print( std::ostream& str ) const
{
   str << "[ ";
   if( this->getKeysRange() > 0 )
   {
      str << this->getValues( 0 );
      for( Index i = 1; i < this->getKeysRange(); i++ )
         str << ",\n  " << this->getValues( i );
   }
   str << " ]";
}

template< int ValuesCount,
          typename Index,
          typename Device,
          typename LocalIndex >
std::ostream& operator << ( std::ostream& str, const StaticEllpackIndexMultimap< ValuesCount, Index, Device, LocalIndex >& multimap )
{
   multimap.print( str );
   return str;
}

} // namespace TNL
+6 −0
Original line number Diff line number Diff line
@@ -13,7 +13,13 @@ SET_TARGET_PROPERTIES( MultimapTest${mpiExt}${debugExt} PROPERTIES COMPILE_FLAGS
TARGET_LINK_LIBRARIES( MultimapTest${mpiExt}${debugExt} ${GTEST_BOTH_LIBRARIES}
                                                           tnl${mpiExt}${debugExt}-${tnlVersion} )

ADD_EXECUTABLE( StaticMultimapTest${mpiExt}${debugExt} ${headers} StaticMultimapTest.cpp )
SET_TARGET_PROPERTIES( StaticMultimapTest${mpiExt}${debugExt} PROPERTIES COMPILE_FLAGS "${CXX_TESTS_FLAGS}" )
TARGET_LINK_LIBRARIES( StaticMultimapTest${mpiExt}${debugExt} ${GTEST_BOTH_LIBRARIES}
                                                           tnl${mpiExt}${debugExt}-${tnlVersion} )


ADD_TEST( MeshTest${mpiExt}${debugExt} ${EXECUTABLE_OUTPUT_PATH}/MeshTest${mpiExt}${debugExt} )
ADD_TEST( MeshEntityTest${mpiExt}${debugExt} ${EXECUTABLE_OUTPUT_PATH}/MeshEntityTest${mpiExt}${debugExt} )
ADD_TEST( MultimapTest${mpiExt}${debugExt} ${EXECUTABLE_OUTPUT_PATH}/MultimapTest${mpiExt}${debugExt} )
ADD_TEST( StaticMultimapTest${mpiExt}${debugExt} ${EXECUTABLE_OUTPUT_PATH}/MultimapTest${mpiExt}${debugExt} )
Loading