使用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.
);
最新文章
- 菜单导航/URHere/面包屑,通过CSS中的content简洁表达代码
- Bash脚本15分钟进阶指导
- Eclipse/MyEclipse 安装国际化资源文件编辑插件(i18n tools)
- FragmentTabHost
- 如何利用java得到当前的时间和前一天的时间
- spring3.0+Atomikos 构建jta的分布式事务 -- NO
- codeforces 630 I(规律&;&;组合)
- (转) launch failed.Binary not found in Linux/Ubuntu解决方案
- Pseudoforest(伪最大生成树)
- 用es6的Array.reduce()方法计算一个字符串中每个字符出现的次数
- Java_修饰符
- Linux第六节课学习笔记
- Oracle 数据库实例简介
- js获取当前有效样式
- 项目方说性能达到百万TPS,如何测试它的可信度?
- eclipse Tomcat和 MYSQL JAVA web新手开发示例--登录界面连接数据库
- DDR中的一些知识点说明(ODT,ZQ校准,OCT,TDQS)
- 【原创】梵高油画用深度卷积神经网络迭代10万次是什么效果? A neural style of convolutional neural networks
- (二)SMO算法
- db2 sequence
热门文章
- 初始化vue项目,报错This is probably not a problem with npm,there is likely additional logging output above
- 【Mac】-NO.100.Mac.1.java.1.001-【Mac Install multiple JDK】-
- 20175211 2018-2019-2 《Java程序设计》第二周学习总结
- 利用python脚本(re)抓取美空mm图片
- ubuntu14安装mantis实践(包含LAMP/PHP)
- 11.c#类的成员初始化顺序
- 原生js实现分页效果(带实例)
- C语言进阶之路(二)----字符串操作常见模型
- ubuntu(centos) server安装vmware tools
- scrapy item pipeline