#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=4;//A的行數(shù),C的行數(shù) const int N=2;//B的列數(shù),C的列數(shù) const int K=3;//A的列數(shù),B的行數(shù) const float alpha=1;
const float beta=0;
const int lda=K;//A的列 const int ldb=N;//B的列 const int ldc=N;//C的列 const float A[M*K]={1,2,3,4,5,6,7,8,9,8,7,6};
const float B[K*N]={5,4,3,2,1,0};
float C[M*N];
cblas_sgemm(Order, TransA, TransB, M, N, K, alpha, A, lda, B, ldb, beta, C, ldc);
for(int i=0;i<M;i++)
{
for(int j=0;j<N;j++)
{
cout<<C[i*N+j]<<"
";
}
cout<<endl;
}
return EXIT_SUCCESS;
}
g++ testblas.c++ -lopenblas -o testoutg++ testblas.c++ -lopenblas_piledriverp-r0.2.9 -o testout 本地編譯openblas版本
創(chuàng)新互聯(lián)響應(yīng)式網(wǎng)站特點(diǎn)就是不管在電腦、平板還是手機(jī)上,HTML5建站都會(huì)根據(jù)屏幕尺寸自動(dòng)調(diào)節(jié)大小、圖片分辨率,并且融入一定的動(dòng)畫特效,讓網(wǎng)站看起來(lái)非常的美觀大方。從網(wǎng)站需求對(duì)接到網(wǎng)站制作設(shè)計(jì)、從代碼編寫到項(xiàng)目上線運(yùn)維,技術(shù)人員全程跟蹤,快速響應(yīng)注意library放在引用library的函數(shù)的后面
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 Bconst float alpha, //Scaling factor for the product of matrices A and Bconst 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的順序顛倒,可以直接得到轉(zhuǎn)制矩陣乘法的結(jié)果,不用作其他變換,(結(jié)果C也是轉(zhuǎn)制)。
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= 10;
blasint in_x=1;
blasint in_y=1;
std::vector<double> x(n);
std::vector<double> y(n);
double alpha = 10;
std::fill(x.begin(),x.end(),1.0);
std::fill(y.begin(),y.end(),2.0);
cblas_daxpy( n, alpha,&x[0], in_x, &y[0], in_y);
//Print y for(int j=0;j<n;j++)
std::cout<< y[j] << " ";
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 Bconst float alpha, //Scaling factor for the product of matrices A and Bconst 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.);
網(wǎng)站題目:使用blas做矩陣乘法-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://www.rwnh.cn/article28/epgcp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供電子商務(wù)、微信小程序、建站公司、虛擬主機(jī)、品牌網(wǎng)站建設(shè)、網(wǎng)頁(yè)設(shè)計(jì)公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容