#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做矩阵乘法的更多相关文章

  1. 【神经网络与深度学习】【C/C++】使用blas做矩阵乘法

    使用blas做矩阵乘法   #define min(x,y) (((x) < (y)) ? (x) : (y)) #include <stdio.h> #include <st ...

  2. numpy.loadtxt() 出现codecError_____ Excel 做矩阵乘法

    1) 用 numpy读入csv文件是报错 UnicodeDecodeError: 'gbk' codec can't decode byte 0xbf in position 2: illegal m ...

  3. cuda中用cublas库做矩阵乘法

    这里矩阵C=A*B,原始文档给的公式是C=alpha*A*B+beta*C,所以这里alpha=1,beta=0. 主要使用cublasSgemm这个函数,这个函数的第二个参数有三种类型,这里CUBL ...

  4. POJ 2778 DNA Sequence (AC自动机,矩阵乘法)

    题意:给定n个不能出现的模式串,给定一个长度m,要求长度为m的合法串有多少种. 思路:用AC自动机,利用AC自动机上的节点做矩阵乘法. #include<iostream> #includ ...

  5. poj3233之经典矩阵乘法

    Matrix Power Series Time Limit: 3000MS   Memory Limit: 131072K Total Submissions: 12346   Accepted:  ...

  6. 51nod 1462 树据结构 | 树链剖分 矩阵乘法

    题目链接 51nod 1462 题目描述 给一颗以1为根的树. 每个点有两个权值:vi, ti,一开始全部是零. Q次操作: 读入o, u, d o = 1 对u到根上所有点的vi += d o = ...

  7. 【BZOJ1706】[usaco2007 Nov]relays 奶牛接力跑 矩阵乘法

    [BZOJ1706][usaco2007 Nov]relays 奶牛接力跑 Description FJ的N(2 <= N <= 1,000,000)头奶牛选择了接力跑作为她们的日常锻炼项 ...

  8. [ZJOI2005]沼泽鳄鱼 矩阵乘法

    ---题面--- 题解: 乍一看还是挺懵逼的.和HH去散步很像,思路也是类似的. 复制一段我在HH去散步的题解里面写的一段话吧: 考虑f[i][j]表示i和j是否右边相连,有为1,否则为0,那么f同时 ...

  9. BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法

    BZOJ_3231_[Sdoi2008]递归数列_矩阵乘法 Description 一个由自然数组成的数列按下式定义: 对于i <= k:ai = bi 对于i > k: ai = c1a ...

随机推荐

  1. HttpResponse输出文件

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm3.aspx. ...

  2. Cocos2d-js3.3 模态对话框的实现

    首先,先了解一下什么是模态对话框,百度百科的给出了下面一个定义: 模态对话框(Modal Dialogue Box,又叫做模式对话框),是指在用户想要对对话框以外的应用程序进行操作时,必须首先对该对话 ...

  3. st2_057

    http://vulapps.evalbug.com/s_struts2_s2-057/ 这大概是全网复现st2-057环境最简单的操作了. 不过首先,你需要安装过docker,安装方法参考官方文档h ...

  4. java poi 合并 word文档

    import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.InputStream;import jav ...

  5. python3.x 读写文件要使用UTF8编码的话需要。。

    读写文件常遇到编码不正确的情况,都用UTF8读写文件就好了,在读写的时候加上编码格式:encoding='UTF-8'如下:with open(filename, 'r', encoding='UTF ...

  6. consul 集群

    主机运行 : consul agent -server -bootstrap-expect 2  -data-dir D:\consul  -node=winyh -bind=192.168.10.1 ...

  7. Windows cmd 一些命令

    运行是Windows的必要组成部分,可以简单理解为一个应用程序快速调用的组件.通过“运行”窗口,可以调用Windows中任何应用程序甚至DOS命令.一般可以搜索百度百科 微软件(窗口) + R    ...

  8. CSS——background-size实现图片自适应

    在网页端,我们经常想让图片能够自适应拉伸缩放,使之可以完美的嵌入我们给定的容器里,比如div,button,input,下面我将用代码来说明如何实现这个功能! 一.div背景图自适应 如果知道图片都有 ...

  9. opencv计算两个轮廓之间hu矩相似程度,MatchShapes

    https://blog.csdn.net/jiake_yang/article/details/52589063 [OpenCV3.3]通过透视变换矫正变形图像 https://blog.csdn. ...

  10. 1. Dubbo原理解析-Dubbo内核实现之SPI简单介绍 (转)

    转载自  斩秋的专栏  http://blog.csdn.net/quhongwei_zhanqiu/article/details/41577159 Dubbo采用 微内核 + 插件体系,使得设计优 ...