Commit 4e62a8ff authored by Tomáš Oberhuber's avatar Tomáš Oberhuber Committed by Tomáš Oberhuber
Browse files

Fixing symmetric sparse matrix.

parent 537a8805
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -145,7 +145,7 @@ class SparseMatrix : public Matrix< Real, Device, Index, RealAllocator >
      void vectorProduct( const InVector& inVector,
                          OutVector& outVector,
                          const RealType& matrixMultiplicator = 1.0,
                          const RealType& inVectorAddition = 0.0 ) const;
                          const RealType& outVectorMultiplicator = 0.0 ) const;

      /*template< typename Real2, typename Index2 >
      void addMatrix( const SparseMatrix< Real2, Segments, Device, Index2 >& matrix,
+2 −2
Original line number Diff line number Diff line
@@ -396,9 +396,9 @@ SparseMatrix< Real, Device, Index, MatrixType, Segments, RealAllocator, IndexAll
vectorProduct( const InVector& inVector,
               OutVector& outVector,
               const RealType& matrixMultiplicator,
               const RealType& inVectorAddition ) const
               const RealType& outVectorMultiplicator ) const
{
   this->view.vectorProduct( inVector, outVector, matrixMultiplicator, inVectorAddition );
   this->view.vectorProduct( inVector, outVector, matrixMultiplicator, outVectorMultiplicator );
   /*TNL_ASSERT_EQ( this->getColumns(), inVector.getSize(), "Matrix columns do not fit with input vector." );
   TNL_ASSERT_EQ( this->getRows(), outVector.getSize(), "Matrix rows do not fit with output vector." );

+2 −2
Original line number Diff line number Diff line
@@ -118,8 +118,8 @@ class SparseMatrixView : public MatrixView< Real, Device, Index >
                typename OutVector >
      void vectorProduct( const InVector& inVector,
                          OutVector& outVector,
                          const RealType& matrixMultiplicator = 1.0,
                          const RealType& inVectorAddition = 0.0 ) const;
                          const RealType matrixMultiplicator = 1.0,
                          const RealType outVectorMultiplicator = 0.0 ) const;

      template< typename Fetch, typename Reduce, typename Keep, typename FetchReal >
      void rowsReduction( IndexType first, IndexType last, Fetch& fetch, Reduce& reduce, Keep& keep, const FetchReal& zero ) const;
+15 −3
Original line number Diff line number Diff line
@@ -13,6 +13,7 @@
#include <functional>
#include <TNL/Matrices/SparseMatrixView.h>
#include <TNL/Algorithms/Reduction.h>
#include <TNL/Atomic.h>

namespace TNL {
namespace Matrices {
@@ -367,8 +368,8 @@ void
SparseMatrixView< Real, Device, Index, MatrixType, SegmentsView >::
vectorProduct( const InVector& inVector,
               OutVector& outVector,
               const RealType& matrixMultiplicator,
               const RealType& inVectorAddition ) const
               const RealType matrixMultiplicator,
               const RealType outVectorMultiplicator ) const
{
   TNL_ASSERT_EQ( this->getColumns(), inVector.getSize(), "Matrix columns do not fit with input vector." );
   TNL_ASSERT_EQ( this->getRows(), outVector.getSize(), "Matrix rows do not fit with output vector." );
@@ -378,11 +379,19 @@ vectorProduct( const InVector& inVector,
   const auto valuesView = this->values.getConstView();
   const auto columnIndexesView = this->columnIndexes.getConstView();
   const IndexType paddingIndex = this->getPaddingIndex();
   if( isSymmetric() )
      outVector *= outVectorMultiplicator;
   auto fetch = [=] __cuda_callable__ ( IndexType row, IndexType localIdx, IndexType globalIdx, bool& compute ) -> RealType {
      const IndexType column = columnIndexesView[ globalIdx ];
      compute = ( column != paddingIndex );
      if( ! compute )
         return 0.0;
      if( isSymmetric() )
      {
         TNL_ASSERT_TRUE( false, "" );
         //Atomic< RealType, DeviceType > atomic;
         //if( isBinary() )
      }
      if( isBinary() )
         return inVectorView[ column ];
      return valuesView[ globalIdx ] * inVectorView[ column ];
@@ -391,7 +400,10 @@ vectorProduct( const InVector& inVector,
      sum += value;
   };
   auto keeper = [=] __cuda_callable__ ( IndexType row, const RealType& value ) mutable {
      outVectorView[ row ] = value;
      if( outVectorMultiplicator == 0.0 )
         outVectorView[ row ] = matrixMultiplicator * value;
      else
         outVectorView[ row ] = outVectorMultiplicator * outVectorView[ row ] + matrixMultiplicator * value;
   };
   this->segments.segmentsReduction( 0, this->getRows(), fetch, reduction, keeper, ( RealType ) 0.0 );

+5 −4
Original line number Diff line number Diff line
@@ -641,13 +641,14 @@ void test_AddElement()
    *    \  0  0  0  0 10 /   \  0  0  0  0  1 /   \  0  0  0  0 21 /
    */

   for( IndexType i = 1; i < rows; i++ )
   for( IndexType i = 0; i < rows; i++ )
   {
      if( i > 0 )
         m.addElement( i, i - 1, 1.0, 2.0 );
      if( i < cols )
         m.addElement( i, i, 0.0, 2.0 );
   }

   std::cerr << m << std::endl;
   EXPECT_EQ( m.getElement( 0, 0 ),  2 );
   EXPECT_EQ( m.getElement( 0, 1 ),  5 );
   EXPECT_EQ( m.getElement( 0, 2 ),  0 );