Commit 39e82f08 authored by pruthvi's avatar pruthvi

- use AlignedBuffer instead of CPUAlignedBuffer

- move allocater class to ngraph/runtime directory from ngraph/runtime/cpu/
parent b474dbef
......@@ -16,7 +16,7 @@
#include "ngraph/runtime/allocator.hpp"
void* ngraph::runtime::Allocator::Malloc(void*, size_t size, size_t alignment)
void* ngraph::runtime::Allocator::Malloc(void* handle, size_t size, size_t alignment)
{
void* ptr = malloc(size);
......@@ -29,7 +29,7 @@ void* ngraph::runtime::Allocator::Malloc(void*, size_t size, size_t alignment)
return ptr;
}
void ngraph::runtime::Allocator::Free(void* ptr, void*)
void ngraph::runtime::Allocator::Free(void* handle, void* ptr)
{
if (ptr)
{
......
......@@ -34,6 +34,6 @@ class ngraph::runtime::Allocator
{
public:
virtual ~Allocator() = default;
virtual void* Malloc(void*, size_t size, size_t alignment);
virtual void Free(void* ptr, void*);
virtual void* Malloc(void* handle, size_t size, size_t alignment);
virtual void Free(void* handle, void* ptr);
};
......@@ -30,6 +30,10 @@ namespace ngraph
{
namespace runtime
{
// aliases for framework provided function pointers as defined in onnx runtime
using AllocateFunc = void* (*)(void*, size_t, size_t);
using DestroyFunc = void (*)(void*, void*);
class Tensor;
class Backend;
}
......
......@@ -17,7 +17,6 @@
include(FindOpenMP)
set(SRC
cpu_aligned_buffer.cpp
cpu_backend.cpp
cpu_builder.cpp
cpu_call_frame.cpp
......
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#include <memory>
#include "ngraph/runtime/cpu/cpu_aligned_buffer.hpp"
#include "ngraph/runtime/cpu/cpu_allocator.hpp"
#include "ngraph/util.hpp"
using namespace ngraph;
runtime::cpu::CPUAlignedBuffer::CPUAlignedBuffer(size_t byte_size,
size_t alignment,
ngraph::runtime::cpu::CPUAllocator* cpu_allocator)
{
m_byte_size = byte_size;
m_cpu_allocator = cpu_allocator;
if (m_byte_size > 0)
{
size_t allocation_size = m_byte_size + alignment;
m_allocated_buffer = static_cast<char*>(m_cpu_allocator->malloc(allocation_size));
m_aligned_buffer = m_allocated_buffer;
size_t mod = size_t(m_aligned_buffer) % alignment;
if (mod != 0)
{
m_aligned_buffer += (alignment - mod);
}
}
else
{
m_allocated_buffer = nullptr;
m_aligned_buffer = nullptr;
}
}
runtime::cpu::CPUAlignedBuffer::~CPUAlignedBuffer()
{
if (m_allocated_buffer != nullptr)
{
m_cpu_allocator->free(m_allocated_buffer);
}
}
//*****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//*****************************************************************************
#pragma once
#include <cstddef>
#include "ngraph/runtime/cpu/cpu_allocator.hpp"
namespace ngraph
{
namespace runtime
{
namespace cpu
{
class CPUAlignedBuffer;
}
}
}
/// \brief Allocates a block of memory on the specified alignment. The actual size of the
/// allocated memory is larger than the requested size by the alignment, so allocating 1 byte
/// on 64 byte alignment will allocate 65 bytes.
class ngraph::runtime::cpu::CPUAlignedBuffer
{
public:
CPUAlignedBuffer(size_t byte_size,
size_t alignment,
ngraph::runtime::cpu::CPUAllocator* cpu_allocator);
~CPUAlignedBuffer();
size_t size() const { return m_byte_size; }
void* get_ptr(size_t offset) const { return m_aligned_buffer + offset; }
void* get_ptr() const { return m_aligned_buffer; }
private:
CPUAlignedBuffer(const CPUAlignedBuffer&) = delete;
CPUAlignedBuffer(CPUAlignedBuffer&&) = delete;
CPUAlignedBuffer& operator=(const CPUAlignedBuffer&) = delete;
ngraph::runtime::cpu::CPUAllocator* m_cpu_allocator;
char* m_allocated_buffer;
char* m_aligned_buffer;
size_t m_byte_size;
};
......@@ -15,31 +15,24 @@
//*****************************************************************************
#include "ngraph/runtime/cpu/cpu_allocator.hpp"
#include <string>
#include "ngraph/runtime/cpu/cpu_external_function.hpp"
ngraph::runtime::cpu::CPUAllocator::CPUAllocator(ngraph::runtime::Allocator* allocator,
size_t alignment)
: m_allocator(std::move(allocator))
, m_alignment(alignment)
namespace ngraph
{
namespace runtime
{
namespace cpu
{
CPUAllocator::CPUAllocator() {}
CPUAllocator::~CPUAllocator() {}
CPUAllocator& GetCPUAllocator()
{
static CPUAllocator cpu_allocator;
return cpu_allocator;
}
}
}
}
ngraph::runtime::cpu::CPUAllocator::~CPUAllocator()
{
}
void* ngraph::runtime::cpu::CPUAllocator::malloc(size_t size)
{
return m_allocator->cpu_malloc(nullptr, size, m_alignment);
}
void ngraph::runtime::cpu::CPUAllocator::free(void* ptr)
{
m_allocator->cpu_free(nullptr, ptr);
}
ngraph::runtime::SystemAllocator::SystemAllocator()
/*ngraph::runtime::SystemAllocator::SystemAllocator()
{
}
......@@ -56,4 +49,4 @@ ngraph::runtime::FrameworkAllocator::FrameworkAllocator(AllocateFunc& allocator,
: m_allocator(allocator)
, m_deallocator(deallocator)
{
}
}*/
//*****************************************************************************
//****************************************************************************
// Copyright 2017-2019 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
......@@ -20,11 +20,11 @@
#include <cstdint>
#include "ngraph/except.hpp"
#include "ngraph/runtime/aligned_buffer.hpp"
#include "ngraph/runtime/allocator.hpp"
#include "ngraph/runtime/backend.hpp"
#include "ngraph/util.hpp"
using AllocateFunc = void* (*)(void*, size_t, size_t);
using DestroyFunc = void (*)(void*, void*);
using namespace ngraph;
namespace mkl
{
extern "C" {
......@@ -45,46 +45,54 @@ namespace ngraph
namespace runtime
{
class Allocator;
class SystemAllocator;
class FrameworkAllocator;
//class SystemAllocator;
//class FrameworkAllocator;
namespace cpu
{
class CPUAllocator;
extern CPUAllocator& GetCPUAllocator();
}
}
}
// This class will be instantiated in the CPUCallFrame with the allocator object which will be used for the
// device memory allocation
class ngraph::runtime::cpu::CPUAllocator
class ngraph::runtime::cpu::CPUAllocator : public ngraph::runtime::Allocator
{
public:
CPUAllocator(ngraph::runtime::Allocator* alloctor, size_t alignment);
CPUAllocator();
~CPUAllocator();
void* malloc(size_t size);
void free(void* ptr);
private:
std::unique_ptr<ngraph::runtime::Allocator> m_allocator;
size_t m_alignment;
void* Malloc(void* handle, size_t size, size_t alignment) override
{
void* 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 Free(void* handle, void* ptr) override
{
if (ptr)
{
free(ptr);
}
}
};
// Abstarct class for the allocator, for allocating and deallocating device memory
/*// Abstarct class for the allocator
class ngraph::runtime::Allocator
{
public:
virtual ~Allocator() = default;
virtual void* cpu_malloc(void*, size_t size, size_t alignment) = 0;
virtual void cpu_free(void* ptr, void*) = 0;
};
// SystemAllocator overides and implements "cpu_malloc" & "cpu_free" of Alloctaor interface class
// this class uses system library malloc and free for device memory allocation
class ngraph::runtime::SystemAllocator : public ngraph::runtime::Allocator
{
public:
SystemAllocator();
SystemAllocator(size_t alignment);
~SystemAllocator();
void* cpu_malloc(void*, size_t size, size_t alignment) override
......@@ -107,14 +115,15 @@ public:
free(ptr);
}
}
private:
size_t m_alignment;
};
// FrameworkAllocator overides and implements "cpu_malloc" & "cpu_free" of Alloctaor interface class,
// this class uses framework provide allocators and deallocators for device memory allocation
class ngraph::runtime::FrameworkAllocator : public ngraph::runtime::Allocator
{
public:
FrameworkAllocator(AllocateFunc& allocator, DestroyFunc& deallocator);
FrameworkAllocator(AllocateFunc allocator, DestroyFunc deallocator, size_t alignment);
~FrameworkAllocator();
void* cpu_malloc(void*, size_t size, size_t alignment) override
......@@ -141,4 +150,5 @@ public:
private:
AllocateFunc m_allocator;
DestroyFunc m_deallocator;
};
size_t m_alignment;
};*/
......@@ -67,6 +67,9 @@ namespace ngraph
bool is_supported(const Node& node) const override;
bool is_supported_property(const Property prop) const override;
//static inline void* MallocHook(size_t size);
//static inline void FreeHook(void *ptr);
private:
std::unordered_map<std::shared_ptr<Function>, std::shared_ptr<Executable>>
m_exec_map;
......
......@@ -143,10 +143,8 @@ void runtime::cpu::CPU_CallFrame::setup_runtime_context()
// Create temporary buffer pools
size_t alignment = runtime::cpu::CPU_ExternalFunction::s_memory_pool_alignment;
ngraph::runtime::cpu::CPUAllocator* allocator = nullptr;
ngraph::runtime::Allocator* _allocator = nullptr;
if (m_framework_allocator && m_framework_deallocator)
ngraph::runtime::Allocator* allocator = new ngraph::runtime::cpu::CPUAllocator();
/*if (m_framework_allocator && m_framework_deallocator)
{
auto fw_allocator =
new ngraph::runtime::FrameworkAllocator(m_framework_allocator, m_framework_deallocator);
......@@ -156,11 +154,11 @@ void runtime::cpu::CPU_CallFrame::setup_runtime_context()
{
auto sys_allocator = new ngraph::runtime::SystemAllocator();
allocator = new ngraph::runtime::cpu::CPUAllocator(sys_allocator, alignment);
}
}*/
for (auto buffer_size : m_external_function->get_memory_buffer_sizes())
{
auto buffer = new AlignedBuffer(buffer_size, alignment, _allocator);
auto buffer = new AlignedBuffer(buffer_size, alignment, allocator);
ctx->memory_buffers.push_back(buffer);
}
const auto& mkldnn_emitter = m_external_function->get_mkldnn_emitter();
......
......@@ -135,7 +135,6 @@
#include "ngraph/pass/reshape_sinking.hpp"
#include "ngraph/pass/zero_dim_tensor_elimination.hpp"
#include "ngraph/runtime/aligned_buffer.hpp"
#include "ngraph/runtime/cpu/cpu_aligned_buffer.hpp"
#include "ngraph/runtime/cpu/cpu_allocator.hpp"
#include "ngraph/runtime/cpu/cpu_backend.hpp"
#include "ngraph/runtime/cpu/cpu_builder.hpp"
......@@ -508,7 +507,6 @@ void runtime::cpu::CPU_ExternalFunction::compile(ngraph::pass::PassConfig& pass_
R"(
#include <cmath>
#include "ngraph/except.hpp"
#include "ngraph/runtime/cpu/cpu_aligned_buffer.hpp"
#include "ngraph/runtime/cpu/cpu_eigen_utils.hpp"
#include "ngraph/runtime/cpu/cpu_kernels.hpp"
#include "ngraph/runtime/cpu/cpu_runtime_context.hpp"
......
......@@ -38,10 +38,6 @@ namespace ngraph
class NodeMap;
class stopwatch;
// aliases for framework provided function pointers as defined in onnx runtime
using AllocateFunc = void* (*)(void*, size_t, size_t);
using DestroyFunc = void (*)(void*, void*);
namespace runtime
{
class Backend;
......
......@@ -43,8 +43,8 @@ TEST(cpu_codegen, abc)
copy_data(c, test::NDArray<float, 2>({{9, 10}, {11, 12}}).get_vector());
ngraph::pass::PassConfig pass_config{ngraph::pass::CompilationMode::CODEGEN};
AllocateFunc framework_allocator = nullptr;
DestroyFunc framework_deallocator = nullptr;
runtime::AllocateFunc framework_allocator = nullptr;
runtime::DestroyFunc framework_deallocator = nullptr;
auto handle = backend->compile(f, pass_config, framework_allocator, framework_deallocator);
handle->call_with_validate({result}, {a, b, c});
EXPECT_TRUE(test::all_close_f(read_vector<float>(result),
......
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