aamaricci / SciFortran

A library of fortran modules and routines for scientific calculations (*in a way* just like scipy for python)
GNU Lesser General Public License v3.0
169 stars 39 forks source link

CMAKE: merge LAPACK_LIBRARIES and BLAS_LIBRARIES #14

Closed band-a-prend closed 2 years ago

band-a-prend commented 2 years ago

This patch merge LAPACK_LIBRARIES and BLAS_LIBRARIES lists with removing of duplicates and replacing ";" with " " within list to provide proper linking via scifor.pc pkgconfig.

Tested against lapack and openblas system libraries (cmake configuration output of SF compilation lines:).

Using lapack initial:
-L${libdir} -lscifor /usr/lib64/liblapack.so;/usr/lib64/libblas.so /usr/lib64/libblas.so patched:
-L${libdir} -lscifor /usr/lib64/liblapack.so /usr/lib64/libblas.so

Using openblas initial:
-L${libdir} -lscifor /usr/lib64/libopenblas.so;/usr/lib64/libopenblas.so /usr/lib64/libopenblas.so patched:
-L${libdir} -lscifor /usr/lib64/libopenblas.so

Related issue: https://github.com/QcmPlab/SciFortran/issues/4#issuecomment-736931796

TESTING: The patched system installed SciFortran was tested for lapack/openblas with quick example:

! inv_triang_matrix.f90 
program inv_test

    use scifor
    implicit none

    real(8), dimension(3, 3) :: A, A_copy
    A = reshape([ 2.0d0, 0.0d0, 0.0d0, 0.0d0, 5.0d0, 0.0d0, 0.0d0, 0.0d0, 8.0d0 ], shape(A))
    write (*, '(3f8.3)' ) A
    A_copy = A

   call inv_triang(A)
   print *, "---------------------------"
   write (*, '(3f8.3)' ) A
   print *, "---------------------------"
   write (*, '(3f8.3)' ) A*A_copy

end program inv_test

! Output:

!   2.000   0.000   0.000
!   0.000   5.000   0.000
!   0.000   0.000   8.000
! ---------------------------
!   0.500   0.000   0.000
!  -0.000   0.200   0.000
!  -0.000  -0.000   0.125
! ---------------------------
!   1.000   0.000   0.000
!  -0.000   1.000   0.000
!  -0.000  -0.000   1.000

Result was compared with python implementation (in fortran case matrix is transpose but it's no matter for triang matrix):

# matrix_inv.py 
from scipy import linalg
import numpy as np

b = np.array([[2., 0., 0.], [0., 5., 0.], [0., 0., 8.]])
print(b)
print('---------------------------')
print(linalg.inv(b))
#[[ 0.5    0.    -0.   ]
# [ 0.     0.2   -0.   ]
# [ 0.     0.     0.125]]
print('---------------------------')
print(np.dot(b, linalg.inv(b)))
#[[1. 0. 0.]
# [0. 1. 0.]
# [0. 0. 1.]]
band-a-prend commented 2 years ago

I.e. in my case cmake LAPACK_LIBRARIES additionally returns blas implementation library too, but I can't find the proof of that in cmake documentation.

Therefore the separate BLAS_LIBRARIES isn't dropped here.

aamaricci commented 2 years ago

Thanks. I merged this in the master branch. Indeed, this patch solve a long standing issue...

Note that BLAS and LAPACK libraries are basically found together within CMake macro.