【问题标题】:Avoid OpenMP RTL mismatch if MKL and OpenMP are used in cmake如果在 cmake 中使用 MKL 和 OpenMP,请避免 OpenMP RTL 不匹配
【发布时间】:2020-06-05 20:28:32
【问题描述】:

我偶然发现了一个无法通过阅读 FindBLAS 和 FindOpenMP 的文档立即解决的问题。也许这里有人对我有提示。 假设我有一个项目,它导入了一个具有 BLAS 依赖关系的接口目标 A。 FindBLAS 确定 MKL 非常合适。现在,在我的 CMakeLists.txt 中,我构建了另一个需要 OpenMP 的目标 B。然后我将A链接为B。 我的具体问题是在以 GCC 作为编译器的 Linux 上,FindBLAS 使用英特尔的 OpenMP 实现并相应地设置 MKL 的接口层(这也是 MKL Link Advisor 所建议的)。 FindOpenMP 当然回退到 libgomp 进行链接。在链接期间,我最终使用了两个运行时库,即 libgomp 和 libiomp,我觉得这有点危险。这里有一个简化的 CMakeLists.txt 来说明问题:

cmake_minimum_required(VERSION 3.12)
project(test LANGUAGES C CXX)

find_package(BLAS REQUIRED)
find_package(OpenMP REQUIRED)

# Dummy interface libary A that depends on MKL
add_library(a INTERFACE)
target_include_directories(a INTERFACE include/)
target_link_libraries(a INTERFACE "${BLAS_LIBRARIES}")
target_link_options(a INTERFACE "${LINKER_FLAGS}")

# target B that depends on OpenMP and A
add_executable(b src/b.cc)
target_link_libraries(b PRIVATE OpenMP::OpenMP_CXX a)

输出如下:

– The C compiler identification is GNU 9.1.0
– The CXX compiler identification is GNU 9.1.0
– Check for working C compiler: /opt/bwhpc/common/compiler/gnu/9.1.0/bin/gcc
– Check for working C compiler: /opt/bwhpc/common/compiler/gnu/9.1.0/bin/gcc – works
– Detecting C compiler ABI info
– Detecting C compiler ABI info - done
– Detecting C compile features
– Detecting C compile features - done
– Check for working CXX compiler: /opt/bwhpc/common/compiler/gnu/9.1.0/bin/g++
– Check for working CXX compiler: /opt/bwhpc/common/compiler/gnu/9.1.0/bin/g++ – works
– Detecting CXX compiler ABI info
– Detecting CXX compiler ABI info - done
– Detecting CXX compile features
– Detecting CXX compile features - done
– Looking for sgemm_
– Looking for sgemm_ - not found
– Looking for pthread.h
– Looking for pthread.h - found
– Looking for pthread_create
– Looking for pthread_create - not found
– Looking for pthread_create in pthreads
– Looking for pthread_create in pthreads - not found
– Looking for pthread_create in pthread
– Looking for pthread_create in pthread - found
– Found Threads: TRUE
– Looking for sgemm_
– Looking for sgemm_ - found
– Found BLAS: /opt/bwhpc/common/compiler/intel/compxe.2018.5.274/compilers_and_libraries_2018.5.274/linux/mkl/lib/intel64_lin/libmkl_intel_lp64.so;/opt/bwhpc/common/compiler/intel/compxe.2018.5.274/compilers_and_libraries_2018.5.274/linux/mkl/lib/intel64_lin/libmkl_intel_thread.so;/opt/bwhpc/common/compiler/intel/compxe.2018.5.274/compilers_and_libraries_2018.5.274/linux/mkl/lib/intel64_lin/libmkl_core.so;/opt/bwhpc/common/compiler/intel/compxe.2018.5.274/compilers_and_libraries_2018.5.274/linux/compiler/lib/intel64_lin/libiomp5.so;-lpthread;-lm;-ldl
– Found OpenMP_C: -fopenmp (found version “4.5”)
– Found OpenMP_CXX: -fopenmp (found version “4.5”)
– Found OpenMP: TRUE (found version “4.5”)
– Configuring done
– Generating done

建筑产量:

