From 4584cbd3fd3a3366462868e09e87aaa61c00bc17 Mon Sep 17 00:00:00 2001
From: Giovanni La Mura <giovanni.lamura@inaf.it>
Date: Tue, 9 Jul 2024 20:12:16 +0200
Subject: [PATCH] Enable LAPACK detection configuration

---
 build/Makefile.am  |   2 +-
 build/configure.ac | 304 ++++++++++++++++++++++++++++-----------------
 2 files changed, 192 insertions(+), 114 deletions(-)

diff --git a/build/Makefile.am b/build/Makefile.am
index 319ff058..ad10a23f 100644
--- a/build/Makefile.am
+++ b/build/Makefile.am
@@ -1,5 +1,5 @@
 AUTOMAKE_OPTIONS=subdir-objects
-LDADD=libnptm/libnptm.la -L/usr/lib64 $(HDF5_LDFLAGS)
+LDADD=libnptm/libnptm.la -L/usr/lib64 ${HDF5_LDFLAGS} ${LAPACK_LDFLAGS}
 lib_LTLIBRARIES=libnptm/libnptm.la
 libnptm_libnptm_la_SOURCES=../src/libnptm/algebraic.cpp ../src/libnptm/clu_subs.cpp ../src/libnptm/Commons.cpp ../src/libnptm/Configuration.cpp ../src/libnptm/file_io.cpp ../src/libnptm/lapack_calls.cpp ../src/libnptm/logging.cpp ../src/libnptm/magma_calls.cpp ../src/libnptm/Parsers.cpp ../src/libnptm/sph_subs.cpp ../src/libnptm/tfrfme.cpp ../src/libnptm/TransitionMatrix.cpp ../src/libnptm/tra_subs.cpp ../src/libnptm/types.cpp
 bin_PROGRAMS=cluster/edfb_clu cluster/clu cluster/np_cluster sphere/edfb_sph sphere/sph sphere/np_sphere trapping/frfme trapping/lffft trapping/np_trapping
