Commit 1f00711d authored by pruthvi's avatar pruthvi

fixed compiler errors

parent d227de8e
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <memory> #include <memory>
#include "ngraph/runtime/aligned_buffer.hpp" #include "ngraph/runtime/aligned_buffer.hpp"
#include "ngraph/runtime/cpu/cpu_mkl_allocator.hpp"
#include "ngraph/util.hpp" #include "ngraph/util.hpp"
using namespace ngraph; using namespace ngraph;
...@@ -31,6 +32,7 @@ runtime::AlignedBuffer::AlignedBuffer() ...@@ -31,6 +32,7 @@ runtime::AlignedBuffer::AlignedBuffer()
runtime::AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment) runtime::AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment)
{ {
m_byte_size = byte_size; m_byte_size = byte_size;
auto cpu_allocator = ngraph::runtime::cpu::GetCPUAllocator();
if (m_byte_size > 0) if (m_byte_size > 0)
{ {
size_t allocation_size = m_byte_size + alignment; size_t allocation_size = m_byte_size + alignment;
......
...@@ -15,16 +15,55 @@ ...@@ -15,16 +15,55 @@
//***************************************************************************** //*****************************************************************************
#include "ngraph/runtime/cpu/cpu_mkl_allocator.hpp" #include "ngraph/runtime/cpu/cpu_mkl_allocator.hpp"
#include <string>
#include "ngraph/except.hpp"
ngraph::runtime::cpu::CPUAllocator::CPUAllocator() ngraph::runtime::cpu::CPUAllocator::CPUAllocator(AllocateFunc allocator,
: m_buffer(nullptr) DestroyFunc deallocator,
, m_byte_size(0) size_t alignment)
: m_framework_allocator(allocator)
, m_framework_deallocator(deallocator)
, m_alignment(alignment)
{ {
mkl::i_malloc = MallocHook;
mkl::i_free = FreeHook;
} }
ngraph::runtime::cpu::CPUAllocator::CPUAllocator(size_t size) void* ngraph::runtime::cpu::CPUAllocator::cpu_malloc(size_t size)
{ {
m_byte_size = size; void* ptr;
mkl::i_malloc = MallocHook; if (m_framework_allocator != nullptr)
mkl::i_free = FreeHook; {
ptr = m_framework_allocator(nullptr, m_alignment, size);
}
else
{
ptr = malloc(size);
}
// check for exception
if (size != 0 && !ptr)
{
throw ngraph_error("malloc failed to allocate memory of size " + std::to_string(size));
throw std::bad_alloc();
}
return ptr;
}
void ngraph::runtime::cpu::CPUAllocator::cpu_free(void* ptr)
{
if (m_framework_deallocator && ptr)
{
m_framework_deallocator(nullptr, ptr);
}
else if (ptr)
{
free(ptr);
}
}
ngraph::runtime::cpu::CPUAllocator& ngraph::runtime::cpu::GetCPUAllocator()
{
static ngraph::runtime::cpu::CPUAllocator cpu_allocator(nullptr, nullptr, 4096);
return cpu_allocator;
} }
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "ngraph/util.hpp" #include "ngraph/util.hpp"
using namespace ngraph; using namespace ngraph;
using AllocateFunc = void* (*)(void*, size_t, size_t);
using DestroyFunc = void (*)(void*, void*);
namespace mkl namespace mkl
{ {
...@@ -45,6 +47,7 @@ namespace ngraph ...@@ -45,6 +47,7 @@ namespace ngraph
namespace cpu namespace cpu
{ {
class CPUAllocator; class CPUAllocator;
extern CPUAllocator& GetCPUAllocator();
} }
} }
} }
...@@ -52,18 +55,24 @@ namespace ngraph ...@@ -52,18 +55,24 @@ namespace ngraph
class ngraph::runtime::cpu::CPUAllocator class ngraph::runtime::cpu::CPUAllocator
{ {
public: public:
CPUAllocator(size_t size); CPUAllocator(AllocateFunc allocator, DestroyFunc deallocator, size_t alignment);
CPUAllocator(); //~CPUAllocator();
~CPUAllocator();
void* cpu_malloc(size_t size);
void cpu_free(void* ptr);
private: private:
void* m_buffer; AllocateFunc m_framework_allocator;
size_t m_byte_size; DestroyFunc m_framework_deallocator;
size_t m_alignment;
static inline void* MallocHook(size_t size) static inline void* MallocHook(size_t size)
{ {
void* ptr = static_cast<void*>(ngraph_malloc(size)); ngraph::runtime::cpu::GetCPUAllocator().cpu_malloc(size);
return ptr; }
static inline void FreeHook(void* ptr)
{
ngraph::runtime::cpu::GetCPUAllocator().cpu_free(ptr);
} }
static inline void FreeHook(void* ptr) { ngraph_free(ptr); }
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment