In short, use MKL_Set_Num_Threads and his friends CamelCased when calling MKL from Python. The same applies to C if you are not #include <mkl.h> .
MKL documentation seems to suggest that the correct type signature is in C:
void mkl_set_num_threads(int nt);
Ok, let him try the minimal program:
void mkl_set_num_threads(int); int main(void) { mkl_set_num_threads(1); return 0; }
Compile it with GCC and arrow, Segmentation fault again. So the problem is not limited to Python.
Running through the debugger (GDB) shows:
Program received signal SIGSEGV, Segmentation fault. 0x0000β¦ in mkl_set_num_threads_ () from /β¦/mkl/lib/intel64/libmkl_intel_lp64.so
Wait a second, mkl_set_num_threads_ ? This is the version of Fortran MKL_Set_Num_Threads ! How did we eventually call the Fortran version? (Keep in mind that the Fortran calling convention requires arguments to be passed as pointers, not by value.)
Turns out the documentation was a complete facade. If you are really looking at the header files for the latest MKL versions, you will find this rather small definition:
void MKL_Set_Num_Threads(int nth); #define mkl_set_num_threads MKL_Set_Num_Threads
... and now everything makes sense! The correct call function (for C code) is MKL_Set_Num_Threads , not MKL_Set_Num_Threads . Checking the symbol table reveals that there are actually four different options:
nm -D /β¦/mkl/lib/intel64/libmkl_rt.so | grep -i mkl_set_num_threads 00000000000e3060 T MKL_SET_NUM_THREADS β¦ 00000000000e30b0 T MKL_Set_Num_Threads β¦ 00000000000e3060 T mkl_set_num_threads 00000000000e3060 T mkl_set_num_threads_ β¦
Why did Intel deliver four different variants of one function, despite the fact that the documentation contains only variants of C and Fortran? I do not know for sure, but I suspect that it is compatible with different Fortran compilers. You see, the Fortran appointment agreement is not standardized. Different compilers will change names differently:
- some use uppercase
- some use lower case with underscore and
- some use lowercase without any decoration.
There may be other ways that I do not know about. This trick allows the MKL library to be used with most Fortran compilers without any changes, the disadvantage is that C functions must be crippled to make room for the three variants of the Fortran assignment convention.