diff --git a/build/configure.ac b/build/configure.ac
index 92494cc5..17cf1706 100644
--- a/build/configure.ac
+++ b/build/configure.ac
@@ -1,7 +1,8 @@
 # CAPABILITY TESTING MACROS
 m4_define(
-	[M4_HDF5_LIB],
-	[cat > nptm_test_hdf5.cpp <<EOF
+  [M4_HDF5_LIB],
+  [
+    cat > nptm_test_hdf5.cpp <<EOF
 #include <hdf5.h>
 int main(int argc, char **argv) {
   hid_t file_id = 0;
@@ -9,14 +10,30 @@ int main(int argc, char **argv) {
   return (int)status;
 }
 EOF
-	$CXX -o nptm_test_hdf5 nptm_test_hdf5.cpp -I$HDF5_INCLUDE -L$HDF5_LIB -lhdf5 > /dev/null 2>>error.log
-	export TEST_HDF5_LIB=$?
-	rm nptm_test_hdf5.cpp
-	if test "x$TEST_HDF5_LIB" = "x0"; then
-	  rm nptm_test_hdf5
-	fi]
+    $CXX -o nptm_test_hdf5 nptm_test_hdf5.cpp -I$HDF5_INCLUDE -L$HDF5_LIB -lhdf5 > /dev/null 2>>error.log
+    export TEST_HDF5_LIB=$?
+    rm nptm_test_hdf5.cpp
+    if test "x$TEST_HDF5_LIB" = "x0"; then
+      rm nptm_test_hdf5
+    fi
+  ]
+)
+
+m4_define(
+  [M4_DETECT_LAPACK],
+  [
+    LAPACK_ROOT=$MKLROOT
+    if test "x$LAPACK_ROOT" != "x"; then
+      export LAPACK_INCLUDE=$(MKLROOT)/include
+      export LAPACK_LIB=$(MKLROOT)/lib
+      export LAPACK_LIBRARY="-Wl,--no-as-needed -lmkl_intel_ilp64 -lmkl_gnu_thread -lmkl_core -lgomp -lpthread -lm -ldl"
+    else
+      export LAPACK_INCLUDE=/usr/include
+      export LAPACK_LIB=/lib/x86_64-linux-gnu
+      export LAPACK_LIBRARY="-llapacke64"
+    fi
+  ]
 )
-  
 # END CAPABILITY TESTING MACROS
 
 # autoconf setup initialization
@@ -34,130 +51,191 @@ AM_INIT_AUTOMAKE([-Wall -Werror foreign])
 # Compiler detection
 AM_PROG_AR
 AC_ARG_ENABLE(
-	[mpi],
-	[AS_HELP_STRING([--enable-mpi], [use MPI if available (default is yes)])],
-	[
-		if test "x$enableval" = "xno"; then
-		  if test "x$F77" = "xmpif90"; then
-		    gfortran --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export F77=gfortran
-		    fi
-		  fi
-		  if test "x$F77" = "xmpif90"; then
-		    f77 --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export F77=f77
-		    fi
-		  fi
-		  if test "x$CXX" = "xmpicxx"; then
-		    g++ --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export CXX=g++
-		    fi
-		  fi
-		  if test "x$CXX" = "xmpicxx"; then
-		    clang --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export CXX=clang
-		    fi
-		  fi
-		  if test "x$F77" = "x"; then
-		    gfortran --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export F77=gfortran
-		    fi
-		  fi
-		  if test "x$F77" = "x"; then
-		    f77 --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export F77=f77
-		    fi
-		  fi
-		  if test "x$CXX" = "x"; then
-		    g++ --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export CXX=g++
-		    fi
-		  fi
-		  if test "x$CXX" = "x"; then
-		    clang --version > /dev/null
-		    if test "x$?" = "x0"; then
-		      export CXX=clang
-		    fi
-		  fi
-		else
-		  mpicxx --version > /dev/null
-		  if test "x$?" = "x0"; then
-		    export CXX=mpicxx
-		    export F77=mpif90
-		  else
-		    AC_MSG_ERROR(["MPI was enabled, but no MPI compiler was found!"])
-		  fi
-		fi
-	],
-	[
-		if test "x$CXX" = "x"; then
-		  mpicxx --version > /dev/null
-		  if test "x$?" = "x0"; then
-		    export CXX=mpicxx
-		    export F77=mpif90
-		  fi
-		fi
-		if test "x$CXX" = "x"; then
-		  g++ --version > /dev/null
-		  if test "x$?" = "x0"; then
-		    export CXX=g++
-		  fi
-		fi
-		if test "x$CXX" = "x"; then
-		  clang --version > /dev/null
-		  if test "x$?" = "x0"; then
-		    export CXX=clang
-		  fi
-		fi
-	]
+  [mpi],
+  [AS_HELP_STRING([--enable-mpi], [use MPI if available [default=yes]])],
+  [
+    if test "x$enableval" = "xno"; then
+      if test "x$F77" = "xmpif90"; then
+        gfortran --version > /dev/null
+        if test "x$?" = "x0"; then
+          AC_SUBST([F77], [gfortran])
+        fi
+      fi
+      if test "x$F77" = "xmpif90"; then
+        f77 --version > /dev/null
+        if test "x$?" = "x0"; then
+          #export F77=f77
+          AC_SUBST([F77], [f77])
+        fi
+      fi
+      if test "x$CXX" = "xmpicxx"; then
+        g++ --version > /dev/null
+        if test "x$?" = "x0"; then
+          #export CXX=g++
+          AC_SUBST([CXX], [g++])
+        fi
+      fi
+      if test "x$CXX" = "xmpicxx"; then
+        clang --version > /dev/null
+        if test "x$?" = "x0"; then
+          #export CXX=clang
+          AC_SUBST([CXX], [clang])
+        fi
+      fi
+      if test "x$F77" = "x"; then
+        gfortran --version > /dev/null
+        if test "x$?" = "x0"; then
+          #export F77=gfortran
+          AC_SUBST([F77], [gfortran])
+        fi
+      fi
+      if test "x$F77" = "x"; then
+        f77 --version > /dev/null
+        if test "x$?" = "x0"; then
+          #export F77=f77
+          AC_SUBST([F77], [f77])
+        fi
+      fi
+      if test "x$CXX" = "x"; then
+        g++ --version > /dev/null
+        if test "x$?" = "x0"; then
+          #export CXX=g++
+          AC_SUBST([CXX], [g++])
+        fi
+      fi
+      if test "x$CXX" = "x"; then
+        clang --version > /dev/null
+        if test "x$?" = "x0"; then
+          #export CXX=clang
+          AC_SUBST([CXX], [clang])
+        fi
+      fi
+    else
+      mpicxx --version > /dev/null
+      if test "x$?" = "x0"; then
+        #export CXX=mpicxx
+        #export F77=mpif90
+        AC_SUBST([CXX], [mpicxx])
+        AC_SUBST([F77], [mpif90])
+      else
+        AC_MSG_ERROR(["MPI was enabled, but no MPI compiler was found!"])
+      fi
+    fi
+  ],
+  [
+    if test "x$CXX" = "x"; then
+      mpicxx --version > /dev/null
+      if test "x$?" = "x0"; then
+        #export CXX=mpicxx
+        #export F77=mpif90
+        AC_SUBST([CXX], [mpicxx])
+        AC_SUBST([F77], [mpif90])
+      fi
+    fi
+    if test "x$CXX" = "x"; then
+      g++ --version > /dev/null
+      if test "x$?" = "x0"; then
+        #export CXX=g++
+        AC_SUBST([CXX], [g++])
+      fi
+    fi
+    if test "x$CXX" = "x"; then
+      clang --version > /dev/null
+      if test "x$?" = "x0"; then
+        #export CXX=clang
+        AC_SUBST([CXX], [clang])
+      fi
+    fi
+  ]
 )
-AC_SUBST([CXX])
-AC_PROG_CXX(CXX)
-AC_SUBST([F77])
-AC_PROG_F77([$F77 mpif90 gfortran f77])
+AC_PROG_CXX([$CXX])
+AC_PROG_F77([$F77])
 
 # Libtool initialization
 LT_INIT
 
 # Environment setup
 AS_IF(
-	[test "x$HDF5_INCLUDE" = "x"],
-	[export HDF5_INCLUDE=/usr/include/hdf5/serial],
-	[AC_MSG_NOTICE(["HDF5_INCLUDE=$HDF5_INCLUDE"])]
+  [test "x$HDF5_INCLUDE" = "x"],
+  [AC_SUBST([HDF5_INCLUDE], ["/usr/include/hdf5/serial"])],
+  [AC_MSG_NOTICE([HDF5_INCLUDE=$(HDF5_INCLUDE)])]
 )
 AS_IF(
-	[test "x$HDF5_LIB" = "x"],
-	[export HDF5_LIB=/usr/lib/x86_64-linux-gnu/hdf5/serial],
-	[AC_MSG_NOTICE(["HDF5_LIB=$HDF5_LIB"])]
+  [test "x$HDF5_LIB" = "x"],
+  [AC_SUBST([HDF5_LIB], ["/usr/lib/x86_64-linux-gnu/hdf5/serial"])],
+  [AC_MSG_NOTICE([HDF5_LIB=$(HDF5_LIB)])]
 )
-FFLAGS="-std=legacy -O3"
-CXXFLAGS="-O3 -ggdb -Wno-format-contains-nul -I $HDF5_INCLUDE"
-SUBDIRS="cluster libnptm sphere testing trapping"
 
 # Check for required headers
 AC_CHECK_HEADER(
-	[$HDF5_INCLUDE/hdf5.h],
-	,
-	AC_MSG_ERROR(["Could not find HDF5 headers!]),
-	)
+  [$HDF5_INCLUDE/hdf5.h],
+  ,
+  AC_MSG_ERROR(["Could not find HDF5 headers!]),
+)
 
 # Check for required libraries
 M4_HDF5_LIB
 AS_IF(
-	[test "x$TEST_HDF5_LIB" = "x0"],
-	[export HDF5_LDFLAGS="-L${HDF5_LIB} -lhdf5"],
-	[AC_MSG_ERROR(["HDF5 library not found!"])]
+  [test "x$TEST_HDF5_LIB" = "x0"],
+  [AC_SUBST([HDF5_LDFLAGS], ["-L${HDF5_LIB} -lhdf5"])],
+  [AC_MSG_ERROR(["HDF5 library not found!"])]
 )
-AC_SUBST([HDF5_LDFLAGS])
 
 # Configure the optional features
+M4_DETECT_LAPACK
+AS_IF(
+  [test "x$LAPACK_LIBRARY" != "x"],
+  [AC_MSG_NOTICE([LAPACK detected])],
+  [AC_MSG_NOTICE([No LAPACK found])]
+)
+
+AC_ARG_ENABLE(
+  [openmp],
+  [AS_HELP_STRING([--enable-openmp], [enable OpneMP multi-threading [default=yes]])],
+  [
+    if test "x$enableval" != "xno"; then
+      AC_SUBST([OMPFLAGS], ["-fopenmp"])
+    fi
+  ],
+  [
+    AC_SUBST([OMPFLAGS], ["-fopenmp"])
+  ]
+)
+
+AC_ARG_WITH(
+  [lapack],
+  [AS_HELP_STRING([--with-lapack], [use LAPACK @<:@default=check@:>@])],
+  [
+    if test "x$withval" = "xno"; then
+      AC_SUBST([LAPACK_FLAGS], [""])
+      AC_SUBST([LAPACK_LDFLAGS], [""])
+    fi
+  ],
+  [
+    if test "x$LAPACK_INCLUDE" != "x"; then
+      AC_SUBST([LAPACK_FLAGS], ["-DUSE_LAPACK -DLAPACK_ILP64 -DUSE_ILP64 -I${LAPACK_INCLUDE}"])
+      AC_SUBST([LAPACK_LDFLAGS], ["-L${LAPACK_LIB} ${LAPACK_LIBRARY}"])
+    fi
+  ]
+)
+
+AC_ARG_WITH(
+  [magma],
+  [AS_HELP_STRING([--with-magma], [use MAGMA @<:@default=check@:>@])],
+  [],
+  []
+)
+
+AC_ARG_ENABLE(
+  [ilp64],
+  [AS_HELP_STRING([--enable-ilp64], [use MAGMA @<:@default=check@:>@])],
+  [],
+  []
+)
+
+FFLAGS="-std=legacy -O3"
+CXXFLAGS="-O3 -ggdb -Wno-format-contains-nul -I$HDF5_INCLUDE $OMPFLAGS $LAPACK_FLAGS"
+SUBDIRS="cluster libnptm sphere testing trapping"
 
 # Send output to Makefiles
 AC_CONFIG_FILES([Makefile])
-- 
GitLab