Linux system C language calls Lapack, Blas Library

Source: Internet
Author: User
Tags lapack gfortran

In the use of C programming, often call other software packages, where the Lapack,blas library is the most commonly used two libraries, here is the Linux system, the C language programming how to call these two libraries:

1. Let's start with the call to the Blas library, with two vector internal product functions as an example:

#include <stdio.h>#include<math.h>DoubleDdot_ (int*,Double*,int*,Double*,int*);intMain () {intn=2, incx=1, incy=1; Doublex[2]={1.0,1.0}; Doubley[2]={2.0,2.0}; Doublere; Re=ddot_ (&n, X, &incx, Y, &incy); printf ("The result is:%f\n", re); return 0;}

Compile the build target file:

Gcc-c Testddot.c-o TESTDDOT.O

To generate an executable file:

Gcc-o Testddot Testddot.o-lblas-lgfortran   

Get results:

is:4.000000

Note: The Blas library is written in Gfortran, so you need to use the Gfortran library to generate the executable file, and need to link Blas library path, because I put the Blas library in the system default path, so here only need-lblas, otherwise, The path to the Blas library needs to be written out.

2. The following is a Lapack library call method, here to solve the linear equation group as an example.

#include <stdio.h>#include"lapacke.h"#defineM 2#defineN 2intMain () {intI, J, N=n, m=M; intinfo, ipiv[n]; DoubleA[m * n]= {1,2,4,2}; DoubleB[m*n] = {5,4,2.5,2}; Dgesv_ (&n, &n, A, &n, Ipiv, B, &n, &info);  for(j=0; j<2*n; j + +) printf ("%f\t", B[j]); printf ("\ n"); return 0;}

Compile the build target file:

Gcc-o test_dgesv.o-c test_dgesv.c

To generate an executable file:

gcc-  o test_dgesv test_dgesv.o  -llapack  -lblas  

To run the program:

./TEST_DGESV

The results are as follows:

1.000000    1.000000    0.500000    0.500000

Note: The code in Blas and Lapack is written in Fortran, and the matrix in Fortran is stored by column, while the matrix in c is stored by row. Make sure you remember that when you're programmed.

Linux system C language calls Lapack, Blas Library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.