Commit fc8cadec authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

Binary sparse matrix is defined by Real set to bool.

parent 28880a18
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -72,6 +72,7 @@ template< typename Value,
          typename Allocator = typename Allocators::Default< Device >::template Allocator< Value > >
class Array
{
   static_assert( std::is_same< std::remove_cv_t< Value>, std::remove_cv_t< typename Allocator::value_type > >::value, "Mismatch of Array::Value and Allocator::value_type. The type must be the same." );
   public:
      /**
       * \brief Type of elements stored in this array.
+5 −42
Original line number Diff line number Diff line
@@ -18,23 +18,17 @@ namespace Matrices {
 * 
 * It is used for specification of \ref SparseMatrix type.
 */
template< bool Symmetric,
          bool Binary >
template< bool Symmetric >
struct MatrixType
{
   static constexpr bool isSymmetric() { return Symmetric; }

   static constexpr bool isBinary() { return Binary; }

   static String getSerializationType() {
      String type;
      if( ! isBinary() && ! isSymmetric() )
      if( ! isSymmetric() )
         type = "General";
      else
      {
         if( isSymmetric() ) type = "Symmetric";
         if( isBinary() ) type += "Binary";
      }
         type = "Symmetric";
      return type;
   }
};
@@ -44,7 +38,7 @@ struct MatrixType
 * 
 * It is used for specification of \ref SparseMatrix type.
 */
struct GeneralMatrix : MatrixType< false, false > {};
struct GeneralMatrix : MatrixType< false > {};

/**
 * \brief Symmetric matrix type.
@@ -53,38 +47,7 @@ struct GeneralMatrix : MatrixType< false, false > {};
 * upper part is reconstructed on the fly.
 * It is used for specification of \ref SparseMatrix type.
 */
struct SymmetricMatrix : MatrixType< true, false > {};

/**
 * \brief Binary matrix type.
 * 
 * Binary matrix does not store explictly values of matrix elements and thus
 * it reduces memory consumption.
 * It is used for specification of \ref SparseMatrix type. 
 */
struct BinaryMatrix : MatrixType< false, true > {};

/**
 * \brief Symmetric and binary matrix type.
 * 
 * Symmetric matrix stores only lower part of the matrix and its diagonal. The
 * upper part is reconstructed on the fly.
 * Binary matrix does not store explictly values of matrix elements and thus
 * it reduces memory consumption.
 * It is used for specification of \ref SparseMatrix type.
 */
struct BinarySymmetricMatrix : MatrixType< true, true > {};

/**
 * \brief Symmetric and binary matrix type.
 * 
 * Symmetric matrix stores only lower part of the matrix and its diagonal. The
 * upper part is reconstructed on the fly.
 * Binary matrix does not store explictly values of matrix elements and thus
 * it reduces memory consumption.
 * It is used for specification of \ref SparseMatrix type.
 */
struct SymmetricBinaryMatrix : MatrixType< true, true > {};
struct SymmetricMatrix : MatrixType< true > {};

} // namespace Matrices
} // namespace TNL
+14 −8
Original line number Diff line number Diff line
@@ -25,17 +25,19 @@ namespace Matrices {
/**
 * \brief Implementation of sparse matrix, i.e. matrix storing only non-zero elements.
 * 
 * \tparam Real is a type of matrix elements.
 * \tparam Real is a type of matrix elements. If \e Real equals \e bool the matrix is treated 
 *    as binary and so the matrix elements values are not stored in the memory since we need
 *    to remember only coordinates of non-zero elements( which equal one). 
 * \tparam Device is a device where the matrix is allocated.
 * \tparam Index is a type for indexing of the matrix elements.
 * \tparam MatrixType specifies the type of matrix - its symmetry or binarity. See \ref MatrixType.
 *    Both symmetric and binary matrix types reduces memory consumption. Binary matrix does not store
 *    the matrix values explicitly since the non-zero elements can have only value equal to one. Symmetric
 * \tparam MatrixType specifies a symmetry of matrix. See \ref MatrixType. Symmetric
 *    matrices store only lower part of the matrix and its diagonal. The upper part is reconstructed on the fly.
 *    GeneralMatrix with no symmetry is used by default.
 * \tparam Segments is a structure representing the sparse matrix format. Depending on the pattern of the non-zero elements
 *    different matrix formats can perform differently especially on GPUs. By default \ref CSR format is used. See also
 *    \ref Ellpack, \ref SlicedEllpack, \ref ChunkedEllpack or \ref BiEllpack.
 * \tparam ComputeReal is the same as \e Real mostly but for binary matrices it is set to \e Index type. This can be changed
 *    bu the user, of course.
 * \tparam RealAllocator is allocator for the matrix elements values.
 * \tparam IndexAllocator is allocator for the matrix elements column indexes.
 */
@@ -44,6 +46,7 @@ template< typename Real,
          typename Index = int,
          typename MatrixType = GeneralMatrix,
          template< typename Device_, typename Index_, typename IndexAllocator_ > class Segments = Containers::Segments::CSR,
          typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type,
          typename RealAllocator = typename Allocators::Default< Device >::template Allocator< Real >,
          typename IndexAllocator = typename Allocators::Default< Device >::template Allocator< Index > >
class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator >
@@ -80,13 +83,15 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator >
       * 
       * \return \e true if the matrix is stored as binary and \e false otherwise.
       */
      static constexpr bool isBinary() { return MatrixType::isBinary(); };
      static constexpr bool isBinary() { return std::is_same< Real, bool >::value; };

      /**
       * \brief The type of matrix elements.
       */
      using RealType = Real;

      using ComputeRealType = ComputeReal;

      /**
       * \brief The device where the matrix is allocated.
       */
@@ -161,9 +166,10 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator >
                typename _Index = Index,
                typename _MatrixType = MatrixType,
                template< typename, typename, typename > class _Segments = Segments,
                typename _ComputeReal = ComputeReal,
                typename _RealAllocator = typename Allocators::Default< _Device >::template Allocator< _Real >,
                typename _IndexAllocator = typename Allocators::Default< _Device >::template Allocator< _Index > >
      using Self = SparseMatrix< _Real, _Device, _Index, _MatrixType, _Segments, _RealAllocator, _IndexAllocator >;
      using Self = SparseMatrix< _Real, _Device, _Index, _MatrixType, _Segments, _ComputeReal, _RealAllocator, _IndexAllocator >;

      /**
       * \brief Constructor only with values and column indexes allocators.
@@ -770,8 +776,8 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator >
                typename OutVector >
      void vectorProduct( const InVector& inVector,
                          OutVector& outVector,
                          const RealType& matrixMultiplicator = 1.0,
                          const RealType& outVectorMultiplicator = 0.0,
                          const ComputeRealType& matrixMultiplicator = 1.0,
                          const ComputeRealType& outVectorMultiplicator = 0.0,
                          const IndexType firstRow = 0,
                          const IndexType lastRow = 0 ) const;

+99 −53

File changed.

Preview size limit exceeded, changes collapsed.

+29 −10
Original line number Diff line number Diff line
@@ -19,29 +19,45 @@
namespace TNL {
namespace Matrices {

template< typename Real, typename Index = int >
struct ChooseSparseMatrixComputeReal
{
   using type = Real;
};

template< typename Index>
struct ChooseSparseMatrixComputeReal< bool, Index >
{
   using type = Index;
};

/**
 * \brief Implementation of sparse matrix view.
 *
 * It serves as an accessor to \ref SparseMatrix for example when passing the
 * matrix to lambda functions. SparseMatrix view can be also created in CUDA kernels.
 * 
 * \tparam Real is a type of matrix elements.
 * \tparam Real is a type of matrix elements. If \e Real equals \e bool the matrix is treated 
 *    as binary and so the matrix elements values are not stored in the memory since we need
 *    to remember only coordinates of non-zero elements( which equal one). 
 * \tparam Device is a device where the matrix is allocated.
 * \tparam Index is a type for indexing of the matrix elements.
 * \tparam MatrixType specifies the type of matrix - its symmetry or binarity. See \ref MatrixType.
 *    Both symmetric and binary matrix types reduces memory consumption. Binary matrix does not store
 *    the matrix values explicitly since the non-zero elements can have only value equal to one. Symmetric
 * \tparam MatrixType specifies a symmetry of matrix. See \ref MatrixType. Symmetric
 *    matrices store only lower part of the matrix and its diagonal. The upper part is reconstructed on the fly.
 *    GeneralMatrix with no symmetry is used by default.
 * \tparam Segments is a structure representing the sparse matrix format. Depending on the pattern of the non-zero elements
 *    different matrix formats can perform differently especially on GPUs. By default \ref CSR format is used. See also
 *    \ref Ellpack, \ref SlicedEllpack, \ref ChunkedEllpack or \ref BiEllpack.
 * \tparam ComputeReal is the same as \e Real mostly but for binary matrices it is set to \e Index type. This can be changed
 *    bu the user, of course.
 * 
 */
template< typename Real,
          typename Device = Devices::Host,
          typename Index = int,
          typename MatrixType = GeneralMatrix,
          template< typename Device_, typename Index_ > class SegmentsView = Containers::Segments::CSRView >
          template< typename Device_, typename Index_ > class SegmentsView = Containers::Segments::CSRView,
          typename ComputeReal = typename ChooseSparseMatrixComputeReal< Real, Index >::type >
class SparseMatrixView : public MatrixView< Real, Device, Index >
{
   static_assert(
@@ -73,13 +89,15 @@ class SparseMatrixView : public MatrixView< Real, Device, Index >
       * 
       * \return \e true if the matrix is stored as binary and \e false otherwise.
       */
      static constexpr bool isBinary() { return MatrixType::isBinary(); };
      static constexpr bool isBinary() { return std::is_same< Real, bool >::value; };

      /**
       * \brief The type of matrix elements.
       */
      using RealType = Real;

      using ComputeRealType = ComputeReal;

      /**
       * \brief The device where the matrix is allocated.
       */
@@ -128,8 +146,9 @@ class SparseMatrixView : public MatrixView< Real, Device, Index >
                typename _Device = Device,
                typename _Index = Index,
                typename _MatrixType = MatrixType,
                template< typename, typename > class _SegmentsView = SegmentsView >
      using Self = SparseMatrixView< _Real, _Device, _Index, _MatrixType, _SegmentsView >;
                template< typename, typename > class _SegmentsView = SegmentsView,
                typename _ComputeReal = ComputeReal >
      using Self = SparseMatrixView< _Real, _Device, _Index, _MatrixType, _SegmentsView, _ComputeReal >;

      /**
       * \brief Constructor with no parameters.
@@ -565,8 +584,8 @@ class SparseMatrixView : public MatrixView< Real, Device, Index >
                typename OutVector >
      void vectorProduct( const InVector& inVector,
                          OutVector& outVector,
                          const RealType matrixMultiplicator = 1.0,
                          const RealType outVectorMultiplicator = 0.0,
                          const ComputeRealType matrixMultiplicator = 1.0,
                          const ComputeRealType outVectorMultiplicator = 0.0,
                          const IndexType begin = 0,
                          IndexType end = 0 ) const;

Loading