Compiling LAPACKe library using C in MacOS

212 Views Asked by At

I need to use LAPACKe functions in a code that should run on Linux and macOS, but the problem is under OsX. I have a MacBook Pro 2021, with an M1 Pro processor, running an OsX 12.6.2

I wrote an example code

#include <stdio.h>
#include <stdlib.h>
#ifdef __APPLE__
#include <Accelerate/Accelerate.h>
#else
#include <lapacke.h>
#endif

const int N= 3, NRHS=2, LDA=N, LDB=N;

const int NN=5, NRHS2=3;

int main(int argc, char** argv) {

    /******* Example of using the lapack library with
             the C interface for solving linear systems
             for a general full matrix                   **************/
    int ipiv[N], info;
    // a[LDA*N]
    double a[] = {
         6.80, -2.11,  5.66,
        -6.05, -3.30,  5.36,
        -0.45,  2.58, -2.70
    };
    // b[LDB*NRHS]
    double b[] = {
         4.02, 6.19, -8.22,
        -1.56, 4.00, -8.67
    };

    printf("\nTest of using LAPACKe Library\n");
    printf("Matrix A : %d by %d\n", N, N);
    for(int i=0; i<N; i++){
        int s = i;
        for(int j=0; j<N; j++, s+=LDA) printf(" % 6.2lf", a[s]);
        printf("\n");
    }

    printf("\nRight hand side %d vectors of %d\n", NRHS, N);
    for(int i=0; i<N; i++){
        int s = i;
        for(int j=0; j<NRHS; j++, s+=LDA) printf(" % 10.6lf", b[s]);
        printf("\n");
    }
    /** As long the LAPACK_COL_MAJOR is used, the matrix is
        filled up by columns, pay attention in the way is printed **/
    info = LAPACKE_dgesv( LAPACK_COL_MAJOR, N, NRHS, a, LDA, ipiv, b, LDB);

    if (info == 0) {
        printf("\nFactorization of LU : %d by %d\n", N, N);
        for(int i=0; i<N; i++){
            int s = i;
            for(int j=0; j<N; j++, s+=LDA) printf(" % 10.5lf", a[s]);
            printf("\n");
        }

        printf("\nSolution of %dright hand side vectors of %d\n", NRHS, N);
        for(int i=0; i<N; i++){
            int s = i;
            for(int j=0; j<NRHS; j++, s+=LDA) printf(" % 10.6lf", b[s]);
            printf("\n");
        }

        printf("\nFactorization pivot indices by %d\n", N);
        for(int i=0; i<N; i++) printf(" % 5.0d", ipiv[i]);
        printf("\n\n");

    } // End of if (info == 0)
    else
        printf("An error ocurred in the LAPACK lib dgesv, with code %d\n", info);


    /******* Example of using the lapack library with
         the C interface for solving linear systems
         for a trigiagonal matrix                     **************/
    int ldb = NN;
    // d1[NN-1] lower diagonal
    double dl[] = {1, 4, 4, 1};
    // d[NN] main diagonal
    double d[] = {-2, -2, -2, -2, -2};
    // du[NN-1] upper diagonal
    double du[] = {1, 4, 4, 1};

    // bb[NN*NRHS2] number of righ hand side vectors
    double bb[] = {
         3.,    5.,    5.,    5.,    3.,
        -1.56,  4.,   -8.67,  1.75,  2.86,
         9.81, -4.09, -4.57, -8.61,  8.99
    };

    info = LAPACKE_dgtsv(LAPACK_COL_MAJOR, NN, NRHS2, dl, d, du, bb, ldb);

    if (info == 0) {
        printf("\nTest of using LAPACKe Library for Tridiagonal systems\n");
        printf("\nSolution of %dright hand side vectors of %d\n", NRHS2, NN);
        for(int i=0; i<NN; i++){
            int s = i;
            for(int j=0; j<NRHS2; j++, s+=ldb) printf(" % 10.6lf", bb[s]);
            printf("\n");
        }

    } // End of if (info == 0)
    else
        printf("An error ocurred in the LAPACK lib dgesv, with code %d\n", info);

    return 0;
}

This runs in a WSL (Windows Subsystem Linux) and compiles successfully making the command gcc example2.c -o lapack -llapacke

But, when I try to compile it in my Mac using the line gcc example2.c -o lapacke -framework Accelerate

I receive the following error:

example2.c:47:9: error: implicit declaration of function 'LAPACKE_dgesv' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        info = LAPACKE_dgesv( LAPACK_COL_MAJOR, N, NRHS, a, LDA, ipiv, b, LDB);
               ^
example2.c:47:24: error: use of undeclared identifier 'LAPACK_COL_MAJOR'
        info = LAPACKE_dgesv( LAPACK_COL_MAJOR, N, NRHS, a, LDA, ipiv, b, LDB);
                              ^
example2.c:91:9: error: implicit declaration of function 'LAPACKE_dgtsv' is invalid in C99 [-Werror,-Wimplicit-function-declaration]
        info = LAPACKE_dgtsv(LAPACK_COL_MAJOR, NN, NRHS2, dl, d, du, bb, ldb);
               ^
example2.c:91:9: note: did you mean 'LAPACKE_dgesv'?
example2.c:47:9: note: 'LAPACKE_dgesv' declared here
        info = LAPACKE_dgesv( LAPACK_COL_MAJOR, N, NRHS, a, LDA, ipiv, b, LDB);
               ^
example2.c:91:23: error: use of undeclared identifier 'LAPACK_COL_MAJOR'
        info = LAPACKE_dgtsv(LAPACK_COL_MAJOR, NN, NRHS2, dl, d, du, bb, ldb);
                             ^
4 errors generated.

It looks like inside the Accelerate.h the headers of LAPACKe functions are not defined. I was looking around but I don't see anything that can help me.

BTW, I have another example using cblas and it runs smoothly

0

There are 0 best solutions below