学习OpenBlas

编译

从OpenBlas Home Page 上下载源代码。make, make install

使用

level 1 向量-向量 操作

#include <iostream>
#include "cblas.h"
#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] << "\t";

    std::cout << std::endl;
}

cblas_daxpy实现了求数乘和 y=ax+y 操作。

level 2 矩阵-向量 操作

  int i=0;
  double x[2] = {1.0,2.0};
  double y[2] = {2.0,1.0};
  double A[4] = {2.0,4.0,6.0,8.0};
  cblas_dger(CblasColMajor,2,2,10,x,1,y,1,A,2);

  for(i=0; i<4; i++)
      std::cout << A[i] << " ";
  std::cout << std::endl;

cblas_dger实现了 A=αxyT+A 操作。

level 3 矩阵-矩阵 操作

  int i=0;
  double A[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
  double B[6] = {1.0,2.0,1.0,-3.0,4.0,-1.0};
  double C[9] = {.5,.5,.5,.5,.5,.5,.5,.5,.5};
  cblas_dgemm(CblasColMajor, //列优先
          CblasNoTrans,  CblasTrans,3,3,2,1,A, 3, B, 3,2,C,3);

  for(i=0; i<9; i++)
      std::cout << C[i] << " ";
  std::cout << std::endl;

cblas_dgemm 运行 C=αop(A)op(B)+βC 操作。

附一张blas函数表

參考

-stanford·using OpenBlas
-OpenBlas User Manual

posted @ 2016-01-31 20:41  lcchuguo  阅读(1349)  评论(0编辑  收藏  举报