17. Calling XLPack functions from C/C++ (Addendum: How to use LAPACKE)

LAPACKE/CBLAS library software for XLPack API is included in XLPack SDK.

LAPACKE/CBLAS is the interface conversion program for LAPACK/BLAS which is written in Fortran. LAPACKE/CBLAS allows you to call LAPACK/BLAS routines directly from C/C++, and can be regraded as the C/C++ version of LAPACK/BLAS. The differences from Fortran version are as follows.

  • Prefix LAPACKE_ or cbls_ is added to the subroutine/function name
  • Temporary work storage will be automatically allocated in the routine (It is also possible to allocate externally like original Fortran version. In that case, suffix _work is added to the name.)
  • It can be specified whether the two-dimensional arrays are stored in row-major or column-major order. When row-major is specified, the storage order is conveniently same as the original C/C++ array order. However, in this case, the arrays are transposed and copied before and after calculation. Then the overhead may be significant if the matrix is large.

In addition to C interface library, Lapacke.lib (or Lapacke_32.lib if 32 bit program) must be linked. The header files lapacke.h and cblas.h are used. For XLPack Lite, Lapacke_Lite.lib (or Lapacke_Lite_32.lib) and lapacke_lite.h are used.

LAPACKE and CBLAS routines corresponding to LAPACK and BLAS routines included in XLPack are supported.

List of supported LAPACKE/CBLAS routines

(*) shows the routines supported by XLPack Lite.

cblas_drotg cblas_drotmg cblas_drot cblas_drotm cblas_dswap cblas_dscal
cblas_dcopy cblas_daxpy cblas_ddot cblas_dsdot cblas_dnrm2 cblas_dasum
cblas_idamax
cblas_dgemv cblas_dgbmv cblas_dger cblas_dsbmv cblas_dspmv cblas_dspr
cblas_dspr2 cblas_dsymv cblas_dsyr cblas_dsyr2 cblas_dtbmv cblas_dtbsv
cblas_dtpmv cblas_dtpsv cblas_dtrmv cblas_dtrsv
cblas_dgemm cblas_dsymm cblas_dsyrk cblas_dsyr2k cblas_dtrmm cblas_dtrsm

cblas_zswap cblas_zscal cblas_zdscal cblas_zcopy cblas_zaxpy cblas_zdotu_sub
cblas_zdotc_sub cblas_dznrm2 cblas_dzasum cblas_izamax
cblas_zgemv cblas_zgbmv cblas_zhemv cblas_zhbmv cblas_zhpmv cblas_ztrmv
cblas_ztbmv cblas_ztpmv cblas_ztrsv cblas_ztbsv cblas_ztpsv cblas_zgeru
cblas_zgerc cblas_zher cblas_zher2 cblas_zhpr cblas_zhpr2
cblas_zgemm cblas_zsymm cblas_zhemm cblas_zherk cblas_zher2k cblas_ztrmm
cblas_ztrsm cblas_zsyrk cblas_zsyr2k

LAPACKE_ddisna LAPACKE_dgbcon LAPACKE_dgbsv LAPACKE_dgbsvx LAPACKE_dgbtrf
LAPACKE_dgbtrs LAPACKE_dgecon(*) LAPACKE_dgees LAPACKE_dgeesx LAPACKE_dgeev
LAPACKE_dgeevx LAPACKE_dgejsv LAPACKE_dgelqf LAPACKE_dgels(*) LAPACKE_dgelss
LAPACKE_dgelsy LAPACKE_dgeqp3 LAPACKE_dgeqrf LAPACKE_dgesv(*) LAPACKE_dgesvd
LAPACKE_dgesvdx LAPACKE_dgesvj LAPACKE_dgesvx LAPACKE_dgetrf LAPACKE_dgetri
LAPACKE_dgetrs LAPACKE_dgges LAPACKE_dggesx LAPACKE_dggev LAPACKE_dggevx
LAPACKE_dggglm LAPACKE_dgglse LAPACKE_dggsvd3 LAPACKE_dgtcon LAPACKE_dgtsv
LAPACKE_dgtsvx LAPACKE_dgttrf LAPACKE_dgttrs LAPACKE_dlamch(*) LAPACKE_dlange(*)
LAPACKE_dlansy(*) LAPACKE_dlantr LAPACKE_dorglq LAPACKE_dorgqr LAPACKE_dormlq
LAPACKE_dormqr LAPACKE_dpbcon LAPACKE_dpbsv LAPACKE_dpbsvx LAPACKE_dpbtrf
LAPACKE_dpbtrs LAPACKE_dpocon(*) LAPACKE_dposv(*) LAPACKE_dposvx LAPACKE_dpotrf
LAPACKE_dpotri LAPACKE_dpotrs LAPACKE_dppcon LAPACKE_dppsv LAPACKE_dppsvx
LAPACKE_dpptrf LAPACKE_dpptri LAPACKE_dpptrs LAPACKE_dptcon LAPACKE_dptsv
LAPACKE_dptsvx LAPACKE_dpttrf LAPACKE_dpttrs LAPACKE_dsbev LAPACKE_dsbevx
LAPACKE_dsbgv LAPACKE_dsbgvx LAPACKE_dsgesv LAPACKE_dspcon LAPACKE_dspev
LAPACKE_dspevx LAPACKE_dspgv LAPACKE_dspgvx LAPACKE_dsposv LAPACKE_dspsv
LAPACKE_dspsvx LAPACKE_dsptrf LAPACKE_dsptri LAPACKE_dsptrs LAPACKE_dstev
LAPACKE_dstevx LAPACKE_dsycon LAPACKE_dsyev(*) LAPACKE_dsyevx LAPACKE_dsygv
LAPACKE_dsygvx LAPACKE_dsysv LAPACKE_dsysvx LAPACKE_dsytrf LAPACKE_dsytri
LAPACKE_dsytrs LAPACKE_dtbcon LAPACKE_dtbtrs LAPACKE_dtpcon LAPACKE_dtptri
LAPACKE_dtptrs LAPACKE_dtrcon LAPACKE_dtrtri LAPACKE_dtrtrs LAPACKE_dtrttf
LAPACKE_dlatms

