Skip to Main Content

How to use MKL on maya

Introduction

MKL (Math Kernel Library) is a library of math procedures from Intel, which includes BLAS, LAPACK, and ScaLAPACK. On this page, we will show how to start using MKL with the Intel compiler on maya. Before you begin, make sure to read the tutorial for compiling C programs. Extensive documentation about programming with MKL can be found in Intel’s reference manual.

BLAS Example: Matrix Multiply

Consider three matrices Am×k, Bk×n, and Cm×n, where all are represented as double precision floating point numbers. We will demonstrate the use of the “dgemm” function which computes

C=αAB+βC.

We will take α=1 and β=0 to compute the simple matrix multiplication C=AB. MKL has several versions of this function; we will use “cblas_dgemm”, which has bit friendlier “C-style” interface than other variants.

The most important line in the code is

cblas_dgemm(CblasColMajor, CblasNoTrans, CblasNoTrans,
   m, n, k, 1.0, A, m, B, k, 0.0, C, m);

Also notice in the Makefile that the option “-mkl” is given in CFLAGS, which enables the use of MKL. Next we compile and run the code.

[araim1@maya-usr1 matrix-multply]$ make clean
rm -f *.o matrix-multiply
[araim1@maya-usr1 matrix-multply]$ make
icc -O3 -std=c99 -mkl   -c matrix-multiply.c -o matrix-multiply.o
icc -O3 -std=c99 -mkl   matrix-multiply.o -o matrix-multiply  
[araim1@maya-usr1 matrix-multply]$ ./matrix-multiply 
Matrix A (3 x 5) is:
  1.0000  0.7290  0.5314  0.3874  0.2824
  0.9000  0.6561  0.4783  0.3487  0.2542
  0.8100  0.5905  0.4305  0.3138  0.2288

Matrix B (5 x 4) is:
  1.0000  0.5905  0.3487  0.2059
  0.9000  0.5314  0.3138  0.1853
  0.8100  0.4783  0.2824  0.1668
  0.7290  0.4305  0.2542  0.1501
  0.6561  0.3874  0.2288  0.1351

Matrix C (3 x 4) = AB is:
  2.5543  1.5083  0.8906  0.5259
  2.2989  1.3575  0.8016  0.4733
  2.0690  1.2217  0.7214  0.4260
[araim1@maya-usr1 matrix-multply]$

For more information about matrix multiplication with MKL, see this tutorial from Intel.

LAPACK Example: SVD

Given a matrix Am×n, we will demonstrate the LAPACK “dgesvd” function for computing the singular value decomposition (SVD)

A=USVT,

where Sm×n is a diagonal matrix of singular values, Um×m, and VTn×n. The MKL function call we will use is “LAPACKE_dgesvd”.

The most important line in the code, where the SVD is being computed, is

int info = LAPACKE_dgesvd(CblasColMajor, 'A', 'A', m, n, A, m, S, U, m,
   VT, n, superb);

Next we compile and run the code.

[araim1@maya-usr1 svd]$ make
icc -O3 -std=c99 -mkl   -c -o matrix-svd.o matrix-svd.c
icc -O3 -std=c99 -mkl   matrix-svd.o -o matrix-svd  
[araim1@maya-usr1 svd]$ ./matrix-svd 
Matrix A (3 x 4) is:
  1.0000  0.5000  0.3333  0.2500
  0.5000  0.3333  0.2500  0.2000
  0.3333  0.2500  0.2000  0.1667

Matrix U (3 x 3) is:
 -0.8199  0.5563  0.1349
 -0.4662 -0.5123 -0.7213
 -0.3322 -0.6543  0.6794

Vector S (3 x 1) is:
  1.4519
  0.1433
  0.0042

Matrix VT (4 x 4) is:
 -0.8015 -0.4466 -0.3143 -0.2435
  0.5729 -0.3919 -0.5127 -0.5053
  0.1692 -0.7398  0.1245  0.6392
 -0.0263  0.3157 -0.7892  0.5261
[araim1@maya-usr1 svd]$