MKL学习——基本操作C++实现
前言
前面介绍了各种向量-向量,矩阵-向量,矩阵-矩阵的函数简介。根据自身目前状况,主要使用实数域的操作,也就是说关注单精度float类型的s
和双精度double类型的d
。还有就是用的基本都是全矩阵,没有经过压缩,也不是对称、三角、带状的某一种情况。所以主要还是总结一般的乘法、加法操作。
【注】代码都以单精度float的情况书写,主要流程要记住,使用mkl_malloc
申请内存,使用mkl_free
释放内存。n年没用过C++了,凑合看看吧。
学MKL的肯定对编程有一定程度了解,智能提示是一个很好的工具,在VS中,输入cblas_s
以后,会自动补全所有的单精度操作函数,那么根据常规经验,就能判断出它到底用于做什么以及需要的参数;比如提示axpby
意思就是a*x
加b*y
。还有就是函数有两种,一种是有返回值,一种无返回值,怎么办,只能提示看函数的声明是void
还是float
或者是double
类型即可。
向量-向量
加法
运算
y=a*x+b*y
代码
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B;//两个向量int a=1, b=1;//标量int n = 5;//向量大小A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);B = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");printf("The 2st vector is ");for (int i = 0; i < n; i++){B[i] =i+1;printf("%2.0f", B[i]);}printf("\n");//计算a*A+b*Bcblas_saxpby(n, a, A, 1, b, B, 1);printf("The a*A+b*B is ");for (int i = 0; i < n; i++){printf("%2.0f", B[i]);}printf("\n");mkl_free(A);mkl_free(B);getchar();return 0;
}
结果
The 1st vector is 0 1 2 3 4
The 2st vector is 1 2 3 4 5
The a*A+b*B is 1 3 5 7 9
乘法
运算:向量点乘
代码:
//乘法
#include<stdio.h>
#include<stdlib.h>
#include <mkl.h>int main()
{float *A, *B;//两个向量int a = 1, b = 1;//标量int n = 5;//向量大小float res;A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);B = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");printf("The 2st vector is ");for (int i = 0; i < n; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");//乘法:对应元素乘积的加和res=cblas_sdot(n, A, 1, B, 1);printf("点乘结果: %2.0f",res);printf("\n");mkl_free(A);mkl_free(B);getchar();return 0;
}
结果:
The 1st vector is 0 1 2 3 4
The 2st vector is 1 2 3 4 5
点乘结果: 40
二范数
运算:二范数或者欧几里得范数,是所有元素平方和开根号
代码
//计算向量二范数
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
void main()
{float *A;int n = 5;float res;A = (float *)mkl_malloc(n*sizeof(float), 64);printf("The original vector:\n");for (int i = 0; i < n; i++){A[i] = i + 1;printf("%2.0f ", A[i]);}printf("\n");res = cblas_snrm2(n, A, 1);//计算二范数printf("The norm2 of vector is:%2.6f", res);mkl_free(A);getchar();
}
结果:
The original vector:1 2 3 4 5
The norm2 of vector is:7.416198
旋转
运算:将空间中一个点,绕原点旋转的角度
代码:以二维坐标点(2,0)(2,0)绕原点旋转45°为例。代码有点问题,一释放内存就出错,具体原因是对两个向量开辟空间以后又让它们指向了别的地址,造成了开辟空间无用。所以调用Cblas
函数,可以直接把指向数组的指针丢进去。暂时先这样理解吧,等把C++复习一遍再来看看分析的对不对。
//旋转,以二维空间中的一个点(2,0)绕原点旋转45°
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
#include<math.h>
#define M_PI 3.14159265358979323846int main()
{float *A, *B;//A是坐标点,B是旋转矩阵float point1[] = { 2 };//旋转点x坐标float point2[] = { 0 };//旋转点y坐标float rotpoint[] = { cos(45.0*M_PI / 180), sin(45.0*M_PI / 180) };//A = (float *)mkl_malloc(1 * sizeof(float), 64);//B = (float *)mkl_malloc(1 * sizeof(float), 64);A = point1;B = point2;printf("The point is (%2.0f,%2.0f)",point1[0],point2[0]);printf("\n");//计算旋转后的点cblas_srot(1, A, 1, B, 1, rotpoint[0], rotpoint[1]);printf("The rotated is (%2.6f,%2.6f)", A[0], B[0]);printf("\n");//mkl_free(A);//mkl_free(B);getchar();return 0;
}
结果:还是比较正确的
The point is ( 2, 0)
The rotated is (1.414214,-1.414214)
缩放
运算
x=a*x
代码
//计算向量缩放
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
void main()
{float *A;int n = 5;float scal=0.1;A = (float *)mkl_malloc(n*sizeof(float), 64);printf("The original vector:\n");for (int i = 0; i < n; i++){A[i] = i + 1;printf("%2.0f ", A[i]);}printf("\n");cblas_sscal(n, scal, A, 1);//缩放printf("The scaled vector:\n");for (int i = 0; i < n; i++){printf("%2.1f ", A[i]);}mkl_free(A);getchar();
}
结果
The original vector:1 2 3 4 5
The scaled vector:
0.1 0.2 0.3 0.4 0.5
交换
运算:交换两个向量
代码
//交换
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>int main()
{float *A, *B;//两个向量int a = 1, b = 1;//标量int n = 5;//向量大小A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);B = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");printf("The 2st vector is ");for (int i = 0; i < n; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");//交换ABcblas_sswap(n, A, 1, B, 1);printf("The 1st swapped vctor is");for (int i = 0; i < n; i++){printf("%2.0f", A[i]);}printf("\n");printf("The 2st swapped vctor is");for (int i = 0; i < n; i++){printf("%2.0f", B[i]);}printf("\n");mkl_free(A);mkl_free(B);getchar();return 0;
}
结果
The 1st vector is 0 1 2 3 4
The 2st vector is 1 2 3 4 5
The 1st swapped vctor is 1 2 3 4 5
The 2st swapped vctor is 0 1 2 3 4
最值
运算:求最大最小值
代码
//求最大最小值
#include<stdlib.h>
#include<stdio.h>
#include<mkl.h>
void main()
{float *A;//向量int n = 5;//向量大小int max, min;A = (float *)mkl_malloc(n * 1 * sizeof(float), 64);printf("The 1st vector is ");for (int i = 0; i < n; i++){A[i] = i;printf("%2.0f", A[i]);}printf("\n");//计算最值位置max=cblas_isamax(n, A, 1);min = cblas_isamin(n, A, 1);printf("The max value is %2.0f, position is %d\n", A[max], max + 1);printf("The min value is %2.0f, position is %d\n", A[min], min + 1);mkl_free(A);getchar();
}
结果
The 1st vector is 0 1 2 3 4
The max value is 4, position is 5
The min value is 0, position is 1
矩阵-向量
乘法1
运算:
y := \alpha*A*x + \beta*y
代码
//矩阵-向量乘积
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B,*C;//A是矩阵,B是向量,C是向量int m = 2;//矩阵行数int n = 5;//向量维度,矩阵列数int a = 1, b = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(n*sizeof(float), 64);C = (float *)mkl_malloc(m*sizeof(float), 64);//赋值,按行存储?printf("数组为\n");for (int i = 0; i < m*n; i++){if (i%n == 0 && i != 0)printf("\n");A[i] = i;printf("%2.0f",A[i]);} printf("\n");printf("向量为\n");for (int i = 0; i < n; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");for (int i = 0; i < m*n; i++)C[i] = 0;//2*5的矩阵与5*1的向量相乘cblas_sgemv(CblasRowMajor, CblasNoTrans, m, n, a, A, n, B, 1, b, C, 1);printf("矩阵-向量乘法结果\n");for (int i = 0; i < m; i++){printf("%2.0f ", C[i]);}mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;
}
结果
数组为0 1 2 3 45 6 7 8 9
向量为1 2 3 4 5
矩阵-向量乘法结果
40 115
乘法2
运算
A := \alpha*x*y'+ A,
代码
//矩阵-向量乘积
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B, *C;//A是矩阵,B是向量int m=2,n = 5;//B,C向量维度int a = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(m*sizeof(float), 64);C = (float *)mkl_malloc(n*sizeof(float), 64);//赋值,按行存储?printf("数组为\n");for (int i = 0; i < m*n; i++){if (i%n == 0 && i != 0)printf("\n");A[i] = 1;printf("%2.0f", A[i]);}printf("\n");printf("向量1为\n");for (int i = 0; i < m; i++){B[i] = i + 1;printf("%2.0f", B[i]);}printf("\n");printf("向量2为\n");for (int i = 0; i < n; i++){C[i] = i+2;printf("%2.0f", C[i]);}printf("\n");//5*1向量乘以1*5向量,加上5*5矩阵cblas_sger(CblasRowMajor, m, n, a, B, 1, C, 1, A, n);printf("向量-向量相乘+矩阵的结果\n");for (int i = 0; i < m*n; i++){if (i%n == 0 && i != 0)printf("\n");printf("%2.0f ", A[i]);}mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;
}
结果
数组为1 1 1 1 11 1 1 1 1
向量1为1 2
向量2为2 3 4 5 6
向量-向量相乘+矩阵的结果3 4 5 6 75 7 9 11 13
矩阵-矩阵
乘法1
运算
C := \alpha*op(A)*op(B) + \beta*C,
代码
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B, *C;int m = 2, n = 3, k = 2;//A维度2*3,B维度2*3(计算时候转置),C维度2*2int a = 1, b = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(n*k*sizeof(float), 64);C = (float *)mkl_malloc(m*k*sizeof(float), 64);printf("矩阵1为\n");for (int i = 0; i < m*n; i++){if (i != 0 && i%n == 0)printf("\n");A[i] = i + 1;printf("%2.0f", A[i]);}printf("\n");printf("矩阵2为\n");for (int i = 0; i < n*k; i++){if (i != 0 && i%k == 0)printf("\n");B[i] = 1;printf("%2.0f", B[i]);}printf("\n");printf("矩阵3为\n");for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");C[i] = i;printf("%2.0f", C[i]);}printf("\n");printf("结果矩阵\n");cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, m, k, n, a, A, n, B, k, b, C, k);//注意mkn的顺序☆for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");printf("%2.0f", C[i]);}printf("\n");mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;
}
结果
矩阵1为1 2 34 5 6
矩阵2为1 11 11 1
矩阵3为0 12 3
结果矩阵6 7
1718
乘法2
依旧是上述的功能,此处尝试一下
#include<stdio.h>
#include<stdlib.h>
#include<mkl.h>
int main()
{float *A, *B, *C;int m = 2, n = 3, k = 2;//A维度2*3,B维度2*3(计算时候转置),C维度2*2int a = 1, b = 1;//缩放因子A = (float *)mkl_malloc(m*n*sizeof(float), 64);B = (float *)mkl_malloc(k*n*sizeof(float), 64);C = (float *)mkl_malloc(m*k*sizeof(float), 64);printf("矩阵1为\n");for (int i = 0; i < m*n; i++){if (i != 0 && i%n == 0)printf("\n");A[i] = i + 1;printf("%2.0f", A[i]);}printf("\n");printf("矩阵2为\n");for (int i = 0; i < k*n; i++){if (i != 0 && i%n == 0)printf("\n");B[i] = 1;printf("%2.0f", B[i]);}printf("\n");printf("矩阵3为\n");for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");C[i] = i;printf("%2.0f", C[i]);}printf("\n");printf("结果矩阵\n");cblas_sgemm(CblasRowMajor, CblasNoTrans, CblasTrans, m, k, n, a, A, n, B, n, b, C, k);//注意mkn的顺序☆for (int i = 0; i < m*k; i++){if (i != 0 && i%k == 0)printf("\n");printf("%2.0f", C[i]);}printf("\n");mkl_free(A);mkl_free(B);mkl_free(C);getchar();return 0;}
结果
矩阵1为1 2 34 5 6
矩阵2为1 1 11 1 1
矩阵3为0 12 3
结果矩阵6 7
1718
注意点
最主要的是记住参数顺序,首先是矩阵op(A)op(A)和的C行数,opop代表操作,乘法2中的opop就是转置;然后是矩阵op(B)op(B)和CC的列数;随后才是op(A)op(A)的列数和op(B)op(B)的行数。对于乘法2中的第二个矩阵,也就是B<script type="math/tex" id="MathJax-Element-399">B</script>矩阵,虽然转置了,但是还是以不转置时候,以行存储方式的引导维度,也就是列数为输入参数。
而且丢入函数的参数,并不一定是mkl_malloc
开辟的空间,也可以是其它数组,用指针指向数组地址,然后丢到函数就行了。
奉上code(vs2013): MKL -C++基本操作
MKL学习——基本操作C++实现相关推荐
- MKL学习——功能简介
基本术语 BLAS : Basic Linear Algebra Subprograms 基本线性代数子程序 BLACS : Basic Linear Algebra Communication Su ...
- MKL学习——线性代数概念相关
前言 回顾一下BLAS Level 1 2 3中的运算都有什么类型 BLAS Level 1 在BLAS Level 1中,进行的是向量-向量的操作.其中相关概念有 向量类型: 实数域,复数域,共轭 ...
- MKL学习——矩阵向量操作
前言 前面介绍的BLAS Level 1是向量-向量的操作,而这里的BLAS Level 2主要还是对矩阵-向量之间的操作.命名规则与前面的一样,主要记住定义的是数据类型 s 实数域,单精度 c 复数 ...
- MKL学习——数学运算库安装调试
前言 最近要用C++折腾一些东西,涉及到矩阵运算,看了一下网上推荐的数学库,貌似MKL还是蛮不错滴,放到VS2013里面试试 国际惯例,来波地址 blas, cblas, openblas, atla ...
- MATLAB的学习——基本操作和矩阵操作
基本操作 命令窗口操作: clc :删除以上的操作及输出: clear x y z 删除上面的 x y z 三个变量的值 输出格式的限定 >> format long >> x ...
- MKL学习——矩阵矩阵操作
前言 前面介绍了BLAS Level 1中向量-向量操作,以及BLAS Level 2中矩阵-向量的操作,就剩下这一篇的BLAS Level 3中的矩阵-矩阵的操作了.对稀疏矩阵的操作以后等要用再看看 ...
- MKL学习——向量操作
前言 推荐两个比较好的教程: BLAS (Basic Linear Algebra Subprograms) LAPACK for Windows 命名规范 BLAS基本线性代数子程序的函数命令都有一 ...
- OpenCV学习——基本操作之绘制几何图形
(1)绘制直线:cv2.line() 函数原型:cv2.line(img,start,end,color,thickness) img:要绘制直线的图像. start,end:直线的起点和终点.(二维 ...
- Matlab学习——基本操作与矩阵输入
一.基础操作 1. 清除MATLAB里面的valuable clear valuable名字 2. MATLAB里面的keyword pi -- 圆周率π Inf -- ∞ NaN -- not a ...
最新文章
- mysql四种事务隔离级别
- leetcode 506. 相对名次(Java版)
- Ehab and another construction problem(水题)
- 计算机过滤器的作用,14种功能强大的Wireshark过滤器介绍
- 常用浏览器的编码设置
- 21天学通python-21天学通Python PDF 高清版
- linux后台执行shell脚本
- ASCII和UTF-8
- pip卸载旧版本后安装新版本(我是Python3)
- 关于USGS 共享光谱库读取问题
- Qt实用技巧:自定义窗口标题栏
- ES可视化工具--Dejavu--下载、安装、使用
- NSSCTF部分复现
- 京东 按时上下班被开除,国内IT业惨状
- 时光倒流-第12届蓝桥杯Scratch选拔赛真题精选
- ZZULIOJ1032
- 泪目!曾风靡全国的国产网游,宣布停运!
- APP测试的入门书籍有哪些?
- hdu 动态规划题集
- 软件工程国考总结——判断题
热门文章
- rk3288 android4.4,ubuntu16.04下firefly rk3288的编译安卓4.4
- 03.Python基础--控制流语句-顺序结构-判断结构-循环语句
- canvas 圆角矩形填充_View绘制系列(9)Canvas八卦图绘制
- 计算机C语言常用语句,计算机二级C语言考试常见知识积累
- php显示mysql数据实例_php 连接mysql数据库并显示数据 实例 转载 aoguren
- 数学--数论--同余及其性质(超详细)
- [机器学习] TF-IDF算法
- Python中的str与unicode处理方法
- uCOS任务堆栈的深入分析(转)
- 【ubuntu-qt-dlib】 配置问题 (二) terminate called after throwing an instance of 'dlib::image_load_error'