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

used something other than aligned_alloc for mac (#316)

* used something other than aligned_alloc for mac

* Fix compile error for aligned_free on Mac
parent 833a8f14
......@@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// ----------------------------------------------------------------------------
#include "ngraph/ops/constant.hpp"
#include <cstdio>
#include "ngraph/log.hpp"
#include "ngraph/ops/constant.hpp"
#include "ngraph/util.hpp"
using namespace ngraph;
......@@ -23,7 +25,7 @@ op::Constant::~Constant()
{
if (m_data)
{
free(m_data);
ngraph::aligned_free(m_data);
}
}
......
......@@ -55,8 +55,8 @@ namespace ngraph
: Node("Constant", {})
, m_element_type(type)
, m_shape(shape)
, m_data(aligned_alloc(m_element_type.size(),
shape_size(m_shape) * m_element_type.size()))
, m_data(ngraph::aligned_alloc(m_element_type.size(),
shape_size(m_shape) * m_element_type.size()))
{
auto vt = std::make_shared<TensorViewType>(type, shape);
set_value_type_checked(vt);
......@@ -84,8 +84,8 @@ namespace ngraph
: Node("Constant", {})
, m_element_type(type)
, m_shape(shape)
, m_data(aligned_alloc(m_element_type.size(),
shape_size(m_shape) * m_element_type.size()))
, m_data(ngraph::aligned_alloc(m_element_type.size(),
shape_size(m_shape) * m_element_type.size()))
{
auto vt = std::make_shared<TensorViewType>(type, shape);
set_value_type_checked(vt);
......@@ -110,7 +110,7 @@ namespace ngraph
, m_data(nullptr)
{
size_t size = shape_size(m_shape) * m_element_type.size();
m_data = aligned_alloc(m_element_type.size(), size);
m_data = ngraph::aligned_alloc(m_element_type.size(), size);
memcpy(m_data, data, size);
auto vt = std::make_shared<TensorViewType>(type, shape);
set_value_type_checked(vt);
......
......@@ -368,3 +368,37 @@ std::shared_ptr<ngraph::Function> ngraph::clone_function(std::shared_ptr<ngraph:
return std::make_shared<ngraph::Function>(
cloned_result, func->get_result_type(), cloned_params);
}
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)];
#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
}
size_t ngraph::round_up(size_t size, size_t alignment)
{
if (alignment == 0)
{
return size;
}
size_t remainder = size % alignment;
if (remainder == 0)
{
return size;
}
return size + alignment - remainder;
}
......@@ -278,4 +278,8 @@ namespace ngraph
// NodeMap output (by reference) fully maps input and cloned function ops
std::shared_ptr<ngraph::Function> clone_function(std::shared_ptr<ngraph::Function> func,
NodeMap& node_map);
void* aligned_alloc(size_t alignment, size_t size);
void aligned_free(void*);
size_t round_up(size_t size, size_t alignment);
} // end namespace ngraph
......@@ -329,3 +329,13 @@ TEST_F(CloneTest, clone_function_full)
auto cloned_func = clone_function(func, node_map);
ASSERT_TRUE(CompareNodes(func->get_ops(), cloned_func->get_ops(), node_map));
}
TEST(util, round_up)
{
EXPECT_EQ(0, round_up(0, 4));
EXPECT_EQ(4, round_up(1, 4));
EXPECT_EQ(4, round_up(2, 4));
EXPECT_EQ(4, round_up(3, 4));
EXPECT_EQ(4, round_up(4, 4));
EXPECT_EQ(8, round_up(5, 4));
}
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