icc和mkl的安装与编译

icc的安装

  1. 注册intel官网账号,进入该网址安装https://registrationcenter-download.intel.com/akdlm/irc_nas/17928/l_dpcpp-cpp-compiler_p_2021.3.0.3168_offline.sh

  2. 在linux中执行:

    1
    2
    3
    4
    5
    6
    ./l_dpcpp-cpp-compiler_p_2021.3.0.3168_offline.sh
    #如果没有权限可以
    chmod +x l_dpcpp-cpp-compiler_p_2021.3.0.3168_offline.sh
    ./l_dpcpp-cpp-compiler_p_2021.3.0.3168_offline.sh
    #或者使用sh命令
    sh l_dpcpp-cpp-compiler_p_2021.3.0.3168_offline.sh
  3. 然后根据图形化界面一步步安装

  4. 检验是否安装成功

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    wuhlan3@ubuntu:~/Desktop/matrix$ . /opt/intel/oneapi/setvars.sh

    :: initializing oneAPI environment ...
    bash: BASH_VERSION = 5.0.17(1)-release
    :: compiler -- latest
    :: debugger -- latest
    :: dev-utilities -- latest
    :: mkl -- latest
    :: tbb -- latest
    :: oneAPI environment initialized ::

mkl的安装

mkl,即英特尔数学核心函数库Intel Math Kernel Library

之前不懂oneapi怎么安装,所以先使用apt安装了mkl~😑

1.直接安装官网上的.sh文件

https://registrationcenter-download.intel.com/akdlm/irc_nas/17901/l_onemkl_p_2021.3.0.520_offline.sh

与icc的安装方法类似

2.apt的安装方法

1
2
3
4
5
6
cd /tmp
wget https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS-2019.PUB
sudo sh -c 'echo deb https://apt.repos.intel.com/mkl all main /etc/apt/sources.list.d/intel-mkl.list'
sudo apt-get update
sudo apt-get install intel-mkl-64bit-2020.2
source /opt/intel/compilers_and_libraries_2020/linux/mkl/bin/mklvars.sh intel64 ilp64

测试程序

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#define min(x,y) (((x) < (y)) ? (x) : (y))

#include <stdio.h>
#include <stdlib.h>
#include "mkl.h"

int main()
{
double *A, *B, *C;
int m, n, k, i, j;
double alpha, beta;

printf ("\n This example computes real matrix C=alpha*A*B+beta*C using \n"
" Intel(R) MKL function dgemm, where A, B, and C are matrices and \n"
" alpha and beta are double precision scalars\n\n");

m = 2000, k = 200, n = 1000;
printf (" Initializing data for matrix multiplication C=A*B for matrix \n"
" A(%ix%i) and matrix B(%ix%i)\n\n", m, k, k, n);
alpha = 1.0; beta = 0.0;

printf (" Allocating memory for matrices aligned on 64-byte boundary for better \n"
" performance \n\n");
A = (double *)mkl_malloc( m*k*sizeof( double ), 64 );
B = (double *)mkl_malloc( k*n*sizeof( double ), 64 );
C = (double *)mkl_malloc( m*n*sizeof( double ), 64 );
if (A == NULL || B == NULL || C == NULL) {
printf( "\n ERROR: Can't allocate memory for matrices. Aborting... \n\n");
mkl_free(A);
mkl_free(B);
mkl_free(C);
return 1;
}

printf (" Intializing matrix data \n\n");
for (i = 0; i < (m*k); i++) {
A[i] = (double)(i+1);
}

for (i = 0; i < (k*n); i++) {
B[i] = (double)(-i-1);
}

for (i = 0; i < (m*n); i++) {
C[i] = 0.0;
}

printf (" Computing matrix product using Intel(R) MKL dgemm function via CBLAS interface \n\n");
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans,
m, n, k, alpha, A, k, B, n, beta, C, n);
printf ("\n Computations completed.\n\n");

printf (" Top left corner of matrix A: \n");
for (i=0; i<min(m,6); i++) {
for (j=0; j<min(k,6); j++) {
printf ("%12.0f", A[j+i*k]);
}
printf ("\n");
}

printf ("\n Top left corner of matrix B: \n");
for (i=0; i<min(k,6); i++) {
for (j=0; j<min(n,6); j++) {
printf ("%12.0f", B[j+i*n]);
}
printf ("\n");
}

printf ("\n Top left corner of matrix C: \n");
for (i=0; i<min(m,6); i++) {
for (j=0; j<min(n,6); j++) {
printf ("%12.5G", C[j+i*n]);
}
printf ("\n");
}

printf ("\n Deallocating memory \n\n");
mkl_free(A);
mkl_free(B);
mkl_free(C);

printf (" Example completed. \n\n");
return 0;
}

编译与运行

gcc编译方法:

1
2
3
$ . /opt/intel/bin/compilervars.sh intel64
$ gcc matrix.c -lmkl_rt
$ ./a.out

icc编译方法:

1
2
3
$ . /opt/intel/oneapi/setvars.sh
$ icc -mkl matrix.c
$ ./a.out

使用icc编译器,可以达到性能优化的效果

小插曲

ubuntu虚拟机的磁盘空间不足:

  1. 在VMware那里手动设置磁盘空间
  2. 下载gparted进行分区
1
2
sudo apt-get install gparted
sudo gparted
image-20210901102704269
image-20210901102644127

icc和mkl的安装与编译
https://wuhlan3.gitee.io/2021/09/01/mkl安装与编译/
Author
Wuhlan3
Posted on
September 1, 2021
Licensed under