MKL example code compiles fine with cmake but crashes when run

Viewed 721

I am trying to run following example from MKL website.

#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.

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:

-------------------------------------------------------------------
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.

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

Sample mean of normal distribution = 4.985218

I am pretty sure I am missing something in the CMakeLists.txt file. 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). It works fine if I first open the oneAPI command prompt and then run the exe. My guess is that there is some environment variable or runtime library which is not available to the exe. If so, what should I change so that the exe can run on its own (without the need of running oneAPI command prompt)?

2 Answers

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. Or just put libiomp5md.dll into exe file directory.

My guess is that you're observing binary incompatibility between 32-bit and 64-bit libraries. You're linking against mkl_intel_ilp64.lib, but you don't have the symbol MKL_ILP64 defined. In particular, this macro defines the size of MKL_INT type: it has 64 bits when MKL_ILP64 is defined and 32 bits otherwise. No wonder that trying to call ILP64 library functions results in a runtime crash.

Add the following line to your CMakeLists.txt file:

target_compile_definitions(rndGen PUBLIC MKL_ILP64)

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

Related