Pinned Update #1

The Darc Library (C++) is now updated as of March, 2012. Click ▷here too browse the entire solution.

Friday, February 25, 2011


LAPACK is a Fortran based linear algebra library, which can be used for solving systems of linear equations, eigenvalue problems and many other things. Although there are several wrappers for all kinds of programming lanugage one should consider writing a custom designed wrapper. First of all it's really easy to implement and secondly you know what you're wrapper is doing. Apart from that i don't think anyone ever uses the full LAPACK functionality.

On this page i will show you how to include LAPACK into your project using C++ and Visual Studio. Before we can get started make sure to obtain the following items.

In order to use the library you must either get the source code, or the precompiled binaries for your system. To save some time just get the DLL and LIB. If you're programming on a Win32 platform, click ▷here.

Since LAPACK makes use of BLAS internally, you will also need the BLAS binaries. The file you just downloaded contains both.

LAPACK (and BLAS for that matter) functionality is included into a project by declaring the corresponding Fortran function header. In order to do that you need to know what the function you're looking for looks like in LAPACK. Assuming your program provides single precision data consider the following ▷overview. The procedure is simple. Select a function, following the link and try to understand the description.

Consider the following function. As you can extract from the cooresponding ▷description the SGESV routine computes the solution to \(Ax=b\) if \(A\) is a \(n\) by \(n\) matrix, by using a LU decomposition. All you need to do now is to declare the Fortran header in a C/C++ conform manner, embedded in an extern "C" block. Don't forget to link the downloaded librabries and to put the DLLs in the right directory.

extern "C" {

    void sgesv_(
         int* n,
         int* nrhs,
         float* a,
         int* lda,
         int* ipiv,
         float* b,
         int* ldb,
         int* info);
Wrapper for SGESV([...])

That's it! The function sgesv_([...]) will now call the respective LAPACK function. The arguments may look a little cryptic at first but it shouldn't be too much of a problem with the documentation at hand. Despite being fully functional the current implementation is clearly not very user friendly. At the expense of a slightly higher funtion overhead you should consider writing a wrapper function for an easier and more intuitive access.

/** On exit A will contain the factors L and U,
    b will contain x.
DC_VALUE MA_API ma_sgesv(GE_Matrix& _A, GE_Matrix& _b){
   int t_info;
   int* t_p = new int[_A.m_cols];

       &_A.m_cols,      // Number of rows in A
       &_b.m_cols,      // Number of columns in b
       _A.m_data,       // A
       &_A.m_rows,      // LDA - Number of columns in A
       t_p,             // P
       _b.m_data,       // b
       &_b.m_rows,      // LDB - Number of columns in b
       &t_info);        // Info

   delete[] t_p;
   if(t_info == 0) return DC_OK;
   return DC_ERROR;
Wrapper for SGESV([...])

The ▷Darc Library uses the above wrapper structure to access LAPACK functions.

No comments:

Post a Comment