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

Refactoring binary sparse matrix unit tests.

parent c5f2624b
Loading
Loading
Loading
Loading
+551 −844
Original line number Diff line number Diff line
@@ -13,11 +13,7 @@
#include <TNL/Math.h>
#include <TNL/Algorithms/ParallelFor.h>
#include <iostream>

// Temporary, until test_OperatorEquals doesn't work for all formats.
#include <TNL/Matrices/Legacy/ChunkedEllpack.h>
#include <TNL/Matrices/Legacy/AdEllpack.h>
#include <TNL/Matrices/Legacy/BiEllpack.h>
#include <sstream>

#ifdef HAVE_GTEST
#include <gtest/gtest.h>
@@ -55,6 +51,10 @@ void test_SetDimensions()

   EXPECT_EQ( m.getRows(), 9 );
   EXPECT_EQ( m.getColumns(), 8 );

   Matrix m2( rows, cols );
   EXPECT_EQ( m2.getRows(), 9 );
   EXPECT_EQ( m2.getColumns(), 8 );
}

template< typename Matrix >
@@ -67,9 +67,7 @@ void test_SetCompressedRowLengths()
   const IndexType rows = 10;
   const IndexType cols = 11;

    Matrix m;
    m.reset();
    m.setDimensions( rows, cols );
   Matrix m( rows, cols );
   typename Matrix::CompressedRowLengthsVector rowLengths;
   rowLengths.setSize( rows );
   rowLengths.setValue( 3 );
@@ -81,7 +79,6 @@ void test_SetCompressedRowLengths()
   m.setCompressedRowLengths( rowLengths );

   // Insert values into the rows.

   for( IndexType i = 0; i < 3; i++ )      // 0th row
      m.setElement( 0, i, 1 );

@@ -128,17 +125,11 @@ void test_SetLike()
   const IndexType rows = 8;
   const IndexType cols = 7;

    Matrix1 m1;
    m1.reset();
    m1.setDimensions( rows + 1, cols + 2 );

    Matrix2 m2;
    m2.reset();
    m2.setDimensions( rows, cols );
   Matrix1 m1( rows + 1, cols + 2 );
   Matrix2 m2( rows, cols );

   m1.setLike( m2 );


   EXPECT_EQ( m1.getRows(), m2.getRows() );
   EXPECT_EQ( m1.getColumns(), m2.getColumns() );
}
@@ -168,10 +159,7 @@ void test_GetNumberOfNonzeroMatrixElements()
   const IndexType rows = 10;
   const IndexType cols = 10;

   Matrix m;
   m.reset();

   m.setDimensions( rows, cols );
   Matrix m( rows, cols );

   typename Matrix::CompressedRowLengthsVector rowLengths;
   rowLengths.setSize( rows );
@@ -180,9 +168,8 @@ void test_GetNumberOfNonzeroMatrixElements()
   rowLengths.setElement( 2, 1 );
   rowLengths.setElement( 3, 1 );
   for( IndexType i = 4; i < rows - 2; i++ )
   {
      rowLengths.setElement( i, 1 );
   }

   rowLengths.setElement( 8, 1 );
   rowLengths.setElement( 9, 1 );
   m.setCompressedRowLengths( rowLengths );
@@ -203,10 +190,8 @@ void test_GetNumberOfNonzeroMatrixElements()
      m.setElement( i, 0, 1 );

   for( IndexType j = 8; j < rows; j++)
   {
      for( IndexType i = 0; i < cols; i++ )
         m.setElement( j, i, 1 );
   }

   EXPECT_EQ( m.getNumberOfNonzeroMatrixElements(), 41 );
}
@@ -231,12 +216,9 @@ void test_Reset()
   const IndexType rows = 5;
   const IndexType cols = 4;

    Matrix m;
    m.setDimensions( rows, cols );

   Matrix m( rows, cols );
   m.reset();


   EXPECT_EQ( m.getRows(), 0 );
   EXPECT_EQ( m.getColumns(), 0 );
}
@@ -275,9 +257,8 @@ void test_GetRow()
   rowLengths.setElement( 2, 8 );
   rowLengths.setElement( 3, 2 );
   for( IndexType i = 4; i < rows - 2; i++ )
    {
       rowLengths.setElement( i, 1 );
    }

   rowLengths.setElement( 8, 10 );
   rowLengths.setElement( 9, 10 );
   m.setCompressedRowLengths( rowLengths );
@@ -464,10 +445,7 @@ void test_SetElement()
   const IndexType rows = 10;
   const IndexType cols = 10;

    Matrix m;
    m.reset();

    m.setDimensions( rows, cols );
   Matrix m( rows, cols );

   typename Matrix::CompressedRowLengthsVector rowLengths;
   rowLengths.setSize( rows );
