Using mkl_set_num_threads with numpy

numpy no multithreading
numpy+mkl
intel mkl set num threads
python mkl install windows
python libmkl
python num_threads
omp_num_threads jupyter
import mkl

I'm trying to set the number of threads for numpy calculations with mkl_set_num_threads like this

import numpy
import ctypes
mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_rt.mkl_set_num_threads(4)

but I keep getting an segmentation fault:

Program received signal SIGSEGV, Segmentation fault.
0x00002aaab34d7561 in mkl_set_num_threads__ () from /../libmkl_intel_lp64.so

Getting the number of threads is no problem:

print mkl_rt.mkl_get_max_threads()

How can I get my code working? Or is there another way to set the number of threads at runtime?

Ophion led me the right way. Despite the documentation, one have to transfer the parameter of mkl_set_num_thread by reference.

Now I have defined to functions, for getting and setting the threads

import numpy
import ctypes
mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_get_max_threads = mkl_rt.mkl_get_max_threads
def mkl_set_num_threads(cores):
    mkl_rt.mkl_set_num_threads(ctypes.byref(ctypes.c_int(cores)))

mkl_set_num_threads(4)
print mkl_get_max_threads() # says 4

and they work as expected.

Edit: according to Rufflewind, the names of the C-Functions are written in capital-case, which expect parameters by value:

import ctypes

mkl_rt = ctypes.CDLL('libmkl_rt.so')
mkl_set_num_threads = mkl_rt.MKL_Set_Num_Threads
mkl_get_max_threads = mkl_rt.MKL_Get_Max_Threads

Configure number of processors available to MKL NumPy, MKL uses the best number of threads by default, e.g. (on Windows):. view source​print? 1, > python -c "import ctypes;  mkl_set_num_threads_local() Note A call to the mkl_set_num_threads or mkl_domain_set_num_threads function changes the number of OpenMP threads available to all in-progress calls (in concurrent threads) and future calls to Intel® MKL and may result in slow Intel® MKL performance and/or race conditions reported by run-time tools, such as Intel® Inspector.

Set number of threads after numpy import · Issue #11826 · numpy , It is not difficult to do in a simple script when we are sure that Numpy or .intel.​com/en-us/mkl-developer-reference-c-mkl-set-num-threads. import numpy import ctypes mkl_rt = ctypes.CDLL('libmkl_rt.so') mkl_get_max_threads = mkl_rt.mkl_get_max_threads def mkl_set_num_threads(cores): mkl_rt.mkl_set_num_threads(ctypes.byref(ctypes.c_int(cores))) mkl_set_num_threads(4) print mkl_get_max_threads() # says 4 and they work as expected.

For people looking for the complete solution, you can use a context manager:

import ctypes


class MKLThreads(object):
    _mkl_rt = None

    @classmethod
    def _mkl(cls):
        if cls._mkl_rt is None:
            try:
                cls._mkl_rt = ctypes.CDLL('libmkl_rt.so')
            except OSError:
                cls._mkl_rt = ctypes.CDLL('mkl_rt.dll')
        return cls._mkl_rt

    @classmethod
    def get_max_threads(cls):
        return cls._mkl().mkl_get_max_threads()

    @classmethod
    def set_num_threads(cls, n):
        assert type(n) == int
        cls._mkl().mkl_set_num_threads(ctypes.byref(ctypes.c_int(n)))

    def __init__(self, num_threads):
        self._n = num_threads
        self._saved_n = self.get_max_threads()

    def __enter__(self):
        self.set_num_threads(self._n)
        return self

    def __exit__(self, type, value, traceback):
        self.set_num_threads(self._saved_n)

Then use it like:

with MKLThreads(2):
    # do some stuff on two cores
    pass

Or just manipulating configuration by calling following functions:

# Example
MKLThreads.set_num_threads(3)
print(MKLThreads.get_max_threads())

Code is also available in this gist.

Better way to set number of threads used by NumPy, What are the pitfalls of my approach? Calling the functions setting the maximum number of threads to use change a global state inside the  Specifies the number of OpenMP* threads to use. mkl_set_num_threads. Intel® Math Kernel Library 2017 Developer Reference - C. mkl_set_num_threads. Specifies the number of OpenMP* threads to use.

For people looking for a cross platform and packaged solution, note that we have recently released threadpoolctl, a module to limit the number of threads used in C-level threadpools called by python (OpenBLAS, OpenMP and MKL). See this answer for more info.

Paste DbContext with Autofac, import numpy import ctypes mkl_rt = ctypes. source share. In short, use MKL_Set_Num_Threads and his friends CamelCased when calling MKL from Python. This commit adds the following methods in numpy.linalg: * get_num_threads () returns the number of threads used by BLAS. * set_num_threads (n) sets the number of threads which will be used by further calls to BLAS. * num_threads (n) is a context manager to temporarily change the number of threads which will be used by BLAS.

python - mkl_set_num_threads con numpy, import numpy import ctypes mkl_rt = ctypes. Este truco permite que la biblioteca MKL se use con la mayoría de los compiladores Fortran sin ninguna  NumPy stores values using its own data types, which are distinct from Python types like float and str. This is because the core of NumPy is written in a programming language called C, which stores data differently than the Python data types. NumPy data types map between Python and C, allowing us to use NumPy arrays without any conversion hitches.

MKL Optimizations, Using mkl_set_num_threads with numpy. I'm trying to set the number of threads for numpy calculations with mkl_set_num_threads like this import numpy import  Now I have defined to functions, for getting and setting the threads import numpy import ctypes mkl_rt = ctypes.CDLL('libmkl_rt.so') mkl_get_max_threads = mkl_rt.mkl_get_max_threads def mkl_set_num_threads(cores): mkl_rt.mkl_set_num_threads(ctypes.byref(ctypes.c_int(cores))) mkl_set_num_threads(4) print mkl_get_max_threads() # says 4

Speed-boosted NumPy, SciPy, scikit-learn, and NumExpr; The packaging of MKL with redistributable binaries in Anaconda for easy access to the MKL runtime  Statsmodels is using joblib in a few places for parallel processing where it's under our control. Current usage is mainly for bootstrap and it is not used in the models directly. However, some of the underlying Blas/Lapack libraries in numpy/scipy also use mutliple cores.

Comments
  • When I had the Enthought Python distribution I could import mkl as a module. I wonder what happened behind the scenes.