MKL库的配置和使用

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


前言

想用MKL库做些矩阵运算,但是配置有点复杂,折腾了两天才弄好。现在做个记录,也给大家分享下历程。
(假定读者有一定的linux基础,并不是事无巨细的全讲!)

操作系统:Ubuntu1804

linux系统都可以参考!!!

一、MKL在哪里下载?

oneapi下载链接
在这里有两个选项:操作系统选Linux、安装方式选offline installer。
然后点击下面的Continue without signing up(download starts immediately)开始下载。

二、如何安装

1.安装命令如下。

sudo sh l_onemkl_p_2024.0.0.49673_offline.sh

安装过程中可以自定义安装路径,比如/opt,后面的步骤注意使用自己的路径。

2.安装好后,在安装的路径下找到setvars.sh

source /opt/intel/oneapi/setvars.sh

输出信息如下。

:: initializing oneAPI environment ...
   bash: BASH_VERSION = 4.4.20(1)-release
   args: Using "$@" for setvars.sh arguments: 
:: compiler -- latest
:: mkl -- latest
:: tbb -- latest
:: oneAPI environment initialized ::

!!!感觉这一步并不是必须的,但是我运行过了,还是分享一下吧。

三、系统环境

这里将MKL运行动态库和静态库加入到系统环境中,不然运行时会找不到库。

vi ~/.bashrc

在末尾加上这两行,分别是

# mkl
export LD_LIBRARY_PATH="/opt/intel/oneapi/mkl/latest/lib:$LD_LIBRARY_PATH"
export LIBRARY_PATH="/opt/intel/one/mkl/latest/lib:$LIBRARY_PATH"

然后更新一下

source ~/.bashrc

四、编译和运行

最重要的是-lmkl_rt这个参数。

1.测试用例是MKL官网的例子,保存为main.c

/*******************************************************************************
*  Copyright (C) 2009-2015 Intel Corporation. All Rights Reserved.
*  The information and material ("Material") provided below is owned by Intel
*  Corporation or its suppliers or licensors, and title to such Material remains
*  with Intel Corporation or its suppliers or licensors. The Material contains
*  proprietary information of Intel or its suppliers and licensors. The Material
*  is protected by worldwide copyright laws and treaty provisions. No part of
*  the Material may be copied, reproduced, published, uploaded, posted,
*  transmitted, or distributed in any way without Intel's prior express written
*  permission. No license under any patent, copyright or other intellectual
*  property rights in the Material is granted to or conferred upon you, either
*  expressly, by implication, inducement, estoppel or otherwise. Any license
*  under such intellectual property rights must be express and approved by Intel
*  in writing.
*
********************************************************************************
*/
/*
   CGESV Example.
   ==============
 
   The program computes the solution to the system of linear
   equations with a square matrix A and multiple
   right-hand sides B, where A is the coefficient matrix:
 
   (  1.23, -5.50) (  7.91, -5.38) ( -9.80, -4.86) ( -7.32,  7.57)
   ( -2.14, -1.12) ( -9.92, -0.79) ( -9.18, -1.12) (  1.37,  0.43)
   ( -4.30, -7.10) ( -6.47,  2.52) ( -6.51, -2.67) ( -5.86,  7.38)
   (  1.27,  7.29) (  8.90,  6.92) ( -8.82,  1.25) (  5.41,  5.37)

   and B is the right-hand side matrix:
 
   (  8.33, -7.32) ( -6.11, -3.81)
   ( -6.18, -4.80) (  0.14, -7.71)
   ( -5.71, -2.80) (  1.41,  3.40)
   ( -1.60,  3.08) (  8.54, -4.05)
 
   Description.
   ============
 
   The routine solves for X the system of linear equations A*X = B,
   where A is an n-by-n matrix, the columns of matrix B are individual
   right-hand sides, and the columns of X are the corresponding
   solutions.

   The LU decomposition with partial pivoting and row interchanges is
   used to factor A as A = P*L*U, where P is a permutation matrix, L
   is unit lower triangular, and U is upper triangular. The factored
   form of A is then used to solve the system of equations A*X = B.

   Example Program Results.
   ========================
 
 CGESV Example Program Results

 Solution
 ( -1.09, -0.18) (  1.28,  1.21)
 (  0.97,  0.52) ( -0.22, -0.97)
 ( -0.20,  0.19) (  0.53,  1.36)
 ( -0.59,  0.92) (  2.22, -1.00)

 Details of LU factorization
 ( -4.30, -7.10) ( -6.47,  2.52) ( -6.51, -2.67) ( -5.86,  7.38)
 (  0.49,  0.47) ( 12.26, -3.57) ( -7.87, -0.49) ( -0.98,  6.71)
 (  0.25, -0.15) ( -0.60, -0.37) (-11.70, -4.64) ( -1.35,  1.38)
 ( -0.83, -0.32) (  0.05,  0.58) (  0.93, -0.50) (  2.66,  7.86)

 Pivot indices
      3      3      3      4
*/
#include <stdlib.h>
#include <stdio.h>

/* Complex datatype */
struct _fcomplex { float re, im; };
typedef struct _fcomplex fcomplex;

/* CGESV prototype */
extern void cgesv( int* n, int* nrhs, fcomplex* a, int* lda, int* ipiv,
                fcomplex* b, int* ldb, int* info );
/* Auxiliary routines prototypes */
extern void print_matrix( char* desc, int m, int n, fcomplex* a, int lda );
extern void print_int_vector( char* desc, int n, int* a );

/* Parameters */
#define N 4
#define NRHS 2
#define LDA N
#define LDB N

/* Main program */
int main() {
        /* Locals */
        int n = N, nrhs = NRHS, lda = LDA, ldb = LDB, info;
        /* Local arrays */
        int ipiv[N];
        fcomplex a[LDA*N] = {
           { 1.23f, -5.50f}, {-2.14f, -1.12f}, {-4.30f, -7.10f}, { 1.27f,  7.29f},
           { 7.91f, -5.38f}, {-9.92f, -0.79f}, {-6.47f,  2.52f}, { 8.90f,  6.92f},
           {-9.80f, -4.86f}, {-9.18f, -1.12f}, {-6.51f, -2.67f}, {-8.82f,  1.25f},
           {-7.32f,  7.57f}, { 1.37f,  0.43f}, {-5.86f,  7.38f}, { 5.41f,  5.37f}
        };
        fcomplex b[LDB*NRHS] = {
           { 8.33f, -7.32f}, {-6.18f, -4.80f}, {-5.71f, -2.80f}, {-1.60f,  3.08f},
           {-6.11f, -3.81f}, { 0.14f, -7.71f}, { 1.41f,  3.40f}, { 8.54f, -4.05f}
        };
        /* Executable statements */
        printf( " CGESV Example Program Results\n" );
        /* Solve the equations A*X = B */
        cgesv( &n, &nrhs, a, &lda, ipiv, b, &ldb, &info );
        /* Check for the exact singularity */
        if( info > 0 ) {
                printf( "The diagonal element of the triangular factor of A,\n" );
                printf( "U(%i,%i) is zero, so that A is singular;\n", info, info );
                printf( "the solution could not be computed.\n" );
                exit( 1 );
        }
        /* Print solution */
        print_matrix( "Solution", n, nrhs, b, ldb );
        /* Print details of LU factorization */
        print_matrix( "Details of LU factorization", n, n, a, lda );
        /* Print pivot indices */
        print_int_vector( "Pivot indices", n, ipiv );
        exit( 0 );
} /* End of CGESV Example */

/* Auxiliary routine: printing a matrix */
void print_matrix( char* desc, int m, int n, fcomplex* a, int lda ) {
        int i, j;
        printf( "\n %s\n", desc );
        for( i = 0; i < m; i++ ) {
                for( j = 0; j < n; j++ )
                        printf( " (%6.2f,%6.2f)", a[i+j*lda].re, a[i+j*lda].im );
                printf( "\n" );
        }
}

/* Auxiliary routine: printing a vector of integers */
void print_int_vector( char* desc, int n, int* a ) {
        int j;
        printf( "\n %s\n", desc );
        for( j = 0; j < n; j++ ) printf( " %6i", a[j] );
        printf( "\n" );
}

2.生成makefile文件

TARGET = test

CFLAGS = -std=c99

# -I 指定头文件路径
CFLAGS += -I/opt/intel/oneapi/mkl/latest/include
# -L 指定运行库路径和编译参数
LDFLAGS = -L/opt/intel/oneapi/mkl/latest/lib -lmkl_rt 

#LDFLAGS += -lm -lpthread

test: main.o
	gcc -o test main.o  $(LDFLAGS)  

main.o: main.c
	gcc -c main.c  $(CFLAGS)

clean:
	rm -f *.o test

编译时要指定头文件路径、动态库路径以及编译参数,这点很重要!

3.make编译
现在有两个文件

main.c   makefile

编译

make

运行生成的可执行文件test

./test

输出结果

CGESV Example Program Results

 Solution
 ( -1.09, -0.18) (  1.28,  1.21)
 (  0.97,  0.52) ( -0.22, -0.97)
 ( -0.20,  0.19) (  0.53,  1.36)
 ( -0.59,  0.92) (  2.22, -1.00)

 Details of LU factorization
 ( -4.30, -7.10) ( -6.47,  2.52) ( -6.51, -2.67) ( -5.86,  7.38)
 (  0.49,  0.47) ( 12.26, -3.57) ( -7.87, -0.49) ( -0.98,  6.71)
 (  0.25, -0.15) ( -0.60, -0.37) (-11.70, -4.64) ( -1.35,  1.38)
 ( -0.83, -0.32) (  0.05,  0.58) (  0.93, -0.50) (  2.66,  7.86)

 Pivot indices
      3      3      3      4

总结

以上就是今天要讲的内容,本文主要介绍了怎么链接intel的MKL库。

  • 5
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
根据引用\[1\]和引用\[2\]的内容,安装MKL可以通过以下步骤进行: 1. 首先,确定你的Python版本。在命令行中输入`python`可以查看你的Python版本号。 2. 在网站https://www.lfd.uci.edu/~gohlke/pythonlibs/上找到你需要的Python。这个网站提供了各种Python的下载,你可以使用Ctrl+F来查找你需要的。 3. 根据你的Python版本和操作系统选择合适的文件进行下载。将下载好的文件拷贝到你的Python安装位置。 4. 打开命令行窗口,进入到你的Python安装位置的Scripts文件夹。 5. 使用`pip install`命令安装你下载到Scripts文件夹中的符合版本的。例如,使用命令`pip install numpy-1.18.1+mkl-cp37-cp37m-win_amd64.whl`安装numpy。 6. 安装成功后,可以在命令行中输入`python`,然后使用`import`命令导入已安装,如`import numpy`。 7. 最后,可以使用`pip list`命令查看已经安装。 需要注意的是,以上步骤是根据引用\[1\]提供的方法进行安装。引用\[2\]提供了另一种手工安装numpy的方法,但需要进行一些配置。根据引用\[3\]的描述,使用引用\[1\]的方法安装numpy即可满足基本使用需求。 #### 引用[.reference_title] - *1* [关于python的numpy安装、numpy+mkl安装 #其他python同理](https://blog.csdn.net/weixin_43690520/article/details/104316553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [[转]Numpy使用MKL提升计算性能](https://blog.csdn.net/weixin_30877181/article/details/97835386)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insert_down28v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值