From 6e3db02825cad8941dfd58618d759d86fec51fd0 Mon Sep 17 00:00:00 2001 From: nihui Date: Fri, 25 Nov 2022 16:00:32 +0800 Subject: [PATCH 1/3] Update cpu.cpp --- src/cpu.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/cpu.cpp b/src/cpu.cpp index 59c9bf4f985..5e1fc4e0c6a 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -1178,6 +1178,15 @@ int cpu_riscv_vlenb() static int get_cpucount() { +#if defined(_OPENMP) && __clang__ + // 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 mobile os, which triggers abort + // ATM there is no known api for controlling the abort behavior, disable kmp affinity capability anyway + // we shall set env before any omp calls, so here may be a good place --- nihui + kmp_set_defaults("KMP_AFFINITY=disabled"); +#endif + int count = 0; #ifdef __EMSCRIPTEN__ if (emscripten_has_threading_support()) From a15b6147763f892bde1cf6b284ed4c05bce4abb9 Mon Sep 17 00:00:00 2001 From: nihuini Date: Fri, 25 Nov 2022 17:38:54 +0800 Subject: [PATCH 2/3] another hack --- src/CMakeLists.txt | 2 ++ src/cpu.cpp | 26 +++++++++++++++++--------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 13ccd5c68a7..dbf25531293 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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_abort_process) elseif(OpenMP_CXX_FOUND) target_link_libraries(ncnn PUBLIC OpenMP::OpenMP_CXX) else() diff --git a/src/cpu.cpp b/src/cpu.cpp index 5e1fc4e0c6a..a3171fc4ad6 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -1178,15 +1178,6 @@ int cpu_riscv_vlenb() static int get_cpucount() { -#if defined(_OPENMP) && __clang__ - // 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 mobile os, which triggers abort - // ATM there is no known api for controlling the abort behavior, disable kmp affinity capability anyway - // we shall set env before any omp calls, so here may be a good place --- nihui - kmp_set_defaults("KMP_AFFINITY=disabled"); -#endif - int count = 0; #ifdef __EMSCRIPTEN__ if (emscripten_has_threading_support()) @@ -2050,3 +2041,20 @@ int set_flush_denormals(int flush_denormals) } } // namespace ncnn + +#if defined __ANDROID__ && defined(_OPENMP) && __clang__ +#ifdef __cplusplus +extern "C" { +#endif +void __wrap___kmp_abort_process(void) +{ + // 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_abort_process with empty body, ugly hack works o_O --- nihui +} +#ifdef __cplusplus +} // extern "C" +#endif +#endif From 3658db7b549deff531a1dedb611ee407ea13ec23 Mon Sep 17 00:00:00 2001 From: nihui Date: Sat, 26 Nov 2022 15:18:36 +0800 Subject: [PATCH 3/3] another approach --- cmake/ncnnConfig.cmake.in | 7 +------ src/CMakeLists.txt | 2 +- src/cpu.cpp | 7 ++++--- 3 files changed, 6 insertions(+), 10 deletions(-) diff --git a/cmake/ncnnConfig.cmake.in b/cmake/ncnnConfig.cmake.in index 9679ccc09fd..528c69da0ad 100644 --- a/cmake/ncnnConfig.cmake.in +++ b/cmake/ncnnConfig.cmake.in @@ -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) @@ -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() diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index dbf25531293..077a7eb524f 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -227,7 +227,7 @@ if(NCNN_OPENMP) 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_abort_process) + target_link_libraries(ncnn PUBLIC -Wl,-wrap,__kmp_affinity_determine_capable) elseif(OpenMP_CXX_FOUND) target_link_libraries(ncnn PUBLIC OpenMP::OpenMP_CXX) else() diff --git a/src/cpu.cpp b/src/cpu.cpp index a3171fc4ad6..85c65335ccc 100644 --- a/src/cpu.cpp +++ b/src/cpu.cpp @@ -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; @@ -2046,13 +2046,14 @@ int set_flush_denormals(int flush_denormals) #ifdef __cplusplus extern "C" { #endif -void __wrap___kmp_abort_process(void) +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_abort_process with empty body, ugly hack works o_O --- nihui + // 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"