Skip to content

Commit

Permalink
fix openmp affinity abort when cpu goes offline (#4370)
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Nov 26, 2022
1 parent bdcbc37 commit c934c6e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
7 changes: 1 addition & 6 deletions cmake/ncnnConfig.cmake.in
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ if(NCNN_VULKAN)
if(NOT NCNN_SHARED_LIB)
if(NCNN_SYSTEM_GLSLANG)
find_package(glslang QUIET)
if(glslang_FOUND)
add_library(glslang ALIAS glslang::glslang)
add_library(SPIRV ALIAS glslang::SPIRV)
else()
if(NOT glslang_FOUND)
set(GLSLANG_TARGET_DIR "@GLSLANG_TARGET_DIR@")
include(${GLSLANG_TARGET_DIR}/OSDependentTargets.cmake)
include(${GLSLANG_TARGET_DIR}/OGLCompilerTargets.cmake)
Expand All @@ -37,8 +34,6 @@ if(NCNN_VULKAN)
else()
set(glslang_DIR "${CMAKE_CURRENT_LIST_DIR}/../../../@CMAKE_INSTALL_LIBDIR@/cmake/glslang")
find_package(glslang QUIET)
add_library(glslang ALIAS glslang::glslang)
add_library(SPIRV ALIAS glslang::SPIRV)
endif()

endif()
Expand Down
2 changes: 2 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,8 @@ if(NCNN_OPENMP)
elseif(ANDROID_NDK_MAJOR AND (ANDROID_NDK_MAJOR GREATER 20))
target_compile_options(ncnn PRIVATE -fopenmp)
target_link_libraries(ncnn PUBLIC -fopenmp -static-openmp)
# see cpu.cpp __wrap___kmp_abort_process comment for the linker magic
target_link_libraries(ncnn PUBLIC -Wl,-wrap,__kmp_affinity_determine_capable)
elseif(OpenMP_CXX_FOUND)
target_link_libraries(ncnn PUBLIC OpenMP::OpenMP_CXX)
else()
Expand Down
20 changes: 19 additions & 1 deletion src/cpu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1704,7 +1704,7 @@ static int setup_thread_affinity_masks()
{
int max_freq_khz = get_max_freq_khz(i);

// NCNN_LOGE("%d max freq = %d khz", i, max_freq_khz);
// NCNN_LOGE("%d max freq = %d khz", i, max_freq_khz);

cpu_max_freq_khz[i] = max_freq_khz;

Expand Down Expand Up @@ -2041,3 +2041,21 @@ int set_flush_denormals(int flush_denormals)
}

} // namespace ncnn

#if defined __ANDROID__ && defined(_OPENMP) && __clang__
#ifdef __cplusplus
extern "C" {
#endif
void __wrap___kmp_affinity_determine_capable(const char* /*env_var*/)
{
// the internal affinity routines in llvm openmp call abort on __NR_sched_getaffinity / __NR_sched_setaffinity fails
// ref KMPNativeAffinity::get_system_affinity/set_system_affinity in openmp/runtime/src/kmp_affinity.h
// and cpu core goes offline in powersave mode on android, which triggers abort
// ATM there is no known api for controlling the abort behavior
// override __kmp_affinity_determine_capable with empty body to disable affinity regardless of KMP_AFFINITY env_var
// ugly hack works >.< --- nihui
}
#ifdef __cplusplus
} // extern "C"
#endif
#endif

0 comments on commit c934c6e

Please sign in to comment.