Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix openmp affinity abort when cpu goes offline #4370

Merged
merged 4 commits into from
Nov 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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