简体   繁体   English

MKL 示例代码使用 cmake 编译良好,但运行时崩溃

[英]MKL example code compiles fine with cmake but crashes when run

I am trying to run following example from MKL website.我正在尝试从 MKL 网站运行以下示例。

#include <stdio.h>
#include "mkl_vsl.h"
 
int main()
{
   double r[1000]; /* buffer for random numbers */
   double s; /* average */
   VSLStreamStatePtr stream;
   int i, j;
    
   /* Initializing */        
   s = 0.0;
   vslNewStream( &stream, VSL_BRNG_MT19937, 777 );
    
   /* Generating */        
   for ( i=0; i<10; i++ ) {
      vdRngGaussian( VSL_RNG_METHOD_GAUSSIAN_ICDF, stream, 1000, r, 5.0, 2.0 );
      for ( j=0; j<1000; j++ ) {
         s += r[j];
      }
   }
   s /= 10000.0;
    
   /* Deleting the stream */        
   vslDeleteStream( &stream );
    
   /* Printing results */        
   printf( "Sample mean of normal distribution = %f\n", s );
    
   return 0;
}

I am using following CMakeLists.txt file to compile the code.我正在使用以下 CMakeLists.txt 文件来编译代码。

cmake_minimum_required(VERSION 3.0.0)
project(rndGen VERSION 0.1.0)

add_executable(rndGen rndGenTest.cpp)

set(CMAKE_VERBOSE_MAKEFILE ON)
set(MKLROOT "C:/Program\ Files\ (x86)/Intel/oneAPI/mkl/latest")
set(MKLLIB "${MKLROOT}/lib/intel64")

target_include_directories(rndGen
PUBLIC ${MKLROOT}/include
)

target_link_libraries(rndGen
PUBLIC ${MKLLIB}/mkl_intel_ilp64.lib
PUBLIC ${MKLLIB}/mkl_intel_thread.lib
PUBLIC ${MKLLIB}/mkl_core.lib
PUBLIC "C:/Program\ Files\ (x86)/Intel/oneAPI/compiler/latest/windows/compiler/lib/intel64_win/libiomp5md.lib"
)

set(CPACK_PROJECT_NAME ${PROJECT_NAME})
set(CPACK_PROJECT_VERSION ${PROJECT_VERSION})
include(CPack)

As stated in the title, the code crashes.如标题所述,代码崩溃。 The debugger output from VSCode is as following: VSCode 的调试器 output 如下:

-------------------------------------------------------------------
You may only use the C/C++ Extension for Visual Studio Code
with Visual Studio Code, Visual Studio or Visual Studio for Mac
software to help you develop and test your applications.
-------------------------------------------------------------------
Loaded 'C:\mklTest\build\Debug\rndGen.exe'. Symbols loaded.
Loaded 'C:\Windows\System32\ntdll.dll'. 
Loaded 'C:\Windows\System32\kernel32.dll'. 
Loaded 'C:\Windows\System32\KernelBase.dll'. 
Loaded 'C:\Windows\System32\vcruntime140d.dll'. 
Loaded 'C:\Windows\System32\ucrtbased.dll'. 
The program '[16972] rndGen.exe' has exited with code -1073741515 (0xc0000135).

However, the code runs fine when compiled manually in cmd using following command.但是,当使用以下命令在 cmd 中手动编译时,代码运行良好。

cl /EHsc rndGenTest.cpp -I "C:\Program Files (x86)\Intel\oneAPI\mkl\latest\include" "C:\Program Files (x86)\Intel\oneAPI\mkl\2021.1.1\lib\intel64\mkl_intel_ilp64.lib" "C:\Program Files (x86)\Intel\oneAPI\mkl\2021.1.1\lib\intel64\mkl_intel_thread.lib" "C:\Program Files (x86)\Intel\oneAPI\mkl\2021.1.1\lib\intel64\mkl_core.lib" "C:\Program Files (x86)\Intel\oneAPI\compiler\latest\windows\compiler\lib\intel64_win\libiomp5md.lib"

The output of the code is代码的output是

Sample mean of normal distribution = 4.985218

I am pretty sure I am missing something in the CMakeLists.txt file.我很确定我在 CMakeLists.txt 文件中遗漏了一些东西。 Can someone point me in the right direction?有人可以指出我正确的方向吗? Thanks in advance.提前致谢。

EDIT编辑

Thanks for the comments and answers.感谢您的评论和回答。 I tried all of them and none worked.我尝试了所有这些,但都没有奏效。 I tried to dig a little bit deeper to find the exact cause of problem.我试图更深入地挖掘问题的确切原因。 Apparently, the code compiles fine both ways.显然,代码编译得很好。 However, it doesn't work if the exe file is executed directly (executed in cmd directly).但是直接执行exe文件就不行了(直接在cmd中执行)。 It works fine if I first open the oneAPI command prompt and then run the exe.如果我首先打开 oneAPI 命令提示符然后运行 exe,它工作正常。 My guess is that there is some environment variable or runtime library which is not available to the exe.我的猜测是某些环境变量或运行时库对 exe 不可用。 If so, what should I change so that the exe can run on its own (without the need of running oneAPI command prompt)?如果是这样,我应该更改什么以便 exe 可以自行运行(无需运行 oneAPI 命令提示符)?

in that case, you may try to relink the test with sequential mkl's library (mkl_sequential.lib instead of mkl_intel_thread.lib ) and try to execute directly in cmd.在这种情况下,您可以尝试使用顺序 mkl 的库(mkl_sequential.lib 而不是 mkl_intel_thread.lib)重新链接测试并尝试直接在 cmd 中执行。 Or just put libiomp5md.dll into exe file directory.或者只是将 libiomp5md.dll 放入 exe 文件目录。

My guess is that you're observing binary incompatibility between 32-bit and 64-bit libraries.我的猜测是您正在观察 32 位和 64 位库之间的二进制不兼容性。 You're linking against mkl_intel_ilp64.lib , but you don't have the symbol MKL_ILP64 defined.您正在链接mkl_intel_ilp64.lib ,但您没有定义符号MKL_ILP64 In particular, this macro defines the size of MKL_INT type: it has 64 bits when MKL_ILP64 is defined and 32 bits otherwise.特别是,这个宏定义了MKL_INT类型的大小:当定义MKL_ILP64时它有 64 位,否则有 32 位。 No wonder that trying to call ILP64 library functions results in a runtime crash.难怪尝试调用ILP64库函数会导致运行时崩溃。

Add the following line to your CMakeLists.txt file:将以下行添加到您的 CMakeLists.txt 文件中:

target_compile_definitions(rndGen PUBLIC MKL_ILP64)

https://software.intel.com/sites/products/mkl/mkl_link_line_advisor.htm https://software.intel.com/sites/products/mkl/mkl_link_line_advisor.htm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM