实战Intel MKL(Math Kernel Library)

MKL官网所有文档:https://software.intel.com/en-us/articles/intel-math-kernel-library-documentation/
MKL使用详细手册:https://software.intel.com/sites/default/files/mkl-2019-developer-reference-c_0.pdf
MKL中文入门博客:https://blog.csdn.net/zb1165048017/article/category/6857730

LAPACK学习文档:https://software.intel.com/sites/products/documentation/doclib/mkl_sa/11/mkl_lapack_examples/index.htm
查找LAPACK函数工具:https://software.intel.com/en-us/articles/intel-mkl-function-finding-advisor
查找链接库工具:https://software.intel.com/en-us/articles/intel-mkl-link-line-advisor/
intel c++ compiler与GNU c++ compiler对MKL的比较
GNU c++ compiler = gcc,gcc -o dgemm_with_timing_gcc dgemm_with_timing.c -lmkl_rt
intel c++ compiler = icc,icc -o dgemm_with_timing_icc dgemm_with_timing.c -mkl
LOOP_COUNT=220,最终结果是4.50294 vs 4.50688 ms,所以姑且认为编译器对MKL并没有多大的影响。
在这里插入图片描述

安装

在intel官网注册并下载mkl:https://software.intel.com/en-us/mkl
Linux下安装:
mklvars.sh说明: https://software.intel.com/en-us/mkl-linux-developer-guide-scripts-to-set-environment-variables

wget http://registrationcenter-download.intel.com/akdlm/irc_nas/tec/14895/l_mkl_2019.1.144.tgz
tar -zxvf l_mkl_2019.1.144.tgz
cd l_mkl_2019.1.144/
./install.sh
sudo vim /etc/ld.so.conf.d/intel-mkl.conf
    /path/intel/mkl/lib/intel64
    /path/intel/lib/intel64
sudo ldconfig
cd /path/intel/mkl/bin
source mklvars.sh intel64
vim dgemm_example.c  # input your code
gcc -o run_dgemm_example dgemm_example.c -lmkl_rt
实例

第一次入门教程:https://software.intel.com/en-us/mkl-tutorial-c-overview
mkl_malloc(), mkl_free(),
cblas_dgemm(), dsecnd(),
mkl_get_max_threads(), mkl_set_num_threads()

所有实例:https://software.intel.com/en-us/product-code-samples

wget https://software.intel.com/sites/default/files/ipsxe2019_samples_lin_20180731.tgz
mkdir ipsxe2019_samples_lin_20180731
tar -zxvf ipsxe2019_samples_lin_20180731.tgz -C ipsxe2019_samples_lin_20180731


(1)源码:dgemm_example.c
介绍mkl_malloc(), mkl_free(), cblas_dgemm()的用法。

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

#include "mkl.h"

#define min(x,y) (((x) < (y)) ? (x) : (y))

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++) {
   
     
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值