Skip to content

Commit

Permalink
Merge pull request #4 from LLNL/feature/corbett/umpire-sidre-github
Browse files Browse the repository at this point in the history
Feature/corbett/umpire sidre GitHub
  • Loading branch information
corbett5 committed May 9, 2019
2 parents 5061932 + ed468b0 commit 6c81acf
Show file tree
Hide file tree
Showing 30 changed files with 1,406 additions and 273 deletions.
12 changes: 6 additions & 6 deletions src/axom/core/Array.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ Array< T >::Array( IndexType num_tuples, IndexType num_components,
m_num_components( 0 ),
m_resize_ratio( DEFAULT_RESIZE_RATIO ),
m_is_external( false )
{
{
initialize( num_tuples, num_components, capacity );
}

Expand Down Expand Up @@ -544,7 +544,7 @@ Array< T >::~Array()
{
if ( m_data != nullptr && !m_is_external )
{
axom::free( m_data );
axom::deallocate( m_data );
}

m_data = nullptr;
Expand Down Expand Up @@ -656,7 +656,7 @@ inline void Array< T >::initialize( IndexType num_tuples,
if ( capacity == 0 )
{
capacity = ( num_tuples > MIN_DEFAULT_CAPACITY ) ?
num_tuples : MIN_DEFAULT_CAPACITY;
num_tuples : MIN_DEFAULT_CAPACITY;
}
setCapacity( capacity );

Expand Down Expand Up @@ -728,7 +728,7 @@ inline void Array< T >::setCapacity( IndexType new_capacity )
updateNumTuples( new_capacity );
}

m_data = axom::realloc( m_data, new_capacity * m_num_components );
m_data = axom::reallocate( m_data, new_capacity * m_num_components );
m_capacity = new_capacity;

assert( m_data != nullptr || m_capacity <= 0 );
Expand Down Expand Up @@ -756,9 +756,9 @@ inline void Array< T >::dynamicRealloc( IndexType new_num_tuples )
utilities::processAbort();
}

m_data = axom::realloc( m_data, new_capacity * m_num_components );
m_data = axom::reallocate( m_data, new_capacity * m_num_components );
m_capacity = new_capacity;

assert( m_data != nullptr || m_capacity <= 0 );
}

Expand Down
8 changes: 4 additions & 4 deletions src/axom/core/Macros.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* \note These will expand to the corresponding CUDA decorations when
* compiled with -DAXOM_USE_CUDA
*/
#if defined(__CUDACC__)
#if defined(__CUDACC__)
#define AXOM_DEVICE __device__
#define AXOM_HOST_DEVICE __host__ __device__
#else
Expand Down Expand Up @@ -62,9 +62,9 @@
*/
#if defined(AXOM_USE_CUDA)
#define AXOM_CUDA_TEST(X, Y) \
static void cuda_test_##X##Y(); \
TEST(X, Y) { cuda_test_##X##Y(); } \
static void cuda_test_##X##Y()
static void cuda_test_ ## X ## Y(); \
TEST(X, Y) { cuda_test_ ## X ## Y(); } \
static void cuda_test_ ## X ## Y()
#else
#define AXOM_CUDA_TEST(X, Y) TEST(X, Y)
#endif
Expand Down
4 changes: 2 additions & 2 deletions src/axom/core/StackArray.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ struct StackArray
T& operator[](IndexType i) noexcept
{ return m_data[ i ]; }

AXOM_HOST_DEVICE
AXOM_HOST_DEVICE
constexpr const T& operator[](IndexType i) const noexcept
{ return m_data[i]; }

Expand All @@ -52,7 +52,7 @@ struct StackArray
AXOM_HOST_DEVICE operator T* () noexcept
{ return &m_data[0]; }

AXOM_HOST_DEVICE
AXOM_HOST_DEVICE
constexpr operator const T* () const noexcept
{ return &m_data[0]; }

Expand Down
1 change: 0 additions & 1 deletion src/axom/core/Types.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,3 @@ const MPI_Datatype mpi_traits< uint64 >::type = MPI_UINT64_T;
#endif /* end AXOM_USE_MPI */

} // end namespace axom

113 changes: 85 additions & 28 deletions src/axom/core/memory_management.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,46 +12,56 @@
// Umpire includes
#ifdef AXOM_USE_UMPIRE
#include "umpire/config.hpp"
#include "umpire/Allocator.hpp"
#include "umpire/ResourceManager.hpp"
#endif

// C/C++ includes
#include <cassert> // for assert()
#include "umpire/op/MemoryOperationRegistry.hpp"
#else
#include <cstring> // for std::memcpy
#include <cstdlib> // for std::malloc, std::realloc, std::free
#endif

namespace axom
{

constexpr int INVALID_ALLOCATOR_ID = -1;

/// \name Memory Management Routines
/// @{

#ifdef AXOM_USE_UMPIRE

/*!
* \brief Returns the umpire allocator associated with the given ID.
* \param [in] allocatorID the ID of the allocator to get.
*/
inline umpire::Allocator getAllocator( int allocatorID )
{
return umpire::ResourceManager::getInstance().getAllocator( allocatorID );
}

/*!
* \brief Sets the default memory space to use. Default is set to HOST
* \param [in] spaceId ID of the memory space to use.
* \param [in] allocator the umpire::Allocator to make default.
*/
inline void setDefaultAllocator( umpire::Allocator allocator )
{
umpire::ResourceManager::getInstance().setDefaultAllocator( allocator );
}

/*!
* \brief Returns the current default memory space used.
* \brief Sets the default memory space to use. Default is set to HOST
* \param [in] allocatorID ID of the umpire::Allocator to use.
*/
inline umpire::Allocator getDefaultAllocator()
inline void setDefaultAllocator( int allocatorID )
{
return umpire::ResourceManager::getInstance().getDefaultAllocator();
setDefaultAllocator( getAllocator( allocatorID ) );
}

/*!
* \brief Returns the umpire allocator associated with the given ID.
* \param [in] allocatorID the ID of the allocator to get.
* \brief Returns the current default memory space used.
*/
inline umpire::Allocator getAllocator( int allocatorID )
inline umpire::Allocator getDefaultAllocator()
{
return umpire::ResourceManager::getInstance().getAllocator( allocatorID );
return umpire::ResourceManager::getInstance().getDefaultAllocator();
}

#endif
Expand All @@ -65,7 +75,7 @@ inline umpire::Allocator getAllocator( int allocatorID )
*
* \tparam T the type of pointer returned.
*
* \note By default alloc() will use the current default memory space. The
* \note By default allocate() will use the current default memory space. The
* caller may explicitly specify the memory space to use by specifying the
* second, optional argument, or change the default memory space by calling
* axom::setDefaultAllocator().
Expand All @@ -76,31 +86,48 @@ inline umpire::Allocator getAllocator( int allocatorID )
*/
template < typename T >
#ifdef AXOM_USE_UMPIRE
inline T* alloc( std::size_t n, umpire::Allocator allocator=getDefaultAllocator() ) noexcept;
inline T* allocate( std::size_t n,
umpire::Allocator allocator=
getDefaultAllocator() ) noexcept;
#else
inline T* alloc( std::size_t n ) noexcept;
inline T* allocate( std::size_t n ) noexcept;
#endif

/*!
* \brief Frees the chunk of memory pointed to by the supplied pointer, p.
* \param [in] p a pointer to memory allocated with alloc/realloc or a nullptr.
* \param [in/out] p a pointer to memory allocated with allocate/reallocate or a
* nullptr.
* \post p == nullptr
*/
template < typename T >
inline void free( T*& p ) noexcept;
inline void deallocate( T*& p ) noexcept;

/*!
* \brief Reallocates the chunk of memory pointed to by the supplied pointer.
*
* \param [in] p pointer to memory allocated with alloc/realloc, or a nullptr.
* \param [in] p pointer to memory allocated with allocate/reallocate, or a
* nullptr.
* \param [in] n the number of elements to allocate.
*
* \tparam T the type pointer p points to.
*
* \return p pointer to the new allocation or a nullptr if allocation failed.
*/
template < typename T >
inline T* realloc( T* p, std::size_t n ) noexcept;
inline T* reallocate( T* p, std::size_t n ) noexcept;

/*!
* \brief Copies memory from the source to the destination.
*
* \param [in/out] dst the destination to copy to.
* \param [in] src the source to copy from.
* \param [in] numbytes the number of bytes to copy.
*
* \note When using Umpire if either src or dst is not registered with the
* ResourceManager then the default host allocation strategy is assumed for
* that pointer.
*/
inline void copy( void* dst, void* src, std::size_t numbytes ) noexcept;

/// @}

Expand All @@ -112,9 +139,10 @@ inline T* realloc( T* p, std::size_t n ) noexcept;
#ifdef AXOM_USE_UMPIRE

template < typename T >
inline T* alloc( std::size_t n, umpire::Allocator allocator ) noexcept
inline T* allocate( std::size_t n, umpire::Allocator allocator ) noexcept
{
if ( n == 0 ) return nullptr;
if ( n == 0 )
return nullptr;

const std::size_t numbytes = n * sizeof( T );
return static_cast< T* >( allocator.allocate( numbytes ) );
Expand All @@ -123,9 +151,10 @@ inline T* alloc( std::size_t n, umpire::Allocator allocator ) noexcept
#else

template < typename T >
inline T* alloc( std::size_t n ) noexcept
inline T* allocate( std::size_t n ) noexcept
{
if ( n == 0 ) return nullptr;
if ( n == 0 )
return nullptr;

const std::size_t numbytes = n * sizeof( T );
return static_cast< T* >( std::malloc( numbytes ) );
Expand All @@ -135,9 +164,10 @@ inline T* alloc( std::size_t n ) noexcept

//------------------------------------------------------------------------------
template < typename T >
inline void free( T*& pointer ) noexcept
inline void deallocate( T*& pointer ) noexcept
{
if ( pointer == nullptr ) return;
if ( pointer == nullptr )
return;

#ifdef AXOM_USE_UMPIRE

Expand All @@ -155,11 +185,11 @@ inline void free( T*& pointer ) noexcept

//------------------------------------------------------------------------------
template < typename T >
inline T* realloc( T* pointer, std::size_t n ) noexcept
inline T* reallocate( T* pointer, std::size_t n ) noexcept
{
if ( n == 0 )
{
axom::free( pointer );
axom::deallocate( pointer );
return nullptr;
}

Expand All @@ -179,6 +209,33 @@ inline T* realloc( T* pointer, std::size_t n ) noexcept
return pointer;
}

inline void copy( void* dst, void* src, std::size_t numbytes ) noexcept
{
#ifdef AXOM_USE_UMPIRE
umpire::ResourceManager & rm = umpire::ResourceManager::getInstance();
umpire::op::MemoryOperationRegistry & op_registry =
umpire::op::MemoryOperationRegistry::getInstance();

auto dstStrategy = rm.getAllocator( "HOST" ).getAllocationStrategy();
auto srcStrategy = dstStrategy;

if (rm.hasAllocator(dst))
{
dstStrategy = rm.findAllocationRecord( dst )->m_strategy;
}

if (rm.hasAllocator(src))
{
srcStrategy = rm.findAllocationRecord( src )->m_strategy;
}

auto op = op_registry.find( "COPY", srcStrategy, dstStrategy );
op->transform( src, &dst, nullptr, nullptr, numbytes );
#else
std::memcpy( dst, src, numbytes );
#endif
}

} // namespace axom

#endif /* AXOM_MEMORYMANAGEMENT_HPP_ */
4 changes: 2 additions & 2 deletions src/axom/core/numerics/LU.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ int lu_solve( const Matrix< T >& A, const int* pivots, const T* b, T* x )
}

const int size = A.getNumRows();
T* rhs = axom::alloc< T >( size );
T* rhs = axom::allocate< T >( size );
memcpy( rhs, b, size*sizeof( T ) );

// forward-solve L part (top-to-bottom)
Expand Down Expand Up @@ -199,7 +199,7 @@ int lu_solve( const Matrix< T >& A, const int* pivots, const T* b, T* x )
} // END for j
} // END for i

axom::free( rhs );
axom::deallocate( rhs );
return LU_SUCCESS;
}

Expand Down
17 changes: 10 additions & 7 deletions src/axom/core/numerics/Matrix.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,8 @@ class Matrix
* \pre cols >= 1
* \pre data != nullptr
*/
AXOM_HOST_DEVICE Matrix( int rows, int cols, T* data, bool useExternal=false );
AXOM_HOST_DEVICE Matrix( int rows, int cols, T* data,
bool useExternal=false );

/*!
* \brief Copy constructor.
Expand Down Expand Up @@ -584,13 +585,14 @@ Matrix< T >::Matrix( int rows, int cols, T val ) :
assert( m_rows > 0 );
assert( m_cols > 0 );

m_data = alloc< T >( m_rows * m_cols );
m_data = allocate< T >( m_rows * m_cols );
this->fill( val );
}

//-----------------------------------------------------------------------------
template < typename T >
AXOM_HOST_DEVICE Matrix< T >::Matrix( int rows, int cols, T* data, bool external ) :
AXOM_HOST_DEVICE Matrix< T >::Matrix( int rows, int cols, T* data,
bool external ) :
m_rows( rows ),
m_cols( cols ),
m_usingExternal( external )
Expand All @@ -608,7 +610,7 @@ AXOM_HOST_DEVICE Matrix< T >::Matrix( int rows, int cols, T* data, bool external
assert(false);
#else
const int nitems = m_rows * m_cols;
m_data = alloc< T >( nitems );
m_data = allocate< T >( nitems );
memcpy( m_data, data, nitems * sizeof(T) );
#endif
}
Expand Down Expand Up @@ -747,7 +749,8 @@ void Matrix< T >::swapColumns( IndexType icol, IndexType jcol )

//-----------------------------------------------------------------------------
template < typename T >
AXOM_HOST_DEVICE const T& Matrix< T >::operator()(IndexType i, IndexType j ) const
AXOM_HOST_DEVICE const T& Matrix< T >::operator()(IndexType i,
IndexType j ) const
{
assert( (i>=0) && (i < m_rows) );
assert( (j>=0) && (j < m_cols) );
Expand Down Expand Up @@ -902,7 +905,7 @@ void Matrix< T >::copy( const Matrix< T >& rhs )

m_rows = rhs.m_rows;
m_cols = rhs.m_cols;
m_data = alloc< T >( m_rows*m_cols );
m_data = allocate< T >( m_rows*m_cols );
}

assert( m_rows==rhs.m_rows );
Expand All @@ -923,7 +926,7 @@ AXOM_HOST_DEVICE void Matrix< T >::clear( )
#else
if ( !m_usingExternal )
{
free( m_data );
deallocate( m_data );
}
#endif

Expand Down

0 comments on commit 6c81acf

Please sign in to comment.