Skip to content

Commit

Permalink
dump mem usage (#974)
Browse files Browse the repository at this point in the history
* dump mem usage

* fix build

* Apply code-format changes

* Apply code-format changes

* rewrite mem info collect

* Apply code-format changes

* rewrite

* Apply code-format changes

* fix build

* Apply code-format changes

* fix build

* Apply code-format changes

* fix build

---------

Co-authored-by: yanghaoqi <yanghaoqi_intern@canaan-creative.com>
Co-authored-by: curioyang <curioyang@users.noreply.github.com>
Co-authored-by: huochenghai <huochenghai@canaan-creative.com>
  • Loading branch information
4 people authored Jun 22, 2023
1 parent 35f9925 commit 38f09b1
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 19 deletions.
35 changes: 20 additions & 15 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ option(BUILD_TESTING "Build test programs" OFF)
option(ENABLE_OP_PROFILE "Profile ops cast time" OFF)
option(ENABLE_DUMP_MANAGER "Enable dump manager" OFF)
option(ENABLE_RVV "Some kernel impl by rvv" OFF)
option(ENABLE_DUMP_MEM "Dump mem usage" OFF)

if (ENABLE_OP_PROFILE)
add_definitions(-DENABLE_OP_PROFILE)
endif()

if(ENABLE_DUMP_MEM)
add_definitions(-DDUMP_MEM)
endif()

if(NOT CMAKE_TOOLCHAIN_FILE)
include(toolchains/native.toolchain.cmake)
endif()

if (BUILDING_RUNTIME)
option(ENABLE_VULKAN_RUNTIME "Enable Vulkan runtime" ON)
option(ENABLE_VULKAN_RUNTIME "Enable Vulkan runtime" OFF)
option(ENABLE_K210_RUNTIME "Enable k210 runtime" OFF)
option(DEFAULT_BUILTIN_RUNTIMES "Use default builtin runtimes" ON)
option(DEFAULT_SHARED_RUNTIME_TENSOR_PLATFORM_IMPL "Use default shared memory platform impl" ON)
Expand All @@ -58,7 +63,7 @@ endif()
include(cmake/configure-conan.cmake)
include(cmake/conan.cmake)

if(NOT CONAN_EXPORTED)
if(NOT CONAN_EXPORTED)
conan_check()
conan_add_remote(NAME sunnycase URL https://conan.sunnycase.moe INDEX 0)
endif()
Expand Down Expand Up @@ -86,7 +91,7 @@ if (BUILDING_RUNTIME)
set(NNCASE_INCLUDE_DIR ${CMAKE_CURRENT_BINARY_DIR}/src/Native/include)
set(THIRD_PARTY ${CMAKE_CURRENT_LIST_DIR}/third_party)
set_property(GLOBAL PROPERTY POSITION_INDEPENDENT_CODE ON)

if (MSVC)
add_definitions(/D_CRT_SECURE_NO_WARNINGS /DNOMINMAX)
add_compile_options(/wd4267 /wd4251 /wd4244 /FC /utf-8 /W3 /WX /wd4297 -Wno-unused-function -Wno-unused-command-line-argument)
Expand All @@ -103,10 +108,10 @@ if (BUILDING_RUNTIME)
endif()
endif()
endif()

include_directories(${NNCASE_MAIN_INCLUDE_DIR})
include_directories(${NNCASE_INCLUDE_DIR})

add_subdirectory(src/Native/include/nncase)
# add_subdirectory(src/Native/src/kernels)
# add_subdirectory(src/Native/src/runtime)
Expand All @@ -115,7 +120,7 @@ if (BUILDING_RUNTIME)
if(BUILD_BENCHMARK)
# add_subdirectory(benchmark)
endif()

# Python binding
if(BUILD_PYTHON_BINDING)
# add_subdirectory(python/nncaseruntime/native)
Expand Down Expand Up @@ -152,7 +157,7 @@ if (BUILDING_RUNTIME)
PATTERN "*.inc"
PATTERN "LICENSE.TXT"
)

install(DIRECTORY src/Native/include/nncase/runtime
DESTINATION include/nncase
COMPONENT nncase-headers
Expand All @@ -178,7 +183,7 @@ else()
else ()
set(CMAKE_INSTALL_RPATH "$ORIGIN")
endif()

if (MSVC)
add_definitions(/D_SILENCE_ALL_CXX17_DEPRECATION_WARNINGS /D_CRT_SECURE_NO_WARNINGS /DNOMINMAX)
add_compile_options(/wd4267 /wd4251 /wd4244 /FC /utf-8 /W3 /WX -Wno-unused-function -Wno-unused-command-line-argument)
Expand All @@ -195,10 +200,10 @@ else()
add_link_options(-Wl,--exclude-libs,ALL)
endif()
endif()

include_directories(${NNCASE_MAIN_INCLUDE_DIR})
include_directories(${NNCASE_INCLUDE_DIR})

add_subdirectory(src/Native/include/nncase)
add_subdirectory(src/Native/src)

Expand All @@ -210,11 +215,11 @@ else()
# Csharp binding
if(BUILD_CSHARP_BINDING)
#add_subdirectory(csharp)
endif()
endif()

# Thrid party
# add_subdirectory(third_party/onnx)

install(DIRECTORY src/Native/include/nncase
DESTINATION include
COMPONENT nncase-headers
Expand All @@ -227,7 +232,7 @@ else()
PATTERN "LICENSE.TXT"
PATTERN "*.h"
)

install(DIRECTORY ${NNCASE_INCLUDE_DIR}/nncase
DESTINATION include
COMPONENT nncase-headers
Expand All @@ -241,14 +246,14 @@ else()
PATTERN "config.h" EXCLUDE
PATTERN "*.h"
)

install(DIRECTORY python/nncase
DESTINATION python
COMPONENT nncase-python
FILES_MATCHING
PATTERN "*.py"
)

# Targets
#add_subdirectory(targets/cpu)
#add_subdirectory(targets/k210)
Expand Down
43 changes: 39 additions & 4 deletions src/Native/src/runtime/host_allocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,44 @@
using namespace nncase;
using namespace nncase::runtime;

#ifdef DUMP_MEM
#include <iomanip>
#include <iostream>
static uint64_t used_mem = 0;
static uint64_t max_mem = 0;
#endif
namespace {
class host_buffer_impl : public host_buffer_node {
public:
host_buffer_impl(gsl::byte *data, size_t bytes,
std::function<void(gsl::byte *)> deleter,
uintptr_t physical_address, buffer_allocator &allocator,
host_sync_status_t host_sync_status)
host_sync_status_t host_sync_status,
[[maybe_unused]] bool collect = false)
: host_buffer_node(bytes, allocator, host_sync_status),
data_(std::move(data)),
physical_address_(physical_address),
deleter_(std::move(deleter)) {}
deleter_(std::move(deleter)) {
#ifdef DUMP_MEM
bytes_size_ = bytes;
collect_ = collect;
#endif
}

~host_buffer_impl() { deleter_(data_); }
~host_buffer_impl() {
deleter_(data_);
#ifdef DUMP_MEM
if (collect_) {
if (max_mem < used_mem)
max_mem = used_mem;
std::cout << "[Used_mem]:" << std::setw(16) << std::setfill(' ')
<< used_mem << "\t[deleter ]:" << std::setw(16)
<< std::setfill(' ') << bytes_size_
<< "\t[Max_mem]: " << max_mem << std::endl;
used_mem -= bytes_size_;
}
#endif
}

bool has_physical_address() const noexcept override {
return physical_address_;
Expand Down Expand Up @@ -58,19 +83,29 @@ class host_buffer_impl : public host_buffer_node {
gsl::byte *data_;
uintptr_t physical_address_;
std::function<void(gsl::byte *)> deleter_;
#ifdef DUMP_MEM
size_t bytes_size_;
bool collect_;
#endif
};

class host_buffer_allocator : public buffer_allocator {
public:
result<buffer_t>
allocate([[maybe_unused]] size_t bytes,
[[maybe_unused]] const buffer_allocate_options &options) override {
#ifdef DUMP_MEM
std::cout << "[Used_mem]:" << std::setw(16) << std::setfill(' ')
<< used_mem << "\t[allocate]:" << std::setw(16)
<< std::setfill(' ') << bytes << std::endl;
used_mem += bytes;
#endif
auto data = new (std::nothrow) gsl::byte[bytes];
if (!data)
return err(std::errc::not_enough_memory);
return ok<buffer_t>(object_t<host_buffer_impl>(
std::in_place, data, bytes, [](gsl::byte *p) { delete[] p; }, 0,
*this, host_sync_status_t::valid));
*this, host_sync_status_t::valid, true));
}

result<buffer_t>
Expand Down

0 comments on commit 38f09b1

Please sign in to comment.