Commit 11c8a4b0 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Jakub Klinkovský
Browse files

Added sanbox sparse matrix.

parent b4e37410
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -49,6 +49,8 @@ std::ostream& printSegments( const Segments& segments, std::ostream& str )
   return str;
}

/// This is to prevent from appearing in Doxygen documentation.
/// \cond HIDDEN_CLASS
template< typename Segments,
          typename Fetch >
struct SegmentsPrinter
@@ -130,6 +132,7 @@ std::ostream& printSegments( const Segments& segments, Fetch&& fetch, std::ostre
   }
   return str;
}
/// \endcond

      } // namespace Segments
   } // namespace Algorithms
+1176 −0

File added.

Preview size limit exceeded, changes collapsed.

+1197 −0

File added.

Preview size limit exceeded, changes collapsed.

+282 −0
Original line number Diff line number Diff line
 /***************************************************************************
                          SparseSandboxMatrixRowView.h -  description
                             -------------------
    begin                : Apr 20, 2021
    copyright            : (C) 2021 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <ostream>

#include <TNL/Cuda/CudaCallable.h>
#include <TNL/Matrices/MatrixRowViewIterator.h>

namespace TNL {
   namespace Matrices {
      namespace Sandbox {

/**
 * \brief RowView is a simple structure for accessing rows of sparse matrix.
 *
 * \tparam ValuesView is a vector view storing the matrix elements values.
 * \tparam ColumnsIndexesView is a vector view storing the column indexes of the matrix element.
 * \tparam isBinary tells if the the parent matrix is a binary matrix.
 *
 * See \ref SparseSandboxMatrix and \ref SparseSandboxMatrixView.
 *
 * \par Example
 * \include Matrices/SparseMatrix/SparseMatrixExample_getRow.cpp
 * \par Output
 * \include SparseMatrixExample_getRow.out
 *
 * \par Example
 * \include Matrices/SparseMatrix/SparseMatrixViewExample_getRow.cpp
 * \par Output
 * \include SparseMatrixViewExample_getRow.out
 */
template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
class SparseSandboxMatrixRowView
{
   public:

      /**
       * \brief The type of matrix elements.
       */
      using RealType = typename ValuesView::RealType;

      /**
       * \brief The type used for matrix elements indexing.
       */
      using IndexType = typename ColumnsIndexesView::IndexType;

      /**
       * \brief Type of container view used for storing the matrix elements values.
       */
      using ValuesViewType = ValuesView;

      /**
       * \brief Type of container view used for storing the column indexes of the matrix elements.
       */
      using ColumnsIndexesViewType = ColumnsIndexesView;

      /**
       * \brief Type of constant container view used for storing the matrix elements values.
       */
      using ConstValuesViewType = typename ValuesViewType::ConstViewType;

      /**
       * \brief Type of constant container view used for storing the column indexes of the matrix elements.
       */
      using ConstColumnsIndexesViewType = typename ColumnsIndexesViewType::ConstViewType;

      /**
       * \brief Type of sparse matrix row view.
       */
      using RowView = SparseSandboxMatrixRowView< ValuesViewType, ColumnsIndexesViewType, isBinary_ >;

      /**
       * \brief Type of constant sparse matrix row view.
       */
      using ConstView = SparseSandboxMatrixRowView< ConstValuesViewType, ConstColumnsIndexesViewType, isBinary_ >;

      /**
       * \brief The type of related matrix element.
       */
      using MatrixElementType = SparseMatrixElement< RealType, IndexType >;

      /**
       * \brief Type of iterator for the matrix row.
       */
      using IteratorType = MatrixRowViewIterator< RowView >;

      /**
       * \brief Tells whether the parent matrix is a binary matrix.
       * @return `true` if the matrix is binary.
       */
      static constexpr bool isBinary() { return isBinary_; };

      /**
       * \brief Constructor with \e segmentView, \e values and \e columnIndexes.
       *
       * \param rowIdx is row index.
       * \param offset is the begining of the matrix row in arrays with values and column indexes of matrix elements.
       * \param size is row size, i.e. number of nonzero matrix elements in the row.
       * \param values is a container view for storing the matrix elements values.
       * \param columnIndexes is a container view for storing the column indexes of the matrix elements.
       */
      __cuda_callable__
      SparseSandboxMatrixRowView( IndexType rowIdx,
                                  IndexType offset,
                                  IndexType size,
                                  const ValuesViewType& values,
                                  const ColumnsIndexesViewType& columnIndexes );

      /**
       * \brief Returns size of the matrix row, i.e. number of matrix elements in this row.
       *
       * \return Size of the matrix row.
       */
      __cuda_callable__
      IndexType getSize() const;

