Install Numpy with OneAPI MKL for AMD in Ubuntu

NumPy uses libraries like BLAS, LAPACK, BLIS, or MKL to execute vector, matrix, and linear algebra operations. It’s acknowledged that Intel with MKL (Math Kernel Library) is quite more mature in this math operation than other libraries due to resources and experiences.

If you want to leverage Intel OneAPI MKL as backend for your Numpy, especially on Intel chip (or AMD if you want to try), here are a quick step for installation in Ubuntu (I use the latest ubuntu 23.04 Lunar Lobster).

First, download the required softwares

sudo apt install build-essential python3-pip python3 python3-dev libomp-dev

A context, libomp-dev will help you to avoid the error “Solve undefined symbol: omp_get_num_procs” when importing the numpy libraries in the python interpreter.

Second, we will install the Intel Base-kit:

Or follow the commands below

# download the key to system keyring
wget -O- \
| gpg --dearmor | sudo tee /usr/share/keyrings/oneapi-archive-keyring.gpg > /dev/null

# add signed entry to apt sources and configure the APT client to use Intel repository:
echo "deb [signed-by=/usr/share/keyrings/oneapi-archive-keyring.gpg] all main" | sudo tee /etc/apt/sources.list.d/oneAPI.list

# Execute apt update
sudo apt update

# Install Intel-base kit contain MKL
sudo apt install intel-basekit

Second, load intel libraries in ldconfig

This is to avoid the error “ImportError: cannot open shared object file: No such file or directory”. We create a new file called “mylibs.conf” in /etc/ with path of Intel MKL Libraries

vim /etc/ and put this inside the file

library_dirs = /opt/intel/oneapi/mkl/2023.1.0/lib/intel64

You need to reload the config with

sudo ldconfig

Third, we install numpy with the MKL library backend.

You can choose either with virtual environment or directly combine with existing OS Python in Ubuntu (you must pass some argument in pip installation)

We need to install numpy with configuration libraries point to our installed Intel base kit. In this case, create a new file called “.numpy-site.cfg” in home path

~/.numpy-site.cfg with content

library_dirs = /opt/intel/oneapi/mkl/2023.1.0/lib/intel64
include_dirs = /opt/intel/oneapi/mkl/2023.1.0/include
libraries = mkl_rt

Then, we install numpy from the sources and follow the configuration

pip install numpy --no-binary :all:

Or, you can use official Numpy recommendation (it has no effect for me)

pip install --no-use-pep517 --global-option=build --global-option="--cpu-dispatch=max" numpy --no-binary :all:

Fourth, check if the installation is a success

You can open Python interpreter and import numpy with show config to see if the MKL library is being used

# open terminal and execute python interpreter

# then, import numpy libraries
import numpy

# show the configuration

Bonus: If you have AMD chipset and want to improve the MKL libraries performance

A. Create a new file called fakeintel.c at home path (~/) and put this code and save the file

int mkl_serv_intel_cpu_true() {
  return 1;
$ gcc -shared -fPIC -o fakeintel.c

B. Compile it via terminal

gcc -shared -fPIC -o fakeintel.c

C. Load the libraries with export and change the USERNAME to your own username


Running Benchmark!

Download the numpy benchmark file here

And execute it with “python”


#AMD without LD_PRELOAD + Intel One API 2023

Dotted two 4096x4096 matrices in 0.25 s.
Dotted two vectors of length 524288 in 0.01 ms.
SVD of a 2048x1024 matrix in 0.28 s.
Cholesky decomposition of a 2048x2048 matrix in 0.11 s.
Eigendecomposition of a 2048x2048 matrix in 3.03 s.

#Benchmark TR 24 cores with MKL + Tweaking
Dotted two 4096x4096 matrices in 0.22 s. 
Dotted two vectors of length 524288 in 0.01 ms. 
SVD of a 2048x1024 matrix in 0.24 s. 
Cholesky decomposition of a 2048x2048 matrix in 0.09 s. 
Eigendecomposition of a 2048x2048 matrix in 2.29 s. 

Leave a Reply

Your email address will not be published.