使用blas做矩阵乘法
#define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h>
#include <stdlib.h>
#include <cublas_v2.h>
#include <iostream>
#include <vector>
//extern "C"
//{
#include <cblas.h>
//} using namespace std;
int main()
{ const enum CBLAS_ORDER Order=CblasRowMajor;
const enum CBLAS_TRANSPOSE TransA=CblasNoTrans;
const enum CBLAS_TRANSPOSE TransB=CblasNoTrans;
const int M=;//A的行数,C的行数
const int N=;//B的列数,C的列数
const int K=;//A的列数,B的行数
const float alpha=;
const float beta=;
const int lda=K;//A的列
const int ldb=N;//B的列
const int ldc=N;//C的列
const float A[M*K]={,,,,,,,,,,,};
const float B[K*N]={,,,,,};
float C[M*N]; cblas_sgemm(Order, TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc); for(int i=;i<M;i++)
{
for(int j=;j<N;j++)
{
cout<<C[i*N+j]<<"\n";
}
cout<<endl;
} return EXIT_SUCCESS; }
g++ testblas.c++ -lopenblas -o testout
g++ testblas.c++ -lopenblas_piledriverp-r0.2.9 -o testout 本地编译openblas版本
注意library放在引用library的函数的后面
cblas_sgemm Multiplies two matrices (single-precision). void cblas_sgemm (
const enum CBLAS_ORDER Order, // Specifies row-major (C) or column-major (Fortran) data ordering.
//typedef enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102} CBLAS_ORDER; const enum CBLAS_TRANSPOSE TransA,//Specifies whether to transpose matrix A.
const enum CBLAS_TRANSPOSE TransB,
const int M, //Number of rows in matrices A and C.
const int N,//Number of rows in matrices A and C.
const int K, //Number of columns in matrix A; number of rows in matrix B
const float alpha, //Scaling factor for the product of matrices A and B
const float *A,
const int lda, //The size of the first dimention of matrix A; if you are passing a matrix A[m][n], the value should be m. stride lda, ldb and ldc (the strides) are not relevant to my problem after all, but here's an explanation of them : The elements of a matrix (i.e a 2D array) are stored contiguously in memory. However, they may be stored in either column-major or row-major fashion. The stride represents the distance in memory between elements in adjacent rows (if row-major) or in adjacent columns (if column-major). This means that the stride is usually equal to the number of rows/columns in the matrix. Matrix A =
[1 2 3]
[4 5 6]
Row-major stores values as {1,2,3,4,5,6}
Stride here is 3 Col-major stores values as {1, 4, 2, 5, 3, 6}
Stride here is 2 Matrix B =
[1 2 3]
[4 5 6]
[7 8 9] Col-major storage is {1, 4, 7, 2, 5, 8, 3, 6, 9}
Stride here is 3 Read more: http://www.physicsforums.com
const float *B,
const int ldb, //The size of the first dimention of matrix B; if you are passing a matrix B[m][n], the value should be m.
const float beta, //Scaling factor for matrix C.
float *C,
const int ldc //The size of the first dimention of matrix C; if you are passing a matrix C[m][n], the value should be m.
); Thus, it calculates either
C←αAB + βC
or
C←αBA + βC
with optional use of transposed forms of A, B, or both.
typedef enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102} CBLAS_ORDER;
typedef enum CBLAS_TRANSPOSE {CblasNoTrans=111, CblasTrans=112, CblasConjTrans=113, CblasConjNoTrans=114} CBLAS_TRANSPOSE;
$C=A*B$
$C^T=(A*B)^T=B^T*A^T$ 把A和B的顺序颠倒,可以直接得到转制矩阵乘法的结果,不用作其他变换,(结果C也是转制)。
Y←αAX + βY
cblas_sgemv
Multiplies a matrix by a vector (single precision).
void cblas_sgemv (
const enum CBLAS_ORDER Order,
const enum CBLAS_TRANSPOSE TransA,
const int M,
const int N,
const float alpha,
const float *A,
const int lda,
const float *X,
const int incX,
const float beta,
float *Y,
const int incY
);
STL版本
cblas_daxpy
Computes a constant times a vector plus a vector (double-precision).
On return, the contents of vector Y are replaced with the result. The value computed is (alpha * X[i]) +
Y[i].
#include <OpenBlas/cblas.h>
#include <OpenBlas/common.h>
#include <iostream>
#include <vector> int main()
{
blasint n = ;
blasint in_x =;
blasint in_y =; std::vector<double> x(n);
std::vector<double> y(n); double alpha = ; std::fill(x.begin(),x.end(),1.0);
std::fill(y.begin(),y.end(),2.0); cblas_daxpy( n, alpha, &x[], in_x, &y[], in_y); //Print y
for(int j=;j<n;j++)
std::cout << y[j] << "\t"; std::cout << std::endl;
}
cublas
cublasStatus_t
cublasCreate(cublasHandle_t *handle)
Return Value Meaning
CUBLAS_STATUS_SUCCESS the initialization succeeded
CUBLAS_STATUS_NOT_INITIALIZED the CUDATM Runtime initialization failed
CUBLAS_STATUS_ALLOC_FAILED the resources could not be allocated
cublasStatus_t
cublasDestroy(cublasHandle_t handle)
Return Value Meaning
CUBLAS_STATUS_SUCCESS the shut down succeeded
CUBLAS_STATUS_NOT_INITIALIZED the library was not initialized
cublasStatus_t cublasSgemm(cublasHandle_t handle, // 唯一的不同:handle to the cuBLAS library context.
cublasOperation_t transa,
cublasOperation_t transb
int m,
int n,
int k,
const float *alpha,
const float*A,
int lda,
const float*B,
int ldb,
const float*beta,
float*C,
int ldc
)
void cblas_sgemm (
const enum CBLAS_ORDER Order, // Specifies row-major (C) or column-major (Fortran) data ordering.
//typedef enum CBLAS_ORDER {CblasRowMajor=101, CblasColMajor=102} CBLAS_ORDER; const enum CBLAS_TRANSPOSE TransA,//Specifies whether to transpose matrix A.
const enum CBLAS_TRANSPOSE TransB,
const int M, //Number of rows in matrices A and C.
const int N,//Number of rows in matrices A and C.
const int K, //Number of columns in matrix A; number of rows in matrix B
const float alpha, //Scaling factor for the product of matrices A and B
const float *A,
const int lda, //The size of the first dimention of matrix A; if you are passing a matrix A[m][n], the value should be m.
const float *B,
const int ldb, //The size of the first dimention of matrix B; if you are passing a matrix B[m][n], the value should be m.
const float beta, //Scaling factor for matrix C.
float *C,
const int ldc //The size of the first dimention of matrix C; if you are passing a matrix C[m][n], the value should be m.
);
使用blas做矩阵乘法的更多相关文章
- 【神经网络与深度学习】【C/C++】使用blas做矩阵乘法
使用blas做矩阵乘法 #define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h> #include <st ...
- numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法
1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...
- cuda中用cublas库做矩阵乘法
这里矩阵C=A*B,原始文档给的公式是C=alpha*A*B+beta*C,所以这里alpha=1,beta=0. 主要使用cublasSgemm这个函数,这个函数的第二个参数有三种类型,这里CUBL ...
- POJ 2778 DNA Sequence (AC自动机,矩阵乘法)
题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...
- poj3233之经典矩阵乘法
Matrix Power Series Time Limit: 3000MS Memory Limit: 131072K Total Submissions: 12346 Accepted: ...
- 51nod 1462 树据结构 | 树链剖分 矩阵乘法
题目链接 51nod 1462 题目描述 给一颗以1为根的树. 每个点有两个权值:vi, ti,一开始全部是零. Q次操作: 读入o, u, d o = 1 对u到根上所有点的vi += d o = ...
- 【BZOJ1706】[usaco2007 Nov]relays 奶牛接力跑 矩阵乘法
[BZOJ1706][usaco2007 Nov]relays 奶牛接力跑 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项 ...
- [ZJOI2005]沼泽鳄鱼 矩阵乘法
---题面--- 题解: 乍一看还是挺懵逼的.和HH去散步很像,思路也是类似的. 复制一段我在HH去散步的题解里面写的一段话吧: 考虑f[i][j]表示i和j是否右边相连,有为1,否则为0,那么f同时 ...
- BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法
BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法 Description 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1a ...
随机推荐
- java中加与不加public
加public表示全局类,该类可以import到任何类内.不加public默认为保留类,只能被同一个包内的其他类引用来源:https://blog.csdn.net/qq_15037231/artic ...
- Python str byte 互相转换
- 字典排序 sorted
a = {6:2,8:0,1:4,-5:6,99:11,4:22} print( sorted(a.items()) ) #默认安照key排序的print( sorted(a.items(),key= ...
- 解决CentOS6.5虚拟机克隆后无法上网(网卡信息不一致)的问题
一.问题描述 虚拟机克隆后,由于网卡信息不一致的问题,导致不能上网或者执行“service network restart”命令失败 [root@lyy 桌面]# ifconfig //查看当前网卡信 ...
- jvm 线上命令
jstat -gc 40015 查看jvm用的是什么gc算法 java -XX:+PrintCommandLineFlags -version
- Android 性能优化之内存泄漏检测以及内存优化(中)
https://blog.csdn.net/self_study/article/details/66969064 上篇博客我们写到了 Java/Android 内存的分配以及相关 GC 的详细分析, ...
- Shell 终端ANSI控制码
Shell 系统交互参数整理 输出颜色 格式: \033[字背景颜色;字体颜色m字符串\033[0m 背景颜色 字体颜色 40: 黑 30: 黑 41: 红 31: 红 42: 绿 32: 绿 43: ...
- Linux 系统开启最大线程数 调优
系统最大线程数说明 系统可开启的最大线程数,可根据系统本身负载配置进行调优. 查看系统最大线程数 1.查看系统开启的最大线程数. ulimit -u [root@izbp1brwu1w35r1dmj8 ...
- D6差分及树上差分
原谅我这篇博客拖了很久才写: 来到学校就和白痴一样缺了一世纪的课 上课特别懵:还有开学考枯了: 差分有列的差分,对于一段区间[l,r]进行修改,显然如果我们对于他的差分数组的l和r+1进行修改就可以了 ...
- Spring Boot Log4j2 日志学习
简介 Java 中比较常用的日志工具类,有: Log4j. SLF4j. Commons-logging(简称jcl). Logback. Log4j2(Log4j 升级版). Jdk Logging ...