Skip to content

Commit

Permalink
[luci-interpreter] Add StaticMemoryManager
Browse files Browse the repository at this point in the history
This commit adds static memory manager.

ONE-DCO-1.0-Signed-off-by: Artem Balyshev a.balyshev@partner.samsung.com
  • Loading branch information
Balyshev Artem committed Sep 23, 2021
1 parent 5a79780 commit ce5c21c
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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.
*/

#ifndef LUCI_INTERPRETER_STATIC_MEMORY_MANAGER_H
#define LUCI_INTERPRETER_STATIC_MEMORY_MANAGER_H

#include "luci_interpreter/MemoryManager.h"

namespace luci_interpreter
{

// Used for allocations in static buffer, using offsets.
class StaticMemoryManager : public IMemoryManager
{
public:
StaticMemoryManager() = delete;

explicit StaticMemoryManager(uint8_t *buffer_ptr) { _buffer_ptr = buffer_ptr; }

~StaticMemoryManager() override { delete[] _buffer_ptr; }

void allocate_memory(luci_interpreter::Tensor &tensor) final;
void release_memory(luci_interpreter::Tensor &tensor) final;

private:
// Stores a pointer to the beginning of the allocated memory buffer.
uint8_t *_buffer_ptr;
};

} // namespace luci_interpreter

#endif // LUCI_INTERPRETER_STATIC_MEMORY_MANAGER_H
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,10 @@ class Tensor

bool is_data_allocated() const { return _data_allocated; }

int32_t get_offset() { return _offset; }

void set_offset(int32_t offset) { _offset = offset; }

private:
DataType _element_type;
Shape _shape;
Expand All @@ -172,6 +176,9 @@ class Tensor
// Kernel configuration could disable allocation of some tensors if they are not needed for
// particular operation.
bool _is_allocatable = true;
// Used by static memory manager.
// Stores the offset from the beginning of the allocated memory buffer.
int32_t _offset = -1;
};

} // namespace luci_interpreter
Expand Down
3 changes: 2 additions & 1 deletion compiler/luci-interpreter/src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ set(SOURCES
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/Interpreter.h"
Interpreter.cpp "${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/SimpleMemoryManager.h" SimpleMemoryManager.cpp
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/TestMemoryManager.h" TestMemoryManager.cpp
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/BuddyMemoryManager.h" BuddyMemoryManager.cpp)
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/BuddyMemoryManager.h" BuddyMemoryManager.cpp
"${LUCI_INTERPRETER_INCLUDE_DIR}/luci_interpreter/StaticMemoryManager.h" StaticMemoryManager.cpp)

if (NOT LUCI_INTERPRETER_STATIC)
add_library(${LUCI_INTERPRETER_BINARY} SHARED ${SOURCES})
Expand Down
43 changes: 43 additions & 0 deletions compiler/luci-interpreter/src/StaticMemoryManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Copyright (c) 2021 Samsung Electronics Co., Ltd. All Rights Reserved
*
* 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 "luci_interpreter/StaticMemoryManager.h"

namespace luci_interpreter
{

void StaticMemoryManager::allocate_memory(luci_interpreter::Tensor &tensor)
{
if (!tensor.is_allocatable())
{
return;
}
if (tensor.is_data_allocated())
{
release_memory(tensor);
}
int32_t offset = tensor.get_offset();
assert(offset >= 0);
auto tensor_ptr = _buffer_ptr + offset;
tensor.set_data_buffer(tensor_ptr);
}

void StaticMemoryManager::release_memory(luci_interpreter::Tensor &tensor)
{
tensor.set_data_buffer(nullptr);
}

} // namespace luci_interpreter

0 comments on commit ce5c21c

Please sign in to comment.