Commit 888869bd authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

Adding matrix values holder.

parent ae650de9
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ class Sparse : public Matrix< Real, Device, Index >
   typedef Real RealType;
   typedef Device DeviceType;
   typedef Index IndexType;
   typedef typename Matrix< RealType, DeviceType, IndexType >::ValuesVector ValuesVector;
   typedef typename Matrix< RealType, DeviceType, IndexType >::ValuesHolderType ValuesVector;
   typedef Containers::Vector< IndexType, DeviceType, IndexType > ColumnIndexesVector;
   typedef Matrix< Real, Device, Index > BaseType;
   typedef SparseRow< RealType, IndexType > MatrixRow;
+7 −5
Original line number Diff line number Diff line
@@ -16,6 +16,7 @@
#include <TNL/Containers/Vector.h>
#include <TNL/Containers/VectorView.h>
#include <TNL/Matrices/MatrixView.h>
#include <TNL/Matrices/details/ValuesHolder.h>

namespace TNL {
/**
@@ -26,7 +27,8 @@ namespace Matrices {
template< typename Real = double,
          typename Device = Devices::Host,
          typename Index = int,
          typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real > >
          typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >,
          typename ValuesHolder = typename details::ValuesHolder< Real, Device, Index, RealAllocator > >
class Matrix : public Object
{
public:
@@ -36,7 +38,7 @@ public:
   using CompressedRowLengthsVector = Containers::Vector< IndexType, DeviceType, IndexType >;
   using CompressedRowLengthsVectorView = Containers::VectorView< IndexType, DeviceType, IndexType >;
   using ConstCompressedRowLengthsVectorView = typename CompressedRowLengthsVectorView::ConstViewType;
   using ValuesVector = Containers::Vector< RealType, DeviceType, IndexType, RealAllocator >;
   using ValuesHolderType = ValuesHolder;
   using RealAllocatorType = RealAllocator;
   using ViewType = MatrixView< Real, Device, Index >;
   using ConstViewType = MatrixView< std::add_const_t< Real >, Device, Index >;
@@ -90,9 +92,9 @@ public:
   virtual Real getElement( const IndexType row,
                            const IndexType column ) const = 0;

   const ValuesVector& getValues() const;
   const ValuesHolderType& getValues() const;

   ValuesVector& getValues();
   ValuesHolderType& getValues();

   // TODO: parallelize and optimize for sparse matrices
   template< typename Matrix >
@@ -131,7 +133,7 @@ public:

   IndexType rows, columns, numberOfColors;

   ValuesVector values;
   ValuesHolderType values;
};

template< typename Real, typename Device, typename Index >
+64 −44
Original line number Diff line number Diff line
@@ -22,8 +22,9 @@ namespace Matrices {
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
Matrix< Real, Device, Index, RealAllocator >::
          typename RealAllocator,
          typename ValuesHolder >
Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::
Matrix( const RealAllocatorType& allocator )
: rows( 0 ),
  columns( 0 ),
@@ -34,8 +35,9 @@ Matrix( const RealAllocatorType& allocator )
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
Matrix< Real, Device, Index, RealAllocator >::
          typename RealAllocator,
          typename ValuesHolder >
Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::
Matrix( const IndexType rows_, const IndexType columns_, const RealAllocatorType& allocator )
: rows( rows_ ),
  columns( columns_ ),
@@ -46,8 +48,9 @@ Matrix( const IndexType rows_, const IndexType columns_, const RealAllocatorType
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
void Matrix< Real, Device, Index, RealAllocator >::setDimensions( const IndexType rows,
          typename RealAllocator,
          typename ValuesHolder >
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::setDimensions( const IndexType rows,
                                                   const IndexType columns )
{
   TNL_ASSERT( rows > 0 && columns > 0,
@@ -60,7 +63,7 @@ void Matrix< Real, Device, Index, RealAllocator >::setDimensions( const IndexTyp
          typename Device,
          typename Index,
          typename RealAllocator >
void Matrix< Real, Device, Index, RealAllocator >::getCompressedRowLengths( CompressedRowLengthsVector& rowLengths ) const
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::getCompressedRowLengths( CompressedRowLengthsVector& rowLengths ) const
{
   rowLengths.setSize( this->getRows() );
   getCompressedRowLengths( rowLengths.getView() );
@@ -69,8 +72,9 @@ void Matrix< Real, Device, Index, RealAllocator >::getCompressedRowLengths( Comp
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
void Matrix< Real, Device, Index, RealAllocator >::getCompressedRowLengths( CompressedRowLengthsVectorView rowLengths ) const
          typename RealAllocator,
          typename ValuesHolder >
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::getCompressedRowLengths( CompressedRowLengthsVectorView rowLengths ) const
{
   TNL_ASSERT_EQ( rowLengths.getSize(), this->getRows(), "invalid size of the rowLengths vector" );
   for( IndexType row = 0; row < this->getRows(); row++ )
@@ -80,9 +84,10 @@ void Matrix< Real, Device, Index, RealAllocator >::getCompressedRowLengths( Comp
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
   template< typename Matrix_ >
void Matrix< Real, Device, Index, RealAllocator >::setLike( const Matrix_& matrix )
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::setLike( const Matrix_& matrix )
{
   setDimensions( matrix.getRows(), matrix.getColumns() );
}
@@ -90,8 +95,9 @@ void Matrix< Real, Device, Index, RealAllocator >::setLike( const Matrix_& matri
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
Index Matrix< Real, Device, Index, RealAllocator >::getAllocatedElementsCount() const
          typename RealAllocator,
          typename ValuesHolder >
Index Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::getAllocatedElementsCount() const
{
   return this->values.getSize();
}
@@ -99,8 +105,9 @@ Index Matrix< Real, Device, Index, RealAllocator >::getAllocatedElementsCount()
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
Index Matrix< Real, Device, Index, RealAllocator >::getNumberOfNonzeroMatrixElements() const
          typename RealAllocator,
          typename ValuesHolder >
Index Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::getNumberOfNonzeroMatrixElements() const
{
   const auto values_view = this->values.getConstView();
   auto fetch = [=] __cuda_callable__ ( const IndexType i ) -> IndexType {
@@ -112,9 +119,10 @@ Index Matrix< Real, Device, Index, RealAllocator >::getNumberOfNonzeroMatrixElem
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
__cuda_callable__
Index Matrix< Real, Device, Index, RealAllocator >::getRows() const
Index Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::getRows() const
{
   return this->rows;
}
@@ -122,9 +130,10 @@ Index Matrix< Real, Device, Index, RealAllocator >::getRows() const
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
__cuda_callable__
Index Matrix< Real, Device, Index, RealAllocator >::getColumns() const
Index Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::getColumns() const
{
   return this->columns;
}
@@ -132,9 +141,10 @@ Index Matrix< Real, Device, Index, RealAllocator >::getColumns() const
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
const typename Matrix< Real, Device, Index, RealAllocator >::ValuesVector&
Matrix< Real, Device, Index, RealAllocator >::
          typename RealAllocator,
          typename ValuesHolder >
const typename Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::ValuesHolderType&
Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::
getValues() const
{
   return this->values;
@@ -143,9 +153,10 @@ getValues() const
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
typename Matrix< Real, Device, Index, RealAllocator >::ValuesVector&
Matrix< Real, Device, Index, RealAllocator >::
          typename RealAllocator,
          typename ValuesHolder >
typename Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::ValuesHolderType&
Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::
getValues()
{
   return this->values;
@@ -154,8 +165,9 @@ getValues()
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
void Matrix< Real, Device, Index, RealAllocator >::reset()
          typename RealAllocator,
          typename ValuesHolder >
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::reset()
{
   this->rows = 0;
   this->columns = 0;
@@ -165,9 +177,10 @@ void Matrix< Real, Device, Index, RealAllocator >::reset()
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
   template< typename MatrixT >
bool Matrix< Real, Device, Index, RealAllocator >::operator == ( const MatrixT& matrix ) const
bool Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::operator == ( const MatrixT& matrix ) const
{
   if( this->getRows() != matrix.getRows() ||
       this->getColumns() != matrix.getColumns() )
@@ -182,9 +195,10 @@ bool Matrix< Real, Device, Index, RealAllocator >::operator == ( const MatrixT&
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
   template< typename MatrixT >
bool Matrix< Real, Device, Index, RealAllocator >::operator != ( const MatrixT& matrix ) const
bool Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::operator != ( const MatrixT& matrix ) const
{
   return ! operator == ( matrix );
}
@@ -192,8 +206,9 @@ bool Matrix< Real, Device, Index, RealAllocator >::operator != ( const MatrixT&
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
void Matrix< Real, Device, Index, RealAllocator >::save( File& file ) const
          typename RealAllocator,
          typename ValuesHolder >
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::save( File& file ) const
{
   Object::save( file );
   file.save( &this->rows );
@@ -204,8 +219,9 @@ void Matrix< Real, Device, Index, RealAllocator >::save( File& file ) const
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
void Matrix< Real, Device, Index, RealAllocator >::load( File& file )
          typename RealAllocator,
          typename ValuesHolder >
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::load( File& file )
{
   Object::load( file );
   file.load( &this->rows );
@@ -216,18 +232,20 @@ void Matrix< Real, Device, Index, RealAllocator >::load( File& file )
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
void Matrix< Real, Device, Index, RealAllocator >::print( std::ostream& str ) const
          typename RealAllocator,
          typename ValuesHolder >
void Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::print( std::ostream& str ) const
{
}

template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
__cuda_callable__
const Index&
Matrix< Real, Device, Index, RealAllocator >::
Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::
getNumberOfColors() const
{
   return this->numberOfColors;
@@ -236,9 +254,10 @@ getNumberOfColors() const
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
void
Matrix< Real, Device, Index, RealAllocator >::
Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::
computeColorsVector(Containers::Vector<Index, Device, Index> &colorsVector)
{
    for( IndexType i = this->getRows() - 1; i >= 0; i-- )
@@ -274,9 +293,10 @@ computeColorsVector(Containers::Vector<Index, Device, Index> &colorsVector)
template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
          typename RealAllocator,
          typename ValuesHolder >
void
Matrix< Real, Device, Index, RealAllocator >::
Matrix< Real, Device, Index, RealAllocator, ValuesHolder >::
copyFromHostToCuda( Matrix< Real, Devices::Host, Index >& matrix )
{
    this->numberOfColors = matrix.getNumberOfColors();
+48 −14
Original line number Diff line number Diff line
@@ -23,9 +23,8 @@ class ValuesHolder
{};

template< typename Device,
          typename Index,
          typename RealAllocator >
class ValuesHolder< bool, Device, Index, RealAllocator >
          typename Index >
class BooleanValuesHolder
{
   public:

@@ -33,10 +32,10 @@ class ValuesHolder< bool, Device, Index, RealAllocator >
      using DeviceType = Device;
      using IndexType = Index;

      ValuesHolder()
      BooleanValuesHolder()
      : size( 0 ){};

      ValuesdHolder( const IndexType& size )
      BooleanValuesHolder( const IndexType& size )
      : size( size ){};

      void setSize( const IndexType& size ) { this->size = size; };
@@ -47,31 +46,66 @@ class ValuesHolder< bool, Device, Index, RealAllocator >
      __cuda_callable__
      bool operator[]( const IndexType& i ) const { return true; };

      
   protected:

      IndexType size;

};

/**
 * \brief Serialization of arrays into binary files.
 * \brief Serialization of values holder into binary files.
 */
template< typename Device, typename Index, typename Allocator >
File& operator<<( File& file, const ValuesHolder< bool, Device, Index, Allocator >& array ) { return file; };
File& operator<<( File& file, const ValuesHolder< bool, Device, Index, Allocator >& holder ) {
   file << holder.getSize();
   return file; };

template< typename Device, typename Index, typename Allocator >
File& operator<<( File&& file, const ValuesHolder< bool, Device, Index, Allocator >& array ) { return file; };
File& operator<<( File&& file, const ValuesHolder< bool, Device, Index, Allocator >& holder ) {
   file << holder.getSize();
   return file; };

/**
 * \brief Deserialization of arrays from binary files.
 * \brief Deserialization of values holder from binary files.
 */
template< typename Device, typename Index, typename Allocator >
File& operator>>( File& file, ValuesHolder< bool, Device, Index, Allocator >& array ) { return file; };
File& operator>>( File& file, ValuesHolder< bool, Device, Index, Allocator >& holder ) {
   Index size;
   file >> size;
   holder.setSize( size );
   return file; };

template< typename Device, typename Index, typename Allocator >
File& operator>>( File&& file, ValuesHolder< bool, Device, Index, Allocator >& array ) { return file; };
File& operator>>( File&& file, ValuesHolder< bool, Device, Index, Allocator >& holder ) {
   Index size;
   file >> size;
   holder.setSize( size );
   return file; };

template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
struct ValuesHolderSetter
{
   using type = ValuesHolder< Real, Device, Index, RealAllocator >;
};

template< typename Real,
          typename Device,
          typename Index,
          typename RealAllocator >
struct SparseMatrixValuesHolderSetter
{
   using type = ValuesHolder< Real, Device, Index, RealAllocator >;
};

template< typename Device,
          typename Index,
          typename RealAllocator >
struct SparseMatrixValuesHolderSetter< bool, Device, Index, RealAllocator >
{
   using type = BooleanValuesHolder< Device, Index >;
};

      } //namespace details
   } //namepsace Matrices