Intro: I've developed an application in C++ that utilizes LAPACK(LAPACKE) and MPI, all on Windows. Works OK in Windows (compiling and linking are taken care of through Code::Blocks IDE), but the execution is way too slow. 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.
Questions: How to COMPILE/LINK and RUN a C++ code that calls LAPACKE on Linux/CentOS? Do I have to have GNU Fortran installed on the CentOS machine in order to compile/link/run C++ with LAPACK(LAPACKE)?
THANKS A LOT!!!
I assume that my Debian is close enough to your CentOS for these tricks to work...
1) Check that LAPACKE is installed on your computed.
/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/lib
or /usr/local/lib
for the static library lapacke.a
or the dynamic library lapacke.so
or lapacke.so.3
.If these files are missing, consider installing the packages liblapacke
and liblapacke-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:
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
. gcc
and gfortran
are useful to recompile lapack and lapacke from scratch.
2) Let's compile a simple code based on 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" );
}
}
//=======================================
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
. Similarly, if the library is in a particula folder, try -L/usr/lib/pathtoliblapackedota
.
Depending on how MPICH2 was installed, it is likely that mpiCC
wraps g++. You can type mpiCC --version
to learn more. To run it using 2 processes:
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). Indeed, it is only required if you wish to recompile LAPACK from scratch.