openblas下载
openblas安装
直接执行
git clone https://github.com/xianyi/OpenBLAS.git
cd OpenBLAS
make
make PREFIX=/path/to/your/installation install
openblas使用
将相应的头文件及库文件考到相应的位置
cp ~/software/openblas/include/* ./include/
cp ~/software/openblas/lib/libopenblas.a ./lib/
cmake文件如下:
cmake_minimum_required (VERSION 2.6)
project (TEST)
set (TEST_VERSION 0.1)
set(CMAKE_BUILD_TYPE "Debug")
set(CMAKE_CXX_FLAGS_DEBUG "$ENV{CXXFLAGS} -O0 -Wall -g -ggdb -DDEBUG")
set(CMAKE_CXX_FLAGS_RELEASE "$ENV{CXXFLAGS} -O3 -Wall")
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/../bin)
aux_source_directory(${PROJECT_SOURCE_DIR}/src DIR_SRC)
include_directories(${PROJECT_SOURCE_DIR}/src)
include_directories(${PROJECT_SOURCE_DIR}/include)
link_directories(${PROJECT_SOURCE_DIR}/lib)
add_executable(main ${DIR_SRC})
target_link_libraries(main libopenblas.a)
target_link_libraries(main -lm)
target_link_libraries(main -lpthread)
注意最后多连接了两个库:
target_link_libraries(main -lm)
target_link_libraries(main -lpthread)
测试程序如下:
#include <cblas.h>
#include <stdio.h>
void main() {
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};
int M = 3; // row of A and C
int N = 3; // col of B and C
int K = 2; // col of A and row of B
double alpha = 1.0;
double beta = 0.0;
cblas_dgemm(CblasRowMajor, CblasNoTrans, CblasNoTrans, M, N, K, alpha, A, K, B, N, beta, C, N);
for (i = 0; i < 9; i++) {
printf("%lf ", C[i]);
}
printf("
");
}
保存为main.c,放在文件夹src中,执行
mkdir build
cd build
cmake ..
make
生成执行文件在文件夹bin中。
接口说明
- 接口说明
如果出现
/usr/bin/ld: 找不到 -lopenblas
则要在openblas的lib文件夹中找到libopenblas.a执行
sudo cp ./lib/libopenblas.a /usr/local/lib/