      /**
       * \brief Returns the matrix row index.
       *
       * \return matrix row index.
       */
      __cuda_callable__
      const IndexType& getRowIndex() const;

      /**
       * \brief Returns constants reference to a column index of an element with given rank in the row.
       *
       * \param localIdx is the rank of the non-zero element in given row.
       *
       * \return constant reference to the matrix element column index.
       */
      __cuda_callable__
      const IndexType& getColumnIndex( const IndexType localIdx ) const;

      /**
       * \brief Returns non-constants reference to a column index of an element with given rank in the row.
       *
       * \param localIdx is the rank of the non-zero element in given row.
       *
       * \return non-constant reference to the matrix element column index.
       */
      __cuda_callable__
      IndexType& getColumnIndex( const IndexType localIdx );

      /**
       * \brief Returns constants reference to value of an element with given rank in the row.
       *
       * \param localIdx is the rank of the non-zero element in given row.
       *
       * \return constant reference to the matrix element value.
       */
      __cuda_callable__
      const RealType& getValue( const IndexType localIdx ) const;

      /**
       * \brief Returns non-constants reference to value of an element with given rank in the row.
       *
       * \param localIdx is the rank of the non-zero element in given row.
       *
       * \return non-constant reference to the matrix element value.
       */
      __cuda_callable__
      RealType& getValue( const IndexType localIdx );

      /**
       * \brief Sets a value of matrix element with given rank in the matrix row.
       *
       * \param localIdx is the rank of the matrix element in the row.
       * \param value is the new value of the matrix element.
       */
      __cuda_callable__
      void setValue( const IndexType localIdx,
                     const RealType& value );

      /**
       * \brief Sets a column index of matrix element with given rank in the matrix row.
       *
       * \param localIdx is the rank of the matrix element in the row.
       * \param columnIndex is the new column index of the matrix element.
       */
      __cuda_callable__
      void setColumnIndex( const IndexType localIdx,
                           const IndexType& columnIndex );

      /**
       * \brief Sets both a value and a column index of matrix element with given rank in the matrix row.
       *
       * \param localIdx is the rank of the matrix element in the row.
       * \param columnIndex is the new column index of the matrix element.
       * \param value is the new value of the matrix element.
       */
      __cuda_callable__
      void setElement( const IndexType localIdx,
                       const IndexType columnIndex,
                       const RealType& value );

      /**
       * \brief Comparison of two matrix rows.
       *
       * The other matrix row can be from any other matrix.
       *
       * \param other is another matrix row.
       * \return \e true if both rows are the same, \e false otherwise.
       */
      template< typename _ValuesView,
                typename _ColumnsIndexesView,
                bool _isBinary >
      __cuda_callable__
      bool operator==( const SparseSandboxMatrixRowView< _ValuesView, _ColumnsIndexesView, _isBinary >& other ) const;

      /**
       * \brief Returns iterator pointing at the beginning of the matrix row.
       *
       * \return iterator pointing at the beginning.
       */
      __cuda_callable__
      IteratorType begin();

      /**
       * \brief Returns iterator pointing at the end of the matrix row.
       *
       * \return iterator pointing at the end.
       */
      __cuda_callable__
      IteratorType end();

      /**
       * \brief Returns constant iterator pointing at the beginning of the matrix row.
       *
       * \return iterator pointing at the beginning.
       */
      __cuda_callable__
      const IteratorType cbegin() const;

      /**
       * \brief Returns constant iterator pointing at the end of the matrix row.
       *
       * \return iterator pointing at the end.
       */
      __cuda_callable__
      const IteratorType cend() const;

   protected:

      IndexType rowIdx, size;

      // SANDBOX_TODO: Replace the following line with data required by your format.
      IndexType offset;

      ValuesViewType values;

      ColumnsIndexesViewType columnIndexes;
};

/**
 * \brief Insertion operator for a sparse matrix row.
 *
 * \param str is an output stream.
 * \param row is an input sparse matrix row.
 * \return  reference to the output stream.
 */
template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
std::ostream& operator<<( std::ostream& str, const SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >& row );

      } // namespace Sandbox
   } // namespace Matrices
} // namespace TNL

#include <TNL/Matrices/Sandbox/SparseSandboxMatrixRowView.hpp>
+240 −0
Original line number Diff line number Diff line
/***************************************************************************
                          SparseSandboxMatrixRowView.hpp -  description
                             -------------------
    begin                : Apr 20, 2021
    copyright            : (C) 2021 by Tomas Oberhuber
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

#pragma once

#include <TNL/Matrices/Sandbox/SparseSandboxMatrixRowView.h>
#include <TNL/Assert.h>

namespace TNL {
   namespace Matrices {
      namespace Sandbox {

// SANDBOX_TODO: Modify the follwing constructor by your needs
template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
SparseSandboxMatrixRowView( IndexType rowIdx,
                            IndexType offset,
                            IndexType size,
                            const ValuesViewType& values,
                            const ColumnsIndexesViewType& columnIndexes )
 : rowIdx( rowIdx ), offset( offset ), size( size ), values( values ), columnIndexes( columnIndexes )
{
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
getSize() const -> IndexType
{
   return this->size;
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__
auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
getRowIndex() const -> const IndexType&
{
   return this->rowIdx;
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
getColumnIndex( const IndexType localIdx ) const -> const IndexType&
{
   TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." );
   // SANDBOX_TODO: Modify the following line to match with your sparse format.
   return columnIndexes[ offset + localIdx ];
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
getColumnIndex( const IndexType localIdx ) -> IndexType&
{
   TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." );
   // SANDBOX_TODO: Modify the following line to match with your sparse format.
   return columnIndexes[ offset + localIdx ];
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
getValue( const IndexType localIdx ) const -> const RealType&
{
   TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." );
   TNL_ASSERT_FALSE( isBinary(), "Cannot call this method for binary matrix row." );
   // SANDBOX_TODO: Modify the following line to match with your sparse format.
   return values[ offset + localIdx ];
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
getValue( const IndexType localIdx ) -> RealType&
{
   TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." );
   TNL_ASSERT_FALSE( isBinary(), "Cannot call this method for binary matrix row." );
   // SANDBOX_TODO: Modify the following line to match with your sparse format.
   return values[ offset + localIdx ];
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ void
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
setValue( const IndexType localIdx,
          const RealType& value )
{
   TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." );
   if( ! isBinary() ) {
      // SANDBOX_TODO: Modify the following line to match with your sparse format.
      const IndexType globalIdx = offset + localIdx;
      values[ globalIdx ] = value;
   }
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ void
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
setColumnIndex( const IndexType localIdx,
                const IndexType& columnIndex )
{
   TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." );
   // SANDBOX_TODO: Modify the following line to match with your sparse format.
   const IndexType globalIdx = offset + localIdx;
   this->columnIndexes[ globalIdx ] = columnIndex;
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ void
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
setElement( const IndexType localIdx,
            const IndexType column,
            const RealType& value )
{
   TNL_ASSERT_LT( localIdx, this->getSize(), "Local index exceeds matrix row capacity." );
   // SANDBOX_TODO: Modify the following line to match with your sparse format.
   const IndexType globalIdx = offset + localIdx;
   columnIndexes[ globalIdx ] = column;
   if( ! isBinary() )
      values[ globalIdx ] = value;
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
   template< typename _ValuesView,
             typename _ColumnsIndexesView,
             bool _isBinary >
__cuda_callable__
bool
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
operator==( const SparseSandboxMatrixRowView< _ValuesView, _ColumnsIndexesView, _isBinary >& other ) const
{
   IndexType i = 0;
   while( i < getSize() && i < other.getSize() ) {
      if( getColumnIndex( i ) != other.getColumnIndex( i ) )
         return false;
      if( ! _isBinary && getValue( i ) != other.getValue( i ) )
         return false;
      ++i;
   }
   for( IndexType j = i; j < getSize(); j++ )
      // TODO: use ... != getPaddingIndex()
      if( getColumnIndex( j ) >= 0 )
         return false;
   for( IndexType j = i; j < other.getSize(); j++ )
      // TODO: use ... != getPaddingIndex()
      if( other.getColumnIndex( j ) >= 0 )
         return false;
   return true;
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
begin() -> IteratorType
{
   return IteratorType( *this, 0 );
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
end() -> IteratorType
{
   return IteratorType( *this, this->getSize() );
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
cbegin() const -> const IteratorType
{
   return IteratorType( *this, 0 );
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
__cuda_callable__ auto
SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::
cend() const -> const IteratorType
{
   return IteratorType( *this, this->getSize() );
}

template< typename ValuesView,
          typename ColumnsIndexesView,
          bool isBinary_ >
std::ostream& operator<<( std::ostream& str, const SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >& row )
{
   using NonConstIndex = std::remove_const_t< typename SparseSandboxMatrixRowView< ValuesView, ColumnsIndexesView, isBinary_ >::IndexType >;
   for( NonConstIndex i = 0; i < row.getSize(); i++ )
      if( isBinary_ )
         // TODO: check getPaddingIndex(), print only the column indices of non-zeros but not the values
         str << " [ " << row.getColumnIndex( i ) << " ] = " << (row.getColumnIndex( i ) >= 0) << ", ";
      else
         str << " [ " << row.getColumnIndex( i ) << " ] = " << row.getValue( i ) << ", ";
   return str;
}

      } // namespace Sandbox
   } // namespace Matrices
} // namespace TNL
Loading