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