How to enable and disable Intel MKL in numpy Python?

2024/9/8 9:29:37

I want to test and compare Numpy matrix multiplication and Eigen decomposition performance with Intel MKL and without Intel MKL.

I have installed MKL using pip install mkl (Windows 10 (64-bit), Python 3.8).

I then used examples from here for matmul and eigen decompositions.

How do I now enable and disable MKL in order to check numpy performance with MKL and without it?

Reference code:

import numpy as np
from time import timedef matrix_mul(size, n=100):# reference: https://markus-beuckelmann.de/blog/boosting-numpy-blas.htmlnp.random.seed(112)a, b = np.random.random((size, size)), np.random.random((size, size))t = time()for _ in range(n):np.dot(a, b)delta = time() - tprint('Dotted two matrices of size %dx%d in %0.4f ms.' % (size, size, delta / n * 1000))def eigen_decomposition(size, n=10):np.random.seed(112)a = np.random.random((size, size))t = time()for _ in range(n):np.linalg.eig(a)delta = time() - tprint('Eigen decomposition of size %dx%d in %0.4f ms.' % (size, size, delta / n * 1000))#Obtaining computation times: for i in range(20): eigen_decomposition(500)for i in range(20): matrix_mul(500)
Answer

You can use different environments for the comparison of Numpy with and without MKL. In each environment you can install the needed packages(numpy with MKL or without) using package installer. Then on that environments you can run your program to compare the performance of Numpy with and without MKL.

NumPy doesn’t depend on any other Python packages, however, it does depend on an accelerated linear algebra library - typically Intel MKL or OpenBLAS.

  • The NumPy wheels on PyPI, which is what pip installs, are built with OpenBLAS.

  • In the conda defaults channel, NumPy is built against Intel MKL. MKL is a separate package that will be installed in the users' environment when they install NumPy.

  • When a user installs NumPy from conda-forge, that BLAS package then gets installed together with the actual library.But it can also be MKL (from the defaults channel), or even BLIS or reference BLAS.

Please refer this link to know about installing Numpy in detail.

You can create two different environments to compare the NumPy performance with MKL and without it. In the first environment install the stand-alone NumPy (that is, the NumPy without MKL) and in the second environment install the one with MKL.

To create environment using NumPy without MKL.

conda create -n <env_name_1> python=<version>
conda activate <env_name_1>
pip install numpy

But depending on your OS, it might be possible that there is no distribution available (Windows).

On Windows, we have always been linking against MKL. However, with the Anaconda 2.5 release we separated the MKL runtime into its own conda package, in order to do things uniformly on all platforms.

In general you can create a new env:

conda create -n wheel_based python
activate wheel
pip install numpy-1.13.3-cp36-none-win_amd64.whl  # or whatever the file is named

In the other environment, install NumPy with MKL using below command

conda create -n <env_name_2> python=<version>
conda activate <env_name_2>
pip install intel-numpy

In these environments <env_name_1> and <env_name_2> you can run your program seperately, so that you can compare the performance of Numpy without MKL and With MKL respectively.

https://en.xdnf.cn/q/73326.html

Related Q&A

getdefaultlocale returning None when running sync.db on Django project in PyCharm

OSX 10.7.3, PyCharm version 2.5 build PY 117.200Ill run through how I get the error:I start a new project Create a new VirtualEnv and select Python 2.7 as my base interpreter (leave inherit global pack…

Redirecting an old URL to a new one with Flask micro-framework

Im making a new website to replace a current one, using Flask micro-framework (based on Werkzeug) which uses Python (2.6 in my case).The core functionality and many pages are the same. However by using…

python decimals - rounding to nearest whole dollar (no cents) - with ROUND_HALF_UP

Im trying to use Decimal.quantize() to achieve the following: -For any amount of money, expressed as a python decimal of default precision, I want to round it using decimal.ROUND_HALF_UP so that it has…

How to use pytest fixtures in a decorator without having it as argument on the decorated function

I was trying to use a fixture in a decorator which is intended to decorate test functions. The intention is to provide registered test data to the test. There are two options:Automatic import Manual im…

Including Python standard libraries in your distribution [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Using watchdog of python to monitoring afp shared folder from linux

I want linux machine(Raspberry pi) to monitor a shared folder by AFP(Apple file protocol, macbook is host).I can mount shared folder by mount_afp, and installed watchdog python library to monitor a sha…

Fitting curve: why small numbers are better?

I spent some time these days on a problem. I have a set of data:y = f(t), where y is very small concentration (10^-7), and t is in second. t varies from 0 to around 12000.The measurements follow an est…

Fast numpy roll

I have a 2d numpy array and I want to roll each row in an incremental fashion. I am using np.roll in a for loop to do so. But since I am calling this thousands of times, my code is really slow. Can you…

IndexError: fail to coerce slice entry of type tensorvariable to integer

I run "ipython debugf.py" and it gave me error message as belowIndexError Traceback (most recent call last) /home/ml/debugf.py in <module>() 8 fff = …

How to detect lines in noisy line images?

I generate noisy images with certain lines in them, like this one:Im trying to detect the lines using OpenCV, but something is going wrong.Heres my code so far, including the code to generate the noisy…