安装 openblas
直接使用 apt-get
命令即可安装:
shell
sudo apt-get install libopenblas-dev
检查是否安装成功,可以用下面的示例代码 example.cpp
:
cpp
#include <stdio.h>
#include <stdlib.h>
#include "cblas.h"
int main() {
const int n = 10; // Array size
const double da = 10; // Double constant
double dx[n]; // Input double array
double dy[n]; // Output double array
// Initialize the arrays
for (int i = 0; i < n; ++i) {
dx[i] = 9 - i;
dy[i] = i;
}
// Print the original dy array
printf("Original dy: ");
for (int i = 0; i < n; ++i) {
printf("%f ", dy[i]);
}
printf("\n");
// Perform daxpy operation: dy = da * dx + dy
cblas_daxpy(n, da, dx, 1, dy, 1);
// Print the modified dy array
printf("Modified dy: ");
for (int i = 0; i < n; ++i) {
printf("%f ", dy[i]);
}
printf("\n");
return 0;
}
编译并运行:
shell
~$ gcc example.cpp -lopenblas -o example
~$ ./example
Original dy: 0.000000 1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 9.000000
Modified dy: 90.000000 81.000000 72.000000 63.000000 54.000000 45.000000 36.000000 27.000000 18.000000 9.000000
安装 Eigen
Eigen 官网:https://eigen.tuxfamily.org/index.php?title=Main_Page
在官网找一个源码下载,解压后安装:
shell
~$ wget https://gitlab.com/libeigen/eigen/-/archive/3.4.0/eigen-3.4.0.tar.gz
~$ tar -zxvf eigen-3.4.0.tar.gz
~$ mkdir build
~$ cd build
~$ cmake ..
~$ sudo make install
eigen3会被默认安装到 /usr/local/include/eigen3
路径下,需要移动到 /usr/local/include
下面:
shell
~$ sudo mv /usr/local/include/eigen3/* /usr/local/include/
然后把 /usr/local/include/eigen3
删除掉:
shell
~$ sudo rm -rf /usr/local/include/eigen3
最后来跑一下官方示例 my_program.cpp
:
cpp
#include <iostream>
#include <Eigen/Dense>
using Eigen::MatrixXd;
int main()
{
MatrixXd m(2, 2);
m(0, 0) = 3;
m(1, 0) = 2.5;
m(0, 1) = -1;
m(1, 1) = m(1, 0) + m(0, 1);
std::cout << m << std::endl;
}
编译运行:
shell
~$ g++ my_program.cpp -o my_program
~$ ./my_program
3 -1
2.5 1.5