...
 
Commits (56)
......@@ -78,6 +78,8 @@ class Array : public Object
Array( Array& array,
const IndexType& begin = 0,
const IndexType& size = 0 );
inline Array( const std::initializer_list< Value > &elems );
/** \brief Returns type of array Value, Device type and the type of Index. */
static String getType();
......@@ -192,7 +194,7 @@ class Array : public Object
* \param i Index position of an element.
*/
Value getElement( const Index& i ) const;
/**
* \brief Accesses specified element at the position \e i and returns a reference to its value.
*
......
......@@ -77,7 +77,9 @@ public:
template< int Size, typename Value_ > // template catches both const and non-const qualified Value
__cuda_callable__
ArrayView( const StaticArray< Size, Value_ >& array );
__cuda_callable__
inline ArrayView( const std::initializer_list< Value > &elems );
// methods for rebinding (reinitialization)
__cuda_callable__
......
......@@ -78,6 +78,21 @@ ArrayView( const StaticArray< Size, Value_ >& array )
this->bind( array.getData(), Size );
}
template< typename Value,
typename Device,
typename Index >
__cuda_callable__
ArrayView< Value, Device, Index >::
ArrayView( const std::initializer_list< Value >& elems )
{
//this->getSize( elems.size() );
int i = 0;
for (const auto &elem : elems)
{
this->data[ i++ ] = elem;
}
}
// methods for rebinding (reinitialization)
template< typename Value,
typename Device,
......
......@@ -92,6 +92,24 @@ Array( Array< Value, Device, Index >& array,
}
}
template< typename Value,
typename Device,
typename Index >
Array< Value, Device, Index >::
Array( const std::initializer_list< Value > &elems )
: size( 0 ),
data( 0 ),
allocationPointer( 0 ),
referenceCounter( 0 )
{
this->setSize( elems.size() );
int i = 0;
for (const auto &elem : elems)
{
this->data[ i++ ] = elem;
}
}
template< typename Value,
typename Device,
typename Index >
......
......@@ -89,6 +89,8 @@ class StaticArray
*/
__cuda_callable__
inline const Value* getData() const;
inline StaticArray( const std::initializer_list< Value > &elems );
/**
* \brief Accesses specified element at the position \e i and returns a reference to its value.
......@@ -221,6 +223,8 @@ class StaticArray< 1, Value >
/** \brief See StaticArray::getData() const.*/
__cuda_callable__
inline const Value* getData() const;
inline StaticArray( const std::initializer_list< Value > &elems );
/** \brief See StaticArray::operator[]( int i ) const.*/
__cuda_callable__
......@@ -335,6 +339,8 @@ class StaticArray< 2, Value >
/** \brief See StaticArray::getData() const.*/
__cuda_callable__
inline const Value* getData() const;
inline StaticArray( const std::initializer_list< Value > &elems );
/** \brief See StaticArray::operator[]( int i ) const.*/
__cuda_callable__
......@@ -457,6 +463,8 @@ class StaticArray< 3, Value >
/** \brief See StaticArray::getData() const.*/
__cuda_callable__
inline const Value* getData() const;
inline StaticArray( const std::initializer_list< Value > &elems );
/** \brief See StaticArray::operator[]( int i ) const.*/
__cuda_callable__
......
......@@ -75,6 +75,16 @@ inline const Value* StaticArray< 1, Value >::getData() const
return data;
}
template< typename Value >
StaticArray< 1, Value >::StaticArray( const std::initializer_list< Value > &elems)
{
int i = 0;
for (const Value &elem : elems)
{
data[i++] = elem;
}
}
template< typename Value >
__cuda_callable__
inline const Value& StaticArray< 1, Value >::operator[]( int i ) const
......
......@@ -87,6 +87,16 @@ inline const Value* StaticArray< 2, Value >::getData() const
return data;
}
template< typename Value >
StaticArray< 2, Value >::StaticArray( const std::initializer_list< Value > &elems)
{
int i = 0;
for (const auto &elem : elems)
{
data[i++] = elem;
}
}
template< typename Value >
__cuda_callable__
inline const Value& StaticArray< 2, Value >::operator[]( int i ) const
......
......@@ -91,6 +91,16 @@ inline const Value* StaticArray< 3, Value >::getData() const
return data;
}
template< typename Value >
StaticArray< 3, Value >::StaticArray( const std::initializer_list< Value > &elems)
{
int i = 0;
for (const auto &elem : elems)
{
data[i++] = elem;
}
}
template< typename Value >
__cuda_callable__
inline const Value& StaticArray< 3, Value >::operator[]( int i ) const
......
......@@ -79,6 +79,16 @@ inline const Value* StaticArray< Size, Value >::getData() const
return data;
}
template< int Size, typename Value >
StaticArray< Size, Value >::StaticArray( const std::initializer_list< Value > &elems)
{
int i = 0;
for (const auto &elem : elems)
{
data[ i++ ] = elem;
}
}
template< int Size, typename Value >
__cuda_callable__
inline const Value& StaticArray< Size, Value >::operator[]( int i ) const
......
......@@ -29,6 +29,7 @@ class StaticVector : public StaticArray< Size, Real >
typedef Real RealType;
typedef StaticVector< Size, Real > ThisType;
enum { size = Size };
constexpr int getSize() { return size; };
/**
* \brief Basic constructor.
......@@ -81,6 +82,15 @@ class StaticVector : public StaticArray< Size, Real >
*/
static String getType();
StaticVector( const std::initializer_list< Real > &elems );
template< typename StaticVectorOperationType >
StaticVector( const StaticVectorOperationType &vo );
Real& operator[] ( const int i );
const Real& operator[] ( const int i ) const;
/**
* \brief Adding operator.
*
......@@ -120,7 +130,8 @@ class StaticVector : public StaticArray< Size, Real >
*/
__cuda_callable__
StaticVector& operator /= ( const Real& c );
#ifdef UNDEF // this gonna be replaced by expression templates
/**
* \brief Addition operator.
*
......@@ -160,7 +171,8 @@ class StaticVector : public StaticArray< Size, Real >
*/
__cuda_callable__
Real operator * ( const StaticVector& u ) const;
#endif
/**
* \brief Compares this static vector with static vector \e v.
*
......@@ -248,6 +260,7 @@ class StaticVector< 1, Real > : public StaticArray< 1, Real >
typedef Real RealType;
typedef StaticVector< 1, Real > ThisType;
enum { size = 1 };
constexpr int getSize() { return size; };
/** \brief See StaticVector::StaticVector().*/
__cuda_callable__
......@@ -269,10 +282,21 @@ class StaticVector< 1, Real > : public StaticArray< 1, Real >
StaticVector( const StaticVector< 1, Real >& v );
bool setup( const Config::ParameterContainer& parameters,
const String& prefix = "" );
const String& prefix = "" );
/** \brief See StaticVector::getType().*/
static String getType();
StaticVector( const std::initializer_list< Real > &elems );
template< typename StaticVectorOperationType >
StaticVector( const StaticVectorOperationType &vo );
Real& operator[] ( const int i );
const Real& operator[] ( const int i ) const;
/** \brief See StaticVector::operator += ( const StaticVector& v ).*/
/** \brief See StaticVector::operator += ( const StaticVector& v ).*/
__cuda_callable__
......@@ -285,11 +309,13 @@ class StaticVector< 1, Real > : public StaticArray< 1, Real >
/** \brief See StaticVector::operator *= ( const Real& c ).*/
__cuda_callable__
StaticVector& operator *= ( const Real& c );
/** \brief See StaticVector::operator /= ( const Real& c ).*/
__cuda_callable__
StaticVector& operator /= ( const Real& c );
StaticVector& operator /= ( const Real& c );
#ifdef UNDEF // this gonna be replaced with expression templates
/** \brief See StaticVector::operator + ( const StaticVector& u ) const.*/
__cuda_callable__
StaticVector operator + ( const StaticVector& u ) const;
......@@ -306,6 +332,8 @@ class StaticVector< 1, Real > : public StaticArray< 1, Real >
__cuda_callable__
Real operator * ( const StaticVector& u ) const;
#endif
/** \brief See StaticVector::operator <.*/
__cuda_callable__
bool operator < ( const StaticVector& v ) const;
......@@ -332,7 +360,7 @@ class StaticVector< 1, Real > : public StaticArray< 1, Real >
/** \brief See StaticVector::lpNorm( const Real& p ) const.*/
__cuda_callable__
Real lpNorm( const Real& p ) const;
Real lpNorm( const Real& p ) const;
#ifdef HAVE_MIC
__cuda_callable__
......@@ -362,6 +390,7 @@ class StaticVector< 2, Real > : public StaticArray< 2, Real >
typedef Real RealType;
typedef StaticVector< 2, Real > ThisType;
enum { size = 2 };
constexpr int getSize() { return size; };
/** \brief See StaticVector::StaticVector().*/
__cuda_callable__
......@@ -390,14 +419,24 @@ class StaticVector< 2, Real > : public StaticArray< 2, Real >
/** \brief See StaticVector::StaticVector( const StaticVector< Size, Real >& v ).*/
__cuda_callable__
StaticVector( const StaticVector< 2, Real >& v );
bool setup( const Config::ParameterContainer& parameters,
const String& prefix = "" );
const String& prefix = "" );
/** \brief See StaticVector::getType().*/
static String getType();
StaticVector( const std::initializer_list< Real > &elems );
template< typename StaticVectorOperationType >
StaticVector( const StaticVectorOperationType &vo );
Real& operator[] ( const int i );
const Real& operator[] ( const int i ) const;
/** \brief See StaticVector::operator += ( const StaticVector& v ).*/
__cuda_callable__
StaticVector& operator += ( const StaticVector& v );
......@@ -411,8 +450,9 @@ class StaticVector< 2, Real > : public StaticArray< 2, Real >
/** \brief See StaticVector::operator /= ( const Real& c ).*/
__cuda_callable__
StaticVector& operator /= ( const Real& c );
StaticVector& operator /= ( const Real& c );
#ifdef UNDEF // this gonna be replaced by expression templates
/** \brief See StaticVector::operator + ( const StaticVector& u ) const.*/
__cuda_callable__
StaticVector operator + ( const StaticVector& u ) const;
......@@ -428,6 +468,7 @@ class StaticVector< 2, Real > : public StaticArray< 2, Real >
/** \brief See StaticVector::operator * ( const StaticVector& u ) const.*/
__cuda_callable__
Real operator * ( const StaticVector& u ) const;
#endif
/** \brief See StaticVector::operator <.*/
__cuda_callable__
......@@ -444,7 +485,7 @@ class StaticVector< 2, Real > : public StaticArray< 2, Real >
/** \brief See StaticVector::operator >=.*/
__cuda_callable__
bool operator >= ( const StaticVector& v ) const;
template< typename OtherReal >
__cuda_callable__
operator StaticVector< 2, OtherReal >() const;
......@@ -455,7 +496,7 @@ class StaticVector< 2, Real > : public StaticArray< 2, Real >
/** \brief See StaticVector::lpNorm( const Real& p ) const.*/
__cuda_callable__
Real lpNorm( const Real& p ) const;
Real lpNorm( const Real& p ) const;
#ifdef HAVE_MIC
__cuda_callable__
......@@ -485,6 +526,7 @@ class StaticVector< 3, Real > : public StaticArray< 3, Real >
typedef Real RealType;
typedef StaticVector< 3, Real > ThisType;
enum { size = 3 };
constexpr int getSize() { return size; };
/** \brief See StaticVector::StaticVector().*/
__cuda_callable__
......@@ -516,11 +558,20 @@ class StaticVector< 3, Real > : public StaticArray< 3, Real >
StaticVector( const StaticVector< 3, Real >& v );
bool setup( const Config::ParameterContainer& parameters,
const String& prefix = "" );
const String& prefix = "" );
/** \brief See StaticVector::getType().*/
static String getType();
StaticVector( const std::initializer_list< Real > &elems );
template< typename StaticVectorOperationType >
StaticVector( const StaticVectorOperationType &vo );
Real& operator[] ( const int i );
const Real& operator[] ( const int i ) const;
/** \brief See StaticVector::operator += ( const StaticVector& v ).*/
__cuda_callable__
StaticVector& operator += ( const StaticVector& v );
......@@ -536,7 +587,8 @@ class StaticVector< 3, Real > : public StaticArray< 3, Real >
/** \brief See StaticVector::operator /= ( const Real& c ).*/
__cuda_callable__
StaticVector& operator /= ( const Real& c );
#ifdef UNDEF // this gonna be replaced by expression templates
/** \brief See StaticVector::operator + ( const StaticVector& u ) const.*/
__cuda_callable__
StaticVector operator + ( const StaticVector& u ) const;
......@@ -552,7 +604,7 @@ class StaticVector< 3, Real > : public StaticArray< 3, Real >
/** \brief See StaticVector::operator * ( const StaticVector& u ) const.*/
__cuda_callable__
Real operator * ( const StaticVector& u ) const;
#endif
/** \brief See StaticVector::operator <.*/
__cuda_callable__
bool operator < ( const StaticVector& v ) const;
......@@ -598,11 +650,11 @@ class StaticVector< 3, Real > : public StaticArray< 3, Real >
}
#endif
};
/*
template< int Size, typename Real, typename Scalar >
__cuda_callable__
StaticVector< Size, Real > operator * ( const Scalar& c, const StaticVector< Size, Real >& u );
*/
template< int Size, typename Real >
__cuda_callable__
StaticVector< Size, Real > abs( const StaticVector< Size, Real >& u ) { return u.abs(); };
......@@ -614,6 +666,7 @@ StaticVector< Size, Real > abs( const StaticVector< Size, Real >& u ) { return u
#include <TNL/Containers/StaticVector1D_impl.h>
#include <TNL/Containers/StaticVector2D_impl.h>
#include <TNL/Containers/StaticVector3D_impl.h>
#include <TNL/Experimental/ExpressionTemplates/StaticVectorExpressions.h>
namespace TNL {
......@@ -711,6 +764,5 @@ Real TriangleArea( const StaticVector< 3, Real >& a,
const StaticVector< 3, Real > v = VectorProduct( u1, u2 );
return 0.5 * TNL::sqrt( ScalarProduct( v, v ) );
}
} // namespace Containers
} // namespace TNL
......@@ -62,6 +62,44 @@ String StaticVector< 1, Real >::getType()
String( " >" );
}
template< typename Real >
StaticVector< 1, Real >::StaticVector( const std::initializer_list< Real > &elems)
: StaticArray< 1, Real >( elems )
{
}
template< typename Real >
template< typename StaticVectorOperationType >
StaticVector< 1, Real >::StaticVector(const StaticVectorOperationType &vo)
{
//static_assert( StaticVectorOperationType::getSize() == getSize(), "StaticArray/Vector size is not equal to StaticArray/VectorOperationType size." );
for( int i = 0; i < getSize(); i++ )
{
this->data[ i ] = vo[ i ];
}
}
/*
template< typename Real >
template< typename StaticVectorOperationType >
StaticVector< 1, Real >::StaticVector(const StaticVectorOperationType &vo)
: StaticArray< 1, Real >( vo )
{
}
*/
template< typename Real >
const Real& StaticVector< 1, Real >::operator[]( const int i ) const
{
return this->data[ i ];
}
template< typename Real >
Real& StaticVector< 1, Real >::operator[]( const int i )
{
return this->data[ i ];
}
template< typename Real >
__cuda_callable__
StaticVector< 1, Real >& StaticVector< 1, Real >::operator += ( const StaticVector& v )
......@@ -93,7 +131,7 @@ StaticVector< 1, Real >& StaticVector< 1, Real >::operator /= ( const Real& c )
this->data[ 0 ] /= c;
return *this;
}
/*
template< typename Real >
__cuda_callable__
StaticVector< 1, Real > StaticVector< 1, Real >::operator + ( const StaticVector& u ) const
......@@ -127,7 +165,7 @@ Real StaticVector< 1, Real >::operator * ( const StaticVector& u ) const
{
return this->data[ 0 ] * u[ 0 ];
}
*/
template< typename Real >
__cuda_callable__
bool StaticVector< 1, Real >::operator < ( const StaticVector& v ) const
......
......@@ -70,6 +70,35 @@ String StaticVector< 2, Real >::getType()
String( " >" );
}
template< typename Real >
StaticVector< 2, Real >::StaticVector( const std::initializer_list< Real > &elems)
: StaticArray< 2, Real >( elems )
{
}
template< typename Real >
template< typename StaticVectorOperationType >
StaticVector< 2, Real >::StaticVector(const StaticVectorOperationType &vo)
{
//static_assert( StaticVectorOperationType::getSize() == getSize(), "StaticArray/Vector size is not equal to StaticArray/VectorOperationType size." );
for( int i = 0; i < getSize(); i++ )
{
this->data[ i ] = vo[ i ];
}
}
template< typename Real >
Real& StaticVector< 2, Real >::operator[]( const int i )
{
return this->data[ i ];
}
template< typename Real >
const Real& StaticVector< 2, Real >::operator[]( const int i ) const
{
return this->data[ i ];
}
template< typename Real >
__cuda_callable__
StaticVector< 2, Real >& StaticVector< 2, Real >::operator += ( const StaticVector& v )
......@@ -106,7 +135,7 @@ StaticVector< 2, Real >& StaticVector< 2, Real >::operator /= ( const Real& c )
this->data[ 1 ] *= d;
return *this;
}
/*
template< typename Real >
__cuda_callable__
StaticVector< 2, Real > StaticVector< 2, Real >::operator + ( const StaticVector& u ) const
......@@ -144,7 +173,7 @@ Real StaticVector< 2, Real >::operator * ( const StaticVector& u ) const
return this->data[ 0 ] * u[ 0 ] +
this->data[ 1 ] * u[ 1 ];
}
*/
template< typename Real >
__cuda_callable__
bool StaticVector< 2, Real >::operator < ( const StaticVector& v ) const
......
......@@ -71,6 +71,35 @@ String StaticVector< 3, Real >::getType()
String( " >" );
}
template< typename Real >
StaticVector< 3, Real >::StaticVector( const std::initializer_list< Real > &elems)
: StaticArray< 3, Real >( elems )
{
}
template< typename Real >
template< typename StaticVectorOperationType >
StaticVector< 3, Real >::StaticVector(const StaticVectorOperationType &vo)
{
//static_assert( StaticVectorOperationType::getSize() == getSize(), "StaticArray/Vector size is not equal to StaticArray/VectorOperationType size." );
for( int i = 0; i < getSize(); i++ )
{
this->data[ i ] = vo[ i ];
}
}
template< typename Real >
Real& StaticVector< 3, Real >::operator[]( const int i )
{
return this->data[ i ];
}
template< typename Real >
const Real& StaticVector< 3, Real >::operator[]( const int i ) const
{
return this->data[ i ];
}
template< typename Real >
__cuda_callable__
StaticVector< 3, Real >& StaticVector< 3, Real >::operator += ( const StaticVector& v )
......@@ -111,7 +140,7 @@ StaticVector< 3, Real >& StaticVector< 3, Real >::operator /= ( const Real& c )
this->data[ 2 ] *= d;
return *this;
}
/*
template< typename Real >
__cuda_callable__
StaticVector< 3, Real > StaticVector< 3, Real >::operator + ( const StaticVector& u ) const
......@@ -153,7 +182,7 @@ Real StaticVector< 3, Real >::operator * ( const StaticVector& u ) const
this->data[ 1 ] * u[ 1 ] +
this->data[ 2 ] * u[ 2 ];
}
*/
template< typename Real >
__cuda_callable__
bool StaticVector< 3, Real >::operator < ( const StaticVector& v ) const
......
......@@ -64,6 +64,35 @@ String StaticVector< Size, Real >::getType()
String( " >" );
}
template< int Size, typename Real >
StaticVector< Size, Real >::StaticVector( const std::initializer_list< Real > &elems)
: StaticArray< Size, Real >( elems )
{
}
template< int Size, typename Real >
template< typename StaticVectorOperationType >
StaticVector< Size, Real >::StaticVector( const StaticVectorOperationType &vo ) //: data(vo.getSize())
{
//static_assert( StaticVectorOperationType::getSize() == getSize(), "StaticArray/Vector size is not equal to StaticArray/VectorOperationType size." );
for( int i = 0; i < getSize(); i++ )
{
this->data[ i ] = vo[ i ];
}
}
template< int Size, typename Real >
const Real& StaticVector< Size, Real >::operator[]( const int i ) const
{
return this->data[ i ];
}
template< int Size, typename Real >
Real& StaticVector< Size, Real >::operator[]( int i )
{
return this->data[ i ];
}
template< int Size, typename Real >
__cuda_callable__
StaticVector< Size, Real >& StaticVector< Size, Real >::operator += ( const StaticVector& v )
......@@ -100,7 +129,7 @@ StaticVector< Size, Real >& StaticVector< Size, Real >::operator /= ( const Real
this->data[ i ] *= d;
return *this;
}
/*
template< int Size, typename Real >
__cuda_callable__
StaticVector< Size, Real > StaticVector< Size, Real >::operator + ( const StaticVector& u ) const
......@@ -140,7 +169,7 @@ Real StaticVector< Size, Real >::operator * ( const StaticVector& u ) const
res += this->data[ i ] * u[ i ];
return res;
}
*/
template< int Size, typename Real >
__cuda_callable__
bool StaticVector< Size, Real >::operator < ( const StaticVector& v ) const
......@@ -228,13 +257,13 @@ StaticVector< Size, Real >::lpNorm( const Real& p ) const
aux += TNL::pow( TNL::abs( this->data[ i ] ), p );
return TNL::pow( aux, 1.0 / p );
}
/*
template< int Size, typename Real, typename Scalar >
__cuda_callable__
StaticVector< Size, Real > operator * ( const Scalar& c, const StaticVector< Size, Real >& u )
{
return u * c;
}
*/
} // namespace Containers
} // namespace TNL
......@@ -39,6 +39,8 @@ class Vector
using Array< Real, Device, Index >::Array;
using Array< Real, Device, Index >::operator=;
Vector( const std::initializer_list< Real > &elems );
/** \brief Returns type of vector Real value, Device type and the type of Index. */
static String getType();
......@@ -69,6 +71,15 @@ class Vector
void addElement( const IndexType i,
const RealType& value,
const Scalar thisElementMultiplicator );
/*
template< typename Expression >
__global__ void expressionTemplatesKernel( Real* data, Index size, Expression expression );
*/
template< typename VectorOperationType >
void evaluate( const VectorOperationType& vo );
template< typename VectorOperationType >
void evaluateFor( const VectorOperationType& vo );
/**
* \brief This function subtracts \e vector from this vector and returns the resulting vector.
......
......@@ -62,6 +62,8 @@ public:
VectorView( const ArrayView< Real_, Device, Index >& view )
: BaseType::ArrayView( view ) {}
__cuda_callable__
VectorView( const std::initializer_list< Real > &elems );
static String getType();
......@@ -73,6 +75,12 @@ public:
void addElement( IndexType i,
RealType value,
Scalar thisElementMultiplicator );
template< typename VectorOperationType >
void evaluate( const VectorOperationType& vo );
template< typename VectorOperationType >
void evaluateFor( const VectorOperationType& vo );
template< typename Vector >
VectorView& operator-=( const Vector& vector );
......
......@@ -12,10 +12,21 @@
#include <TNL/Containers/VectorView.h>
#include <TNL/Containers/Algorithms/VectorOperations.h>
#include <TNL/ParallelFor.h>
namespace TNL {
namespace Containers {
template< typename Real,
typename Device,
typename Index >
__cuda_callable__
VectorView< Real, Device, Index >::
VectorView( const std::initializer_list< Real >& elems )
: ArrayView< Real, Device, Index >( elems )
{
}
template< typename Real,
typename Device,
typename Index >
......@@ -51,6 +62,61 @@ addElement( IndexType i, RealType value, Scalar thisElementMultiplicator )
Algorithms::VectorOperations< Device >::addElement( *this, i, value, thisElementMultiplicator );
}
#ifdef HAVE_CUDA
template< typename Real,
typename Index,
typename Expression >
__global__ void expressionTemplatesKernel( Real* dt, Expression expression, Index size )
{
const int idx = blockIdx.x * blockDim.x + threadIdx.x;
int stride = blockDim.x * gridDim.x;
for (int i = idx; i < size; i += stride)
dt[ i ] = expression[ i ];
}
#endif
template< typename Real,
typename Device,
typename Index >
template< typename VectorOperationType >
void
VectorView< Real, Device, Index >::
evaluate( const VectorOperationType& vo )
{
Real* dt = this->data;
auto assign = [=] __cuda_callable__ ( Index i )
{
dt[ i ] = vo[ i ];
};
ParallelFor< DeviceType >::exec( 0, this->getSize(), assign );
}
template< typename Real,
typename Device,
typename Index >
template< typename VectorOperationType >
void
VectorView< Real, Device, Index >::
evaluateFor( const VectorOperationType& vo )
{
if( std::is_same< DeviceType, Devices::Host >::value )
{
for( int i = 0; i < this->getSize(); i++ )
{
this->data[ i ] = vo[ i ];
}
}
#ifdef HAVE_CUDA
else if( std::is_same< DeviceType, Devices::Cuda >::value )
{
dim3 cudaBlockSize( 256 ), cudaGridSize( Devices::Cuda::getMaxGridSize() );
expressionTemplatesKernel<<< cudaGridSize, cudaBlockSize >>>( this->data, vo, this->getSize() );
TNL_CHECK_CUDA_DEVICE;
}
#endif
}
template< typename Real,
typename Device,
typename Index >
......
......@@ -12,10 +12,20 @@
#include <TNL/Containers/Vector.h>
#include <TNL/Containers/Algorithms/VectorOperations.h>
#include <TNL/ParallelFor.h>
namespace TNL {
namespace Containers {
template< typename Real,
typename Device,
typename Index >
Vector< Real, Device, Index >::
Vector( const std::initializer_list< Real > &elems )
: Array< Real, Device, Index >( elems )
{
}
template< typename Real,
typename Device,
typename Index >
......@@ -82,6 +92,59 @@ addElement( const IndexType i,
{
Algorithms::VectorOperations< Device >::addElement( *this, i, value, thisElementMultiplicator );
}
/*
template< typename Real,
typename Device,
typename Index >
template< typename Expression >
__global__ void expressionTemplatesKernel( Real* data, Index size, Expression expression )
{
Index idx = index vlakna;
if( idx < size )
data[ idx ] = expression[ idx ];
}
*/
template< typename Real,
typename Device,
typename Index >
template< typename VectorOperationType >
void
Vector< Real, Device, Index >::
evaluate( const VectorOperationType& vo )
{
Real* dt = this->data;
auto assign = [=] __cuda_callable__ ( Index i )
{
dt[ i ] = vo[ i ];
};
ParallelFor< DeviceType >::exec( 0, this->getSize(), assign );
}
template< typename Real,
typename Device,
typename Index >
template< typename VectorOperationType >
void
Vector< Real, Device, Index >::
evaluateFor( const VectorOperationType& vo )
{
//if( std::is_same< DeviceType, Devices::Host >::value )
//{
for( int i = 0; i < this->getSize(); i++ )
{
this->data[ i ] = vo[ i ];
}
/*
}
if( std::is_same< DeviceType, Devices::Cuda >::value )
{
expressionTemplatesKernel<<< gridSize, blockSize >>( this->data, this->getSize(), vo );
TNL_CHECK_CUDA_DEVICE;
}
*/
}
template< typename Real,
typename Device,
......
add_subdirectory( Arithmetics )
add_subdirectory( ExpressionTemplates )
if( ${WITH_EXAMPLES} )
add_subdirectory( Hamilton-Jacobi )
endif()
set( headers StaticVectorExpressions.h
VectorExpressions.h
)
IF( BUILD_CUDA )
CUDA_ADD_EXECUTABLE( tnl-expression-templates-cuda expression-templates.cu )
ELSE( BUILD_CUDA )
ADD_EXECUTABLE( tnl-expression-templates expression-templates.cpp )
ADD_EXECUTABLE( tnl-expression-templates-static expression-templates-static.cpp )
ADD_EXECUTABLE( tnl-expression-templates-temp expression-templates-temp.cpp )
ADD_EXECUTABLE( tnl-expression-templates-static-temp expression-templates-static-temp.cpp )
ENDIF( BUILD_CUDA )
IF( BUILD_CUDA )
INSTALL( TARGETS tnl-expression-templates-cuda
RUNTIME DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE )
ELSE( BUILD_CUDA )
INSTALL( TARGETS tnl-expression-templates
RUNTIME DESTINATION bin
PERMISSIONS OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE )
ENDIF( BUILD_CUDA )
INSTALL( FILES ${headers} DESTINATION ${TNL_TARGET_INCLUDE_DIRECTORY}/Experimental/ExpressionTemplates )
TNL_INCLUDE_DIR=${HOME}/tnl-dev/src
CXX = g++
CUDA_CXX = nvcc
CXX_FLAGS = -I$(TNL_INCLUDE_DIR) -std=c++11 -pthread -Wall -Wno-unused-local-typedefs -Wno-unused-variable -O3 -march=native -mtune=native -DNDEBUG
OUTDIR = release_gcc
all: $(OUTDIR)/tnl-expression-templates $(OUTDIR)/tnl-expression-templates-static $(OUTDIR)/tnl-expression-templates-temp $(OUTDIR)/tnl-expression-templates-static-temp
$(OUTDIR)/tnl-expression-templates: expression-templates.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates expression-templates.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-static: expression-templates-static.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-static expression-templates-static.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-temp: expression-templates-temp.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-temp expression-templates-temp.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-static-temp: expression-templates-static-temp.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-static-temp expression-templates-static-temp.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-cuda: expression-templates.cu
$(CUDA_CXX) -o $(OUTDIR)/tnl-expression-templates-cuda expression-templates.cu $(CXX_FLAGS)
TNL_INCLUDE_DIR=${HOME}/tnl-dev/src
CXX = clang++
CUDA_CXX = nvcc
CXX_FLAGS = -I$(TNL_INCLUDE_DIR) -std=c++11 -pthread -Wall -Wno-unused-local-typedefs -Wno-unused-variable -O3 -march=native -mtune=native -DNDEBUG
OUTDIR = release_clang
all: $(OUTDIR)/tnl-expression-templates $(OUTDIR)/tnl-expression-templates-static $(OUTDIR)/tnl-expression-templates-temp $(OUTDIR)/tnl-expression-templates-static-temp
$(OUTDIR)/tnl-expression-templates: expression-templates.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates expression-templates.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-static: expression-templates-static.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-static expression-templates-static.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-temp: expression-templates-temp.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-temp expression-templates-temp.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-static-temp: expression-templates-static-temp.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-static-temp expression-templates-static-temp.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-cuda: expression-templates.cu
$(CUDA_CXX) -o $(OUTDIR)/tnl-expression-templates-cuda expression-templates.cu $(CXX_FLAGS)
TNL_INCLUDE_DIR=${HOME}/tnl-dev/src
CXX = g++
CUDA_CXX = nvcc
CXX_FLAGS = -I$(TNL_INCLUDE_DIR) -std=c++11 -pthread -Wall -Wno-unused-local-typedefs -Wno-unused-variable -g
OUTDIR = debug
all: $(OUTDIR)/tnl-expression-templates $(OUTDIR)/tnl-expression-templates-static $(OUTDIR)/tnl-expression-templates-temp $(OUTDIR)/tnl-expression-templates-static-temp
$(OUTDIR)/tnl-expression-templates: expression-templates.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates expression-templates.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-static: expression-templates-static.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-static expression-templates-static.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-temp: expression-templates-temp.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-temp expression-templates-temp.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-static-temp: expression-templates-static-temp.cpp
$(CXX) -o $(OUTDIR)/tnl-expression-templates-static-temp expression-templates-static-temp.cpp $(CXX_FLAGS)
$(OUTDIR)/tnl-expression-templates-cuda: expression-templates.cu
$(CUDA_CXX) -o $(OUTDIR)/tnl-expression-templates-cuda expression-templates.cu $(CXX_FLAGS)
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: OverloadedOperators.h
* Author: legler
*
* Created on December 4, 2018, 7:55 PM
*/
#ifndef OVERLOADEDOPERATORS_H
#define OVERLOADEDOPERATORS_H
#include <vector>
//using std::vector;
template< class T >
std::vector<T> operator+(const std::vector<T> &a, const std::vector<T> &b)
{
std::vector<T> res(a.size());
for (std::size_t i = 0; i<a.size(); i++)
res[i] = a[i] + b[i];
return res;
}
template< class T >
std::vector<T> operator-(const std::vector<T> &a, const std::vector<T> &b)
{
std::vector<T> res(a.size());
for (std::size_t i = 0; i<a.size(); i++)
res[i] = a[i] + b[i];
return res;
}
/*
template< class T>
std::vector<T> operator*(const T &a, const std::vector<T> &b)
{
std::vector<T> res(a.size());
for (size_t i = 0; i<a.size(); i++)
res[i] = a + b[i];
return res;
}
template< class T>
std::vector<T> operator*(const std::vector<T> &a, const T &b)
{
std::vector<T> res(a.size());
for (size_t i = 0; i<a.size(); i++)
res[i] = a[i] + b;
return res;
}
*/
/*
template<typename T>
class Vec
{
public:
std::vector<T> data;
Vec(const std::size_t size) : data(size)
{}
Vec(const std::size_t size, const double init) : data(size, init)
{}
Vec operator+ ( const Vec& a, const Vec& b )
{
Vec<T> res(a.data.size());
for (std::size_t i = 0; i<a.data.size(); i++)
res[i] = a[i] + b[i];
return res;
}
Vec operator- ( const Vec& a, const Vec& b )
{
Vec<T> res(a.data.size());
for (std::size_t i = 0; i<a.data.size(); i++)
res[i] = a[i] - b[i];
return res;
}
Vec operator* ( const T& a, const Vec& b )
{
Vec<T> res(b.data.size());
for (std::size_t i = 0; i<b.data.size(); i++)
res[i] = a * b[i];
return res;
}
};
*/
#endif /* OVERLOADEDOPERATORS_H */
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: StaticVectorExpressions.h
* Author: oberhuber
*
* Created on November 29, 2018, 2:53 PM
*/
#pragma once
#include <TNL/Containers/StaticVector.h>
template< typename T1 , typename T2 >
class StaticVectorAddition
{
const T1 &op1;
const T2 &op2;
public:
StaticVectorAddition( const T1& a, const T2& b ): op1( a ), op2( b ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return op1[ i ] + op2[ i ];
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1, typename T2 >
class StaticVectorSubtraction
{
const T1 &op1;
const T2 &op2;
public:
StaticVectorSubtraction( const T1& a, const T2& b ): op1( a ), op2( b ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return op1[ i ] - op2[ i ];
}
int getSize() const
{
return op1.getSize();
}
};
template< typename Scalar, typename T2 >
class StaticVectorMultiplicationLeftSide
{
const Scalar &c;
const T2 &op2;
public:
StaticVectorMultiplicationLeftSide( const Scalar& a, const T2& b ): c( a ), op2( b ){}
using RealType = typename T2::RealType;
RealType operator[]( const int i ) const
{
return c * op2[ i ];
}
int getSize() const
{
return op2.getSize();
}
};
template< typename T1 >
class StaticVectorAbsoluteValue
{
const T1 &op1;
public:
StaticVectorAbsoluteValue( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::abs( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1 >
class StaticVectorExponentialFunction
{
const T1 &op1;
public:
StaticVectorExponentialFunction( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::exp( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1 >
class StaticVectorNaturalLogarithm
{
const T1 &op1;
public:
StaticVectorNaturalLogarithm( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::log( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
/*
template< typename T1 >
class StaticVectorSquareRoot
{
const T1 &op1;
public:
StaticVectorSquareRoot( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::sqrt( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};*/
template< typename T1 >
class StaticVectorSine
{
const T1 &op1;
public:
StaticVectorSine( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::sin( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1 >
class StaticVectorCosine
{
const T1 &op1;
public:
StaticVectorCosine( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::cos( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1 >
class StaticVectorTangent
{
const T1 &op1;
public:
StaticVectorTangent( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::tan( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1 >
class StaticVectorArcSine
{
const T1 &op1;
public:
StaticVectorArcSine( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::asin( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1 >
class StaticVectorArcCosine
{
const T1 &op1;
public:
StaticVectorArcCosine( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::acos( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1 >
class StaticVectorArcTangent
{
const T1 &op1;
public:
StaticVectorArcTangent( const T1& a ): op1( a ){}
using RealType = typename T1::RealType;
RealType operator[]( const int i ) const
{
return std::atan( op1[ i ] );
}
int getSize() const
{
return op1.getSize();
}
};
template< typename T1, typename T2 >
StaticVectorAddition< T1, T2 > operator+( const T1 &a, const T2 &b )
{
return StaticVectorAddition< T1, T2 >( a, b );
}
template< typename T1, typename T2 >
StaticVectorSubtraction< T1, T2 > operator-( const T1 &a, const T2 &b )
{
return StaticVectorSubtraction< T1, T2 >( a, b );
}
template< typename Scalar, typename T2 >
StaticVectorMultiplicationLeftSide< Scalar, T2 > operator*( const Scalar &a, const T2 &b )
{
return StaticVectorMultiplicationLeftSide< Scalar, T2 >( a, b );