如何在Windows上使用Cmake和mingw构建MKL?

问题描述 投票:0回答:1

我正在尝试构建一个 C 项目,其中使用 Intel 的数学内核库 (MKL) 来加速矩阵乘法,如下所示:

#define MKL
#ifdef MKL
    #include <mkl.h>
#endif
void gemm_cpu(int TA, int TB, int M, int N, int K, float ALPHA, float *A, int lda, float *B, int ldb, float BETA, float *C, int ldc)
{
#ifdef MKL
    if (!TA && !TB)
        cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, ALPHA, A, lda, B, ldb, BETA, C, ldc);
    else if (TA && !TB)
        cblas_sgemm(CblasRowMajor, CblasTrans, CblasNoTrans, M, N, K, ALPHA, A, lda, B, ldb, BETA, C, ldc);
    else if (!TA && TB)
        cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, M, N, K, ALPHA, A, lda, B, ldb, BETA, C, ldc);
    else
        cblas_sgemm(CblasRowMajor, CblasTrans, CblasTrans, M, N, K, ALPHA, A, lda, B, ldb, BETA, C, ldc);
#else
    for (int i = 0; i < N * M; ++i)
        C[i] *= BETA;
    if (!TA && !TB)
        gemm_nn(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
    else if (TA && !TB)
        gemm_tn(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
    else if (!TA && TB)
        gemm_nt(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
    else
        gemm_tt(M, N, K, ALPHA, A, lda, B, ldb, C, ldc);
#endif
}

我使用的是 Windows 10,使用 CLion 作为 IDE,并使用 gcc.exe(MinGW-W64 x86_64-ucrt-posix-seh,由 Brecht Sanders 构建)12.2.0 作为编译器。不幸的是,我的构建失败并且无法链接 MKL。我用于构建 MKL 的 cmake 如下:

# The Mkl has to be installed locally
set(external_folder "D:/Users/superkogito/Desktop/workspace/project/external")
set(mkl_dir "${external_folder}/mkl")

# link options provided by https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor
message(STATUS "[MKL] MKL path: ${mkl_dir}.")

# 64bits
set(mkl_libraries
        "${mkl_dir}/lib/intel64/mkl_intel_lp64.lib"
        "${mkl_dir}/lib/intel64/mkl_core.lib"
        "${mkl_dir}/lib/intel64/mkl_sequential.lib"
        )

set(mkl_dlls
        "${mkl_dir}/lib/intel64/mkl_intel_lp64_dll.lib"
        "${mkl_dir}/lib/intel64/mkl_core_dll.lib"
        "${mkl_dir}/lib/intel64/mkl_sequential_dll.lib"
        )


add_library(mkl INTERFACE)
add_compile_definitions(MKL_LP64)
add_compile_definitions(function=cblas_sgemm)

target_link_directories(mkl INTERFACE "${mkl_dir}/lib/intel64")
target_link_libraries(mkl INTERFACE ${mkl_dlls})
target_include_directories(mkl INTERFACE "${mkl_dir}/include")

## install mkl libs
INSTALL(FILES ${mkl_libraries} DESTINATION lib)

# Also install the dll on windows
install(FILES ${mkl_dlls} DESTINATION lib)

我为我的 Cmake 文件尝试了不同的配置,但我总是不断收到像这样的

undefined reference to ..
错误:

Warning: .drectve `-defaultlib:"uuid.lib" ' unrecognized
Warning: corrupt .drectve at end of def file
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.text[sgemm]+0x219): undefined reference to `__security_check_cookie'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.text[sgemm]+0x37c): undefined reference to `__security_check_cookie'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.xdata+0x1c): undefined reference to `__GSHandlerCheck'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.xdata+0x3c): undefined reference to `__GSHandlerCheck'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.text[sgemm_64]+0x1a8): undefined reference to `__security_check_cookie'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.text[sgemm_64]+0x2fc): undefined reference to `__security_check_cookie'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.xdata+0x1c): undefined reference to `__GSHandlerCheck'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(_sgemm_lp64.obj):(.xdata+0x3c): undefined reference to `__GSHandlerCheck'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_printf_s]+0x54): undefined reference to `__stdio_common_vfprintf'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_fprintf_s]+0x78): undefined reference to `__stdio_common_vsnprintf_s'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_fprintf_s]+0xb8): undefined reference to `__security_check_cookie'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.xdata+0x10): undefined reference to `__GSHandlerCheck'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_fprintf_stdout_s]+0x54): undefined reference to `__stdio_common_vfprintf'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_vfprintf_s]+0x2e): undefined reference to `__stdio_common_vfprintf'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_vfprintf_stdout_s]+0x38): undefined reference to `__stdio_common_vfprintf'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_sprintf_s]+0x5f): undefined reference to `__stdio_common_vsnprintf_s'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_sprintf_char_s]+0x65): undefined reference to `__stdio_common_vsnprintf_s'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_vsprintf_s]+0x43): undefined reference to `__stdio_common_vsnprintf_s'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_vsnprintf_s]+0x48): undefined reference to `__stdio_common_vsnprintf_s'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_snprintf_s]+0x5e): undefined reference to `__stdio_common_vsnprintf_s'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_sscanf_s]+0x54): undefined reference to `__stdio_common_vsscanf'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_fscanf_s]+0x46): undefined reference to `__stdio_common_vfscanf'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_vfprintf_stderr_s]+0x38): undefined reference to `__stdio_common_vfprintf'
D:/wecode_build_tools/mingw/bin/../lib/gcc/x86_64-w64-mingw32/9.3.0/../../../../x86_64-w64-mingw32/bin/ld.exe: D:/Users/superkogito/Desktop/workspace/cpp/audio_framework_v2/plugin/my_plugins/ImGuiExample/external/mkl/lib/intel64/mkl_intel_lp64_dll.lib(mkl_libc.obj):(.text[mkl_serv_fprintf_stderr_s]+0x54): undefined reference to `__stdio_common_vfprintf'
collect2.exe: error: ld returned 1 exit status
ninja: build stopped: subcommand failed.

这似乎是由于 MKL 和 mingw 之间不兼容造成的,如以下链接所述:

但我希望有人有补丁,或者也许因为我只需要

cblas_sgemm
,我也许能够围绕不兼容性进行构建。我主要需要避免使用 Visual Studio,因此如果有解决方法,我想听听。

windows gcc cmake mingw-w64 intel-mkl
1个回答
0
投票

此外,由于 MKL 源代码已关闭,我无法从源代码构建,因此我希望获得补丁或确认这是不可能的。

我在文档中没有看到任何暗示对

MinGW
的任何支持。看起来唯一支持的平台是 Windows、Linux 和 MacOS。

我主要需要避免使用 Visual Studio,因此如果有解决方法,我想听听。

我会考虑使用

Visual Studio Code
它对于跨平台 CMake C++ 开发来说绝对是可爱的。

但是由于您已经在使用

clion
,值得指出的是您可以将 MSVC 工具链与 CLion 一起使用:

https://www.jetbrains.com/help/clion/quick-tutorial-on-configuring-clion-on-windows.html#MSVC

这将使您能够与 Intel 提供的

pre-built
Windows 二进制文件兼容。

© www.soinside.com 2019 - 2024. All rights reserved.