Commit 600fd464 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Moved StaticMatrix from the Containers namespace into Matrices, added...

Moved StaticMatrix from the Containers namespace into Matrices, added matrix-vector multiplication operator
parent 7346aa9b
Loading
Loading
Loading
Loading
+0 −29
Original line number Diff line number Diff line
@@ -367,35 +367,6 @@ public:
   using Base::operator=;
};

template< typename Value,
          std::size_t Rows,
          std::size_t Columns,
          typename Permutation = std::index_sequence< 0, 1 > >  // identity by default
class StaticMatrix
: public StaticNDArray< Value,
                        SizesHolder< std::size_t, Rows, Columns >,
                        Permutation >
{
   using Base = StaticNDArray< Value,
                        SizesHolder< std::size_t, Rows, Columns >,
                        Permutation >;

public:
   // inherit all assignment operators
   using Base::operator=;

   static constexpr std::size_t getRows()
   {
      return Rows;
   }

   __cuda_callable__
   static constexpr std::size_t getColumns()
   {
      return Columns;
   }
};

template< typename Value,
          typename SizesHolder,
          typename Permutation = std::make_index_sequence< SizesHolder::getDimension() >,  // identity by default
+65 −0
Original line number Diff line number Diff line
/***************************************************************************
                          StaticMatrix.h  -  description
                             -------------------
    begin                : Dec 3, 2020
    copyright            : (C) 2020 by Tomas Oberhuber et al.
    email                : tomas.oberhuber@fjfi.cvut.cz
 ***************************************************************************/

/* See Copyright Notice in tnl/Copyright */

// Implemented by: Jakub Klinkovsky

#pragma once

#include <TNL/Containers/NDArray.h>
#include <TNL/Containers/StaticVector.h>

namespace TNL {
namespace Matrices {

template< typename Value,
          std::size_t Rows,
          std::size_t Columns,
          typename Permutation = std::index_sequence< 0, 1 > >  // identity by default
class StaticMatrix
: public Containers::StaticNDArray< Value,
                                    Containers::SizesHolder< std::size_t, Rows, Columns >,
                                    Permutation >
{
   using Base = Containers::StaticNDArray< Value,
                                           Containers::SizesHolder< std::size_t, Rows, Columns >,
                                           Permutation >;

public:
   // inherit all assignment operators
   using Base::operator=;

   static constexpr std::size_t getRows()
   {
      return Rows;
   }

   __cuda_callable__
   static constexpr std::size_t getColumns()
   {
      return Columns;
   }

   __cuda_callable__
   Containers::StaticVector< Rows, Value >
   operator*( const Containers::StaticVector< Columns, Value > & vector ) const
   {
      Containers::StaticVector< Rows, Value > result;
      for( std::size_t i = 0; i < Rows; i++ ) {
         Value v = 0;
         for( std::size_t j = 0; j < Columns; j++ )
            v += (*this)( i, j ) * vector[ j ];
         result[ i ] = v;
      }
      return result;
   }
};

} // namespace Matrices
} // namespace TNL
+3 −1
Original line number Diff line number Diff line
@@ -20,7 +20,9 @@ set( COMMON_TESTS
            LambdaMatrixTest
)

set( CPP_TESTS )
set( CPP_TESTS
            StaticMatrixTest
)
set( CUDA_TESTS )
if( BUILD_CUDA )
   set( CUDA_TESTS  ${CUDA_TESTS} ${COMMON_TESTS} )
+113 −0
Original line number Diff line number Diff line
#ifdef HAVE_GTEST
#include "gtest/gtest.h"

#include <TNL/Matrices/StaticMatrix.h>

using namespace TNL::Containers;
using namespace TNL::Matrices;

TEST( StaticNDArrayTest, 3x4_row_major )
{
   constexpr int I = 3, J = 4;
   StaticMatrix< int, I, J > M;
   StaticVector< I, int > a, row_sums;
   StaticVector< J, int > b;

   row_sums.setValue( 0 );
   a.setValue( 0 );
   b.setValue( 1 );

   int v = 0;
   for( int i = 0; i < I; i++ )
   for( int j = 0; j < J; j++ )
   {
      M( i, j ) = v;
      row_sums[ i ] += v;
      v++;
   }

   a = M * b;

   EXPECT_EQ( a, row_sums );
}

TEST( StaticNDArrayTest, 4x3_row_major )
{
   constexpr int I = 4, J = 3;
   StaticMatrix< int, I, J > M;
   StaticVector< I, int > a, row_sums;
   StaticVector< J, int > b;

   row_sums.setValue( 0 );
   a.setValue( 0 );
   b.setValue( 1 );

   int v = 0;
   for( int i = 0; i < I; i++ )
   for( int j = 0; j < J; j++ )
   {
      M( i, j ) = v;
      row_sums[ i ] += v;
      v++;
   }

   a = M * b;

   EXPECT_EQ( a, row_sums );
}

TEST( StaticNDArrayTest, 3x4_column_major )
{
   constexpr int I = 3, J = 4;
   using Permutation = std::index_sequence< 1, 0 >;
   StaticMatrix< int, I, J, Permutation > M;
   StaticVector< I, int > a, row_sums;
   StaticVector< J, int > b;

   row_sums.setValue( 0 );
   a.setValue( 0 );
   b.setValue( 1 );

   int v = 0;
   for( int i = 0; i < I; i++ )
   for( int j = 0; j < J; j++ )
   {
      M( i, j ) = v;
      row_sums[ i ] += v;
      v++;
   }

   a = M * b;

   EXPECT_EQ( a, row_sums );
}

TEST( StaticNDArrayTest, 4x3_column_major )
{
   constexpr int I = 4, J = 3;
   using Permutation = std::index_sequence< 1, 0 >;
   StaticMatrix< int, I, J, Permutation > M;
   StaticVector< I, int > a, row_sums;
   StaticVector< J, int > b;

   row_sums.setValue( 0 );
   a.setValue( 0 );
   b.setValue( 1 );

   int v = 0;
   for( int i = 0; i < I; i++ )
   for( int j = 0; j < J; j++ )
   {
      M( i, j ) = v;
      row_sums[ i ] += v;
      v++;
   }

   a = M * b;

   EXPECT_EQ( a, row_sums );
}
#endif // HAVE_GTEST


#include "../main.h"