$ VERBOSE=1 make
/pfs/data1/software_uc1/bwhpc/common/devel/cmake/3.14.0/bin/cmake -S/home/hd/hd_hd/hd_em426/psidm2 -B/home/hd/hd_hd/hd_em426/psidm2/build --check-build-system CMakeFiles/Makefile.cmake 0
/pfs/data1/software_uc1/bwhpc/common/devel/cmake/3.14.0/bin/cmake -E cmake_progress_start /home/hd/hd_hd/hd_em426/psidm2/build/CMakeFiles /home/hd/hd_hd/hd_em426/psidm2/build/CMakeFiles/progress.marks
make -f CMakeFiles/Makefile2 all
make[1]: Entering directory /pfs/data2/home/hd/hd_hd/hd_em426/psidm2/build' make -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/depend make[2]: Entering directory /pfs/data2/home/hd/hd_hd/hd_em426/psidm2/build’
cd /home/hd/hd_hd/hd_em426/psidm2/build && /pfs/data1/software_uc1/bwhpc/common/devel/cmake/3.14.0/bin/cmake -E cmake_depends “Unix Makefiles” /home/hd/hd_hd/hd_em426/psidm2 /home/hd/hd_hd/hd_em426/psidm2 /home/hd/hd_hd/hd_em426/psidm2/build /home/hd/hd_hd/hd_em426/psidm2/build /home/hd/hd_hd/hd_em426/psidm2/build/CMakeFiles/b.dir/DependInfo.cmake --color=
Dependee “/home/hd/hd_hd/hd_em426/psidm2/build/CMakeFiles/b.dir/DependInfo.cmake” is newer than depender “/home/hd/hd_hd/hd_em426/psidm2/build/CMakeFiles/b.dir/depend.internal”.
Dependee “/home/hd/hd_hd/hd_em426/psidm2/build/CMakeFiles/CMakeDirectoryInformation.cmake” is newer than depender “/home/hd/hd_hd/hd_em426/psidm2/build/CMakeFiles/b.dir/depend.internal”.
Scanning dependencies of target b
make[2]: Leaving directory /pfs/data2/home/hd/hd_hd/hd_em426/psidm2/build' make -f CMakeFiles/b.dir/build.make CMakeFiles/b.dir/build make[2]: Entering directory /pfs/data2/home/hd/hd_hd/hd_em426/psidm2/build’
[ 50%] Building CXX object CMakeFiles/b.dir/src/b.cc.o
/opt/bwhpc/common/compiler/gnu/9.1.0/bin/g++ -I/home/hd/hd_hd/hd_em426/psidm2/include -fopenmp -o CMakeFiles/b.dir/src/b.cc.o -c /home/hd/hd_hd/hd_em426/psidm2/src/b.cc
[100%] Linking CXX executable b
/pfs/data1/software_uc1/bwhpc/common/devel/cmake/3.14.0/bin/cmake -E cmake_link_script CMakeFiles/b.dir/link.txt --verbose=1
/opt/bwhpc/common/compiler/gnu/9.1.0/bin/g++ CMakeFiles/b.dir/src/b.cc.o -o b /pfs/data1/software_uc1/bwhpc/common/compiler/gnu/9.1.0/lib64/libgomp.so -lpthread -lmkl_intel_lp64 -lmkl_intel_thread -lmkl_core /opt/bwhpc/common/compiler/intel/compxe.2018.5.274/compilers_and_libraries_2018.5.274/linux/compiler/lib/intel64_lin/libiomp5.so -lpthread -lm -ldl

所以我想我的问题是:这是 FindBLAS 的预期行为吗?如果 FindBLAS 检测到 GNU 编译器,那么 FindBLAS 选择 MKL 的 GNU 接口并使用 libgomp 会不会更好,这样与 FindOpenMP 混合不会导致上面讨论的问题?

【问题讨论】:

    标签: linux gcc cmake openmp intel-mkl


    【解决方案1】:

    LLVM(和 Intel)OpenMP 运行时具有允许 GCC 编译的 OPenMP 代码与它一起运行的垫片*。因此,您的最佳解决方案可能是:到处链接 libiomp5。

    避免在此过程中同时使用这两个库是绝对正确的。除了潜在的不正确执行之外,这可能导致超额订阅(线程多于逻辑 CPU)和性能不佳。

    * 如果您使用卸载到其他设备(例如 GPU)可能会出现问题,但对于仅 CPU 代码,GCC 兼容性是 LLVM/Intel 运行时的关键目标,正是为了让您可以轻松解决此问题。

    【讨论】:

      猜你喜欢
      • 2018-03-06
      • 2012-05-25
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 2014-09-29
      • 1970-01-01
      • 2019-02-08
      • 1970-01-01
      相关资源
      最近更新 更多