@@ -476,9 +454,8 @@ void test_SetElement()
   rowLengths.setElement( 2, 8 );
   rowLengths.setElement( 3, 2 );
   for( IndexType i = 4; i < rows - 2; i++ )
    {
       rowLengths.setElement( i, 1 );
    }

   rowLengths.setElement( 8, 10 );
   rowLengths.setElement( 9, 10 );
   m.setCompressedRowLengths( rowLengths );
@@ -499,10 +476,8 @@ void test_SetElement()
       m.setElement( i, 0, 1 );

   for( IndexType j = 8; j < rows; j++)
    {
       for( IndexType i = 0; i < cols; i++ )
           m.setElement( j, i, 1 );
    }

   EXPECT_EQ( m.getElement( 0, 0 ), 1 );
   EXPECT_EQ( m.getElement( 0, 1 ), 0 );
@@ -706,13 +681,11 @@ void test_VectorProduct()

   m_2.vectorProduct( inVector_2, outVector_2 );


   EXPECT_EQ( outVector_2.getElement( 0 ), 6 );
   EXPECT_EQ( outVector_2.getElement( 1 ), 2 );
   EXPECT_EQ( outVector_2.getElement( 2 ), 6 );
   EXPECT_EQ( outVector_2.getElement( 3 ), 2 );


   /*
    * Sets up the following 4x4 sparse matrix:
    *
@@ -1085,268 +1058,6 @@ void test_PerformSORIteration()
   EXPECT_EQ( xVector[ 3 ], 0.25 );
}

// This test is only for AdEllpack
template< typename Matrix >
void test_OperatorEquals()
{
   using RealType = typename Matrix::RealType;
   using DeviceType = typename Matrix::DeviceType;
   using IndexType = typename Matrix::IndexType;

   if( std::is_same< DeviceType, TNL::Devices::Cuda >::value )
      return;
   else
   {
      using AdELL_host = TNL::Matrices::AdEllpack< RealType, TNL::Devices::Host, IndexType >;
      using AdELL_cuda = TNL::Matrices::AdEllpack< RealType, TNL::Devices::Cuda, IndexType >;

      /*
       * Sets up the following 8x8 sparse matrix:
       *
       *    /  1  2  3  0  4  5  0  1 \   6
       *    |  0  6  0  7  0  0  0  1 |   3
       *    |  0  8  9  0 10  0  0  1 |   4
       *    |  0 11 12 13 14  0  0  1 |   5
       *    |  0 15  0  0  0  0  0  1 |   2
       *    |  0 16 17 18 19 20 21  1 |   7
       *    | 22 23 24 25 26 27 28  1 |   8
       *    \ 29 30 31 32 33 34 35 36 /   8
       */

      const IndexType m_rows = 8;
      const IndexType m_cols = 8;

      AdELL_host m_host;

      m_host.reset();
      m_host.setDimensions( m_rows, m_cols );
      typename AdELL_host::CompressedRowLengthsVector rowLengths;
      rowLengths.setSize( m_rows );
      rowLengths.setElement(0, 6);
      rowLengths.setElement(1, 3);
      rowLengths.setElement(2, 4);
      rowLengths.setElement(3, 5);
      rowLengths.setElement(4, 2);
      rowLengths.setElement(5, 7);
      rowLengths.setElement(6, 8);
      rowLengths.setElement(7, 8);
      m_host.setCompressedRowLengths( rowLengths );

      RealType value = 1;
      for( IndexType i = 0; i < 3; i++ )   // 0th row
          m_host.setElement( 0, i, value++ );

      m_host.setElement( 0, 4, value++ );           // 0th row
      m_host.setElement( 0, 5, value++ );

      m_host.setElement( 1, 1, value++ );           // 1st row
      m_host.setElement( 1, 3, value++ );

      for( IndexType i = 1; i < 3; i++ )            // 2nd row
          m_host.setElement( 2, i, value++ );

      m_host.setElement( 2, 4, value++ );           // 2nd row


      for( IndexType i = 1; i < 5; i++ )            // 3rd row
          m_host.setElement( 3, i, value++ );

      m_host.setElement( 4, 1, value++ );           // 4th row

      for( IndexType i = 1; i < 7; i++ )            // 5th row
          m_host.setElement( 5, i, value++ );

      for( IndexType i = 0; i < 7; i++ )            // 6th row
          m_host.setElement( 6, i, value++ );

      for( IndexType i = 0; i < 8; i++ )            // 7th row
          m_host.setElement( 7, i, value++ );

      for( IndexType i = 0; i < 7; i++ )            // 1s at the end or rows: 5, 6
          m_host.setElement( i, 7, 1);

      EXPECT_EQ( m_host.getElement( 0, 0 ),  1 );
      EXPECT_EQ( m_host.getElement( 0, 1 ),  2 );
      EXPECT_EQ( m_host.getElement( 0, 2 ),  3 );
      EXPECT_EQ( m_host.getElement( 0, 3 ),  0 );
      EXPECT_EQ( m_host.getElement( 0, 4 ),  4 );
      EXPECT_EQ( m_host.getElement( 0, 5 ),  5 );
      EXPECT_EQ( m_host.getElement( 0, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 0, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 1, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 1 ),  6 );
      EXPECT_EQ( m_host.getElement( 1, 2 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 3 ),  7 );
      EXPECT_EQ( m_host.getElement( 1, 4 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 2, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 1 ),  8 );
      EXPECT_EQ( m_host.getElement( 2, 2 ),  9 );
      EXPECT_EQ( m_host.getElement( 2, 3 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 4 ), 10 );
      EXPECT_EQ( m_host.getElement( 2, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 3, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 3, 1 ), 11 );
      EXPECT_EQ( m_host.getElement( 3, 2 ), 12 );
      EXPECT_EQ( m_host.getElement( 3, 3 ), 13 );
      EXPECT_EQ( m_host.getElement( 3, 4 ), 14 );
      EXPECT_EQ( m_host.getElement( 3, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 3, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 3, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 4, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 1 ), 15 );
      EXPECT_EQ( m_host.getElement( 4, 2 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 3 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 4 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 5, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 5, 1 ), 16 );
      EXPECT_EQ( m_host.getElement( 5, 2 ), 17 );
      EXPECT_EQ( m_host.getElement( 5, 3 ), 18 );
      EXPECT_EQ( m_host.getElement( 5, 4 ), 19 );
      EXPECT_EQ( m_host.getElement( 5, 5 ), 20 );
      EXPECT_EQ( m_host.getElement( 5, 6 ), 21 );
      EXPECT_EQ( m_host.getElement( 5, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 6, 0 ), 22 );
      EXPECT_EQ( m_host.getElement( 6, 1 ), 23 );
      EXPECT_EQ( m_host.getElement( 6, 2 ), 24 );
      EXPECT_EQ( m_host.getElement( 6, 3 ), 25 );
      EXPECT_EQ( m_host.getElement( 6, 4 ), 26 );
      EXPECT_EQ( m_host.getElement( 6, 5 ), 27 );
      EXPECT_EQ( m_host.getElement( 6, 6 ), 28 );
      EXPECT_EQ( m_host.getElement( 6, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 7, 0 ), 29 );
      EXPECT_EQ( m_host.getElement( 7, 1 ), 30 );
      EXPECT_EQ( m_host.getElement( 7, 2 ), 31 );
      EXPECT_EQ( m_host.getElement( 7, 3 ), 32 );
      EXPECT_EQ( m_host.getElement( 7, 4 ), 33 );
      EXPECT_EQ( m_host.getElement( 7, 5 ), 34 );
      EXPECT_EQ( m_host.getElement( 7, 6 ), 35 );
      EXPECT_EQ( m_host.getElement( 7, 7 ), 36 );

      AdELL_cuda m_cuda;

      // Copy the host matrix into the cuda matrix
      m_cuda = m_host;

      // Reset the host matrix
      m_host.reset();

      // Copy the cuda matrix back into the host matrix
      m_host = m_cuda;

      // Check the newly created double-copy host matrix
      EXPECT_EQ( m_host.getElement( 0, 0 ),  1 );
      EXPECT_EQ( m_host.getElement( 0, 1 ),  2 );
      EXPECT_EQ( m_host.getElement( 0, 2 ),  3 );
      EXPECT_EQ( m_host.getElement( 0, 3 ),  0 );
      EXPECT_EQ( m_host.getElement( 0, 4 ),  4 );
      EXPECT_EQ( m_host.getElement( 0, 5 ),  5 );
      EXPECT_EQ( m_host.getElement( 0, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 0, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 1, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 1 ),  6 );
      EXPECT_EQ( m_host.getElement( 1, 2 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 3 ),  7 );
      EXPECT_EQ( m_host.getElement( 1, 4 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 1, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 2, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 1 ),  8 );
      EXPECT_EQ( m_host.getElement( 2, 2 ),  9 );
      EXPECT_EQ( m_host.getElement( 2, 3 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 4 ), 10 );
      EXPECT_EQ( m_host.getElement( 2, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 2, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 3, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 3, 1 ), 11 );
      EXPECT_EQ( m_host.getElement( 3, 2 ), 12 );
      EXPECT_EQ( m_host.getElement( 3, 3 ), 13 );
      EXPECT_EQ( m_host.getElement( 3, 4 ), 14 );
      EXPECT_EQ( m_host.getElement( 3, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 3, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 3, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 4, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 1 ), 15 );
      EXPECT_EQ( m_host.getElement( 4, 2 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 3 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 4 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 5 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 6 ),  0 );
      EXPECT_EQ( m_host.getElement( 4, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 5, 0 ),  0 );
      EXPECT_EQ( m_host.getElement( 5, 1 ), 16 );
      EXPECT_EQ( m_host.getElement( 5, 2 ), 17 );
      EXPECT_EQ( m_host.getElement( 5, 3 ), 18 );
      EXPECT_EQ( m_host.getElement( 5, 4 ), 19 );
      EXPECT_EQ( m_host.getElement( 5, 5 ), 20 );
      EXPECT_EQ( m_host.getElement( 5, 6 ), 21 );
      EXPECT_EQ( m_host.getElement( 5, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 6, 0 ), 22 );
      EXPECT_EQ( m_host.getElement( 6, 1 ), 23 );
      EXPECT_EQ( m_host.getElement( 6, 2 ), 24 );
      EXPECT_EQ( m_host.getElement( 6, 3 ), 25 );
      EXPECT_EQ( m_host.getElement( 6, 4 ), 26 );
      EXPECT_EQ( m_host.getElement( 6, 5 ), 27 );
      EXPECT_EQ( m_host.getElement( 6, 6 ), 28 );
      EXPECT_EQ( m_host.getElement( 6, 7 ),  1 );

      EXPECT_EQ( m_host.getElement( 7, 0 ), 29 );
      EXPECT_EQ( m_host.getElement( 7, 1 ), 30 );
      EXPECT_EQ( m_host.getElement( 7, 2 ), 31 );
      EXPECT_EQ( m_host.getElement( 7, 3 ), 32 );
      EXPECT_EQ( m_host.getElement( 7, 4 ), 33 );
      EXPECT_EQ( m_host.getElement( 7, 5 ), 34 );
      EXPECT_EQ( m_host.getElement( 7, 6 ), 35 );
      EXPECT_EQ( m_host.getElement( 7, 7 ), 36 );

      // Try vectorProduct with copied cuda matrix to see if it works correctly.
      using VectorType = TNL::Containers::Vector< RealType, TNL::Devices::Cuda, IndexType >;

      VectorType inVector;
      inVector.setSize( m_cols );
      for( IndexType i = 0; i < inVector.getSize(); i++ )
          inVector.setElement( i, 2 );

      VectorType outVector;
      outVector.setSize( m_rows );
      for( IndexType j = 0; j < outVector.getSize(); j++ )
          outVector.setElement( j, 0 );

      m_cuda.vectorProduct( inVector, outVector );

      EXPECT_EQ( outVector.getElement( 0 ),  32 );
      EXPECT_EQ( outVector.getElement( 1 ),  28 );
      EXPECT_EQ( outVector.getElement( 2 ),  56 );
      EXPECT_EQ( outVector.getElement( 3 ), 102 );
      EXPECT_EQ( outVector.getElement( 4 ),  32 );
      EXPECT_EQ( outVector.getElement( 5 ), 224 );
      EXPECT_EQ( outVector.getElement( 6 ), 352 );
      EXPECT_EQ( outVector.getElement( 7 ), 520 );
   }
}

template< typename Matrix >
void test_SaveAndLoad( const char* filename )
{
@@ -1366,9 +1077,7 @@ void test_SaveAndLoad( const char* filename )
   const IndexType m_rows = 4;
   const IndexType m_cols = 4;

    Matrix savedMatrix;
    savedMatrix.reset();
    savedMatrix.setDimensions( m_rows, m_cols );
   Matrix savedMatrix( m_rows, m_cols );
   typename Matrix::CompressedRowLengthsVector rowLengths;
   rowLengths.setSize( m_rows );
   rowLengths.setValue( 3 );
@@ -1486,7 +1195,6 @@ void test_Print()
   for( IndexType i = 2; i < m_cols; i++ )       // 4th row
       m.setElement( 4, i, 1 );

    #include <sstream>
   std::stringstream printed;
   std::stringstream couted;

@@ -1503,7 +1211,6 @@ void test_Print()
              "Row: 3 ->  Col:1->1	 Col:2->1	 Col:3->1\t\n"
              "Row: 4 ->  Col:2->1	 Col:3->1\t\n";


   EXPECT_EQ( printed.str(), couted.str() );
}