简体   繁体   English

在LINUX上使用C ++进行LAPACK / LAPACKE - 编译,链接和运行?

[英]LAPACK/LAPACKE with C++ on LINUX — Compiling, Linking and Running?

Intro: I've developed an application in C++ that utilizes LAPACK(LAPACKE) and MPI, all on Windows. 简介:我开发了一个使用LAPACK(LAPACKE)和MPI的C ++应用程序,所有这些都在Windows上。 Works OK in Windows (compiling and linking are taken care of through Code::Blocks IDE), but the execution is way too slow. 在Windows中正常工作(编译和链接通过Code :: Blocks IDE处理),但执行速度太慢。 Hence, I want to migrate the code to our small "supercomputer" running under CentOS Linux where we have already installed GNU C++, MPICH2 and LAPACK. 因此,我想将代码迁移到运行在CentOS Linux下的小型“超级计算机”,我们已经安装了GNU C ++,MPICH2和LAPACK。

Questions: How to COMPILE/LINK and RUN a C++ code that calls LAPACKE on Linux/CentOS? 问题:如何在Linux / CentOS上调用/链接和运行调用LAPACKE的C ++代码? Do I have to have GNU Fortran installed on the CentOS machine in order to compile/link/run C++ with LAPACK(LAPACKE)? 我是否必须在CentOS机器上安装GNU Fortran才能使用LAPACK(LAPACKE)编译/链接/运行C ++?

THANKS A LOT!!! 非常感谢!!!

I assume that my Debian is close enough to your CentOS for these tricks to work... 我假设我的Debian足够接近你的CentOS,这些技巧可以运作......

1) Check that LAPACKE is installed on your computed. 1)检查计算机上是否安装了LAPACKE。

  • You can search locations like /usr/include , /usr/local/include for files lapacke.h , lapacke_config.h , lapacke_mangling.h , lapacke_mangling_with_flags.h and lapacke_utils.h . 您可以搜索/usr/include/usr/local/include等文件lapacke.hlapacke_config.hlapacke_mangling.hlapacke_mangling_with_flags.hlapacke_utils.h
  • You can search locations like /usr/lib or /usr/local/lib for the static library lapacke.a or the dynamic library lapacke.so or lapacke.so.3 . 您可以在/usr/lib/usr/local/lib类的位置搜索静态库lapacke.a或动态库lapacke.solapacke.so.3

If these files are missing, consider installing the packages liblapacke and liblapacke-dev . 如果缺少这些文件,请考虑安装liblapackeliblapacke-dev软件包。 Alternatively, (in particular if you don't have root privileges), you can download source of netlib's lapack and lapacke at http://www.netlib.org/lapack/#_lapack_version_3_6_1 To compile LAPACKE, rename make.inc.example to make.inc , then type: 或者,(特别是如果你没有root权限),你可以在http://www.netlib.org/lapack/#_lapack_version_3_6_1下载netlib的lapack和lapacke的源代码来编译LAPACKE,重命名make.inc.examplemake.inc ,然后输入:

    make
    make lapackelib

The include files will be located in lapack-3.6.1/LAPACKE/include and the library will be in lapack-3.6.1 . 包含文件将位于lapack-3.6.1/LAPACKE/include ,库将位于lapack-3.6.1 gcc and gfortran are useful to recompile lapack and lapacke from scratch. gccgfortran可以从头开始重新编译lapack和lapacke。

2) Let's compile a simple code based on this example : 2)让我们根据这个例子编译一个简单的代码:

#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" );
    }
}
//=======================================

The command to compile is: 编译命令是:

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

If the include files are in a particular folder, use -I/usr/pathtolapackedoth . 如果包含文件位于特定文件夹中,请使用-I/usr/pathtolapackedoth Similarly, if the library is in a particula folder, try -L/usr/lib/pathtoliblapackedota . 同样,如果库位于particula文件夹中,请尝试-L/usr/lib/pathtoliblapackedota Depending on how MPICH2 was installed, it is likely that mpiCC wraps g++. 根据MPICH2的安装方式, mpiCC很可能包含g ++。 You can type mpiCC --version to learn more. 您可以输入mpiCC --version来了解更多信息。 To run it using 2 processes: 要使用2个进程运行它:

mpirun -np 2 main

Finally, you do not have to install GNU Fortran installed on the CentOS machine in order to compile/link/run C++ with LAPACK(LAPACKE). 最后,您不必在CentOS机器上安装GNU Fortran,以便使用LAPACK(LAPACKE)编译/链接/运行C ++。 Indeed, it is only required if you wish to recompile LAPACK from scratch. 实际上,只有在您希望从头开始重新编译LAPACK时才需要它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM