Commit a2829ba2 authored by Robert Kimball's avatar Robert Kimball Committed by Scott Cyphers

remove ngraph::aligned_alloc and replaced with AlignedBuffer (#2740)

* remove ngraph::aligned_alloc and replaced with AlignedBuffer

* allocate a minimum sized buffer is zero size is requested

* move memory alignment value to static constant

* move static constant to constexpr function
parent 86641478
......@@ -54,10 +54,6 @@ string to_cpp_string(T value)
op::Constant::~Constant()
{
if (m_data)
{
aligned_free(m_data);
}
}
vector<string> op::Constant::get_value_strings() const
......@@ -161,12 +157,12 @@ vector<string> op::Constant::get_value_strings() const
shared_ptr<Node> op::Constant::copy_with_new_args(const NodeVector& new_args) const
{
check_new_args_count(this, new_args);
return make_shared<Constant>(m_element_type, m_shape, m_data);
return make_shared<Constant>(m_element_type, m_shape, m_data->get_ptr());
}
shared_ptr<op::Constant> op::ScalarConstantLikeBase::as_constant() const
{
return std::make_shared<op::Constant>(m_element_type, m_shape, m_data);
return std::make_shared<op::Constant>(m_element_type, m_shape, m_data->get_ptr());
}
std::shared_ptr<Node> op::ScalarConstantLike::copy_with_new_args(const NodeVector& new_args) const
......@@ -179,7 +175,7 @@ void op::ScalarConstantLike::infer_element_type()
m_element_type = get_input_element_type(0);
if (nullptr == m_data)
{
m_data = ngraph::aligned_alloc(m_element_type.size(), m_element_type.size());
m_data.reset(new runtime::AlignedBuffer(m_element_type.size(), m_element_type.size()));
write_values(std::vector<double>(1, m_value));
}
}
......
......@@ -19,8 +19,8 @@
#include <cstring>
#include <sstream>
#include "ngraph/log.hpp"
#include "ngraph/node.hpp"
#include "ngraph/runtime/aligned_buffer.hpp"
#include "ngraph/type/bfloat16.hpp"
#include "ngraph/type/element_type.hpp"
#include "ngraph/util.hpp"
......@@ -44,8 +44,8 @@ namespace ngraph
: Node("Constant", {})
, m_element_type(type)
, m_shape(shape)
, m_data(ngraph::aligned_alloc(m_element_type.size(),
shape_size(m_shape) * m_element_type.size()))
, m_data(new runtime::AlignedBuffer(shape_size(m_shape) * m_element_type.size(),
host_alignment()))
{
NODE_VALIDATION_CHECK(
this,
......@@ -80,8 +80,8 @@ namespace ngraph
: Node("Constant", {})
, m_element_type(type)
, m_shape(shape)
, m_data(ngraph::aligned_alloc(m_element_type.size(),
shape_size(m_shape) * m_element_type.size()))
, m_data(new runtime::AlignedBuffer(shape_size(m_shape) * m_element_type.size(),
host_alignment()))
{
NODE_VALIDATION_CHECK(
this,
......@@ -112,8 +112,9 @@ namespace ngraph
, m_data(nullptr)
{
size_t size = shape_size(m_shape) * m_element_type.size();
m_data = ngraph::aligned_alloc(m_element_type.size(), size);
std::memcpy(m_data, data, size);
m_data.reset(new runtime::AlignedBuffer(shape_size(m_shape) * m_element_type.size(),
host_alignment()));
std::memcpy(m_data->get_ptr(), data, size);
constructor_validate_and_infer_types();
}
......@@ -168,7 +169,7 @@ namespace ngraph
}
std::vector<T> rc;
const T* p = reinterpret_cast<const T*>(m_data);
const T* p = reinterpret_cast<const T*>(m_data->get_ptr());
for (size_t i = 0; i < shape_size(m_shape); i++)
{
rc.push_back(p[i]);
......@@ -176,15 +177,16 @@ namespace ngraph
return rc;
}
const void* get_data_ptr() const { return m_data; }
const void* get_data_ptr() const { return (m_data ? m_data->get_ptr() : nullptr); }
template <typename T>
const T* get_data_ptr() const
{
return reinterpret_cast<T*>(m_data);
return reinterpret_cast<const T*>(get_data_ptr());
}
bool is_constant() const override { return true; }
protected:
void* get_data_ptr_nc() { return (m_data ? m_data->get_ptr() : nullptr); }
Constant(const std::string& name, const NodeVector& args)
: Node(name, args)
, m_shape({})
......@@ -195,7 +197,8 @@ namespace ngraph
template <typename T>
void write_values(const std::vector<T>& values)
{
write_to_buffer(m_element_type, m_shape, values, m_data, shape_size(m_shape));
write_to_buffer(
m_element_type, m_shape, values, get_data_ptr_nc(), shape_size(m_shape));
}
template <typename T, typename U>
......@@ -273,9 +276,10 @@ namespace ngraph
}
}
static constexpr size_t host_alignment() { return 64; }
element::Type m_element_type;
Shape m_shape{};
void* m_data{nullptr};
std::unique_ptr<runtime::AlignedBuffer> m_data;
Constant(const Constant&) = delete;
Constant operator=(const Constant&) = delete;
};
......
......@@ -14,6 +14,7 @@
// limitations under the License.
//*****************************************************************************
#include <algorithm>
#include <memory>
#include "ngraph/runtime/aligned_buffer.hpp"
......@@ -30,23 +31,15 @@ runtime::AlignedBuffer::AlignedBuffer()
runtime::AlignedBuffer::AlignedBuffer(size_t byte_size, size_t alignment)
{
m_byte_size = byte_size;
if (m_byte_size > 0)
{
size_t allocation_size = m_byte_size + alignment;
m_allocated_buffer = static_cast<char*>(ngraph_malloc(allocation_size));
m_aligned_buffer = m_allocated_buffer;
size_t mod = size_t(m_aligned_buffer) % alignment;
m_byte_size = std::max<size_t>(1, byte_size);
size_t allocation_size = m_byte_size + alignment;
m_allocated_buffer = static_cast<char*>(ngraph_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
if (mod != 0)
{
m_allocated_buffer = nullptr;
m_aligned_buffer = nullptr;
m_aligned_buffer += (alignment - mod);
}
}
......
......@@ -160,26 +160,6 @@ size_t ngraph::hash_combine(const std::vector<size_t>& list)
return seed;
}
void* ngraph::aligned_alloc(size_t alignment, size_t size)
{
#ifdef __APPLE__
return new uint64_t[round_up(size, sizeof(uint64_t)) / sizeof(uint64_t)];
#elif defined _WIN32
return new uint64_t[round_up(size, sizeof(uint64_t)) / sizeof(uint64_t)];
#else
return ::aligned_alloc(alignment, size);
#endif
}
void ngraph::aligned_free(void* p)
{
#ifdef __APPLE__
delete[] reinterpret_cast<uint64_t*>(p);
#else
free(p);
#endif
}
void* ngraph::ngraph_malloc(size_t size)
{
auto ptr = malloc(size);
......
......@@ -184,9 +184,6 @@ namespace ngraph
void check_fp_values_isnan(const char* name, const float* array, size_t n);
void check_fp_values_isnan(const char* name, const double* array, size_t n);
void* aligned_alloc(size_t alignment, size_t size);
void aligned_free(void*);
void* ngraph_malloc(size_t size);
void ngraph_free(void*);
......
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