From 6c9c68c93dfdb143422f26868abab97ffd9d727a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Thu, 20 May 2021 19:21:59 +0200 Subject: [PATCH 1/2] Added LAPACK option to test_h5.cpp to compare residuals and number of operations. --- Makefile | 14 ++++++++------ include/SM_Helpers.hpp | 3 +++ src/SM_Helpers.cpp | 18 ++++++++++++++++++ tests/test_h5.cpp | 3 +++ 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index f144ad7..9cf057d 100644 --- a/Makefile +++ b/Makefile @@ -21,11 +21,13 @@ else $(error No valid compiler environment set in $$ENV. \ First run: $$ source smvars.sh {intel | llvm | gnu}) endif -CXXFLAGS = $(OPT) $(ARCH) $(DEBUG) -fPIC $(THRESHOLD) +CXXFLAGS = $(OPT) $(ARCH) $(DEBUG) $(THRESHOLD) -fPIC FFLAGS = $(CXXFLAGS) H5CXX = h5c++ H5CXXFLAGS = $(CXXFLAGS) FLIBS = -lstdc++ +LFLAGS = -mkl=sequential +H5LFLAGS = -L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl ## Includes and dependencies INCLUDE = -I $(INC_DIR)/ @@ -93,16 +95,16 @@ $(OBJ_DIR)/%.o: $(TST_DIR)/%.f90 | $(OBJ_DIR) #### LINKING $(BIN_DIR)/cMaponiA3_test_3x3_3: $(OBJ_DIR)/cMaponiA3_test_3x3_3.o $(DEPS_CXX) | $(BIN_DIR) - $(CXX) -o $@ $^ + $(CXX) $(LFLAGS) -o $@ $^ $(BIN_DIR)/test_h5: $(OBJ_DIR)/test_h5.o $(DEPS_CXX) | $(BIN_DIR) - $(H5CXX) -o $@ $^ + $(H5CXX) $(H5LFLAGS) -o $@ $^ $(BIN_DIR)/fMaponiA3_test_3x3_3: $(DEPS_F) $(OBJ_DIR)/fMaponiA3_test_3x3_3.o | $(BIN_DIR) - $(FC) $(FLIBS) -o $@ $^ + $(FC) $(LFLAGS) $(FLIBS) -o $@ $^ $(BIN_DIR)/fMaponiA3_test_4x4_2: $(DEPS_F) $(OBJ_DIR)/fMaponiA3_test_4x4_2.o | $(BIN_DIR) - $(FC) $(FLIBS) -o $@ $^ + $(FC) $(LFLAGS) $(FLIBS) -o $@ $^ $(BIN_DIR)/QMCChem_dataset_test: $(DEPS_F) $(OBJ_DIR)/QMCChem_dataset_test.o | $(BIN_DIR) - $(FC) $(FLIBS) -o $@ $^ + $(FC) $(LFLAGS) $(FLIBS) -o $@ $^ diff --git a/include/SM_Helpers.hpp b/include/SM_Helpers.hpp index 0840dcc..334af69 100644 --- a/include/SM_Helpers.hpp +++ b/include/SM_Helpers.hpp @@ -4,6 +4,7 @@ #include #include #include +#include // #define DEBUG @@ -18,6 +19,8 @@ void selectLargestDenominator(unsigned int l, unsigned int N_updates, unsigned int *Updates_index, unsigned int *p, double ***ylk); +lapack_int inverse(double *A, unsigned n); + template void showScalar(T scalar, std::string name) { std::cout << name << " = " << scalar << std::endl << std::endl; } diff --git a/src/SM_Helpers.cpp b/src/SM_Helpers.cpp index 157bac4..5bc9e1c 100644 --- a/src/SM_Helpers.cpp +++ b/src/SM_Helpers.cpp @@ -39,3 +39,21 @@ void selectLargestDenominator(unsigned int l, unsigned int N_updates, } Switch(p, l, lbar); } + +// Inplace inverse n x n matrix A. +// returns: +// ret = 0 on success +// ret < 0 illegal argument value +// ret > 0 singular matrix +lapack_int inverse(double *A, unsigned n) { + int ipiv[n + 1]; + lapack_int ret; + + ret = LAPACKE_dgetrf(LAPACK_COL_MAJOR, n, n, A, n, ipiv); + + if (ret != 0) + return ret; + + ret = LAPACKE_dgetri(LAPACK_COL_MAJOR, n, A, n, ipiv); + return ret; +} diff --git a/tests/test_h5.cpp b/tests/test_h5.cpp index 2724512..7cf1ec5 100644 --- a/tests/test_h5.cpp +++ b/tests/test_h5.cpp @@ -81,6 +81,9 @@ int test_cycle(H5File file, int cycle, std::string version, double tolerance) { SM3(slater_inverse, dim, nupdates, u, col_update_index); } else if (version == "sm4") { SM4(slater_inverse, dim, nupdates, u, col_update_index); + } else if (version == "lapack") { + memcpy(slater_inverse, slater_matrix, dim * dim * sizeof(double)); + inverse(slater_inverse, dim); } else { std::cerr << "Unknown version " << version << std::endl; exit(1); From c8df88b4efeb38497c2e294b72a863bb6dcdf486 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fran=C3=A7ois=20Coppens?= Date: Fri, 21 May 2021 11:54:57 +0200 Subject: [PATCH 2/2] Made lapack/MKL inclusion in code dependent on preprocessor macro 'MKL'. Automatic build should now succeed. --- Makefile | 7 +++++-- include/SM_Helpers.hpp | 5 ++++- smvars.sh | 40 ++++++++++++++++++++++++++++++++++++---- src/SM_Helpers.cpp | 2 ++ tests/test_h5.cpp | 2 ++ 5 files changed, 49 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 9cf057d..29134de 100644 --- a/Makefile +++ b/Makefile @@ -22,12 +22,15 @@ else First run: $$ source smvars.sh {intel | llvm | gnu}) endif CXXFLAGS = $(OPT) $(ARCH) $(DEBUG) $(THRESHOLD) -fPIC +ifeq ($(MKL),-DMKL) +CXXFLAGS += $(MKL) +LFLAGS = -mkl=sequential +H5LFLAGS = -L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl +endif FFLAGS = $(CXXFLAGS) H5CXX = h5c++ H5CXXFLAGS = $(CXXFLAGS) FLIBS = -lstdc++ -LFLAGS = -mkl=sequential -H5LFLAGS = -L${MKLROOT}/lib/intel64 -Wl,--no-as-needed -lmkl_intel_lp64 -lmkl_sequential -lmkl_core -lpthread -lm -ldl ## Includes and dependencies INCLUDE = -I $(INC_DIR)/ diff --git a/include/SM_Helpers.hpp b/include/SM_Helpers.hpp index 334af69..3543936 100644 --- a/include/SM_Helpers.hpp +++ b/include/SM_Helpers.hpp @@ -4,8 +4,9 @@ #include #include #include +#ifdef MKL #include - +#endif // #define DEBUG #ifndef THRESHOLD @@ -19,7 +20,9 @@ void selectLargestDenominator(unsigned int l, unsigned int N_updates, unsigned int *Updates_index, unsigned int *p, double ***ylk); +#ifdef MKL lapack_int inverse(double *A, unsigned n); +#endif template void showScalar(T scalar, std::string name) { std::cout << name << " = " << scalar << std::endl << std::endl; diff --git a/smvars.sh b/smvars.sh index db22e5a..3159bd9 100644 --- a/smvars.sh +++ b/smvars.sh @@ -1,7 +1,7 @@ #!/usr/bin/env bash unset THRESHOLD +unset MKL ENV=$1 -THRESHOLD=$2 ## Set Sherman-Morrison root dir PWD=$(pwd) @@ -22,22 +22,26 @@ export SMROOT ## Set environment for hdf5-tools and Makefile case $ENV in intel) + echo "* SM build environment set to 'intel'" export HDF5_CXX=icpc export HDF5_CXXLINKER=icpc export ENV=INTEL ;; llvm) + echo "* SM build environment set to 'llvm'" export HDF5_CXX=clang++ export HDF5_CXXLINKER=clang++ export ENV=LLVM ;; vfc) + echo "* SM build environment set to 'vfc'" export HDF5_CXX=clang++ export HDF5_CXXLINKER=clang++ export ENV=LLVM export VFC_BACKENDS="libinterflop_ieee.so --count-op" ;; gnu) + echo "* SM build environment set to 'gnu'" export HDF5_CXX=g++ export HDF5_CXXLINKER=g++ export ENV=GNU @@ -45,7 +49,10 @@ case $ENV in *) echo "Unknown environment descriptor given." echo "Usage: source smvars.sh {intel | llvm | vfc | gnu}" + echo "Usage: source smvars.sh {intel | llvm | vfc | gnu} [mkl]" echo "Usage: source smvars.sh {intel | llvm | vfc | gnu} [threshold]" + echo "Usage: source smvars.sh {intel | llvm | vfc | gnu} [mkl] [threshold]" + echo "Usage: source smvars.sh {intel | llvm | vfc | gnu} [threshold] [mkl]" return 1 ;; esac @@ -57,8 +64,33 @@ then export SMVARS=true fi -## If a threshold is provided, export compiler flag -if [[ $# -gt 1 ]] +## Argument parsing +if [[ $# -eq 1 ]] then - export THRESHOLD="-DTHRESHOLD=$THRESHOLD" + echo "* Default threshold of '1e-3' will be used in next build." fi +if [[ $# -eq 2 ]] +then + if [[ $2 == mkl ]] + then + echo "* oneMKL-dependent parts of the code are enable in next build." + echo "* Default threshold of '1e-3' will be used in next build." + export MKL="-DMKL" + else + echo "* Threshold of '$2' will be used in next build." + export THRESHOLD="-DTHRESHOLD=$2" + fi +fi +if [[ $# -eq 3 ]] +then + echo "* oneMKL-dependent parts of the code are enable in next build." + export MKL="-DMKL" + if [[ $2 == mkl ]] + then + echo "* Threshold of '$3' will be used in next build." + export THRESHOLD="-DTHRESHOLD=$3" + else + echo "* Threshold of '$2' will be used in next build." + export THRESHOLD="-DTHRESHOLD=$2" + fi +fi \ No newline at end of file diff --git a/src/SM_Helpers.cpp b/src/SM_Helpers.cpp index 5bc9e1c..4d084c0 100644 --- a/src/SM_Helpers.cpp +++ b/src/SM_Helpers.cpp @@ -40,6 +40,7 @@ void selectLargestDenominator(unsigned int l, unsigned int N_updates, Switch(p, l, lbar); } +#ifdef MKL // Inplace inverse n x n matrix A. // returns: // ret = 0 on success @@ -57,3 +58,4 @@ lapack_int inverse(double *A, unsigned n) { ret = LAPACKE_dgetri(LAPACK_COL_MAJOR, n, A, n, ipiv); return ret; } +#endif \ No newline at end of file diff --git a/tests/test_h5.cpp b/tests/test_h5.cpp index 7cf1ec5..fb0ceda 100644 --- a/tests/test_h5.cpp +++ b/tests/test_h5.cpp @@ -81,9 +81,11 @@ int test_cycle(H5File file, int cycle, std::string version, double tolerance) { SM3(slater_inverse, dim, nupdates, u, col_update_index); } else if (version == "sm4") { SM4(slater_inverse, dim, nupdates, u, col_update_index); +#ifdef MKL } else if (version == "lapack") { memcpy(slater_inverse, slater_matrix, dim * dim * sizeof(double)); inverse(slater_inverse, dim); +#endif } else { std::cerr << "Unknown version " << version << std::endl; exit(1);