LAPACKE_slamch

LAPACKE_zcgesv LAPACKE_zcposv LAPACKE_zgbcon LAPACKE_zgbsv LAPACKE_zgbsvx
LAPACKE_zgbtrf LAPACKE_zgbtrs LAPACKE_zgecon LAPACKE_zgees LAPACKE_zgeesx
LAPACKE_zgeev LAPACKE_zgeevx LAPACKE_zgejsv LAPACKE_zgelqf LAPACKE_zgels
LAPACKE_zgelss LAPACKE_zgelsy LAPACKE_zgeqp3 LAPACKE_zgeqrf LAPACKE_zgesv
LAPACKE_zgesvd LAPACKE_zgesvdx LAPACKE_zgesvj LAPACKE_zgesvx LAPACKE_zgetrf
LAPACKE_zgetri LAPACKE_zgetrs LAPACKE_zgges LAPACKE_zggesx LAPACKE_zggev
LAPACKE_zggevx LAPACKE_zggglm LAPACKE_zgglse LAPACKE_zggsvd3 LAPACKE_zgtcon
LAPACKE_zgtsv LAPACKE_zgtsvx LAPACKE_zgttrf LAPACKE_zgttrs LAPACKE_zhbev
LAPACKE_zhbevx LAPACKE_zhbgv LAPACKE_zhbgvx LAPACKE_zhecon LAPACKE_zheev
LAPACKE_zheevx LAPACKE_zhegv LAPACKE_zhegvx LAPACKE_zhesv LAPACKE_zhesvx
LAPACKE_zhetrf LAPACKE_zhetri LAPACKE_zhetrs LAPACKE_zhpcon LAPACKE_zhpev
LAPACKE_zhpevx LAPACKE_zhpgv LAPACKE_zhpgvx LAPACKE_zhpsv LAPACKE_zhpsvx
LAPACKE_zhptrf LAPACKE_zhptri LAPACKE_zhptrs LAPACKE_zlange LAPACKE_zlanhe
LAPACKE_zlansy LAPACKE_zlantr LAPACKE_zpbcon LAPACKE_zpbsv LAPACKE_zpbsvx
LAPACKE_zpbtrf LAPACKE_zpbtrs LAPACKE_zpocon LAPACKE_zposv LAPACKE_zposvx
LAPACKE_zpotrf LAPACKE_zpotri LAPACKE_zpotrs LAPACKE_zppcon LAPACKE_zppsv
LAPACKE_zppsvx LAPACKE_zpptrf LAPACKE_zpptri LAPACKE_zpptrs LAPACKE_zptcon
LAPACKE_zptsv LAPACKE_zptsvx LAPACKE_zpttrf LAPACKE_zpttrs LAPACKE_zspcon
LAPACKE_zspsv LAPACKE_zspsvx LAPACKE_zsptrf LAPACKE_zsptri LAPACKE_zsptrs
LAPACKE_zsycon LAPACKE_zsyr LAPACKE_zsysv LAPACKE_zsysvx LAPACKE_zsytrf
LAPACKE_zsytri LAPACKE_zsytrs LAPACKE_ztbcon LAPACKE_ztbtrs LAPACKE_ztpcon
LAPACKE_ztptri LAPACKE_ztptrs LAPACKE_ztpttf LAPACKE_ztrcon LAPACKE_ztrtri
LAPACKE_ztrtrs LAPACKE_ztrttf LAPACKE_zunglq LAPACKE_zungqr LAPACKE_zunmlq
LAPACKE_zunmqr LAPACKE_zlatms

In the current version, there are some differences on the run-time error handling from the original software as follows.

  • For some errors, error messages are output by the LAPACK/BLAS base routines instead of LAPACKE/CBLAS routines. In those cases, the displayed argument number may be different from LAPACKE/CBLAS.

Please refer to the original sites for more details.

Description of LAPACKE
LAPACK function reference manual including LAPACKE and CBLAS functions

Example program which solves the example problem of chapter 15 using LAPACKE is shown below.

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

int main(void)
{
    double a[3][3], b[3];
    int n = 3, nrhs = 1, lda = 3, ldb = 1, ipiv[3], info;

    a[0][0] = 10.0; a[0][1] = -7.0; a[0][2] = 0.0;
    a[1][0] = -3.0; a[1][1] = 2.0;  a[1][2] = 6.0;
    a[2][0] = 5.0;  a[2][1] = -1.0; a[2][2] = 5.0;
    b[0] = 7.0;
    b[1] = 4.0;
    b[2] = 6.0;

    info = LAPACKE_dgesv(LAPACK_ROW_MAJOR, n, nrhs, (double *)a, lda, ipiv, b, ldb);
    printf("info = %d\n", info);
    printf("x = %f  %f  %f\n", b[0], b[1], b[2]);
    return 0;
}
Top