Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linking LAPACK from Intel MKL with gfortran

I have a problem to link lapack to fortran example program. Here is the program example.f95

Program LinearEquations
! solving the matrix equation A*x=b using LAPACK
Implicit none

! declarations
double precision :: A(3,3), b(3)
integer :: i, pivot(3), ok

! matrix A
A(1,:)=(/3, 1, 3/)
A(2,:)=(/1, 5, 9/)
A(3,:)=(/2, 6, 5/)

! vector b
b(:)=(/-1, 3, -3/)
!b(:)=(/2, 2, 9/)

! find the solution using the LAPACK routine DGESV
call DGESV(3, 1, A, 3, pivot, b, 3, ok)

! print the solution x
do i=1, 3
  write(*,9) i, b(i)
end do  

9  format('x[', i1, ']= ', f5.2)  
end program LinearEquations

I have installed libraries here

/opt/intel/compilers_and_libraries_2017.4.196/linux/mkl/lib/intel64_lin/libmkl_lapack95_ilp64.a

I am using gfortran to compile program:

gfortran -o example example.f95 -L/opt/intel/compilers_and_libraries_2017.4.196/linux/mkl/lib/intel64_lin/libmkl_lapack95_ilp64.a

it complain

 /tmp/ccWtxMFP.o: In function `MAIN__':
 example.f95:(.text+0xf0): undefined reference to `dgesv_'
 collect2: error: ld returned 1 exit status

Can someone help me with this issue please ? Many thanks

like image 530
Vlada Avatar asked Aug 15 '17 16:08

Vlada


1 Answers

There are two types of linking :

  • Static Linking : you link your program with static library.

    example: gfortran program.f90 /path-to-lib/libmy.a -o program.x

  • Dynamic Linking : you link your program with a shared library :

    example : gfortran program.f90 -L/path-to-lib -lmy -o program.x

    which link your program with libmy.so .

According to MKL Advisor you should use this:

-Wl,--start-group ${MKLROOT}/lib/intel64/libmkl_gf_lp64.a ${MKLROOT}/lib/intel64/libmkl_sequential.a ${MKLROOT}/lib/intel64/libmkl_core.a -Wl,--end-group -lpthread -lm -ldl

for static linking. or :

-L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_gf_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl

for dynamic linking.

Where ${MKLROOT} is the path to MKL.

like image 126
M. Chinoune Avatar answered Oct 20 '22 15:10

M. Chinoune