gpt4 book ai didi

c++ - 在 LINUX 上使用 C++ 的 LAPACK/LAPACKE --- 编译、链接和运行?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:47:31 25 4
gpt4 key购买 nike

简介:我用 C++ 开发了一个应用程序,它在 Windows 上使用 LAPACK(LAPACKE) 和 MPI。在 Windows 中工作正常(编译和链接通过 Code::Blocks IDE 处理),但执行速度太慢。因此,我想将代码迁移到我们在 CentOS Linux 下运行的小型“ super 计算机”,我们已经在其中安装了 GNU C++、MPICH2 和 LAPACK。

问题:如何在 Linux/CentOS 上编译/链接和运行调用 LAPACKE 的 C++ 代码?我是否必须在 CentOS 机器上安装 GNU Fortran 才能使用 LAPACK(LAPACKE) 编译/链接/运行 C++?

非常感谢!!!

最佳答案

我假设我的 Debian 与您的 CentOS 足够接近,可以让这些技巧起作用...

1) 检查您的计算机上是否安装了 LAPACKE。

  • 您可以在 /usr/include/usr/local/include 等位置搜索文件 lapacke.hlapacke_config .hlapacke_mangling.hlapacke_mangling_with_flags.hlapacke_utils.h
  • 您可以在 /usr/lib/usr/local/lib 等位置搜索静态库 lapacke.a 或动态库库 lapacke.solapacke.so.3

如果缺少这些文件,请考虑安装包 liblapackeliblapacke-dev。或者,(特别是如果您没有 root 权限),您可以在 http://www.netlib.org/lapack/#_lapack_version_3_6_1 下载 netlib 的 lapack 和 lapacke 的源代码。要编译 LAPACKE,请将 make.inc.example 重命名为 make.inc,然后键入:

    make
make lapackelib

包含文件将位于 lapack-3.6.1/LAPACKE/include 中,库将位于 lapack-3.6.1 中。 gccgfortran 可用于从头重新编译 lapack 和 lapacke。

2) 让我们根据this example 编译一个简单的代码:

#include <iostream>
#include <string>
#include <fstream>

#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#include <lapacke.h>
#include "mpi.h"

void print_matrix_rowmajor(const char* desc, lapack_int m, lapack_int n, double* a, lapack_int lda );

int main(int argc, char *argv[])
{
MPI_Init(&argc,&argv);
std::cout << "Start..." << std::endl;
//std::string fn_VALS;

/* Locals */
double A[5][3] = {1,1,1,2,3,4,3,5,2,4,2,5,5,4,3};
double b[5][2] = {-10,-3,12,14,14,12,16,16,18,16};
lapack_int info,m,n,lda,ldb,nrhs;

/* Initialization */
m = 5;
n = 3;
nrhs = 2;
lda = 3;
ldb = 2;

/* Print Entry Matrix */
print_matrix_rowmajor( "Entry Matrix A", m, n, *A, lda );
/* Print Right Rand Side */
print_matrix_rowmajor( "Right Hand Side b", n, nrhs, *b, ldb );
printf( "\n" );
/* Executable statements */
printf( "LAPACKE_dgels (row-major, high-level) Example Program Results\n" );
/* Solve least squares problem*/
info = LAPACKE_dgels(LAPACK_ROW_MAJOR,'N',m,n,nrhs,*A,lda,*b,ldb);

/* Print Solution */
print_matrix_rowmajor( "Solution", n, nrhs, *b, ldb );
printf( "\n" );


std::cout << "info = " << info << std::endl;




std::cout << "Done :-) !!!" <<std::endl;
MPI_Finalize();
return 0;
}


////////////////////////////////////////////////////////* Auxiliary routine: printing a matrix */
void print_matrix_rowmajor(const char* desc, lapack_int m, lapack_int n, double* a, lapack_int lda )
{
lapack_int i, j;
printf( "\n %s\n", desc );
for( i = 0; i < m; i++ )
{
for( j = 0; j < n; j++ )
{
printf( " %6.2f", a[i*lda+j]);
}
printf( "\n" );
}
}
//=======================================

编译命令为:

mpiCC main.cpp -o main -llapacke -llapack -lblas -lm -Wall

如果包含文件位于特定文件夹中,请使用 -I/usr/pathtolapackedoth。同样,如果库位于特定文件夹中,请尝试 -L/usr/lib/pathtoliblapackedota。根据 MPICH2 的安装方式,mpiCC 可能包含 g++。您可以键入 mpiCC --version 了解更多信息。使用 2 个进程运行它:

mpirun -np 2 main

最后,您无需安装 CentOS 机器上安装的 GNU Fortran 即可使用 LAPACK(LAPACKE) 编译/链接/运行 C++。事实上,只有当您希望从头开始重新编译 LAPACK 时才需要它。

关于c++ - 在 LINUX 上使用 C++ 的 LAPACK/LAPACKE --- 编译、链接和运行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40980752/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com