文章

Intel TBB malloc 使用 (windows)

CMake 查找 Intel TBB

1
2
3
4
5
6
7
8
find_package(
    TBB
    COMPONENTS tbb tbbmalloc tbbmalloc_proxy
    REQUIRED)

if(TBB_FOUND)
    message(STATUS "TBB version: ${TBB_VERSION}")
endif()

链接引入 TBB 库

1
2
3
4
if (MSVC)
    target_link_libraries(${target_lib} PRIVATE TBB::tbb TBB::tbbmalloc
                                                TBB::tbbmalloc_proxy)
endif()

cpp 代码中引入 TBB 符号,防止库链接被优化掉

1
2
3
4
5
6
#if defined(WITH_TBB_MALLOC) && defined(_MSC_VER) //&& defined(NDEBUG)
#include "oneapi/tbb/scalable_allocator.h"
#include "oneapi/tbb/tbbmalloc_proxy.h"
#pragma comment(lib, "tbbmalloc_proxy.lib")
#pragma comment(linker, "/include:__TBB_malloc_proxy")
#endif

查看内存分配函数替换是否出错

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#if defined(WITH_TBB_MALLOC) && defined(_MSC_VER) //&& defined(NDEBUG)
  {
    // https://emfomenk.github.io/versions/latest/elements/oneTBB/source/memory_allocation/c_interface_to_scalable_allocator.html
    const auto mode_ret = scalable_allocation_mode(TBBMALLOC_USE_HUGE_PAGES, 1);
    if (mode_ret == TBBMALLOC_NO_EFFECT) {
      spdlog::warn("huge pages not supported by OS");
    }
    char** func_replacement_log;
    int func_replacement_status = TBB_malloc_replacement_log(&func_replacement_log);
    if (func_replacement_status != 0) {
      printf("tbbmalloc_proxy cannot replace memory allocation routines\n");
      for (char** log_string = func_replacement_log; *log_string != 0; log_string++) {
        spdlog::warn("{}", *log_string);
      }
    }
  }
#endif

资料

本文由作者按照 CC BY 4.0 进行授权