From afd9207162333ee9830e12b023812582b2dfe8b0 Mon Sep 17 00:00:00 2001 From: StarGrys <771582678@qq.com> Date: Mon, 15 Jan 2024 15:25:14 +0800 Subject: [PATCH 1/9] add performace test of sphbes functions. --- CMakeLists.txt | 65 +++++++++++ source/module_base/test/CMakeLists.txt | 7 ++ .../test/math_sphbes_perf_test.cpp | 103 ++++++++++++++++++ 3 files changed, 175 insertions(+) create mode 100644 source/module_base/test/math_sphbes_perf_test.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 75e5f8ce67..cc6f9f4b8e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,6 +34,8 @@ option(DEBUG_INFO "Print message for developers to debug." OFF) option(ENABLE_NATIVE_OPTIMIZATION "Enable compilation optimization for the native machine's CPU type" OFF) option(COMMIT_INFO "Print commit information in log" ON) option(ENABLE_FFT_TWO_CENTER "Enable FFT-based two-center integral method." ON) +option(ENABLE_GOOGLEBENCH "Enable GOOGLE-benchmark usage." OFF) + if (USE_CUDA) set(USE_CUSOLVER_LCAO ON) else() @@ -608,6 +610,69 @@ IF (BUILD_TESTING) endfunction(AddTest) endif() + +# Add performance test in abacus +IF (ENABLE_GOOGLEBENCH) + set(CMAKE_CXX_STANDARD 14) # Required in orbital + include(CTest) + enable_testing() + find_package(GTest HINTS /usr/local/lib/ ${GTEST_DIR}) + if(NOT ${GTest_FOUND}) + include(FetchContent) + FetchContent_Declare( + googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG "origin/main" + GIT_SHALLOW TRUE + GIT_PROGRESS TRUE + ) + FetchContent_MakeAvailable(googletest) + endif() + + find_package(benchmark REQUIRED) + if(NOT ${benchmark_FOUND}) + include(FetchContent) + FetchContent_Declare( + benchmark + GIT_REPOSITORY https://github.com/google/benchmark.git + GIT_TAG "origin/main" + GIT_SHALLOW TRUE + GIT_PROGRESS TRUE + ) + FetchContent_MakeAvailable(benchmark) + endif() + + function(AddPerfTest) # function for UT + add_compile_options(-O2) + cmake_parse_arguments(UT "DYN" "TARGET" "LIBS;DYN_LIBS;STATIC_LIBS;SOURCES;DEPENDS" ${ARGN}) + add_executable(${UT_TARGET} ${UT_SOURCES}) + + if(ENABLE_COVERAGE) + add_coverage(${UT_TARGET}) + endif() + + + # Link Google Benchmark to the project + target_link_libraries(${UT_TARGET} benchmark::benchmark) + + + #dependencies & link library + target_link_libraries(${UT_TARGET} ${UT_LIBS} + Threads::Threads GTest::gtest_main GTest::gmock_main) + if(USE_OPENMP) + target_link_libraries(${UT_TARGET} OpenMP::OpenMP_CXX) + endif() + install(TARGETS ${UT_TARGET} DESTINATION ${CMAKE_BINARY_DIR}/tests ) + add_test(NAME ${UT_TARGET} + COMMAND ${UT_TARGET} + WORKING_DIRECTORY $ + ) + endfunction(AddTest) +endif() + + + + add_subdirectory(source) target_link_libraries(${ABACUS_BIN_NAME} diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index 666152b476..b12cc616f3 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -217,3 +217,10 @@ AddTest( SOURCES assoc_laguerre_test.cpp ../assoc_laguerre.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp LIBS ${math_libs} formatter ) +if(ENABLE_GOOGLEBENCH) + AddPerfTest( + TARGET math_sphbes_perf_test + LIBS formatter + SOURCES math_sphbes_perf_test.cpp ../math_sphbes.cpp ../timer.cpp + ) +endif() \ No newline at end of file diff --git a/source/module_base/test/math_sphbes_perf_test.cpp b/source/module_base/test/math_sphbes_perf_test.cpp new file mode 100644 index 0000000000..998a8091e9 --- /dev/null +++ b/source/module_base/test/math_sphbes_perf_test.cpp @@ -0,0 +1,103 @@ +#include"../math_sphbes.h" +#include +#include +#include +#include + +/************************************************ +* performace test of class Integral +***********************************************/ + +/** + * Note: this performace test try to measure the CPU time + * of the spherical Bessel produced by class Sphbes, + * and the memory usage of these functions; + * at 2024-1-14 + * + * Tested function: + * - Spherical_Bessel. + * - Spherical_Bessel_Roots + * - overloading of Spherical_Bessel. This funnction sets sjp[i] to 1.0 when i < msh. + * - sphbesj + * - sphbes_zeros + */ + + +int msh = 700; +int l0 = 0; +int l1 = 1; +int l2 = 2; +int l3 = 3; +int l4 = 4; +int l5 = 5; +int l6 = 6; +int l7 = 7; +double q = 1.0; +double *r = new double[msh]; +double *jl = new double[msh]; +double *djl = new double[msh]; + +double mean(const double* vect, const int totN) +{ + double meanv = 0.0; + for (int i=0; i< totN; ++i) {meanv += vect[i]/totN;} + return meanv; +} + + + + +// Below are the wapper functions which contain the function to measure performance +void SphericalBessel_func(){ + //int l = 0; + ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l0,jl); +} + +void dSpherical_Bessel_dx_func(){ + int il=5; + ModuleBase::Sphbes::dSpherical_Bessel_dx(msh,r,q,il,djl); +} +void SphericalBesselRoots_func(){ + int i=7; + int neign = 100; + double *eign = new double[neign]; + + ModuleBase::Sphbes::Spherical_Bessel_Roots(neign,i,1.0e-12,eign,10.0); + free(eign); + +} + +void Sphbesj_func(){ + ModuleBase::Sphbes::sphbesj(3,0); +} + + + +//Below are the test time functions +static void SphericalBessel(benchmark::State& state) { + for (auto _ : state) + SphericalBessel_func(); +} + +static void dSpherical_Bessel_dx(benchmark::State& state) { + for (auto _ : state) + dSpherical_Bessel_dx_func(); +} + +static void SphericalBesselRoots(benchmark::State& state) { + for (auto _ : state) + SphericalBesselRoots_func(); +} + +static void Sphbesj(benchmark::State& state) { + for (auto _ : state) + Sphbesj_func(); +} + + +//Add the test time functions into google benchmark +BENCHMARK(SphericalBessel); +BENCHMARK(dSpherical_Bessel_dx); +BENCHMARK(SphericalBesselRoots); +BENCHMARK(Sphbesj); +BENCHMARK_MAIN(); \ No newline at end of file From d5eeb6c48ce1044c4c74a30bbe58b9d3903b66b5 Mon Sep 17 00:00:00 2001 From: Jie Li Date: Thu, 18 Jan 2024 03:04:11 +0000 Subject: [PATCH 2/9] fix benchmark cmake errors --- CMakeLists.txt | 86 +++++++------------------- source/module_base/test/CMakeLists.txt | 2 +- 2 files changed, 25 insertions(+), 63 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index cc6f9f4b8e..12f12fc582 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -568,52 +568,26 @@ if(INFO) # modifications on blas_connector and lapack_connector endif() -IF (BUILD_TESTING) - set_if_higher(CMAKE_CXX_STANDARD 14) # Required in orbital - include(CTest) - enable_testing() - find_package(GTest HINTS /usr/local/lib/ ${GTEST_DIR}) - if(NOT ${GTest_FOUND}) +# Add performance test in abacus +IF (ENABLE_GOOGLEBENCH) + set(BUILD_TESTING ON) + find_package(benchmark HINTS ${BENCHMARK_DIR}) + if(NOT ${benchmark_FOUND}) + set(BENCHMARK_USE_BUNDLED_GTEST OFF) include(FetchContent) FetchContent_Declare( - googletest - GIT_REPOSITORY https://github.com/google/googletest.git + benchmark + GIT_REPOSITORY https://github.com/google/benchmark.git GIT_TAG "origin/main" GIT_SHALLOW TRUE GIT_PROGRESS TRUE ) - FetchContent_MakeAvailable(googletest) + FetchContent_MakeAvailable(benchmark) endif() - # TODO: Try the GoogleTest module. - # https://cmake.org/cmake/help/latest/module/GoogleTest.html - add_subdirectory(tests) # Contains integration tests - - function(AddTest) # function for UT - cmake_parse_arguments(UT "DYN" "TARGET" "LIBS;DYN_LIBS;STATIC_LIBS;SOURCES;DEPENDS" ${ARGN}) - add_executable(${UT_TARGET} ${UT_SOURCES}) - - if(ENABLE_COVERAGE) - add_coverage(${UT_TARGET}) - endif() - - #dependencies & link library - target_link_libraries(${UT_TARGET} ${UT_LIBS} - Threads::Threads GTest::gtest_main GTest::gmock_main) - if(USE_OPENMP) - target_link_libraries(${UT_TARGET} OpenMP::OpenMP_CXX) - endif() - install(TARGETS ${UT_TARGET} DESTINATION ${CMAKE_BINARY_DIR}/tests ) - add_test(NAME ${UT_TARGET} - COMMAND ${UT_TARGET} - WORKING_DIRECTORY $ - ) - endfunction(AddTest) endif() - -# Add performance test in abacus -IF (ENABLE_GOOGLEBENCH) - set(CMAKE_CXX_STANDARD 14) # Required in orbital +IF (BUILD_TESTING) + set_if_higher(CMAKE_CXX_STANDARD 14) # Required in orbital include(CTest) enable_testing() find_package(GTest HINTS /usr/local/lib/ ${GTEST_DIR}) @@ -628,22 +602,11 @@ IF (ENABLE_GOOGLEBENCH) ) FetchContent_MakeAvailable(googletest) endif() + # TODO: Try the GoogleTest module. + # https://cmake.org/cmake/help/latest/module/GoogleTest.html + add_subdirectory(tests) # Contains integration tests - find_package(benchmark REQUIRED) - if(NOT ${benchmark_FOUND}) - include(FetchContent) - FetchContent_Declare( - benchmark - GIT_REPOSITORY https://github.com/google/benchmark.git - GIT_TAG "origin/main" - GIT_SHALLOW TRUE - GIT_PROGRESS TRUE - ) - FetchContent_MakeAvailable(benchmark) - endif() - - function(AddPerfTest) # function for UT - add_compile_options(-O2) + function(AddTest) # function for UT cmake_parse_arguments(UT "DYN" "TARGET" "LIBS;DYN_LIBS;STATIC_LIBS;SOURCES;DEPENDS" ${ARGN}) add_executable(${UT_TARGET} ${UT_SOURCES}) @@ -651,14 +614,15 @@ IF (ENABLE_GOOGLEBENCH) add_coverage(${UT_TARGET}) endif() - - # Link Google Benchmark to the project - target_link_libraries(${UT_TARGET} benchmark::benchmark) - - #dependencies & link library - target_link_libraries(${UT_TARGET} ${UT_LIBS} - Threads::Threads GTest::gtest_main GTest::gmock_main) + if(ENABLE_GOOGLEBENCH) + target_link_libraries(${UT_TARGET} ${UT_LIBS} + Threads::Threads GTest::gtest_main GTest::gmock_main benchmark::benchmark) + else() + target_link_libraries(${UT_TARGET} ${UT_LIBS} + Threads::Threads GTest::gtest_main GTest::gmock_main) + endif() + if(USE_OPENMP) target_link_libraries(${UT_TARGET} OpenMP::OpenMP_CXX) endif() @@ -668,10 +632,8 @@ IF (ENABLE_GOOGLEBENCH) WORKING_DIRECTORY $ ) endfunction(AddTest) -endif() - - +endif() add_subdirectory(source) diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index b12cc616f3..3b29fa9eb3 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -218,7 +218,7 @@ AddTest( LIBS ${math_libs} formatter ) if(ENABLE_GOOGLEBENCH) - AddPerfTest( + AddTest( TARGET math_sphbes_perf_test LIBS formatter SOURCES math_sphbes_perf_test.cpp ../math_sphbes.cpp ../timer.cpp From ebdb5c4765463744552e6f3e57e13af67c8188d7 Mon Sep 17 00:00:00 2001 From: Jie Li Date: Fri, 19 Jan 2024 09:52:21 +0000 Subject: [PATCH 3/9] add dependencies for docker --- CMakeLists.txt | 3 --- Dockerfile.cuda | 2 +- Dockerfile.gnu | 2 +- Dockerfile.intel | 2 +- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index dd5d79ff09..9b8ad6333d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -34,9 +34,7 @@ option(DEBUG_INFO "Print message for developers to debug." OFF) option(ENABLE_NATIVE_OPTIMIZATION "Enable compilation optimization for the native machine's CPU type" OFF) option(COMMIT_INFO "Print commit information in log" ON) option(ENABLE_FFT_TWO_CENTER "Enable FFT-based two-center integral method." ON) -<<<<<<< HEAD option(ENABLE_GOOGLEBENCH "Enable GOOGLE-benchmark usage." OFF) -======= option(ENABLE_RAPIDJSON "Enable rapid-json usage." OFF) @@ -64,7 +62,6 @@ if(ENABLE_RAPIDJSON) include_directories(${RapidJSON_INCLUDE_PATH}) endif() ->>>>>>> upstream/develop if (USE_CUDA) set(USE_CUSOLVER_LCAO ON) diff --git a/Dockerfile.cuda b/Dockerfile.cuda index 719f7c4278..e950f097f9 100644 --- a/Dockerfile.cuda +++ b/Dockerfile.cuda @@ -2,7 +2,7 @@ FROM nvidia/cuda:12.2.0-devel-ubuntu22.04 RUN apt update && apt install -y --no-install-recommends \ libopenblas-openmp-dev liblapack-dev libscalapack-mpi-dev libelpa-dev libfftw3-dev libcereal-dev \ - libxc-dev libgtest-dev libgmock-dev python3-numpy \ + libxc-dev libgtest-dev libgmock-dev libbenchmark-dev python3-numpy \ bc cmake git g++ make bc time sudo unzip vim wget ENV GIT_SSL_NO_VERIFY=true TERM=xterm-256color \ diff --git a/Dockerfile.gnu b/Dockerfile.gnu index 0b6b45d248..060d930563 100644 --- a/Dockerfile.gnu +++ b/Dockerfile.gnu @@ -1,7 +1,7 @@ FROM ubuntu:22.04 RUN apt update && apt install -y --no-install-recommends \ libopenblas-openmp-dev liblapack-dev libscalapack-mpi-dev libelpa-dev libfftw3-dev libcereal-dev \ - libxc-dev libgtest-dev libgmock-dev python3-numpy \ + libxc-dev libgtest-dev libgmock-dev libbenchmark-dev python3-numpy \ bc cmake git g++ make bc time sudo unzip vim wget gfortran ENV GIT_SSL_NO_VERIFY=true TERM=xterm-256color \ diff --git a/Dockerfile.intel b/Dockerfile.intel index 6cac8c9f5f..3947f05b9e 100644 --- a/Dockerfile.intel +++ b/Dockerfile.intel @@ -2,7 +2,7 @@ FROM ubuntu:22.04 RUN apt-get update && apt-get install -y \ bc cmake git gnupg gcc g++ python3-numpy sudo wget vim unzip \ - libcereal-dev libxc-dev libgtest-dev libgmock-dev + libcereal-dev libxc-dev libgtest-dev libgmock-dev libbenchmark-dev # Following steps by https://software.intel.com/content/www/us/en/develop/documentation/installation-guide-for-intel-oneapi-toolkits-linux/top/installation/install-using-package-managers/apt.html . RUN wget -O- https://apt.repos.intel.com/intel-gpg-keys/GPG-PUB-KEY-INTEL-SW-PRODUCTS.PUB \ From ea439d14ee5a509e79314a391c505b4c79ec20c2 Mon Sep 17 00:00:00 2001 From: Jie Li Date: Mon, 22 Jan 2024 03:35:16 +0000 Subject: [PATCH 4/9] update docs --- docs/advanced/install.md | 10 ++++++++++ docs/quick_start/easy_install.md | 1 + 2 files changed, 11 insertions(+) diff --git a/docs/advanced/install.md b/docs/advanced/install.md index e929fac34c..d6201a060f 100644 --- a/docs/advanced/install.md +++ b/docs/advanced/install.md @@ -69,6 +69,16 @@ After building and installing, unit tests can be performed with `ctest`. To run a subset of unit test, use `ctest -R ` to perform tests with name matched by given pattern. +## Build Performance Tests + +To build performance tests for ABACUS, define `ENABLE_GOOGLEBENCH` flag. You can also specify the path to a local installation of [Google Benchmark](https://github.com/google/benchmark.git) by setting `BENCHMARK_DIR` flags. If not found locally, the configuration process will try to download it automatically. + +```bash +cmake -B build -DENABLE_GOOGLEBENCH=1 +``` + +Google Benchmark requires Google Test to build and run the tests. When setting `ENABLE_GOOGLEBENCH` to ON, `BUILD_TESTING` is automatically enabled. After building and installing, performance tests can be executed with `ctest`. + ## Build with CUDA support ### Extra prerequisites diff --git a/docs/quick_start/easy_install.md b/docs/quick_start/easy_install.md index 32ae5b87d3..edbfd41f67 100644 --- a/docs/quick_start/easy_install.md +++ b/docs/quick_start/easy_install.md @@ -126,6 +126,7 @@ Here, 'build' is the path for building ABACUS; and '-D' is used for setting up s - `ENABLE_LIBRI=OFF`: [Enable LibRI](../advanced/install.md#add-libri-support) to suppport variety of functionals. If `LIBRI_DIR` and `LIBCOMM_DIR` is defined, `ENABLE_LIBRI` will set to 'ON'. - `USE_OPENMP=ON`: Enable OpenMP support. Building ABACUS without OpenMP is not fully tested yet. - `BUILD_TESTING=OFF`: [Build unit tests](../advanced/install.md#build-unit-tests). + - `ENABLE_GOOGLEBENCH=OFF`: [Build performance tests](../advanced/install.md#build-performance-tests) - `ENABLE_MPI=ON`: Enable MPI parallel compilation. If set to `OFF`, a serial version of ABACUS with PW basis only will be compiled. Currently serial version of ABACUS with LCAO basis is not supported yet, so `ENABLE_LCAO` will be automatically set to `OFF`. - `ENABLE_COVERAGE=OFF`: Build ABACUS executable supporting [coverage analysis](../CONTRIBUTING.md#generating-code-coverage-report). This feature has a drastic impact on performance. - `ENABLE_ASAN=OFF`: Build with Address Sanitizer. This feature would help detecting memory problems. From fbaefc8f86caa88ff6068485801bba02749bffdd Mon Sep 17 00:00:00 2001 From: Jie Li Date: Tue, 23 Jan 2024 08:20:45 +0000 Subject: [PATCH 5/9] add performance tests for sphbes --- source/module_base/test/CMakeLists.txt | 4 +- .../test/math_sphbes_perf_test.cpp | 103 ------------------ source/module_base/test/perf_sphbes_test.cpp | 77 +++++++++++++ 3 files changed, 79 insertions(+), 105 deletions(-) delete mode 100644 source/module_base/test/math_sphbes_perf_test.cpp create mode 100644 source/module_base/test/perf_sphbes_test.cpp diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index 65e7c80743..008df422e5 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -219,9 +219,9 @@ AddTest( ) if(ENABLE_GOOGLEBENCH) AddTest( - TARGET math_sphbes_perf_test + TARGET perf_sphbes LIBS formatter - SOURCES math_sphbes_perf_test.cpp ../math_sphbes.cpp ../timer.cpp + SOURCES perf_sphbes_test.cpp ../math_sphbes.cpp ../timer.cpp ) endif() diff --git a/source/module_base/test/math_sphbes_perf_test.cpp b/source/module_base/test/math_sphbes_perf_test.cpp deleted file mode 100644 index 998a8091e9..0000000000 --- a/source/module_base/test/math_sphbes_perf_test.cpp +++ /dev/null @@ -1,103 +0,0 @@ -#include"../math_sphbes.h" -#include -#include -#include -#include - -/************************************************ -* performace test of class Integral -***********************************************/ - -/** - * Note: this performace test try to measure the CPU time - * of the spherical Bessel produced by class Sphbes, - * and the memory usage of these functions; - * at 2024-1-14 - * - * Tested function: - * - Spherical_Bessel. - * - Spherical_Bessel_Roots - * - overloading of Spherical_Bessel. This funnction sets sjp[i] to 1.0 when i < msh. - * - sphbesj - * - sphbes_zeros - */ - - -int msh = 700; -int l0 = 0; -int l1 = 1; -int l2 = 2; -int l3 = 3; -int l4 = 4; -int l5 = 5; -int l6 = 6; -int l7 = 7; -double q = 1.0; -double *r = new double[msh]; -double *jl = new double[msh]; -double *djl = new double[msh]; - -double mean(const double* vect, const int totN) -{ - double meanv = 0.0; - for (int i=0; i< totN; ++i) {meanv += vect[i]/totN;} - return meanv; -} - - - - -// Below are the wapper functions which contain the function to measure performance -void SphericalBessel_func(){ - //int l = 0; - ModuleBase::Sphbes::Spherical_Bessel(msh,r,q,l0,jl); -} - -void dSpherical_Bessel_dx_func(){ - int il=5; - ModuleBase::Sphbes::dSpherical_Bessel_dx(msh,r,q,il,djl); -} -void SphericalBesselRoots_func(){ - int i=7; - int neign = 100; - double *eign = new double[neign]; - - ModuleBase::Sphbes::Spherical_Bessel_Roots(neign,i,1.0e-12,eign,10.0); - free(eign); - -} - -void Sphbesj_func(){ - ModuleBase::Sphbes::sphbesj(3,0); -} - - - -//Below are the test time functions -static void SphericalBessel(benchmark::State& state) { - for (auto _ : state) - SphericalBessel_func(); -} - -static void dSpherical_Bessel_dx(benchmark::State& state) { - for (auto _ : state) - dSpherical_Bessel_dx_func(); -} - -static void SphericalBesselRoots(benchmark::State& state) { - for (auto _ : state) - SphericalBesselRoots_func(); -} - -static void Sphbesj(benchmark::State& state) { - for (auto _ : state) - Sphbesj_func(); -} - - -//Add the test time functions into google benchmark -BENCHMARK(SphericalBessel); -BENCHMARK(dSpherical_Bessel_dx); -BENCHMARK(SphericalBesselRoots); -BENCHMARK(Sphbesj); -BENCHMARK_MAIN(); \ No newline at end of file diff --git a/source/module_base/test/perf_sphbes_test.cpp b/source/module_base/test/perf_sphbes_test.cpp new file mode 100644 index 0000000000..5c6d0da1ce --- /dev/null +++ b/source/module_base/test/perf_sphbes_test.cpp @@ -0,0 +1,77 @@ +#include"../math_sphbes.h" +#include +#include +#include +#include +#include + +/************************************************ +* performace test of class Sphbes +***********************************************/ + +/** + * Tested function: + * - sphbesj + * - Spherical_Bessel + */ + +const double q = 1; +const int n = 1000; +double* rc, * rinf, * jc, * jinf; +double stop = 1000.0; + +void setupcut(const int n, const double rcut, double* r){ + // generate a list of r from 0 to rcut in log grid + double rmin = 0.0001; + double log_rmin = std::log(rmin); + double log_rcut = std::log(rcut); + double dr = (log_rcut - log_rmin) / (n-1); + memset(r, 0, n * sizeof(double)); + for (int i = 0; i < n; i++) + r[i] = std::exp(log_rmin + i * dr); + +} + +void setupinf(const int n, const double rcut, double* r){ + memset(r, 0, n * sizeof(double)); + r[0] = rcut; + double dr = (stop - rcut) / (n-1); + for (int i = 1; i < n; i++) + r[i] += r[i-1] + dr; +} + +static void DoSetup(const benchmark::State& state) { + const double rcut = state.range(0) + 0.5; + rc = new double[n + 10]; + rinf = new double[n + 10]; + jc = new double[n + 10]; + jinf = new double[n + 10]; + setupcut(n, rcut, rc); + setupinf(n, rcut, rinf); +} + +static void DoTearDown(const benchmark::State& state) { + delete[] rc; + delete[] rinf; + delete[] jc; + delete[] jinf; +} + +static void BM_sphbesj(benchmark::State& state) { + while (state.KeepRunning()) { + ModuleBase::Sphbes::sphbesj(n, rc, q, state.range(0), jc); + ModuleBase::Sphbes::sphbesj(n, rinf, q, state.range(0), jinf); + } +} + +static void BM_Spherical_Bessel(benchmark::State& state) { + while (state.KeepRunning()) { + ModuleBase::Sphbes::Spherical_Bessel(n, rc, q, state.range(0), jc); + ModuleBase::Sphbes::Spherical_Bessel(n, rinf, q, state.range(0), jinf); + } +} +//Add the test time functions into google benchmark +// display result in microsecond +BENCHMARK(BM_sphbesj)->DenseRange(0, 11, 1)->Setup(DoSetup)->Teardown(DoTearDown)->Unit(benchmark::kMicrosecond); +BENCHMARK(BM_Spherical_Bessel)->DenseRange(0, 11, 1)->Setup(DoSetup)->Teardown(DoTearDown)->Unit(benchmark::kMicrosecond); +BENCHMARK_MAIN(); \ No newline at end of file From ed80949a20a657f942715c48030487394d315708 Mon Sep 17 00:00:00 2001 From: Jie Li Date: Tue, 23 Jan 2024 08:28:32 +0000 Subject: [PATCH 6/9] add google benchmark --- .github/workflows/benchmark.yml | 41 +++++++++++++++++++++++++++++++++ CMakeLists.txt | 1 + 2 files changed, 42 insertions(+) create mode 100644 .github/workflows/benchmark.yml diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml new file mode 100644 index 0000000000..5e62086496 --- /dev/null +++ b/.github/workflows/benchmark.yml @@ -0,0 +1,41 @@ +name: Unit Test and Benchmark Test + +on: + pull_request: + +concurrency: + group: ${{ github.workflow }}-${{ github.ref }} + cancel-in-progress: true + +jobs: + test: + name: Test + runs-on: self-hosted + if: github.repository_owner == 'deepmodeling' + container: + image: ghcr.io/deepmodeling/abacus-gnu + volumes: + - /tmp/ccache:/github/home/.ccache + steps: + - name: Checkout + uses: actions/checkout@v4 + with: + submodules: recursive + + - name: Install Ccache + run: | + sudo apt-get update + sudo apt-get install -y ccache + + - name: Build + run: | + cmake -B build -DENABLE_GOOGLEBENCH=ON -DENABLE_DEEPKS=ON -DENABLE_LIBXC=ON -DENABLE_LIBRI=ON -DENABLE_PAW=ON + cmake --build build -j8 + cmake --install build + + - name: Test + env: + GTEST_COLOR: 'yes' + OMP_NUM_THREADS: '2' + run: | + cmake --build build --target test ARGS="-V --timeout 1600" diff --git a/CMakeLists.txt b/CMakeLists.txt index 9b8ad6333d..e1592502a2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -608,6 +608,7 @@ IF (ENABLE_GOOGLEBENCH) GIT_TAG "origin/main" GIT_SHALLOW TRUE GIT_PROGRESS TRUE + CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_TESTING=OFF ) FetchContent_MakeAvailable(benchmark) endif() From af5cc0c6547ecb406c9a7d0199da8da99ca9039b Mon Sep 17 00:00:00 2001 From: Jie Li Date: Wed, 24 Jan 2024 12:23:50 +0000 Subject: [PATCH 7/9] rewrite benchmark tests in fixtures --- source/module_base/test/perf_sphbes_test.cpp | 95 ++++++++++---------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/source/module_base/test/perf_sphbes_test.cpp b/source/module_base/test/perf_sphbes_test.cpp index 5c6d0da1ce..4c574baa8e 100644 --- a/source/module_base/test/perf_sphbes_test.cpp +++ b/source/module_base/test/perf_sphbes_test.cpp @@ -15,63 +15,58 @@ * - Spherical_Bessel */ -const double q = 1; -const int n = 1000; -double* rc, * rinf, * jc, * jinf; -double stop = 1000.0; +class PerfSphbes : public benchmark::Fixture { +public: + const double q = 1; + const int n = 1000; + double stop = 1000.0; + double dr = 0.0; + double* rc, *rinf, *jc, *jinf; + void SetUp(const benchmark::State& state){ + const double rcut = state.range(0) + 0.5; + rc = new double[n + 10]; + rinf = new double[n + 10]; + jc = new double[n + 10]; + jinf = new double[n + 10]; -void setupcut(const int n, const double rcut, double* r){ - // generate a list of r from 0 to rcut in log grid - double rmin = 0.0001; - double log_rmin = std::log(rmin); - double log_rcut = std::log(rcut); - double dr = (log_rcut - log_rmin) / (n-1); - memset(r, 0, n * sizeof(double)); - for (int i = 0; i < n; i++) - r[i] = std::exp(log_rmin + i * dr); - -} - -void setupinf(const int n, const double rcut, double* r){ - memset(r, 0, n * sizeof(double)); - r[0] = rcut; - double dr = (stop - rcut) / (n-1); - for (int i = 1; i < n; i++) - r[i] += r[i-1] + dr; -} - -static void DoSetup(const benchmark::State& state) { - const double rcut = state.range(0) + 0.5; - rc = new double[n + 10]; - rinf = new double[n + 10]; - jc = new double[n + 10]; - jinf = new double[n + 10]; - setupcut(n, rcut, rc); - setupinf(n, rcut, rinf); -} + // generate data points in (0, rcut] in log scale + double rmin = 0.0001; + double log_rmin = std::log(rmin); + double log_rcut = std::log(rcut); + dr = (log_rcut - log_rmin) / (n-1); + memset(rc, 0, (n+10) * sizeof(double)); + for (int i = 0; i < n; i++) + rc[i] = std::exp(log_rmin + i * dr); + + // generate data points in [rcut, stop] in linear scale + memset(rinf, 0, (n+10) * sizeof(double)); + rinf[0] = rcut; + dr = (stop - rcut) / (n-1); + for (int i = 1; i < n; i++) + rinf[i] += rinf[i-1] + dr; + } + void TearDown(const benchmark::State& state){ + delete[] rc; + delete[] rinf; + delete[] jc; + delete[] jinf; + } +}; -static void DoTearDown(const benchmark::State& state) { - delete[] rc; - delete[] rinf; - delete[] jc; - delete[] jinf; +BENCHMARK_DEFINE_F(PerfSphbes, BM_Spherical_Bessel)(benchmark::State& state) { + for (auto _ : state) { + ModuleBase::Sphbes::Spherical_Bessel(n, rc, q, state.range(0), jc); + ModuleBase::Sphbes::Spherical_Bessel(n, rinf, q, state.range(0), jinf); + } } -static void BM_sphbesj(benchmark::State& state) { - while (state.KeepRunning()) { +BENCHMARK_DEFINE_F(PerfSphbes, BM_sphbesj)(benchmark::State& state) { + for (auto _ : state) { ModuleBase::Sphbes::sphbesj(n, rc, q, state.range(0), jc); ModuleBase::Sphbes::sphbesj(n, rinf, q, state.range(0), jinf); } } -static void BM_Spherical_Bessel(benchmark::State& state) { - while (state.KeepRunning()) { - ModuleBase::Sphbes::Spherical_Bessel(n, rc, q, state.range(0), jc); - ModuleBase::Sphbes::Spherical_Bessel(n, rinf, q, state.range(0), jinf); - } -} -//Add the test time functions into google benchmark -// display result in microsecond -BENCHMARK(BM_sphbesj)->DenseRange(0, 11, 1)->Setup(DoSetup)->Teardown(DoTearDown)->Unit(benchmark::kMicrosecond); -BENCHMARK(BM_Spherical_Bessel)->DenseRange(0, 11, 1)->Setup(DoSetup)->Teardown(DoTearDown)->Unit(benchmark::kMicrosecond); +BENCHMARK_REGISTER_F(PerfSphbes, BM_sphbesj)->DenseRange(0, 11, 1)->Unit(benchmark::kMicrosecond); +BENCHMARK_REGISTER_F(PerfSphbes, BM_Spherical_Bessel)->DenseRange(0, 11, 1)->Unit(benchmark::kMicrosecond); BENCHMARK_MAIN(); \ No newline at end of file From 5d35c57509e14de3fc6ccd0881c5bb008240a3b9 Mon Sep 17 00:00:00 2001 From: Jie Li Date: Thu, 25 Jan 2024 08:30:01 +0000 Subject: [PATCH 8/9] disable internal testing in benchmark --- CMakeLists.txt | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index e1592502a2..d32f94e999 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -600,17 +600,19 @@ IF (ENABLE_GOOGLEBENCH) set(BUILD_TESTING ON) find_package(benchmark HINTS ${BENCHMARK_DIR}) if(NOT ${benchmark_FOUND}) - set(BENCHMARK_USE_BUNDLED_GTEST OFF) include(FetchContent) FetchContent_Declare( benchmark GIT_REPOSITORY https://github.com/google/benchmark.git GIT_TAG "origin/main" GIT_SHALLOW TRUE - GIT_PROGRESS TRUE - CMAKE_ARGS -DCMAKE_BUILD_TYPE=Release -DBENCHMARK_ENABLE_TESTING=OFF + GIT_PROGRESS TRUE ) FetchContent_MakeAvailable(benchmark) + set(BENCHMARK_USE_BUNDLED_GTEST OFF CACHE INTERNAL "disable benchmark bundled gtest") + set(CMAKE_BUILD_TYPE "Release" CACHE INTERNAL "build benchmark in release mode") + set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "disable benchmark testing") + add_subdirectory(tests) endif() endif() From ca35da70e8fc99679b7e458dc319465aba85a5c3 Mon Sep 17 00:00:00 2001 From: Jie Li Date: Thu, 25 Jan 2024 10:25:19 +0000 Subject: [PATCH 9/9] merge benchmark into integration test --- .github/workflows/benchmark.yml | 41 --------------------------------- .github/workflows/test.yml | 2 +- CMakeLists.txt | 6 ++--- 3 files changed, 3 insertions(+), 46 deletions(-) delete mode 100644 .github/workflows/benchmark.yml diff --git a/.github/workflows/benchmark.yml b/.github/workflows/benchmark.yml deleted file mode 100644 index 5e62086496..0000000000 --- a/.github/workflows/benchmark.yml +++ /dev/null @@ -1,41 +0,0 @@ -name: Unit Test and Benchmark Test - -on: - pull_request: - -concurrency: - group: ${{ github.workflow }}-${{ github.ref }} - cancel-in-progress: true - -jobs: - test: - name: Test - runs-on: self-hosted - if: github.repository_owner == 'deepmodeling' - container: - image: ghcr.io/deepmodeling/abacus-gnu - volumes: - - /tmp/ccache:/github/home/.ccache - steps: - - name: Checkout - uses: actions/checkout@v4 - with: - submodules: recursive - - - name: Install Ccache - run: | - sudo apt-get update - sudo apt-get install -y ccache - - - name: Build - run: | - cmake -B build -DENABLE_GOOGLEBENCH=ON -DENABLE_DEEPKS=ON -DENABLE_LIBXC=ON -DENABLE_LIBRI=ON -DENABLE_PAW=ON - cmake --build build -j8 - cmake --install build - - - name: Test - env: - GTEST_COLOR: 'yes' - OMP_NUM_THREADS: '2' - run: | - cmake --build build --target test ARGS="-V --timeout 1600" diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 69d08d9b36..76f48347a8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -29,7 +29,7 @@ jobs: - name: Build run: | - cmake -B build -DBUILD_TESTING=ON -DENABLE_DEEPKS=ON -DENABLE_LIBXC=ON -DENABLE_LIBRI=ON -DENABLE_PAW=ON + cmake -B build -DBUILD_TESTING=ON -DENABLE_DEEPKS=ON -DENABLE_LIBXC=ON -DENABLE_LIBRI=ON -DENABLE_PAW=ON -DENABLE_GOOGLEBENCH=ON cmake --build build -j8 cmake --install build diff --git a/CMakeLists.txt b/CMakeLists.txt index d32f94e999..c474d23ae1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -600,6 +600,7 @@ IF (ENABLE_GOOGLEBENCH) set(BUILD_TESTING ON) find_package(benchmark HINTS ${BENCHMARK_DIR}) if(NOT ${benchmark_FOUND}) + set(BENCHMARK_USE_BUNDLED_GTEST OFF) include(FetchContent) FetchContent_Declare( benchmark @@ -608,11 +609,8 @@ IF (ENABLE_GOOGLEBENCH) GIT_SHALLOW TRUE GIT_PROGRESS TRUE ) + set(BENCHMARK_ENABLE_TESTING OFF) FetchContent_MakeAvailable(benchmark) - set(BENCHMARK_USE_BUNDLED_GTEST OFF CACHE INTERNAL "disable benchmark bundled gtest") - set(CMAKE_BUILD_TYPE "Release" CACHE INTERNAL "build benchmark in release mode") - set(BENCHMARK_ENABLE_TESTING OFF CACHE INTERNAL "disable benchmark testing") - add_subdirectory(tests) endif() endif()