Skip to content

Commit

Permalink
[NeoMathEngine] Refactored StackAllocators (#1064)
Browse files Browse the repository at this point in the history
* [NeoMathEngine] unite StackAllocators common code

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>

* [NeoMathEngine] Avoid excess new in CStackBlockManager

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>

* [NeoMathEngine] MemoryHandle update

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>

* [NeoMathEngine] no union in CStackMemoryHandle

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>

---------

Signed-off-by: Kirill Golikov <kirill.golikov@abbyy.com>
  • Loading branch information
favorart authored Aug 26, 2024
1 parent 8ec20ce commit a5f6b51
Show file tree
Hide file tree
Showing 19 changed files with 497 additions and 682 deletions.
4 changes: 2 additions & 2 deletions NeoML/Python/src/PyDnnBlob.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2017-2021 ABBYY Production LLC
/* Copyright © 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -102,7 +102,7 @@ class CPyMemoryHandle : public CMemoryHandle {
{
}

void* GetPtr() const { return const_cast<void*>( CMemoryHandle::object ); }
void* GetPtr() const { return const_cast<void*>( CMemoryHandle::Object ); }
};

static CBlobDesc createBlobDesc( TBlobType type, std::initializer_list<int> dimensions )
Expand Down
106 changes: 45 additions & 61 deletions NeoMathEngine/include/NeoMathEngine/MemoryHandle.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2017-2020 ABBYY Production LLC
/* Copyright © 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,54 +27,40 @@ class CMemoryHandleInternal;
// Wraps the pointer to memory allocated by a math engine
class NEOMATHENGINE_API CMemoryHandle {
public:
CMemoryHandle() : mathEngine( 0 ), object( 0 ), offset( 0 ) {}
CMemoryHandle( const CMemoryHandle& other ) : mathEngine( other.mathEngine ), object( other.object ), offset( other.offset ) {}

CMemoryHandle& operator=( const CMemoryHandle& other )
{
mathEngine = other.mathEngine;
object = other.object;
offset = other.offset;
return *this;
}

constexpr CMemoryHandle() = default;
// Be copied and moved by default

bool operator!=( const CMemoryHandle& other ) const { return !operator==( other ); }
bool operator==( const CMemoryHandle& other ) const
{
return mathEngine == other.mathEngine && object == other.object && offset == other.offset;
}

bool operator!=( const CMemoryHandle& other ) const
{
return !operator==( other );
}
{ return MathEngine == other.MathEngine && Object == other.Object && Offset == other.Offset; }

bool IsNull() const
{
return mathEngine == 0 && object == 0 && offset == 0;
}
bool IsNull() const { return *this == CMemoryHandle{}; }

IMathEngine* GetMathEngine() const { return mathEngine; }
IMathEngine* GetMathEngine() const { return MathEngine; }

protected:
IMathEngine* mathEngine; // the math engine
const void* object; // the base object
std::ptrdiff_t offset; // the offset in the base object, in bytes
IMathEngine* MathEngine = nullptr; // the math engine owner
const void* Object = nullptr; // the memory allocated base pointer
std::ptrdiff_t Offset = 0; // the offset in the memory allocated volume, in bytes

friend class CMemoryHandleInternal;

explicit CMemoryHandle( IMathEngine* _mathEngine, const void* _object, ptrdiff_t _offset ) : mathEngine( _mathEngine ), object( _object ), offset( _offset ) {}
explicit CMemoryHandle( IMathEngine* mathEngine, const void* object, ptrdiff_t offset ) :
MathEngine( mathEngine ), Object( object ), Offset( offset ) {}

CMemoryHandle CopyMemoryHandle( ptrdiff_t shift ) const { return CMemoryHandle( mathEngine, object, offset + shift ); }
CMemoryHandle Copy( ptrdiff_t shift ) const { return CMemoryHandle( MathEngine, Object, Offset + shift ); }
};

//------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------

// Wraps the typed pointer to memory allocated by a math engine
template <class T>
class CTypedMemoryHandle : public CMemoryHandle {
public:
CTypedMemoryHandle() = default;
constexpr CTypedMemoryHandle() = default;
// Converting ctor
explicit CTypedMemoryHandle( const CMemoryHandle& other ) : CMemoryHandle( other ) {}
// Be copied and moved by default

void SetValueAt( int index, T value ) const;
T GetValueAt( int index ) const;
Expand All @@ -89,45 +75,45 @@ class CTypedMemoryHandle : public CMemoryHandle {

CTypedMemoryHandle& operator+=( ptrdiff_t shift )
{
offset += shift * sizeof( T );
Offset += shift * sizeof( T );
return *this;
}

CTypedMemoryHandle& operator-=( ptrdiff_t shift )
{
offset -= shift * sizeof( T );
Offset -= shift * sizeof( T );
return *this;
}

CTypedMemoryHandle& operator++()
{
offset += sizeof( T );
Offset += sizeof( T );
return *this;
}

CTypedMemoryHandle<T> operator++( int )
{
CTypedMemoryHandle result( *this );
offset += sizeof( T );
Offset += sizeof( T );
return result;
}

CTypedMemoryHandle& operator--()
{
offset -= sizeof( T );
Offset -= sizeof( T );
return *this;
}

CTypedMemoryHandle<const T> operator--( int )
{
CTypedMemoryHandle result( *this );
offset -= sizeof( T );
Offset -= sizeof( T );
return result;
}

CTypedMemoryHandle operator+( ptrdiff_t shift ) const
{
return CTypedMemoryHandle<T>( CopyMemoryHandle( shift * sizeof( T ) ) );
return CTypedMemoryHandle<T>( Copy( shift * sizeof( T ) ) );
}

CTypedMemoryHandle operator-( ptrdiff_t shift ) const
Expand All @@ -137,13 +123,13 @@ class CTypedMemoryHandle : public CMemoryHandle {

int operator-( const CTypedMemoryHandle& handle ) const
{
return ( int ) ( offset - handle.offset );
return ( int ) ( Offset - handle.Offset );
}
};

//------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------

// CMemoryHandleVar is a variable or fixed-size array for a math engine
// CMemoryHandleVar is a variable or a fixed-size array for a math engine
template<class T>
class CMemoryHandleVarBase {
public:
Expand All @@ -152,7 +138,7 @@ class CMemoryHandleVarBase {
void SetValue( T value );
T GetValue() const;

const CTypedMemoryHandle<T>& GetHandle() const { return data; }
const CTypedMemoryHandle<T>& GetHandle() const { return Data; }

// Operators for easier use
operator const CTypedMemoryHandle<T>&( ) const { return GetHandle(); }
Expand All @@ -163,44 +149,40 @@ class CMemoryHandleVarBase {
bool operator!=( const CTypedMemoryHandle<const T>& other ) const { return GetHandle() != other; }

CTypedMemoryHandle<T> operator+( ptrdiff_t shift ) const { return GetHandle() + shift; }

CTypedMemoryHandle<T> operator-( ptrdiff_t shift ) const { return GetHandle() - shift; }

int operator-( const CTypedMemoryHandle<T>& handle ) const { return GetHandle() - handle; }

int Size() const { return static_cast<int>( size ); }

IMathEngine* GetMathEngine() const { return mathEngine; }
int Size() const { return static_cast<int>( DataSize ); }
IMathEngine* GetMathEngine() const { return &MathEngine; }

protected:
CMemoryHandleVarBase( IMathEngine& _mathEngine, size_t _size ) : mathEngine( &_mathEngine ), size( _size ) {}
IMathEngine& MathEngine; // the math engine owner
mutable CTypedMemoryHandle<T> Data; // the typed memory handler
const size_t DataSize; // the typed memory size

mutable IMathEngine* mathEngine;
mutable CTypedMemoryHandle<T> data;
const size_t size;
CMemoryHandleVarBase( IMathEngine& mathEngine, size_t size ) : MathEngine( mathEngine ), DataSize( size ) {}
~CMemoryHandleVarBase() = default;

private:
// may not be copied, only passed by reference
CMemoryHandleVarBase( const CMemoryHandleVarBase& );
CMemoryHandleVarBase& operator=( const CMemoryHandleVarBase& );
CMemoryHandleVarBase( const CMemoryHandleVarBase& ) = delete;
CMemoryHandleVarBase& operator=( const CMemoryHandleVarBase& ) = delete;
};

//------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------

// A variable or array
// A variable or an array
template<class T>
class CMemoryHandleVar : public CMemoryHandleVarBase<T> {
public:
explicit CMemoryHandleVar( IMathEngine& mathEngine, size_t size = 1 );

~CMemoryHandleVar();

const CTypedMemoryHandle<T>& GetHandle() const { return CMemoryHandleVarBase<T>::GetHandle(); }
};

//------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------

// A variable or array on stack
// A variable or an array on the stack
template<class T>
class CMemoryHandleStackVar : public CMemoryHandleVarBase<T> {
public:
Expand All @@ -209,8 +191,10 @@ class CMemoryHandleStackVar : public CMemoryHandleVarBase<T> {
~CMemoryHandleStackVar();
};

//------------------------------------------------------------------------------------------------------------
//---------------------------------------------------------------------------------------------------------------------

// typedefs

typedef CTypedMemoryHandle<float> CFloatHandle;
typedef CTypedMemoryHandle<const float> CConstFloatHandle;

Expand Down
32 changes: 16 additions & 16 deletions NeoMathEngine/include/NeoMathEngine/MemoryHandle.inl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* Copyright © 2017-2020 ABBYY Production LLC
/* Copyright © 2017-2024 ABBYY
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -23,30 +23,30 @@ template<class T>
inline void CTypedMemoryHandle<T>::SetValueAt( int index, T value ) const
{
CTypedMemoryHandle<T> result = *this + index;
mathEngine->DataExchangeRaw( result, &value, sizeof( T ) );
MathEngine->DataExchangeRaw( result, &value, sizeof( T ) );
}

template<class T>
inline T CTypedMemoryHandle<T>::GetValueAt( int index ) const
{
char result[sizeof(T)];
CTypedMemoryHandle<T> source = *this + index;
mathEngine->DataExchangeRaw( result, source, sizeof( T ) );
MathEngine->DataExchangeRaw( result, source, sizeof( T ) );
T* value = reinterpret_cast<T*>( &result );
return *value;
}

template<class T>
inline void CTypedMemoryHandle<T>::SetValue( T value ) const
{
mathEngine->DataExchangeRaw( *this, &value, sizeof( T ) );
MathEngine->DataExchangeRaw( *this, &value, sizeof( T ) );
}

template<class T>
inline T CTypedMemoryHandle<T>::GetValue() const
{
char result[sizeof(T)];
mathEngine->DataExchangeRaw( result, *this, sizeof( T ) );
MathEngine->DataExchangeRaw( result, *this, sizeof( T ) );
T* value = reinterpret_cast<T*>( &result );
return *value;
}
Expand All @@ -57,25 +57,25 @@ inline T CTypedMemoryHandle<T>::GetValue() const
template<class T>
inline void CMemoryHandleVarBase<T>::SetValueAt( int index, T value )
{
data.SetValueAt( index, value );
Data.SetValueAt( index, value );
}

template<class T>
inline T CMemoryHandleVarBase<T>::GetValueAt( int index ) const
{
return data.GetValueAt( index );
return Data.GetValueAt( index );
}

template<class T>
inline void CMemoryHandleVarBase<T>::SetValue( T value )
{
data.SetValue( value );
Data.SetValue( value );
}

template<class T>
inline T CMemoryHandleVarBase<T>::GetValue() const
{
return data.GetValue();
return Data.GetValue();
}

//------------------------------------------------------------------------------------------------------------
Expand All @@ -86,15 +86,15 @@ inline CMemoryHandleVar<T>::CMemoryHandleVar( IMathEngine& mathEngine, size_t si
CMemoryHandleVarBase<T>( mathEngine, size )
{
if( size != 0 ) {
CMemoryHandleVarBase<T>::data = CMemoryHandleVarBase<T>::mathEngine->template HeapAllocTyped<T>( size );
CMemoryHandleVarBase<T>::Data = CMemoryHandleVarBase<T>::MathEngine.template HeapAllocTyped<T>( size );
}
}

template<class T>
inline CMemoryHandleVar<T>::~CMemoryHandleVar()
{
if( !CMemoryHandleVarBase<T>::data.IsNull() ) {
CMemoryHandleVarBase<T>::mathEngine->HeapFree( CMemoryHandleVarBase<T>::data );
if( !CMemoryHandleVarBase<T>::Data.IsNull() ) {
CMemoryHandleVarBase<T>::MathEngine.HeapFree( CMemoryHandleVarBase<T>::Data );
}
}

Expand All @@ -105,16 +105,16 @@ inline CMemoryHandleStackVar<T>::CMemoryHandleStackVar( IMathEngine& mathEngine,
CMemoryHandleVarBase<T>( mathEngine, size )
{
if( size != 0 ) {
CMemoryHandleVarBase<T>::data =
CTypedMemoryHandle<T>( CMemoryHandleVarBase<T>::mathEngine->StackAlloc( size * sizeof(T) ) );
CMemoryHandleVarBase<T>::Data =
CTypedMemoryHandle<T>( CMemoryHandleVarBase<T>::MathEngine.StackAlloc( size * sizeof( T ) ) );
}
}

template<class T>
inline CMemoryHandleStackVar<T>::~CMemoryHandleStackVar()
{
if( !CMemoryHandleVarBase<T>::data.IsNull() ) {
CMemoryHandleVarBase<T>::mathEngine->StackFree( CMemoryHandleVarBase<T>::data );
if( !CMemoryHandleVarBase<T>::Data.IsNull() ) {
CMemoryHandleVarBase<T>::MathEngine.StackFree( CMemoryHandleVarBase<T>::Data );
}
}

Expand Down
8 changes: 3 additions & 5 deletions NeoMathEngine/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@ set(CPU_COMMON_SOURCES
CPU/CpuMathEngineVectorMath.cpp
CrtAllocatedObject.cpp
DllLoader.cpp
MathEngineDeviceStackAllocator.cpp
MathEngineDnnDropout.cpp
MathEngine.cpp
MathEngineHostStackAllocator.cpp
MathEngineStackAllocator.cpp
MemoryEngineMixin.cpp
MemoryPool.cpp
ThreadPool.cpp
Expand All @@ -44,13 +43,12 @@ target_sources(${PROJECT_NAME}
common.h
MathEngineAllocator.h
MathEngineCommon.h
MathEngineDeviceStackAllocator.h
MathEngineDll.h
MathEngineDnnConv.h
MathEngineDnnDropout.h
MathEngineDnnLrn.h
MathEngineDnnPoolings.h
MathEngineHostStackAllocator.h
MathEngineStackAllocator.h
MemoryEngineMixin.h
MemoryHandleInternal.h
MemoryPool.h
Expand Down Expand Up @@ -104,7 +102,7 @@ set_target_properties( ${PROJECT_NAME} PROPERTIES
UNITY_BUILD_MODE GROUP
)
set_property(SOURCE ${CPU_COMMON_SOURCES} PROPERTY UNITY_GROUP 1)
set_property(SOURCE MathEngineDeviceStackAllocator.cpp PROPERTY SKIP_UNITY_BUILD_INCLUSION ON)
set_property(SOURCE MathEngineStackAllocator.cpp PROPERTY SKIP_UNITY_BUILD_INCLUSION ON)

target_compile_features(${PROJECT_NAME} PUBLIC cxx_std_14)

Expand Down
2 changes: 0 additions & 2 deletions NeoMathEngine/src/CPU/CpuMathEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ limitations under the License.
#pragma hdrstop

#include <CpuMathEngine.h>
#include <MathEngineDeviceStackAllocator.h>
#include <MathEngineHostStackAllocator.h>
#include <MemoryHandleInternal.h>
#include <MathEngineCommon.h>
#include <NeoMathEngine/SimdMathEngine.h>
Expand Down
Loading

0 comments on commit a5f6b51

Please sign in to comment.