Skip to content
Snippets Groups Projects
Commit 402faa22 authored by Tomáš Oberhuber's avatar Tomáš Oberhuber
Browse files

[WIP] Implementing new Array constructors.

parent c2f01704
No related branches found
No related tags found
1 merge request!29Revision
...@@ -53,6 +53,11 @@ class ArrayOperations< Devices::Host > ...@@ -53,6 +53,11 @@ class ArrayOperations< Devices::Host >
const SourceElement* source, const SourceElement* source,
const Index size ); const Index size );
template< typename DestinationElement,
typename SourceElement >
static void copySTLList( DestinationElement* destination,
const std::list< SourceElement >& source );
template< typename Element1, template< typename Element1,
typename Element2, typename Element2,
typename Index > typename Index >
...@@ -260,6 +265,6 @@ class ArrayOperations< Devices::Host, Devices::MIC > ...@@ -260,6 +265,6 @@ class ArrayOperations< Devices::Host, Devices::MIC >
} // namespace Containers } // namespace Containers
} // namespace TNL } // namespace TNL
#include <TNL/Containers/Algorithms/ArrayOperationsHost_impl.h> #include <TNL/Containers/Algorithms/ArrayOperationsHost.hpp>
#include <TNL/Containers/Algorithms/ArrayOperationsCuda_impl.h> #include <TNL/Containers/Algorithms/ArrayOperationsCuda.hpp>
#include <TNL/Containers/Algorithms/ArrayOperationsMIC_impl.h> #include <TNL/Containers/Algorithms/ArrayOperationsMIC.hpp>
...@@ -171,6 +171,27 @@ copyMemory( DestinationElement* destination, ...@@ -171,6 +171,27 @@ copyMemory( DestinationElement* destination,
#endif #endif
} }
template< typename DestinationElement,
typename SourceElement >
ArrayOperations< Devices::Cuda >::
void copySTLList( DestinationElement* destination,
const std::list< SourceElement >& source )
{
const auto size = source.size();
const std::streamsize copy_buffer_size = std::min( Devices::Cuda::TransferBufferSize / (std::streamsize) sizeof(SourceType), size );
using BaseType = typename std::remove_cv< SourceType >::type;
std::unique_ptr< BaseType[] > copy_buffer{ new BaseType[ copy_buffer_size ] };
size_t copiedElements = 0;
auto it = source.begin();
while( copiedElements < size )
{
const auto copySize = std::min( size - copiedElements, copy_buffer_size );
for( size_t i = 0; i < copySize; i++ )
copy_buffer[ copiedElements ++ ] = static_cast< DestinationElement >( * it ++ );
ArrayOperations< Devices::Cuda, Devices::Host >::copyMemory( destination, copy_buffer, copySize );
}
}
template< typename Element1, template< typename Element1,
typename Element2, typename Element2,
typename Index > typename Index >
......
...@@ -100,6 +100,18 @@ copyMemory( DestinationElement* destination, ...@@ -100,6 +100,18 @@ copyMemory( DestinationElement* destination,
destination[ i ] = ( DestinationElement ) source[ i ]; destination[ i ] = ( DestinationElement ) source[ i ];
} }
template< typename DestinationElement,
typename SourceElement >
ArrayOperations< Devices::Host >::
void copySTLList( DestinationElement* destination,
const std::list< SourceElement >& source )
{
size_t i = 0;
for( SourceElement& e : source )
destination[ i ++ ] = static_cast< DesitnationElement >( e );
}
template< typename DestinationElement, template< typename DestinationElement,
typename SourceElement, typename SourceElement,
typename Index > typename Index >
......
...@@ -139,6 +139,15 @@ copyMemory( DestinationElement* destination, ...@@ -139,6 +139,15 @@ copyMemory( DestinationElement* destination,
#endif #endif
} }
template< typename DestinationElement,
typename SourceElement >
ArrayOperations< Devices::MIC >::
void copySTLList( DestinationElement* destination,
const std::list< SourceElement >& source )
{
TNL_ASSERT( false, std::cerr << "TODO" );
}
template< typename Element1, template< typename Element1,
typename Element2, typename Element2,
typename Index > typename Index >
......
...@@ -80,33 +80,47 @@ class Array : public Object ...@@ -80,33 +80,47 @@ class Array : public Object
const IndexType& size = 0 ); const IndexType& size = 0 );
/** /**
* \brief Initialize the array from initializer list, i.e. { ... }
* *
* @param list * @param list Initializer list.
*/ */
Array( const std::initializer_list< Value >& list ); template< typename InValue >
Array( const std::initializer_list< InValue >& list );
/** /**
* \brief Initialize the array from std::list.
* *
* @param list * @param list Input STL list.
*/ */
Array( const std::list< Value >& list ); template< typename InValue >
Array( const std::list< InValue >& list );
/** /**
* \brief Initialize the array from std::vector.
* *
* @param vector * @param vector Input STL vector.
*/ */
Array( const std::vector< Value >& vector ); template< typename InValue >
Array( const std::vector< InValue >& vector );
/** \brief Returns type of array Value, Device type and the type of Index. */ /**
* \brief Returns type of array Value, Device type and the type of Index.
*/
static String getType(); static String getType();
/** \brief Returns type of array Value, Device type and the type of Index. */ /**
* \brief Returns type of array Value, Device type and the type of Index.
*/
virtual String getTypeVirtual() const; virtual String getTypeVirtual() const;
/** \brief Returns (host) type of array Value, Device type and the type of Index. */ /**
* \brief Returns (host) type of array Value, Device type and the type of Index.
*/
static String getSerializationType(); static String getSerializationType();
/** \brief Returns (host) type of array Value, Device type and the type of Index. */ /**
* \brief Returns (host) type of array Value, Device type and the type of Index.
*/
virtual String getSerializationTypeVirtual() const; virtual String getSerializationTypeVirtual() const;
/** /**
...@@ -375,4 +389,4 @@ std::ostream& operator << ( std::ostream& str, const Array< Value, Device, Index ...@@ -375,4 +389,4 @@ std::ostream& operator << ( std::ostream& str, const Array< Value, Device, Index
} // namespace Containers } // namespace Containers
} // namespace TNL } // namespace TNL
#include <TNL/Containers/Array_impl.h> #include <TNL/Containers/Array.hpp>
...@@ -97,40 +97,50 @@ Array( Array< Value, Device, Index >& array, ...@@ -97,40 +97,50 @@ Array( Array< Value, Device, Index >& array,
template< typename Value, template< typename Value,
typename Device, typename Device,
typename Index > typename Index >
template< typename InValue >
Array< Value, Device, Index >:: Array< Value, Device, Index >::
Array( const std::initializer_list< Value >& list ) Array( const std::initializer_list< InValue >& list )
: size( 0 ), : size( 0 ),
data( 0 ), data( 0 ),
allocationPointer( 0 ), allocationPointer( 0 ),
referenceCounter( 0 ) referenceCounter( 0 )
{ {
this->setSize( list.size() ); this->setSize( list.size() );
////
// Here we assume that the underlying array for initializer_list is const T[N]
// as noted here:
// https://en.cppreference.com/w/cpp/utility/initializer_list
Algorithms::ArrayOperations< Device >::copyMemory( this->getData(), &( *list.begin() ), list.size() );
} }
template< typename Value, template< typename Value,
typename Device, typename Device,
typename Index > typename Index >
template< typename InValue >
Array< Value, Device, Index >:: Array< Value, Device, Index >::
Array( const std::list< Value >& list ) Array( const std::list< InValue >& list )
: size( 0 ), : size( 0 ),
data( 0 ), data( 0 ),
allocationPointer( 0 ), allocationPointer( 0 ),
referenceCounter( 0 ) referenceCounter( 0 )
{ {
this->setSize( list.size() ); this->setSize( list.size() );
Algorithms::ArrayOperations< Device >::copySTLList( this->getData(), list );
} }
template< typename Value, template< typename Value,
typename Device, typename Device,
typename Index > typename Index >
template< typename InValue >
Array< Value, Device, Index >:: Array< Value, Device, Index >::
Array( const std::vector< Value >& vector ) Array( const std::vector< InValue >& vector )
: size( 0 ), : size( 0 ),
data( 0 ), data( 0 ),
allocationPointer( 0 ), allocationPointer( 0 ),
referenceCounter( 0 ) referenceCounter( 0 )
{ {
this->setSize( vector.size() ); this->setSize( vector.size() );
Algorithms::ArrayOperations< Device >::copyMemory( this->getData(), vector.data(), vector.size() );
} }
template< typename Value, template< typename Value,
......
...@@ -172,6 +172,15 @@ class Cuda ...@@ -172,6 +172,15 @@ class Cuda
static inline Timer& getSmartPointersSynchronizationTimer(); static inline Timer& getSmartPointersSynchronizationTimer();
////
// When we transfer data between the GPU and the CPU we use 5 MB buffer. This
// size should ensure good performance -- see.
// http://wiki.accelereyes.com/wiki/index.php/GPU_Memory_Transfer .
// We use the same buffer size even for retyping data during IO operations.
//
static constexpr std::streamsize TransferBufferSize = 5 * 2<<20;
protected: protected:
static inline Pointers::SmartPointersRegister& getSmartPointersRegister(); static inline Pointers::SmartPointersRegister& getSmartPointersRegister();
......
...@@ -179,7 +179,7 @@ class File ...@@ -179,7 +179,7 @@ class File
std::fstream file; std::fstream file;
String fileName; String fileName;
//// ////
// When we transfer data between the GPU and the CPU we use 5 MB buffer. This // When we transfer data between the GPU and the CPU we use 5 MB buffer. This
// size should ensure good performance -- see. // size should ensure good performance -- see.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment