Commit abe7ae92 authored by Jakub Klinkovský's avatar Jakub Klinkovský
Browse files

Cleaned up ArrayAssignment and VectorAssignment

parent bcc1b92a
Loading
Loading
Loading
Loading
+9 −9
Original line number Diff line number Diff line
@@ -18,7 +18,7 @@ namespace TNL {
namespace Containers {
namespace Algorithms {

namespace Details {
namespace detail {
/**
 * SFINAE for checking if T has getArrayData method
 */
@@ -35,11 +35,11 @@ private:
public:
    static constexpr bool value = ( sizeof( test< T >(0) ) == sizeof( YesType ) );
};
} // namespace Details
} // namespace detail

template< typename Array,
          typename T,
          bool hasGetArrayData = Details::HasGetArrayData< T >::value >
          bool hasGetArrayData = detail::HasGetArrayData< T >::value >
struct ArrayAssignment{};

/**
@@ -62,7 +62,7 @@ struct ArrayAssignment< Array, T, true >
         ArrayOperations< typename Array::DeviceType, typename T::DeviceType >::template
            copyMemory< typename Array::ValueType, typename T::ValueType, typename Array::IndexType >
            ( a.getArrayData(), t.getArrayData(), t.getSize() );
   };
   }
};

/**
@@ -75,15 +75,15 @@ struct ArrayAssignment< Array, T, false >
{
   static void resize( Array& a, const T& t )
   {
   };
   }

   static void assign( Array& a, const T& t )
   {
      TNL_ASSERT_FALSE( a.empty(), "Cannot assign value to empty array." );
      ArrayOperations< typename Array::DeviceType >::template
         setMemory< typename Array::ValueType, typename Array::IndexType >
         ( a.getArrayData(), ( typename Array::ValueType ) t, a.getSize() );
   };

   }
};

} // namespace Algorithms
+31 −41
Original line number Diff line number Diff line
@@ -19,7 +19,7 @@ namespace TNL {
namespace Containers {
namespace Algorithms {

namespace Details {
namespace detail {
/**
 * SFINAE for checking if T has getSize method
 * TODO: We should better test operator[] but we need to know the indexing type.
@@ -37,14 +37,14 @@ private:
public:
    static constexpr bool value = ( sizeof( test< T >(0) ) == sizeof( YesType ) );
};
} // namespace Details
} // namespace detail

/**
 * \brief Vector assignment
 */
template< typename Vector,
          typename T,
          bool hasSubscriptOperator = Details::HasSubscriptOperator< T >::value >
          bool hasSubscriptOperator = detail::HasSubscriptOperator< T >::value >
struct VectorAssignment{};

/**
@@ -52,7 +52,7 @@ struct VectorAssignment{};
 */
template< typename Vector,
          typename T,
          bool hasSubscriptOperator = Details::HasSubscriptOperator< T >::value >
          bool hasSubscriptOperator = detail::HasSubscriptOperator< T >::value >
struct VectorAddition{};

/**
@@ -60,7 +60,7 @@ struct VectorAddition{};
 */
template< typename Vector,
          typename T,
          bool hasSubscriptOperator = Details::HasSubscriptOperator< T >::value >
          bool hasSubscriptOperator = detail::HasSubscriptOperator< T >::value >
struct VectorSubtraction{};

/**
@@ -68,7 +68,7 @@ struct VectorSubtraction{};
 */
template< typename Vector,
          typename T,
          bool hasSubscriptOperator = Details::HasSubscriptOperator< T >::value >
          bool hasSubscriptOperator = detail::HasSubscriptOperator< T >::value >
struct VectorMultiplication{};

/**
@@ -76,7 +76,7 @@ struct VectorMultiplication{};
 */
template< typename Vector,
          typename T,
          bool hasSubscriptOperator = Details::HasSubscriptOperator< T >::value >
          bool hasSubscriptOperator = detail::HasSubscriptOperator< T >::value >
struct VectorDivision{};

/**
@@ -97,7 +97,7 @@ struct VectorAssignment< Vector, T, true >
      TNL_ASSERT_EQ( v.getSize(), t.getSize(), "The sizes of the vectors must be equal." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] = t[ i ];
   };
   }

   static void assign( Vector& v, const T& t )
   {
@@ -112,8 +112,7 @@ struct VectorAssignment< Vector, T, true >
         data[ i ] = t[ i ];
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), ass );
      TNL_CHECK_CUDA_DEVICE;
   };
   }
};

/**
@@ -126,7 +125,7 @@ struct VectorAssignment< Vector, T, false >
{
   static void resize( Vector& v, const T& t )
   {
   };
   }

   __cuda_callable__
   static void assignStatic( Vector& v, const T& t )
@@ -134,7 +133,7 @@ struct VectorAssignment< Vector, T, false >
      TNL_ASSERT_GT( v.getSize(), 0, "Cannot assign value to empty vector." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] = t;
   };
   }

   static void assign( Vector& v, const T& t )
   {
@@ -148,7 +147,6 @@ struct VectorAssignment< Vector, T, false >
         data[ i ] = t;
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), ass );
      TNL_CHECK_CUDA_DEVICE;
   }
};

@@ -165,7 +163,7 @@ struct VectorAddition< Vector, T, true >
      TNL_ASSERT_EQ( v.getSize(), t.getSize(), "The sizes of the vectors must be equal." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] += t[ i ];
   };
   }

   static void addition( Vector& v, const T& t )
   {
@@ -180,8 +178,7 @@ struct VectorAddition< Vector, T, true >
         data[ i ] += t[ i ];
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), add );
      TNL_CHECK_CUDA_DEVICE;
   };
   }
};

/**
@@ -198,7 +195,7 @@ struct VectorAddition< Vector, T, false >
      TNL_ASSERT_GT( v.getSize(), 0, "Cannot assign value to empty vector." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] += t;
   };
   }

   static void addition( Vector& v, const T& t )
   {
@@ -212,7 +209,6 @@ struct VectorAddition< Vector, T, false >
         data[ i ] += t;
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), add );
      TNL_CHECK_CUDA_DEVICE;
   }
};

@@ -229,7 +225,7 @@ struct VectorSubtraction< Vector, T, true >
      TNL_ASSERT_EQ( v.getSize(), t.getSize(), "The sizes of the vectors must be equal." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] -= t[ i ];
   };
   }

   static void subtraction( Vector& v, const T& t )
   {
@@ -244,8 +240,7 @@ struct VectorSubtraction< Vector, T, true >
         data[ i ] -= t[ i ];
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), subtract );
      TNL_CHECK_CUDA_DEVICE;
   };
   }
};

/**
@@ -262,7 +257,7 @@ struct VectorSubtraction< Vector, T, false >
      TNL_ASSERT_GT( v.getSize(), 0, "Cannot assign value to empty vector." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] -= t;
   };
   }

   static void subtraction( Vector& v, const T& t )
   {
@@ -276,7 +271,6 @@ struct VectorSubtraction< Vector, T, false >
         data[ i ] -= t;
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), subtract );
      TNL_CHECK_CUDA_DEVICE;
   }
};

@@ -293,7 +287,7 @@ struct VectorMultiplication< Vector, T, true >
      TNL_ASSERT_EQ( v.getSize(), t.getSize(), "The sizes of the vectors must be equal." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] *= t[ i ];
   };
   }

   static void multiplication( Vector& v, const T& t )
   {
@@ -308,8 +302,7 @@ struct VectorMultiplication< Vector, T, true >
         data[ i ] *= t[ i ];
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), multiply );
      TNL_CHECK_CUDA_DEVICE;
   };
   }
};

/**
@@ -326,7 +319,7 @@ struct VectorMultiplication< Vector, T, false >
      TNL_ASSERT_GT( v.getSize(), 0, "Cannot assign value to empty vector." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] *= t;
   };
   }

   static void multiplication( Vector& v, const T& t )
   {
@@ -340,7 +333,6 @@ struct VectorMultiplication< Vector, T, false >
         data[ i ] *= t;
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), multiply );
      TNL_CHECK_CUDA_DEVICE;
   }
};

@@ -358,7 +350,7 @@ struct VectorDivision< Vector, T, true >
      TNL_ASSERT_EQ( v.getSize(), t.getSize(), "The sizes of the vectors must be equal." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] /= t[ i ];
   };
   }

   static void division( Vector& v, const T& t )
   {
@@ -373,8 +365,7 @@ struct VectorDivision< Vector, T, true >
         data[ i ] /= t[ i ];
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), divide );
      TNL_CHECK_CUDA_DEVICE;
   };
   }
};

/**
@@ -391,7 +382,7 @@ struct VectorDivision< Vector, T, false >
      TNL_ASSERT_GT( v.getSize(), 0, "Cannot assign value to empty vector." );
      for( decltype( v.getSize() ) i = 0; i < v.getSize(); i ++ )
         v[ i ] /= t;
   };
   }

   static void division( Vector& v, const T& t )
   {
@@ -405,7 +396,6 @@ struct VectorDivision< Vector, T, false >
         data[ i ] /= t;
      };
      ParallelFor< DeviceType >::exec( ( IndexType ) 0, v.getSize(), divide );
      TNL_CHECK_CUDA_DEVICE;
   }
};

+9 −12
Original line number Diff line number Diff line
@@ -11,14 +11,11 @@
#pragma once

#include <TNL/Containers/Array.h>
#include <TNL/Containers/Algorithms/PrefixSumType.h>
#include <TNL/Containers/VectorView.h>

namespace TNL {
namespace Containers {

template< typename Real, typename Device, typename Index >
class VectorView;

/**
 * \brief This class extends TNL::Array with algebraic operations.
 *
+8 −10
Original line number Diff line number Diff line
@@ -11,8 +11,6 @@
#pragma once

#include <TNL/Containers/Vector.h>
#include <TNL/Containers/Algorithms/VectorOperations.h>
#include <TNL/Containers/VectorView.h>

namespace TNL {
namespace Containers {
@@ -113,7 +111,7 @@ template< typename Real,
Vector< Real, Device, Index >&
Vector< Real, Device, Index >::operator=( const VectorExpression& expression )
{
   Algorithms::VectorAssignment< Vector< Real, Device, Index >, VectorExpression >::assign( *this, expression );
   Algorithms::VectorAssignment< Vector, VectorExpression >::assign( *this, expression );
   return *this;
}

@@ -148,7 +146,7 @@ Vector< Real, Device, Index >::
operator-=( const VectorExpression& expression )
{
   //addVector( vector, -1.0 );
   Algorithms::VectorSubtraction< Vector< Real, Device, Index >, VectorExpression >::subtraction( *this, expression );
   Algorithms::VectorSubtraction< Vector, VectorExpression >::subtraction( *this, expression );
   return *this;
}

@@ -161,7 +159,7 @@ Vector< Real, Device, Index >::
operator+=( const VectorExpression& expression )
{
   //addVector( vector );
   Algorithms::VectorAddition< Vector< Real, Device, Index >, VectorExpression >::addition( *this, expression );
   Algorithms::VectorAddition< Vector, VectorExpression >::addition( *this, expression );
   return *this;
}

@@ -174,7 +172,7 @@ Vector< Real, Device, Index >::
operator*=( const VectorExpression& expression )
{
   //Algorithms::VectorOperations< Device >::vectorScalarMultiplication( *this, c );
   Algorithms::VectorMultiplication< Vector< Real, Device, Index >, VectorExpression >::multiplication( *this, expression );
   Algorithms::VectorMultiplication< Vector, VectorExpression >::multiplication( *this, expression );
   return *this;
}

@@ -187,7 +185,7 @@ Vector< Real, Device, Index >::
operator/=( const VectorExpression& expression )
{
   //Algorithms::VectorOperations< Device >::vectorScalarMultiplication( *this, 1.0 / c );
   Algorithms::VectorDivision< Vector< Real, Device, Index >, VectorExpression >::division( *this, expression );
   Algorithms::VectorDivision< Vector, VectorExpression >::division( *this, expression );
   return *this;
}

+5 −27
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ template< typename Real,
VectorView< Real, Device, Index >&
VectorView< Real, Device, Index >::operator=( const VectorExpression& expression )
{
   Algorithms::VectorAssignment< VectorView< Real, Device, Index >, VectorExpression >::assign( *this, expression );
   Algorithms::VectorAssignment< VectorView, VectorExpression >::assign( *this, expression );
   return *this;
}

@@ -141,7 +141,7 @@ VectorView< Real, Device, Index >&
VectorView< Real, Device, Index >::
operator-=( const VectorExpression& expression )
{
   Algorithms::VectorSubtraction< VectorView< Real, Device, Index >, VectorExpression >::subtraction( *this, expression );
   Algorithms::VectorSubtraction< VectorView, VectorExpression >::subtraction( *this, expression );
   return *this;
}

@@ -153,7 +153,7 @@ VectorView< Real, Device, Index >&
VectorView< Real, Device, Index >::
operator+=( const VectorExpression& expression )
{
   Algorithms::VectorAddition< VectorView< Real, Device, Index >, VectorExpression >::addition( *this, expression );
   Algorithms::VectorAddition< VectorView, VectorExpression >::addition( *this, expression );
   return *this;
}

@@ -165,7 +165,7 @@ VectorView< Real, Device, Index >&
VectorView< Real, Device, Index >::
operator*=( const VectorExpression& expression )
{
   Algorithms::VectorMultiplication< VectorView< Real, Device, Index >, VectorExpression >::multiplication( *this, expression );
   Algorithms::VectorMultiplication< VectorView, VectorExpression >::multiplication( *this, expression );
   return *this;
}

@@ -177,32 +177,10 @@ VectorView< Real, Device, Index >&
VectorView< Real, Device, Index >::
operator/=( const VectorExpression& expression )
{
   Algorithms::VectorDivision< VectorView< Real, Device, Index >, VectorExpression >::division( *this, expression );
   Algorithms::VectorDivision< VectorView, VectorExpression >::division( *this, expression );
   return *this;
}

/*template< typename Real,
          typename Device,
          typename Index >
   template< typename Real_, typename Device_, typename Index_ >
bool
VectorView< Real, Device, Index >::
operator==( const VectorView< Real_, Device_, Index_ >& v ) const
{
   return ArrayView< Real, Device, Index >::operator ==( v );
}

template< typename Real,
          typename Device,
          typename Index >
   template< typename Real_, typename Device_, typename Index_ >
bool
VectorView< Real, Device, Index >::
operator!=( const VectorView< Real_, Device_, Index_ >& v ) const
{
   return !ArrayView< Real, Device, Index >::operator ==( v );
}*/

template< typename Real,
          typename Device,
          typename Index >
Loading