Search code examples
pythonnumpymatlabintel-mkl

Intel MKL ERROR when callyng Python from Matlab


I've always been working in Python but now I have to share a function with a colleague working in Matlab. Since the thing is quite complex, calls compiled C code, etc. I thought it was easier to just call the Python code from Matlab.

Everything seems fine up to this line: points = np.random.multivariate_normal(means, correlation, nSamples)

I get the errors:

Intel MKL ERROR: Parameter 12 was incorrect on entry to DGBBRDM.

Intel MKL ERROR: Parameter 10 was incorrect on entry to DGESDD.
init_gesdd failed init

and the execution continues smoothly with points being an array of zeroes.

Of course when running the Python code directly (from the same conda env) I get my multivariate normal sample.

Searching around and here I find only solutions that mention how the code was compiled, how it links and the interface that may be C or Fortran. In here I didn't compile anything, and I don't understand why the intermediate Matlab step should mess with the libraries used by numpy.

I found something related on the Anaconda guide, but seems Windows specific and mentions different software than Matlab, so I'm not even sure is the same problem.

An answer to a (maybe) similar question suggested

conda config --add channels intel
conda update --all

but this didn't help either.

Overall it doesn't seem a convenient way to share some function with a colleague....

Edit

I'm using Ubuntu 22.04.2 LTS and, if it matters, is running on 11th Gen Intel® Core™ i7-1165G7 @ 2.80GHz × 8, Python 3.9.12, MatlabR2022b.

2 steps to reproduce:

  • file testMatlabIntel.py contains:
      import numpy as np
      def test():
          means = [0,0]
          correlation = [[1,0.3],[0.3,1]]
          nSamples = 300
          points = np.random.multivariate_normal(means, correlation, Samples)
          return np.std(points)
    
  • open Matlab from the terminal, if needed add the path to the python file in the python path (insert(py.sys.path,int32(0),'path/to/file');) then run:
      py.testMatlabIntel.test()
    
    The output in the Matlab prompt is:
      /path/to/file/testMatlabIntel.py:7: RuntimeWarning: covariance is not positive-semidefinite.
        points = np.random.multivariate_normal(means, correlation, Samples)
    
      ans =
    
           0
    
    while on the terminal I used to launch Matlab I see:
      Intel MKL ERROR: Parameter 12 was incorrect on entry to DGBBRDM.
    
      Intel MKL ERROR: Parameter 10 was incorrect on entry to DGESDD.
    

Note that is not just an array with zero variance but an array of zeros.

By contrast on a different machine (Ubuntu 20.04.6 LTS, Intel® Core™ i5-8250U CPU @ 1.60GHz × 8) I get simply:

  ans =

      1.0136

and no warning as calling the test function from Python and as expected from a unitary variance sample.

Edit 2

Machine running Ubuntu 22.04.2 LTS:

    Python: 3.9.16 (main, Feb 22 2023, 02:08:36) 
    [GCC 11.2.0]

    Numpy: 1.23.5

    MKL: Intel(R) oneAPI Math Kernel Library Version 2023.1-Product Build 20230303 for Intel(R) 64 architecture applications

I double-checked that the same version is called both from the terminal and Matlab.

From the terminal, when calling Python I get [GCC 11.2.0] :: Intel Corporation on linux

Also, setting MKL_VERBOSE=1 it prints:

    MKL_VERBOSE DGESDD(A,8589934594,8589934594,0x7fbfc9a789b0,8589934594,0x7fbfc9a789d0,0x7fbfc9a789e0,8589934594,0x7fbfc9a78a00,-4294967294,0x7fc12b3f9010,1382979469311,0x7fbfc9a78a20,-10) 580.03us CNR:OFF Dyn:1 FastMM:1 TID:0  NThr:4
    init_gesdd failed init
    MKL_VERBOSE DGEMM(N,T,2,2,2,0x7fc12b3fabc0,0x7fbfc9a991e0,2,0x7fbfc9a97600,2,0x7fc12b3fabc8,0x7fbfc9685760,2) 131.85us CNR:OFF Dyn:1 FastMM:1 TID:0  NThr:4
    MKL_VERBOSE DGEMM(N,N,2,300,2,0x7fc12b3fabc0,0x7fbfc9685760,2,0x7fc05d198a00,2,0x7fc12b3fabc8,0x7fc05d199cd0,2) 15.14us CNR:OFF Dyn:1 FastMM:1 TID:0  NThr:4

If this can be of any help.

On the other machine (Ubuntu 20.04.6 LTS), from inside Matlab I get:

    Python: 3.9.16 (main, Mar  8 2023, 14:08:28) 
    [GCC 11.2.0]
    
    Numpy: 1.23.5

    MKL: Intel(R) oneAPI Math Kernel Library Version 2023.1-Product Build 20230303 for Intel(R) 64 architecture applications

From the terminal, when calling Python I get [GCC 11.2.0] :: Anaconda, Inc. on linux

Summarising:

works doesn't
Ubuntu 20.04.6 LTS 22.04.2 LTS
Matlab R2023a R2022b
Python 3.9.16 3.9.16
From INTEL? No? Yes
Numpy 1.23.5 1.23.5
MKL 2023.1 2023.1

Solution

  • I had a similar problem. The solution was to add the following line to the start up file:

    py.sys.setdlopenflags(int32(bitor(int64(py.os.RTLD_LAZY),int64(py.os.RTLD_DEEPBIND))));