Commit efc53f25 authored by Robert Kimball's avatar Robert Kimball

Add new memory handler for generated code

parent a68f0715
...@@ -74,6 +74,7 @@ ...@@ -74,6 +74,7 @@
#include "ngraph/pass/memory_layout.hpp" #include "ngraph/pass/memory_layout.hpp"
#include "ngraph/pass/topological_sort.hpp" #include "ngraph/pass/topological_sort.hpp"
#include "ngraph/runtime/cpu/call_frame.hpp" #include "ngraph/runtime/cpu/call_frame.hpp"
#include "ngraph/runtime/cpu/cpu_backend.hpp"
#include "ngraph/runtime/cpu/emitter.hpp" #include "ngraph/runtime/cpu/emitter.hpp"
#include "ngraph/runtime/cpu/external_function.hpp" #include "ngraph/runtime/cpu/external_function.hpp"
#include "ngraph/runtime/utils.hpp" #include "ngraph/runtime/utils.hpp"
...@@ -93,25 +94,6 @@ static StaticInitializers s_static_initializers; ...@@ -93,25 +94,6 @@ static StaticInitializers s_static_initializers;
using ngraph::descriptor::layout::DenseTensorViewLayout; using ngraph::descriptor::layout::DenseTensorViewLayout;
extern "C" void
allocate_aligned_buffer(size_t size, size_t alignment, char** allocated, char** aligned_ptr)
{
size_t allocation_size = size + alignment;
*allocated = static_cast<char*>(malloc(allocation_size));
*aligned_ptr = *allocated;
size_t mod = size_t(*aligned_ptr) % alignment;
if (mod != 0)
{
(*aligned_ptr) += (alignment - mod);
}
}
extern "C" void free_aligned_buffer(void* allocated)
{
free(allocated);
}
#define TI(x) type_index(typeid(x)) #define TI(x) type_index(typeid(x))
static const OpMap dispatcher{ static const OpMap dispatcher{
...@@ -211,29 +193,19 @@ void ExternalFunction::compile() ...@@ -211,29 +193,19 @@ void ExternalFunction::compile()
#include "ngraph/runtime/cpu/cpu_kernels.hpp" #include "ngraph/runtime/cpu/cpu_kernels.hpp"
#include "ngraph/runtime/cpu/eigen_utils.hpp" #include "ngraph/runtime/cpu/eigen_utils.hpp"
#include "ngraph/runtime/cpu/memory_handler.hpp"
using namespace ngraph::runtime::cpu::eigen; using namespace ngraph::runtime::cpu::eigen;
extern "C" void allocate_aligned_buffer(
size_t size,
size_t alignment,
char** allocated,
char** aligned_ptr);
extern "C" void free_aligned_buffer(void* allocated);
)"; )";
TU << "// Declare any functions that are not main\n"; TU << "// Declare all functions\n";
for (shared_ptr<Function> f : pass_manager.get_state().get_functions()) for (shared_ptr<Function> f : pass_manager.get_state().get_functions())
{
if (f != m_function)
{ {
TU << "extern \"C\" void " << f->get_name() << "(\n"; TU << "extern \"C\" void " << f->get_name() << "(\n";
TU << " const std::vector<void*>& inputs,\n"; TU << " const std::vector<void*>& inputs,\n";
TU << " const std::vector<void*>& outputs);\n"; TU << " const std::vector<void*>& outputs);\n";
} }
}
TU << "\n"; TU << "\n";
for (shared_ptr<Function> current_function : pass_manager.get_state().get_functions()) for (shared_ptr<Function> current_function : pass_manager.get_state().get_functions())
...@@ -244,12 +216,10 @@ extern "C" void free_aligned_buffer(void* allocated); ...@@ -244,12 +216,10 @@ extern "C" void free_aligned_buffer(void* allocated);
TU << "{\n"; TU << "{\n";
TU.indent++; TU.indent++;
TU << "// Allocate the memory pool\n";
size_t temp_pool_size = pass_manager.get_state().get_temporary_pool_size(); size_t temp_pool_size = pass_manager.get_state().get_temporary_pool_size();
TU << "char* allocated_buffer_pool;\n"; TU << "// Allocate the memory pool\n";
TU << "char* aligned_buffer_pool;\n"; TU << "ngraph::runtime::cpu::MemoryHandler memory_handler(" << temp_pool_size << ", "
TU << "allocate_aligned_buffer(" << temp_pool_size << ", 64" << ngraph::runtime::cpu::alignment << ");\n";
<< ", &allocated_buffer_pool, &aligned_buffer_pool);\n";
TU << "\n"; TU << "\n";
TU << "// Define temporary tensors\n"; TU << "// Define temporary tensors\n";
...@@ -258,8 +228,8 @@ extern "C" void free_aligned_buffer(void* allocated); ...@@ -258,8 +228,8 @@ extern "C" void free_aligned_buffer(void* allocated);
for (descriptor::Tensor* tensor : node->liveness_new_list) for (descriptor::Tensor* tensor : node->liveness_new_list)
{ {
TU << tensor->get_element_type() << "* " << tensor->get_name() << " = (" TU << tensor->get_element_type() << "* " << tensor->get_name() << " = ("
<< tensor->get_element_type() << "*)(aligned_buffer_pool + " << tensor->get_element_type() << "*)(memory_handler.get_ptr("
<< tensor->get_pool_offset() << ");\n"; << tensor->get_pool_offset() << "));\n";
} }
} }
TU << "\n"; TU << "\n";
...@@ -321,8 +291,6 @@ extern "C" void free_aligned_buffer(void* allocated); ...@@ -321,8 +291,6 @@ extern "C" void free_aligned_buffer(void* allocated);
handler->second(&emitter, node.get(), this, in, out); handler->second(&emitter, node.get(), this, in, out);
} }
TU << "\nfree_aligned_buffer(allocated_buffer_pool);\n";
TU.indent--; TU.indent--;
// End TU // End TU
......
...@@ -20,35 +20,47 @@ ...@@ -20,35 +20,47 @@
using namespace ngraph; using namespace ngraph;
using namespace std; using namespace std;
extern "C" void
allocate_aligned_buffer(size_t size, size_t alignment, char** allocated, char** aligned_ptr);
extern "C" void free_aligned_buffer(void* allocated);
runtime::cpu::CPUTensorView::CPUTensorView(const ngraph::element::Type& element_type, runtime::cpu::CPUTensorView::CPUTensorView(const ngraph::element::Type& element_type,
const Shape& shape) const Shape& shape)
: runtime::TensorView(std::make_shared<ngraph::descriptor::PrimaryTensorView>( : runtime::TensorView(std::make_shared<ngraph::descriptor::PrimaryTensorView>(
std::make_shared<ngraph::TensorViewType>(element_type, shape), "external", true, true)) std::make_shared<ngraph::TensorViewType>(element_type, shape), "external", true, true))
, m_allocated_buffer_pool(nullptr)
, m_aligned_buffer_pool(nullptr)
{ {
m_descriptor->set_tensor_view_layout( m_descriptor->set_tensor_view_layout(
std::make_shared<ngraph::descriptor::layout::DenseTensorViewLayout>(*m_descriptor)); std::make_shared<ngraph::descriptor::layout::DenseTensorViewLayout>(*m_descriptor));
m_buffer_size = m_descriptor->get_tensor_view_layout()->get_size() * element_type.size(); m_buffer_size = m_descriptor->get_tensor_view_layout()->get_size() * element_type.size();
allocate_aligned_buffer(m_buffer_size, runtime::cpu::alignment, &m_allocated, &m_buffer); if (m_buffer_size > 0)
{
size_t allocation_size = m_buffer_size + runtime::cpu::alignment;
m_allocated_buffer_pool = static_cast<char*>(malloc(allocation_size));
m_aligned_buffer_pool = m_allocated_buffer_pool;
size_t mod = size_t(m_aligned_buffer_pool) % alignment;
if (mod != 0)
{
m_aligned_buffer_pool += (alignment - mod);
}
}
} }
runtime::cpu::CPUTensorView::~CPUTensorView() runtime::cpu::CPUTensorView::~CPUTensorView()
{ {
free_aligned_buffer(m_allocated); if (m_allocated_buffer_pool != nullptr)
{
free(m_allocated_buffer_pool);
}
} }
char* runtime::cpu::CPUTensorView::get_data_ptr() char* runtime::cpu::CPUTensorView::get_data_ptr()
{ {
return m_buffer; return m_aligned_buffer_pool;
} }
const char* runtime::cpu::CPUTensorView::get_data_ptr() const const char* runtime::cpu::CPUTensorView::get_data_ptr() const
{ {
return m_buffer; return m_aligned_buffer_pool;
} }
void runtime::cpu::CPUTensorView::write(const void* source, size_t tensor_offset, size_t n) void runtime::cpu::CPUTensorView::write(const void* source, size_t tensor_offset, size_t n)
......
...@@ -52,7 +52,7 @@ public: ...@@ -52,7 +52,7 @@ public:
void read(void* p, size_t tensor_offset, size_t n) const override; void read(void* p, size_t tensor_offset, size_t n) const override;
private: private:
char* m_allocated; char* m_allocated_buffer_pool;
char* m_buffer; char* m_aligned_buffer_pool;
size_t m_buffer_size; size_t m_buffer_size;
}; };
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