I want to test and compare NumPy matrix multiplication and eigendecomposition 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 eigendecompositions.
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 time
def matrix_mul(size, n=100):
# Reference: https://markus-beuckelmann.de/blog/boosting-numpy-blas.html
np.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() - t
print('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() - t
print('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)
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 a 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 to this 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 an 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 isn’t any 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 environment:
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 the 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 separately, so that you can compare the performance of NumPy without MKL and with MKL, respectively.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With