From 3e7ca1a55b5ff20a221ecabf6c90e041fd3cc75e Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Thu, 2 Jan 2025 20:20:46 +0800 Subject: [PATCH 01/15] Initial commit --- source/module_base/blas_connector.cpp | 207 +++++++++++++++++++++++--- 1 file changed, 183 insertions(+), 24 deletions(-) diff --git a/source/module_base/blas_connector.cpp b/source/module_base/blas_connector.cpp index 85ea4584e9..fc99819d01 100644 --- a/source/module_base/blas_connector.cpp +++ b/source/module_base/blas_connector.cpp @@ -5,32 +5,101 @@ #include "module_base/global_variable.h" #endif +#ifdef __CUDA +#include +#include +#include +#include +#include +#include "module_base/tool_quit.h" + +#include "cublas_v2.h" + +namespace BlasUtils{ + + static cublasHandle_t cublas_handle = nullptr; + + void createGpuBlasHandle(){ + if (cublas_handle == nullptr) { + cublasErrcheck(cublasCreate(&cublas_handle)); + } + } + + void destoryBLAShandle(){ + if (cublas_handle != nullptr) { + cublasErrcheck(cublasDestroy(cublas_handle)); + cublas_handle = nullptr; + } + } + + + cublasOperation_t judge_trans(bool is_complex, const char& trans, const char* name) + { + if (trans == 'N') + { + return CUBLAS_OP_N; + } + else if(trans == 'T') + { + return CUBLAS_OP_T; + } + else if(is_complex && trans == 'C') + { + return CUBLAS_OP_C; + } + return CUBLAS_OP_N; + } + +} // namespace BlasUtils + +#endif + void BlasConnector::axpy( const int n, const float alpha, const float *X, const int incX, float *Y, const int incY, base_device::AbacusDevice_t device_type) { if (device_type == base_device::AbacusDevice_t::CpuDevice) { saxpy_(&n, &alpha, X, &incX, Y, &incY); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasErrcheck(cublasSaxpy(BlasUtils::cublas_handle, n, &alpha, X, incX, Y, incY)); +#endif + } } void BlasConnector::axpy( const int n, const double alpha, const double *X, const int incX, double *Y, const int incY, base_device::AbacusDevice_t device_type) { if (device_type == base_device::AbacusDevice_t::CpuDevice) { daxpy_(&n, &alpha, X, &incX, Y, &incY); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasErrcheck(cublasDaxpy(BlasUtils::cublas_handle, n, &alpha, X, incX, Y, incY)); +#endif + } } void BlasConnector::axpy( const int n, const std::complex alpha, const std::complex *X, const int incX, std::complex *Y, const int incY, base_device::AbacusDevice_t device_type) { if (device_type == base_device::AbacusDevice_t::CpuDevice) { caxpy_(&n, &alpha, X, &incX, Y, &incY); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasErrcheck(cublasCaxpy(BlasUtils::cublas_handle, n, (float2*)&alpha, (float2*)X, incX, (float2*)Y, incY)); +#endif + } } void BlasConnector::axpy( const int n, const std::complex alpha, const std::complex *X, const int incX, std::complex *Y, const int incY, base_device::AbacusDevice_t device_type) { if (device_type == base_device::AbacusDevice_t::CpuDevice) { zaxpy_(&n, &alpha, X, &incX, Y, &incY); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasErrcheck(cublasZaxpy(BlasUtils::cublas_handle, n, (double2*)&alpha, (double2*)X, incX, (double2*)Y, incY)); +#endif + } } @@ -39,28 +108,48 @@ void BlasConnector::scal( const int n, const float alpha, float *X, const int i { if (device_type == base_device::AbacusDevice_t::CpuDevice) { sscal_(&n, &alpha, X, &incX); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice) { +#ifdef __CUDA + cublasErrcheck(cublasSscal(BlasUtils::cublas_handle, n, &alpha, X, incX)); +#endif + } } void BlasConnector::scal( const int n, const double alpha, double *X, const int incX, base_device::AbacusDevice_t device_type) { if (device_type == base_device::AbacusDevice_t::CpuDevice) { dscal_(&n, &alpha, X, &incX); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice) { +#ifdef __CUDA + cublasErrcheck(cublasDscal(BlasUtils::cublas_handle, n, &alpha, X, incX)); +#endif + } } void BlasConnector::scal( const int n, const std::complex alpha, std::complex *X, const int incX, base_device::AbacusDevice_t device_type) { if (device_type == base_device::AbacusDevice_t::CpuDevice) { cscal_(&n, &alpha, X, &incX); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice) { +#ifdef __CUDA + cublasErrcheck(cublasCscal(BlasUtils::cublas_handle, n, (float2*)&alpha, (float2*)X, incX)); +#endif + } } void BlasConnector::scal( const int n, const std::complex alpha, std::complex *X, const int incX, base_device::AbacusDevice_t device_type) { if (device_type == base_device::AbacusDevice_t::CpuDevice) { zscal_(&n, &alpha, X, &incX); -} + } + else if (device_type == base_device::AbacusDevice_t::GpuDevice) { +#ifdef __CUDA + cublasErrcheck(cublasZscal(BlasUtils::cublas_handle, n, (double2*)&alpha, (double2*)X, incX)); +#endif + } } @@ -70,6 +159,13 @@ float BlasConnector::dot( const int n, const float *X, const int incX, const flo if (device_type == base_device::AbacusDevice_t::CpuDevice) { return sdot_(&n, X, &incX, Y, &incY); } + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + float result = 0.0; + cublasErrcheck(cublasSdot(BlasUtils::cublas_handle, n, X, incX, Y, incY, &result)); + return result; +#endif + } return sdot_(&n, X, &incX, Y, &incY); } @@ -78,6 +174,13 @@ double BlasConnector::dot( const int n, const double *X, const int incX, const d if (device_type == base_device::AbacusDevice_t::CpuDevice) { return ddot_(&n, X, &incX, Y, &incY); } + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + double result = 0.0; + cublasErrcheck(cublasDdot(BlasUtils::cublas_handle, n, X, incX, Y, incY, &result)); + return result; +#endif + } return ddot_(&n, X, &incX, Y, &incY); } @@ -92,13 +195,20 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons &alpha, b, &ldb, a, &lda, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice){ sgemm_mth_(&transb, &transa, &n, &m, &k, &alpha, b, &ldb, a, &lda, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasSgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, &alpha, b, ldb, a, lda, &beta, c, ldc)); +#endif + } } void BlasConnector::gemm(const char transa, const char transb, const int m, const int n, const int k, @@ -110,13 +220,20 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons &alpha, b, &ldb, a, &lda, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice){ dgemm_mth_(&transb, &transa, &n, &m, &k, &alpha, b, &ldb, a, &lda, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasDgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, &alpha, b, ldb, a, lda, &beta, c, ldc)); +#endif + } } void BlasConnector::gemm(const char transa, const char transb, const int m, const int n, const int k, @@ -128,13 +245,20 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons &alpha, b, &ldb, a, &lda, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice) { cgemm_mth_(&transb, &transa, &n, &m, &k, &alpha, b, &ldb, a, &lda, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasCgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, (float2*)&alpha, (float2*)b, ldb, (float2*)a, lda, (float2*)&beta, (float2*)c, ldc)); +#endif + } } void BlasConnector::gemm(const char transa, const char transb, const int m, const int n, const int k, @@ -146,13 +270,20 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons &alpha, b, &ldb, a, &lda, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice) { zgemm_mth_(&transb, &transa, &n, &m, &k, &alpha, b, &ldb, a, &lda, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasZgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, (double2*)&alpha, (double2*)b, ldb, (double2*)a, lda, (double2*)&beta, (double2*)c, ldc)); +#endif + } } // Col-Major part @@ -165,13 +296,20 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c &alpha, a, &lda, b, &ldb, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice){ sgemm_mth_(&transb, &transa, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasSgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)); +#endif + } } void BlasConnector::gemm_cm(const char transa, const char transb, const int m, const int n, const int k, @@ -183,13 +321,20 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c &alpha, a, &lda, b, &ldb, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice){ dgemm_mth_(&transa, &transb, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasDgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)); +#endif + } } void BlasConnector::gemm_cm(const char transa, const char transb, const int m, const int n, const int k, @@ -201,13 +346,20 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c &alpha, a, &lda, b, &ldb, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice) { cgemm_mth_(&transa, &transb, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasCgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, (float2*)&alpha, (float2*)a, lda, (float2*)b, ldb, (float2*)&beta, (float2*)c, ldc)); +#endif + } } void BlasConnector::gemm_cm(const char transa, const char transb, const int m, const int n, const int k, @@ -219,13 +371,20 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c &alpha, a, &lda, b, &ldb, &beta, c, &ldc); } - #ifdef __DSP +#ifdef __DSP else if (device_type == base_device::AbacusDevice_t::DspDevice) { zgemm_mth_(&transa, &transb, &m, &n, &k, &alpha, a, &lda, b, &ldb, &beta, c, &ldc, GlobalV::MY_RANK); } - #endif +#endif + else if (device_type == base_device::AbacusDevice_t::GpuDevice){ +#ifdef __CUDA + cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasErrcheck(cublasZgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, (double2*)&alpha, (double2*)a, lda, (double2*)b, ldb, (double2*)&beta, (double2*)c, ldc)); +#endif + } } // Symm and Hemm part. Only col-major is supported. From 5198e2db58ceca4dc3dff98b304f9a52c731c621 Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Thu, 2 Jan 2025 22:02:14 +0800 Subject: [PATCH 02/15] Modify CMakeLists --- source/module_base/blas_connector.cpp | 32 +++++++++++++------------- source/module_base/test/CMakeLists.txt | 30 ++++++++++++------------ 2 files changed, 31 insertions(+), 31 deletions(-) diff --git a/source/module_base/blas_connector.cpp b/source/module_base/blas_connector.cpp index fc99819d01..3bb91e2f01 100644 --- a/source/module_base/blas_connector.cpp +++ b/source/module_base/blas_connector.cpp @@ -204,8 +204,8 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasSgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, &alpha, b, ldb, a, lda, &beta, c, ldc)); #endif } @@ -229,8 +229,8 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasDgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, &alpha, b, ldb, a, lda, &beta, c, ldc)); #endif } @@ -254,8 +254,8 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasCgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, (float2*)&alpha, (float2*)b, ldb, (float2*)a, lda, (float2*)&beta, (float2*)c, ldc)); #endif } @@ -279,8 +279,8 @@ void BlasConnector::gemm(const char transa, const char transb, const int m, cons #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasZgemm(BlasUtils::cublas_handle, cutransA, cutransB, n, m, k, (double2*)&alpha, (double2*)b, ldb, (double2*)a, lda, (double2*)&beta, (double2*)c, ldc)); #endif } @@ -305,8 +305,8 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasSgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)); #endif } @@ -330,8 +330,8 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasDgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, &alpha, a, lda, b, ldb, &beta, c, ldc)); #endif } @@ -355,8 +355,8 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasCgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, (float2*)&alpha, (float2*)a, lda, (float2*)b, ldb, (float2*)&beta, (float2*)c, ldc)); #endif } @@ -380,8 +380,8 @@ void BlasConnector::gemm_cm(const char transa, const char transb, const int m, c #endif else if (device_type == base_device::AbacusDevice_t::GpuDevice){ #ifdef __CUDA - cublasOperation_t cutransA = judge_trans(false, transa, "gemm_op"); - cublasOperation_t cutransB = judge_trans(false, transb, "gemm_op"); + cublasOperation_t cutransA = BlasUtils::judge_trans(false, transa, "gemm_op"); + cublasOperation_t cutransB = BlasUtils::judge_trans(false, transb, "gemm_op"); cublasErrcheck(cublasZgemm(BlasUtils::cublas_handle, cutransA, cutransB, m, n, k, (double2*)&alpha, (double2*)a, lda, (double2*)b, ldb, (double2*)&beta, (double2*)c, ldc)); #endif } diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index 09b77c7404..15b52c048e 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -2,7 +2,7 @@ remove_definitions(-D__MPI) install(DIRECTORY data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) AddTest( TARGET base_blas_connector - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES blas_connector_test.cpp ../blas_connector.cpp ) AddTest( @@ -31,7 +31,7 @@ AddTest( ) ADDTest( TARGET base_global_function - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES global_function_test.cpp ../blas_connector.cpp ../global_function.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../memory.cpp ../timer.cpp ) AddTest( @@ -41,7 +41,7 @@ AddTest( ) AddTest( TARGET base_matrix3 - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES matrix3_test.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp ../blas_connector.cpp ) AddTest( @@ -56,7 +56,7 @@ AddTest( ) AddTest( TARGET base_matrix - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES matrix_test.cpp ../blas_connector.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp ) AddTest( @@ -66,7 +66,7 @@ AddTest( ) AddTest( TARGET base_complexmatrix - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES complexmatrix_test.cpp ../blas_connector.cpp ../complexmatrix.cpp ../matrix.cpp ) AddTest( @@ -93,12 +93,12 @@ AddTest( ) AddTest( TARGET base_mathzone - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES mathzone_test.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp ../blas_connector.cpp ) AddTest( TARGET base_mathzone_add1 - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES mathzone_add1_test.cpp ../blas_connector.cpp ../mathzone_add1.cpp ../math_sphbes.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp ) AddTest( @@ -108,7 +108,7 @@ AddTest( ) AddTest( TARGET base_gram_schmidt_orth - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES gram_schmidt_orth_test.cpp ../blas_connector.cpp ../gram_schmidt_orth.h ../gram_schmidt_orth-inl.h ../global_function.h ../math_integral.cpp ) AddTest( @@ -118,7 +118,7 @@ AddTest( ) AddTest( TARGET base_inverse_matrix - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} device SOURCES inverse_matrix_test.cpp ../blas_connector.cpp ../inverse_matrix.cpp ../complexmatrix.cpp ../matrix.cpp ../timer.cpp ) AddTest( @@ -140,19 +140,19 @@ AddTest( AddTest( TARGET base_lapack_connector - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES lapack_connector_test.cpp ../blas_connector.cpp ../lapack_connector.h ) AddTest( TARGET base_opt_CG - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES opt_CG_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( TARGET base_opt_TN - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device SOURCES opt_TN_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) @@ -195,13 +195,13 @@ AddTest( AddTest( TARGET spherical_bessel_transformer SOURCES spherical_bessel_transformer_test.cpp ../blas_connector.cpp ../spherical_bessel_transformer.cpp ../math_sphbes.cpp ../math_integral.cpp ../timer.cpp - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device ) AddTest( TARGET cubic_spline SOURCES cubic_spline_test.cpp ../blas_connector.cpp ../cubic_spline.cpp - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device ) AddTest( @@ -215,7 +215,7 @@ AddTest( AddTest( TARGET assoc_laguerre_test SOURCES assoc_laguerre_test.cpp ../blas_connector.cpp ../assoc_laguerre.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp - LIBS parameter ${math_libs} + LIBS parameter ${math_libs} base device ) AddTest( From 2aa45242966d753861ad550c84cf0d9af1620dee Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Thu, 2 Jan 2025 22:19:53 +0800 Subject: [PATCH 03/15] Complete CMakeLists in module_base --- source/module_base/test/CMakeLists.txt | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index 15b52c048e..18baf6d572 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -81,7 +81,7 @@ AddTest( ) AddTest( TARGET base_ylmreal - LIBS parameter ${math_libs} device + LIBS parameter ${math_libs} base device SOURCES math_ylmreal_test.cpp ../blas_connector.cpp ../math_ylmreal.cpp ../complexmatrix.cpp ../global_variable.cpp ../ylm.cpp ../realarray.cpp ../timer.cpp ../matrix.cpp ../vector3.h ../parallel_reduce.cpp ../parallel_global.cpp ../parallel_comm.cpp ../parallel_common.cpp ../memory.cpp ../libm/branred.cpp ../libm/sincos.cpp @@ -118,7 +118,7 @@ AddTest( ) AddTest( TARGET base_inverse_matrix - LIBS parameter ${math_libs} device + LIBS parameter ${math_libs} base device SOURCES inverse_matrix_test.cpp ../blas_connector.cpp ../inverse_matrix.cpp ../complexmatrix.cpp ../matrix.cpp ../timer.cpp ) AddTest( @@ -134,7 +134,7 @@ AddTest( AddTest( TARGET base_math_chebyshev - LIBS parameter ${math_libs} device container + LIBS parameter ${math_libs} base device container SOURCES math_chebyshev_test.cpp ../blas_connector.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp ) @@ -209,7 +209,7 @@ AddTest( SOURCES clebsch_gordan_coeff_test.cpp ../blas_connector.cpp ../clebsch_gordan_coeff.cpp ../intarray.cpp ../realarray.cpp ../complexmatrix.cpp ../matrix.cpp ../timer.cpp ../math_ylmreal.cpp ../global_variable.cpp ../ylm.cpp ../timer.cpp ../vector3.h ../parallel_reduce.cpp ../parallel_global.cpp ../parallel_comm.cpp ../parallel_common.cpp ../memory.cpp ../libm/branred.cpp ../libm/sincos.cpp ../inverse_matrix.cpp ../lapack_connector.h - LIBS parameter ${math_libs} device + LIBS parameter ${math_libs} base device ) AddTest( From 3c86d0bdadfdcc937f096a1f29ebe15ee1dbf546 Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Thu, 2 Jan 2025 22:53:01 +0800 Subject: [PATCH 04/15] Add blas_connector.cpp definition --- source/module_base/blas_connector.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/source/module_base/blas_connector.cpp b/source/module_base/blas_connector.cpp index 3bb91e2f01..5bd369ed62 100644 --- a/source/module_base/blas_connector.cpp +++ b/source/module_base/blas_connector.cpp @@ -1,3 +1,5 @@ +#ifndef BLAS_CONNECTOR_CPP +#define BLAS_CONNECTOR_CPP #include "blas_connector.h" #ifdef __DSP @@ -531,4 +533,6 @@ void BlasConnector::copy(const long n, const std::complex *a, const int if (device_type == base_device::AbacusDevice_t::CpuDevice) { zcopy_(&n, a, &incx, b, &incy); } -} \ No newline at end of file +} + +#endif \ No newline at end of file From f310a90f39f8a37b5757255cf81396acf21b653d Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 00:28:30 +0800 Subject: [PATCH 05/15] Fix module_base tests --- source/module_base/blas_connector.cpp | 6 +-- source/module_base/test/CMakeLists.txt | 40 +++++++++---------- .../test/clebsch_gordan_coeff_test.cpp | 12 ------ .../module_base/test/complexmatrix_test.cpp | 6 --- .../module_base/test/inverse_matrix_test.cpp | 6 --- source/module_base/test/math_ylmreal_test.cpp | 10 ----- 6 files changed, 19 insertions(+), 61 deletions(-) diff --git a/source/module_base/blas_connector.cpp b/source/module_base/blas_connector.cpp index 5bd369ed62..3bb91e2f01 100644 --- a/source/module_base/blas_connector.cpp +++ b/source/module_base/blas_connector.cpp @@ -1,5 +1,3 @@ -#ifndef BLAS_CONNECTOR_CPP -#define BLAS_CONNECTOR_CPP #include "blas_connector.h" #ifdef __DSP @@ -533,6 +531,4 @@ void BlasConnector::copy(const long n, const std::complex *a, const int if (device_type == base_device::AbacusDevice_t::CpuDevice) { zcopy_(&n, a, &incx, b, &incy); } -} - -#endif \ No newline at end of file +} \ No newline at end of file diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index 18baf6d572..e0f61062b4 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -3,7 +3,7 @@ install(DIRECTORY data DESTINATION ${CMAKE_CURRENT_BINARY_DIR}) AddTest( TARGET base_blas_connector LIBS parameter ${math_libs} base device - SOURCES blas_connector_test.cpp ../blas_connector.cpp + SOURCES blas_connector_test.cpp ) AddTest( TARGET base_atom_in @@ -32,7 +32,7 @@ AddTest( ADDTest( TARGET base_global_function LIBS parameter ${math_libs} base device - SOURCES global_function_test.cpp ../blas_connector.cpp ../global_function.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../memory.cpp ../timer.cpp + SOURCES global_function_test.cpp ) AddTest( TARGET base_vector3 @@ -42,7 +42,7 @@ AddTest( AddTest( TARGET base_matrix3 LIBS parameter ${math_libs} base device - SOURCES matrix3_test.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp ../blas_connector.cpp + SOURCES matrix3_test.cpp ) AddTest( TARGET base_intarray @@ -57,7 +57,7 @@ AddTest( AddTest( TARGET base_matrix LIBS parameter ${math_libs} base device - SOURCES matrix_test.cpp ../blas_connector.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp + SOURCES matrix_test.cpp ) AddTest( TARGET base_complexarray @@ -67,7 +67,7 @@ AddTest( AddTest( TARGET base_complexmatrix LIBS parameter ${math_libs} base device - SOURCES complexmatrix_test.cpp ../blas_connector.cpp ../complexmatrix.cpp ../matrix.cpp + SOURCES complexmatrix_test.cpp ) AddTest( TARGET base_integral @@ -82,9 +82,7 @@ AddTest( AddTest( TARGET base_ylmreal LIBS parameter ${math_libs} base device - SOURCES math_ylmreal_test.cpp ../blas_connector.cpp ../math_ylmreal.cpp ../complexmatrix.cpp ../global_variable.cpp ../ylm.cpp ../realarray.cpp ../timer.cpp ../matrix.cpp ../vector3.h - ../parallel_reduce.cpp ../parallel_global.cpp ../parallel_comm.cpp ../parallel_common.cpp - ../memory.cpp ../libm/branred.cpp ../libm/sincos.cpp + SOURCES math_ylmreal_test.cpp ../libm/branred.cpp ../libm/sincos.cpp ) AddTest( TARGET base_math_sphbes @@ -94,12 +92,12 @@ AddTest( AddTest( TARGET base_mathzone LIBS parameter ${math_libs} base device - SOURCES mathzone_test.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp ../blas_connector.cpp + SOURCES mathzone_test.cpp ) AddTest( TARGET base_mathzone_add1 LIBS parameter ${math_libs} base device - SOURCES mathzone_add1_test.cpp ../blas_connector.cpp ../mathzone_add1.cpp ../math_sphbes.cpp ../matrix3.cpp ../matrix.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp + SOURCES mathzone_add1_test.cpp ) AddTest( TARGET base_math_polyint @@ -109,7 +107,7 @@ AddTest( AddTest( TARGET base_gram_schmidt_orth LIBS parameter ${math_libs} base device - SOURCES gram_schmidt_orth_test.cpp ../blas_connector.cpp ../gram_schmidt_orth.h ../gram_schmidt_orth-inl.h ../global_function.h ../math_integral.cpp + SOURCES gram_schmidt_orth_test.cpp ) AddTest( TARGET base_math_bspline @@ -119,7 +117,7 @@ AddTest( AddTest( TARGET base_inverse_matrix LIBS parameter ${math_libs} base device - SOURCES inverse_matrix_test.cpp ../blas_connector.cpp ../inverse_matrix.cpp ../complexmatrix.cpp ../matrix.cpp ../timer.cpp + SOURCES inverse_matrix_test.cpp ) AddTest( TARGET base_mymath @@ -135,25 +133,25 @@ AddTest( AddTest( TARGET base_math_chebyshev LIBS parameter ${math_libs} base device container - SOURCES math_chebyshev_test.cpp ../blas_connector.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp + SOURCES math_chebyshev_test.cpp ) AddTest( TARGET base_lapack_connector LIBS parameter ${math_libs} base device - SOURCES lapack_connector_test.cpp ../blas_connector.cpp ../lapack_connector.h + SOURCES lapack_connector_test.cpp ) AddTest( TARGET base_opt_CG LIBS parameter ${math_libs} base device - SOURCES opt_CG_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + SOURCES opt_CG_test.cpp opt_test_tools.cpp ) AddTest( TARGET base_opt_TN LIBS parameter ${math_libs} base device - SOURCES opt_TN_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + SOURCES opt_TN_test.cpp opt_test_tools.cpp ) AddTest( @@ -194,27 +192,25 @@ AddTest( AddTest( TARGET spherical_bessel_transformer - SOURCES spherical_bessel_transformer_test.cpp ../blas_connector.cpp ../spherical_bessel_transformer.cpp ../math_sphbes.cpp ../math_integral.cpp ../timer.cpp + SOURCES spherical_bessel_transformer_test.cpp LIBS parameter ${math_libs} base device ) AddTest( TARGET cubic_spline - SOURCES cubic_spline_test.cpp ../blas_connector.cpp ../cubic_spline.cpp + SOURCES cubic_spline_test.cpp LIBS parameter ${math_libs} base device ) AddTest( TARGET clebsch_gordan_coeff_test - SOURCES clebsch_gordan_coeff_test.cpp ../blas_connector.cpp ../clebsch_gordan_coeff.cpp ../intarray.cpp ../realarray.cpp ../complexmatrix.cpp ../matrix.cpp ../timer.cpp - ../math_ylmreal.cpp ../global_variable.cpp ../ylm.cpp ../timer.cpp ../vector3.h ../parallel_reduce.cpp ../parallel_global.cpp ../parallel_comm.cpp ../parallel_common.cpp - ../memory.cpp ../libm/branred.cpp ../libm/sincos.cpp ../inverse_matrix.cpp ../lapack_connector.h + SOURCES clebsch_gordan_coeff_test.cpp LIBS parameter ${math_libs} base device ) AddTest( TARGET assoc_laguerre_test - SOURCES assoc_laguerre_test.cpp ../blas_connector.cpp ../assoc_laguerre.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../timer.cpp + SOURCES assoc_laguerre_test.cpp LIBS parameter ${math_libs} base device ) diff --git a/source/module_base/test/clebsch_gordan_coeff_test.cpp b/source/module_base/test/clebsch_gordan_coeff_test.cpp index 16efa091b5..888249765f 100644 --- a/source/module_base/test/clebsch_gordan_coeff_test.cpp +++ b/source/module_base/test/clebsch_gordan_coeff_test.cpp @@ -16,18 +16,6 @@ * - functions: gen_rndm_r and compute_ap */ -namespace ModuleBase -{ -void WARNING_QUIT(const std::string& file, const std::string& description) -{ - return; -} -void WARNING(const std::string& file, const std::string& description) -{ - return; -} -} // namespace ModuleBase - TEST(ClebschGordanTest, ClebschGordanExit) { int lmaxkb = -2; diff --git a/source/module_base/test/complexmatrix_test.cpp b/source/module_base/test/complexmatrix_test.cpp index 0adc52363a..da11fafcfd 100644 --- a/source/module_base/test/complexmatrix_test.cpp +++ b/source/module_base/test/complexmatrix_test.cpp @@ -38,12 +38,6 @@ * */ -//a mock function of WARNING_QUIT, to avoid the uncorrected call by matrix.cpp at line 37. -namespace ModuleBase -{ - void WARNING_QUIT(const std::string &file,const std::string &description) {exit(1);} -} - inline void EXPECT_COMPLEX_EQ(const std::complex& a,const std::complex& b) { EXPECT_DOUBLE_EQ(a.real(),b.real()); diff --git a/source/module_base/test/inverse_matrix_test.cpp b/source/module_base/test/inverse_matrix_test.cpp index a871f906cd..b88e556af1 100644 --- a/source/module_base/test/inverse_matrix_test.cpp +++ b/source/module_base/test/inverse_matrix_test.cpp @@ -16,12 +16,6 @@ * - computes the inverse of a dim*dim real matrix */ -//a mock function of WARNING_QUIT, to avoid the uncorrected call by matrix.cpp at line 37. -namespace ModuleBase -{ - void WARNING_QUIT(const std::string &file,const std::string &description) {exit(1);} -} - TEST(InverseMatrixComplexTest, InverseMatrixComplex) { int dim = 10; diff --git a/source/module_base/test/math_ylmreal_test.cpp b/source/module_base/test/math_ylmreal_test.cpp index c973d8cd28..891c948f7e 100644 --- a/source/module_base/test/math_ylmreal_test.cpp +++ b/source/module_base/test/math_ylmreal_test.cpp @@ -36,16 +36,6 @@ * */ - - -//mock functions of WARNING_QUIT and WARNING -namespace ModuleBase -{ - void WARNING_QUIT(const std::string &file,const std::string &description) {exit(1);} - void WARNING(const std::string &file,const std::string &description) {return ;} -} - - class YlmRealTest : public testing::Test { protected: From 0464af2cda4ab6b9244f9dc87c89463561f14e77 Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 13:09:23 +0800 Subject: [PATCH 06/15] Fix tests failure --- source/module_base/test/CMakeLists.txt | 16 ++++++++-------- source/module_base/test/global_function_test.cpp | 1 - 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index e0f61062b4..ffe15daf80 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -31,8 +31,8 @@ AddTest( ) ADDTest( TARGET base_global_function - LIBS parameter ${math_libs} base device - SOURCES global_function_test.cpp + LIBS parameter ${math_libs} + SOURCES global_function_test.cpp ../global_function.cpp ../tool_quit.cpp ../global_variable.cpp ../global_file.cpp ../memory.cpp ../timer.cpp ) AddTest( TARGET base_vector3 @@ -132,8 +132,8 @@ AddTest( AddTest( TARGET base_math_chebyshev - LIBS parameter ${math_libs} base device container - SOURCES math_chebyshev_test.cpp + LIBS parameter ${math_libs} device container + SOURCES math_chebyshev_test.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp ) AddTest( @@ -144,14 +144,14 @@ AddTest( AddTest( TARGET base_opt_CG - LIBS parameter ${math_libs} base device - SOURCES opt_CG_test.cpp opt_test_tools.cpp + LIBS parameter ${math_libs} + SOURCES opt_CG_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( TARGET base_opt_TN - LIBS parameter ${math_libs} base device - SOURCES opt_TN_test.cpp opt_test_tools.cpp + LIBS parameter ${math_libs} + SOURCES opt_TN_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( diff --git a/source/module_base/test/global_function_test.cpp b/source/module_base/test/global_function_test.cpp index 013396d6b1..9b2c49fba6 100644 --- a/source/module_base/test/global_function_test.cpp +++ b/source/module_base/test/global_function_test.cpp @@ -4,7 +4,6 @@ #include "module_parameter/parameter.h" #undef private #include "../vector3.h" -#include "../blas_connector.h" #include "../tool_quit.h" #include #include From 2a0cfd6115ec5ccd2b5bcd7fa290b41bac0dbb19 Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 13:44:23 +0800 Subject: [PATCH 07/15] fix opt_test --- source/module_base/test/CMakeLists.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index ffe15daf80..d2e30ee8d7 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -133,7 +133,7 @@ AddTest( AddTest( TARGET base_math_chebyshev LIBS parameter ${math_libs} device container - SOURCES math_chebyshev_test.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp + SOURCES math_chebyshev_test.cpp ../blas_connector.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp ) AddTest( @@ -144,14 +144,14 @@ AddTest( AddTest( TARGET base_opt_CG - LIBS parameter ${math_libs} - SOURCES opt_CG_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + LIBS parameter ${math_libs} device + SOURCES opt_CG_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( TARGET base_opt_TN - LIBS parameter ${math_libs} - SOURCES opt_TN_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + LIBS parameter ${math_libs} device + SOURCES opt_TN_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( From f558e070df50817433b834c8657ee45f6faaa27f Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 13:51:36 +0800 Subject: [PATCH 08/15] OPTFIX2 --- source/module_base/opt_CG.h | 4 ++++ source/module_base/test/CMakeLists.txt | 4 ++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/module_base/opt_CG.h b/source/module_base/opt_CG.h index 32707d6ca7..8014271cf5 100644 --- a/source/module_base/opt_CG.h +++ b/source/module_base/opt_CG.h @@ -5,7 +5,11 @@ #include +#ifdef __CUDA +#undef __CUDA #include "global_function.h" +#define __CUDA +#endif #include "module_base/parallel_reduce.h" namespace ModuleBase diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index d2e30ee8d7..4e2bb170fc 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -144,13 +144,13 @@ AddTest( AddTest( TARGET base_opt_CG - LIBS parameter ${math_libs} device + LIBS parameter ${math_libs} SOURCES opt_CG_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( TARGET base_opt_TN - LIBS parameter ${math_libs} device + LIBS parameter ${math_libs} SOURCES opt_TN_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) From bab57273ad907d76934e165687cd7a0bf360470a Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 13:57:21 +0800 Subject: [PATCH 09/15] Return all changes --- source/module_base/opt_CG.h | 4 ---- source/module_base/test/CMakeLists.txt | 12 ++++++------ 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/source/module_base/opt_CG.h b/source/module_base/opt_CG.h index 8014271cf5..32707d6ca7 100644 --- a/source/module_base/opt_CG.h +++ b/source/module_base/opt_CG.h @@ -5,11 +5,7 @@ #include -#ifdef __CUDA -#undef __CUDA #include "global_function.h" -#define __CUDA -#endif #include "module_base/parallel_reduce.h" namespace ModuleBase diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index 4e2bb170fc..ade269a1e6 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -132,8 +132,8 @@ AddTest( AddTest( TARGET base_math_chebyshev - LIBS parameter ${math_libs} device container - SOURCES math_chebyshev_test.cpp ../blas_connector.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp + LIBS parameter ${math_libs} base device container + SOURCES math_chebyshev_test.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp ) AddTest( @@ -144,14 +144,14 @@ AddTest( AddTest( TARGET base_opt_CG - LIBS parameter ${math_libs} - SOURCES opt_CG_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + LIBS parameter ${math_libs} base device + SOURCES opt_CG_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( TARGET base_opt_TN - LIBS parameter ${math_libs} - SOURCES opt_TN_test.cpp opt_test_tools.cpp ../blas_connector.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + LIBS parameter ${math_libs} base device + SOURCES opt_TN_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp ) AddTest( From 8a8ec04024c0fb9ab074afaee1cb7bf39bd8af39 Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 14:00:52 +0800 Subject: [PATCH 10/15] Fix global_func_text --- source/module_base/test/global_function_test.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/module_base/test/global_function_test.cpp b/source/module_base/test/global_function_test.cpp index 9b2c49fba6..05d4d70877 100644 --- a/source/module_base/test/global_function_test.cpp +++ b/source/module_base/test/global_function_test.cpp @@ -691,6 +691,9 @@ TEST_F(GlobalFunctionTest,MemAvailable) TEST_F(GlobalFunctionTest,BlockHere) { +#ifdef __MPI +#undef __MPI +#endif std::string output2; std::string block_in="111"; GlobalV::MY_RANK=1; @@ -705,6 +708,9 @@ TEST_F(GlobalFunctionTest,BlockHere) TEST_F(GlobalFunctionTest,BlockHere2) { +#ifdef __MPI +#undef __MPI +#endif std::string output2; std::string block_in="111"; GlobalV::MY_RANK=0; @@ -723,6 +729,9 @@ TEST_F(GlobalFunctionTest,BlockHere2) TEST_F(GlobalFunctionTest,BlockHere3) { +#ifdef __MPI +#undef __MPI +#endif std::string output2; std::string block_in="111"; GlobalV::MY_RANK=0; From 1cd1ffc2bd6c96c75739f653fbb16f2572e4909a Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 14:08:50 +0800 Subject: [PATCH 11/15] Fix MPI Bug --- source/module_base/test/math_chebyshev_test.cpp | 3 +++ source/module_base/test/opt_CG_test.cpp | 15 +++++++++++++++ source/module_base/test/opt_TN_test.cpp | 6 ++++++ 3 files changed, 24 insertions(+) diff --git a/source/module_base/test/math_chebyshev_test.cpp b/source/module_base/test/math_chebyshev_test.cpp index 125dbdaeaa..3c3aef26c4 100644 --- a/source/module_base/test/math_chebyshev_test.cpp +++ b/source/module_base/test/math_chebyshev_test.cpp @@ -336,6 +336,9 @@ TEST_F(MathChebyshevTest, tracepolyA) TEST_F(MathChebyshevTest, checkconverge) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; p_chetest = new ModuleBase::Chebyshev(norder); auto fun_sigma_y diff --git a/source/module_base/test/opt_CG_test.cpp b/source/module_base/test/opt_CG_test.cpp index 4b324c7cbb..d2faf8763b 100644 --- a/source/module_base/test/opt_CG_test.cpp +++ b/source/module_base/test/opt_CG_test.cpp @@ -143,6 +143,9 @@ class CG_test : public testing::Test TEST_F(CG_test, Stand_Solve_LinearEq) { +#ifdef __MPI +#undef __MPI +#endif CG_Solve_LinearEq(); EXPECT_NEAR(x[0], 0.5, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 1.6429086563584579739e-18, DOUBLETHRESHOLD); @@ -153,6 +156,9 @@ TEST_F(CG_test, Stand_Solve_LinearEq) TEST_F(CG_test, PR_Solve_LinearEq) { +#ifdef __MPI +#undef __MPI +#endif Solve(1, 0); EXPECT_NEAR(x[0], 0.50000000000003430589, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], -3.4028335704761047964e-14, DOUBLETHRESHOLD); @@ -163,6 +169,9 @@ TEST_F(CG_test, PR_Solve_LinearEq) TEST_F(CG_test, HZ_Solve_LinearEq) { +#ifdef __MPI +#undef __MPI +#endif Solve(2, 0); EXPECT_NEAR(x[0], 0.49999999999999944489, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], -9.4368957093138305936e-16, DOUBLETHRESHOLD); @@ -173,6 +182,9 @@ TEST_F(CG_test, HZ_Solve_LinearEq) TEST_F(CG_test, PR_Min_Func) { +#ifdef __MPI +#undef __MPI +#endif Solve(1, 1); EXPECT_NEAR(x[0], 4.0006805979150792396, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 2.0713759992720870429, DOUBLETHRESHOLD); @@ -183,6 +195,9 @@ TEST_F(CG_test, PR_Min_Func) TEST_F(CG_test, HZ_Min_Func) { +#ifdef __MPI +#undef __MPI +#endif Solve(2, 1); EXPECT_NEAR(x[0], 4.0006825378033568086, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 2.0691732100663737803, DOUBLETHRESHOLD); diff --git a/source/module_base/test/opt_TN_test.cpp b/source/module_base/test/opt_TN_test.cpp index db523b53e9..b136194881 100644 --- a/source/module_base/test/opt_TN_test.cpp +++ b/source/module_base/test/opt_TN_test.cpp @@ -110,6 +110,9 @@ class TN_test : public testing::Test TEST_F(TN_test, TN_Solve_LinearEq) { +#ifdef __MPI +#undef __MPI +#endif Solve(0); EXPECT_NEAR(x[0], 0.50000000000003430589, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], -3.4028335704761047964e-14, DOUBLETHRESHOLD); @@ -120,6 +123,9 @@ TEST_F(TN_test, TN_Solve_LinearEq) TEST_F(TN_test, TN_Min_Func) { +#ifdef __MPI +#undef __MPI +#endif Solve(1); EXPECT_NEAR(x[0], 4.0049968540891525137, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 2.1208751163987624722, DOUBLETHRESHOLD); From 0a40350d707d59a30310ecbb4cd460e5c7cbc58f Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 14:17:31 +0800 Subject: [PATCH 12/15] return base_math_chebyshev --- source/module_base/test/CMakeLists.txt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/source/module_base/test/CMakeLists.txt b/source/module_base/test/CMakeLists.txt index ade269a1e6..0c8fd53461 100644 --- a/source/module_base/test/CMakeLists.txt +++ b/source/module_base/test/CMakeLists.txt @@ -133,7 +133,7 @@ AddTest( AddTest( TARGET base_math_chebyshev LIBS parameter ${math_libs} base device container - SOURCES math_chebyshev_test.cpp ../math_chebyshev.cpp ../tool_quit.cpp ../global_variable.cpp ../timer.cpp ../global_file.cpp ../global_function.cpp ../memory.cpp ../parallel_reduce.cpp + SOURCES math_chebyshev_test.cpp ) AddTest( @@ -145,13 +145,13 @@ AddTest( AddTest( TARGET base_opt_CG LIBS parameter ${math_libs} base device - SOURCES opt_CG_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + SOURCES opt_CG_test.cpp opt_test_tools.cpp ) AddTest( TARGET base_opt_TN LIBS parameter ${math_libs} base device - SOURCES opt_TN_test.cpp opt_test_tools.cpp ../opt_CG.cpp ../opt_DCsrch.cpp ../global_variable.cpp ../parallel_reduce.cpp + SOURCES opt_TN_test.cpp opt_test_tools.cpp ) AddTest( From 2391a681f27807c172b5fb1b254a842bcb9e31f1 Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 15:08:11 +0800 Subject: [PATCH 13/15] Fix MPI bug --- .../module_base/test/math_chebyshev_test.cpp | 48 +++++++++++++++++++ source/module_base/test/opt_CG_test.cpp | 3 ++ source/module_base/test/opt_test_tools.cpp | 3 ++ 3 files changed, 54 insertions(+) diff --git a/source/module_base/test/math_chebyshev_test.cpp b/source/module_base/test/math_chebyshev_test.cpp index 3c3aef26c4..c937dc7fcf 100644 --- a/source/module_base/test/math_chebyshev_test.cpp +++ b/source/module_base/test/math_chebyshev_test.cpp @@ -107,6 +107,9 @@ class MathChebyshevTest : public testing::Test TEST_F(MathChebyshevTest, calcoef_real) { +#ifdef __MPI +#undef __MPI +#endif auto fun_x6 = [&](double x) { return fun.x6(x); }; auto fun_x7 = [&](double x) { return fun.x7(x); }; p_chetest = new ModuleBase::Chebyshev(10); @@ -130,6 +133,9 @@ TEST_F(MathChebyshevTest, calcoef_real) TEST_F(MathChebyshevTest, calcoef_pair) { +#ifdef __MPI +#undef __MPI +#endif auto fun_x6 = [&](double x) { return fun.x6(x); }; auto fun_x7 = [&](double x) { return fun.x7(x); }; p_chetest = new ModuleBase::Chebyshev(10); @@ -148,6 +154,9 @@ TEST_F(MathChebyshevTest, calcoef_pair) TEST_F(MathChebyshevTest, calcoef_complex) { +#ifdef __MPI +#undef __MPI +#endif auto fun_expi = [&](std::complex x) { return fun.expi(x); }; const int norder = 100; const double PI = 3.14159265358979323846; @@ -169,6 +178,9 @@ TEST_F(MathChebyshevTest, calcoef_complex) TEST_F(MathChebyshevTest, calfinalvec_real) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; const double E = 2.718281828459046; p_chetest = new ModuleBase::Chebyshev(norder); @@ -203,6 +215,9 @@ TEST_F(MathChebyshevTest, calfinalvec_real) TEST_F(MathChebyshevTest, calfinalvec_complex) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; const double E = 2.718281828459046; p_chetest = new ModuleBase::Chebyshev(norder); @@ -237,6 +252,9 @@ TEST_F(MathChebyshevTest, calfinalvec_complex) TEST_F(MathChebyshevTest, calpolyvec_complex) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; const double E = 2.718281828459046; p_chetest = new ModuleBase::Chebyshev(norder); @@ -284,6 +302,9 @@ TEST_F(MathChebyshevTest, calpolyvec_complex) TEST_F(MathChebyshevTest, tracepolyA) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; p_chetest = new ModuleBase::Chebyshev(norder); @@ -384,6 +405,9 @@ TEST_F(MathChebyshevTest, checkconverge) TEST_F(MathChebyshevTest, recurs) { +#ifdef __MPI +#undef __MPI +#endif testing::internal::CaptureStdout(); EXPECT_EXIT(ModuleBase::Chebyshev noneche(0), ::testing::ExitedWithCode(1), ""); std::string output = testing::internal::GetCapturedStdout(); @@ -399,6 +423,9 @@ TEST_F(MathChebyshevTest, recurs) #ifdef __ENABLE_FLOAT_FFTW TEST_F(MathChebyshevTest, calcoef_real_float) { +#ifdef __MPI +#undef __MPI +#endif auto fun_x6f = [&](float x) { return fun.x6(x); }; auto fun_x7f = [&](float x) { return fun.x7(x); }; p_fchetest = new ModuleBase::Chebyshev(10); @@ -421,6 +448,9 @@ TEST_F(MathChebyshevTest, calcoef_real_float) TEST_F(MathChebyshevTest, calcoef_pair_float) { +#ifdef __MPI +#undef __MPI +#endif auto fun_x6f = [&](float x) { return fun.x6(x); }; auto fun_x7f = [&](float x) { return fun.x7(x); }; p_fchetest = new ModuleBase::Chebyshev(10); @@ -439,6 +469,9 @@ TEST_F(MathChebyshevTest, calcoef_pair_float) TEST_F(MathChebyshevTest, calcoef_complex_float) { +#ifdef __MPI +#undef __MPI +#endif auto fun_expif = [&](std::complex x) { return fun.expi(x); }; const int norder = 100; const float PI = 3.14159265358979323846; @@ -460,6 +493,9 @@ TEST_F(MathChebyshevTest, calcoef_complex_float) TEST_F(MathChebyshevTest, calfinalvec_real_float) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; const float E = 2.718281828459046; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -494,6 +530,9 @@ TEST_F(MathChebyshevTest, calfinalvec_real_float) TEST_F(MathChebyshevTest, calfinalvec_complex_float) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; const float E = 2.718281828459046; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -528,6 +567,9 @@ TEST_F(MathChebyshevTest, calfinalvec_complex_float) TEST_F(MathChebyshevTest, calpolyvec_float) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; const float E = 2.718281828459046; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -575,6 +617,9 @@ TEST_F(MathChebyshevTest, calpolyvec_float) TEST_F(MathChebyshevTest, tracepolyA_float) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -624,6 +669,9 @@ TEST_F(MathChebyshevTest, tracepolyA_float) TEST_F(MathChebyshevTest, checkconverge_float) { +#ifdef __MPI +#undef __MPI +#endif const int norder = 100; p_fchetest = new ModuleBase::Chebyshev(norder); diff --git a/source/module_base/test/opt_CG_test.cpp b/source/module_base/test/opt_CG_test.cpp index d2faf8763b..2661d997f7 100644 --- a/source/module_base/test/opt_CG_test.cpp +++ b/source/module_base/test/opt_CG_test.cpp @@ -1,3 +1,6 @@ +#ifdef __MPI +#undef __MPI +#endif #include "gtest/gtest.h" #include "../opt_CG.h" #include "../opt_DCsrch.h" diff --git a/source/module_base/test/opt_test_tools.cpp b/source/module_base/test/opt_test_tools.cpp index 1c90b79bca..71e136b3ef 100644 --- a/source/module_base/test/opt_test_tools.cpp +++ b/source/module_base/test/opt_test_tools.cpp @@ -1,3 +1,6 @@ +#ifdef __MPI +#undef __MPI +#endif #include "./opt_test_tools.h" #include From 735329559f6e7c6824c7356e65eaa0a7eb0c7aa2 Mon Sep 17 00:00:00 2001 From: critsium-xy Date: Fri, 3 Jan 2025 16:59:24 +0800 Subject: [PATCH 14/15] Finish --- .../module_base/test/math_chebyshev_test.cpp | 51 +------------------ source/module_base/test/opt_CG_test.cpp | 15 ++++-- source/module_base/test/opt_TN_test.cpp | 6 ++- 3 files changed, 16 insertions(+), 56 deletions(-) diff --git a/source/module_base/test/math_chebyshev_test.cpp b/source/module_base/test/math_chebyshev_test.cpp index c937dc7fcf..a7ea215266 100644 --- a/source/module_base/test/math_chebyshev_test.cpp +++ b/source/module_base/test/math_chebyshev_test.cpp @@ -107,9 +107,6 @@ class MathChebyshevTest : public testing::Test TEST_F(MathChebyshevTest, calcoef_real) { -#ifdef __MPI -#undef __MPI -#endif auto fun_x6 = [&](double x) { return fun.x6(x); }; auto fun_x7 = [&](double x) { return fun.x7(x); }; p_chetest = new ModuleBase::Chebyshev(10); @@ -133,9 +130,6 @@ TEST_F(MathChebyshevTest, calcoef_real) TEST_F(MathChebyshevTest, calcoef_pair) { -#ifdef __MPI -#undef __MPI -#endif auto fun_x6 = [&](double x) { return fun.x6(x); }; auto fun_x7 = [&](double x) { return fun.x7(x); }; p_chetest = new ModuleBase::Chebyshev(10); @@ -154,9 +148,6 @@ TEST_F(MathChebyshevTest, calcoef_pair) TEST_F(MathChebyshevTest, calcoef_complex) { -#ifdef __MPI -#undef __MPI -#endif auto fun_expi = [&](std::complex x) { return fun.expi(x); }; const int norder = 100; const double PI = 3.14159265358979323846; @@ -178,9 +169,6 @@ TEST_F(MathChebyshevTest, calcoef_complex) TEST_F(MathChebyshevTest, calfinalvec_real) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; const double E = 2.718281828459046; p_chetest = new ModuleBase::Chebyshev(norder); @@ -215,9 +203,6 @@ TEST_F(MathChebyshevTest, calfinalvec_real) TEST_F(MathChebyshevTest, calfinalvec_complex) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; const double E = 2.718281828459046; p_chetest = new ModuleBase::Chebyshev(norder); @@ -252,9 +237,6 @@ TEST_F(MathChebyshevTest, calfinalvec_complex) TEST_F(MathChebyshevTest, calpolyvec_complex) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; const double E = 2.718281828459046; p_chetest = new ModuleBase::Chebyshev(norder); @@ -302,9 +284,6 @@ TEST_F(MathChebyshevTest, calpolyvec_complex) TEST_F(MathChebyshevTest, tracepolyA) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; p_chetest = new ModuleBase::Chebyshev(norder); @@ -359,7 +338,6 @@ TEST_F(MathChebyshevTest, checkconverge) { #ifdef __MPI #undef __MPI -#endif const int norder = 100; p_chetest = new ModuleBase::Chebyshev(norder); auto fun_sigma_y @@ -401,13 +379,12 @@ TEST_F(MathChebyshevTest, checkconverge) delete[] v; delete p_chetest; +#define __MPI +#endif } TEST_F(MathChebyshevTest, recurs) { -#ifdef __MPI -#undef __MPI -#endif testing::internal::CaptureStdout(); EXPECT_EXIT(ModuleBase::Chebyshev noneche(0), ::testing::ExitedWithCode(1), ""); std::string output = testing::internal::GetCapturedStdout(); @@ -423,9 +400,6 @@ TEST_F(MathChebyshevTest, recurs) #ifdef __ENABLE_FLOAT_FFTW TEST_F(MathChebyshevTest, calcoef_real_float) { -#ifdef __MPI -#undef __MPI -#endif auto fun_x6f = [&](float x) { return fun.x6(x); }; auto fun_x7f = [&](float x) { return fun.x7(x); }; p_fchetest = new ModuleBase::Chebyshev(10); @@ -448,9 +422,6 @@ TEST_F(MathChebyshevTest, calcoef_real_float) TEST_F(MathChebyshevTest, calcoef_pair_float) { -#ifdef __MPI -#undef __MPI -#endif auto fun_x6f = [&](float x) { return fun.x6(x); }; auto fun_x7f = [&](float x) { return fun.x7(x); }; p_fchetest = new ModuleBase::Chebyshev(10); @@ -469,9 +440,6 @@ TEST_F(MathChebyshevTest, calcoef_pair_float) TEST_F(MathChebyshevTest, calcoef_complex_float) { -#ifdef __MPI -#undef __MPI -#endif auto fun_expif = [&](std::complex x) { return fun.expi(x); }; const int norder = 100; const float PI = 3.14159265358979323846; @@ -493,9 +461,6 @@ TEST_F(MathChebyshevTest, calcoef_complex_float) TEST_F(MathChebyshevTest, calfinalvec_real_float) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; const float E = 2.718281828459046; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -530,9 +495,6 @@ TEST_F(MathChebyshevTest, calfinalvec_real_float) TEST_F(MathChebyshevTest, calfinalvec_complex_float) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; const float E = 2.718281828459046; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -567,9 +529,6 @@ TEST_F(MathChebyshevTest, calfinalvec_complex_float) TEST_F(MathChebyshevTest, calpolyvec_float) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; const float E = 2.718281828459046; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -617,9 +576,6 @@ TEST_F(MathChebyshevTest, calpolyvec_float) TEST_F(MathChebyshevTest, tracepolyA_float) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; p_fchetest = new ModuleBase::Chebyshev(norder); @@ -669,9 +625,6 @@ TEST_F(MathChebyshevTest, tracepolyA_float) TEST_F(MathChebyshevTest, checkconverge_float) { -#ifdef __MPI -#undef __MPI -#endif const int norder = 100; p_fchetest = new ModuleBase::Chebyshev(norder); diff --git a/source/module_base/test/opt_CG_test.cpp b/source/module_base/test/opt_CG_test.cpp index 2661d997f7..ef16ee4a0d 100644 --- a/source/module_base/test/opt_CG_test.cpp +++ b/source/module_base/test/opt_CG_test.cpp @@ -148,64 +148,69 @@ TEST_F(CG_test, Stand_Solve_LinearEq) { #ifdef __MPI #undef __MPI -#endif CG_Solve_LinearEq(); EXPECT_NEAR(x[0], 0.5, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 1.6429086563584579739e-18, DOUBLETHRESHOLD); EXPECT_NEAR(x[2], 1.5, DOUBLETHRESHOLD); ASSERT_EQ(final_iter, 4); ASSERT_EQ(cg.get_iter(), 4); +#define __MPI +#endif } TEST_F(CG_test, PR_Solve_LinearEq) { #ifdef __MPI #undef __MPI -#endif Solve(1, 0); EXPECT_NEAR(x[0], 0.50000000000003430589, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], -3.4028335704761047964e-14, DOUBLETHRESHOLD); EXPECT_NEAR(x[2], 1.5000000000000166533, DOUBLETHRESHOLD); ASSERT_EQ(final_iter, 3); ASSERT_EQ(cg.get_iter(), 3); +#define __MPI +#endif } TEST_F(CG_test, HZ_Solve_LinearEq) { #ifdef __MPI #undef __MPI -#endif Solve(2, 0); EXPECT_NEAR(x[0], 0.49999999999999944489, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], -9.4368957093138305936e-16, DOUBLETHRESHOLD); EXPECT_NEAR(x[2], 1.5000000000000011102, DOUBLETHRESHOLD); ASSERT_EQ(final_iter, 3); ASSERT_EQ(cg.get_iter(), 3); +#define __MPI +#endif } TEST_F(CG_test, PR_Min_Func) { #ifdef __MPI #undef __MPI -#endif Solve(1, 1); EXPECT_NEAR(x[0], 4.0006805979150792396, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 2.0713759992720870429, DOUBLETHRESHOLD); EXPECT_NEAR(x[2], 9.2871067233169171118, DOUBLETHRESHOLD); ASSERT_EQ(final_iter, 18); ASSERT_EQ(cg.get_iter(), 18); +#define __MPI +#endif } TEST_F(CG_test, HZ_Min_Func) { #ifdef __MPI #undef __MPI -#endif Solve(2, 1); EXPECT_NEAR(x[0], 4.0006825378033568086, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 2.0691732100663737803, DOUBLETHRESHOLD); EXPECT_NEAR(x[2], 9.2780872787668311474, DOUBLETHRESHOLD); ASSERT_EQ(final_iter, 18); ASSERT_EQ(cg.get_iter(), 18); +#define __MPI +#endif } // g++ -std=c++11 ../opt_CG.cpp ../opt_DCsrch.cpp ./CG_test.cpp ./test_tools.cpp -lgtest -lpthread -lgtest_main -o test.exe \ No newline at end of file diff --git a/source/module_base/test/opt_TN_test.cpp b/source/module_base/test/opt_TN_test.cpp index b136194881..ee3b10ad7b 100644 --- a/source/module_base/test/opt_TN_test.cpp +++ b/source/module_base/test/opt_TN_test.cpp @@ -112,24 +112,26 @@ TEST_F(TN_test, TN_Solve_LinearEq) { #ifdef __MPI #undef __MPI -#endif Solve(0); EXPECT_NEAR(x[0], 0.50000000000003430589, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], -3.4028335704761047964e-14, DOUBLETHRESHOLD); EXPECT_NEAR(x[2], 1.5000000000000166533, DOUBLETHRESHOLD); ASSERT_EQ(final_iter, 1); ASSERT_EQ(tn.get_iter(), 1); +#define __MPI +#endif } TEST_F(TN_test, TN_Min_Func) { #ifdef __MPI #undef __MPI -#endif Solve(1); EXPECT_NEAR(x[0], 4.0049968540891525137, DOUBLETHRESHOLD); EXPECT_NEAR(x[1], 2.1208751163987624722, DOUBLETHRESHOLD); EXPECT_NEAR(x[2], 9.4951527720891863993, DOUBLETHRESHOLD); ASSERT_EQ(final_iter, 6); ASSERT_EQ(tn.get_iter(), 6); +#define __MPI +#endif } \ No newline at end of file From d03b7b69b074bc730bc94e61e275dc4c08b70b73 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 3 Jan 2025 09:38:55 +0000 Subject: [PATCH 15/15] [pre-commit.ci lite] apply automatic fixes --- source/module_base/test/opt_CG_test.cpp | 23 ++++++++++++++--------- source/module_base/test/opt_TN_test.cpp | 18 +++++++++++------- 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/source/module_base/test/opt_CG_test.cpp b/source/module_base/test/opt_CG_test.cpp index ef16ee4a0d..b8abeb5760 100644 --- a/source/module_base/test/opt_CG_test.cpp +++ b/source/module_base/test/opt_CG_test.cpp @@ -21,10 +21,10 @@ class CG_test : public testing::Test double residual = 10.; double tol = 1e-5; int final_iter = 0; - char *task = NULL; - double *Ap = NULL; - double *p = NULL; - double *x = NULL; + char *task = nullptr; + double *Ap = nullptr; + double *p = nullptr; + double *x = nullptr; void SetUp() { @@ -68,7 +68,8 @@ class CG_test : public testing::Test tools.le.get_Ap(tools.le.A, p, Ap); int ifPD = 0; step = cg.step_length(Ap, p, ifPD); - for (int i = 0; i < 3; ++i) x[i] += step * p[i]; + for (int i = 0; i < 3; ++i) { x[i] += step * p[i]; +} residual = cg.get_residual(); } } @@ -105,14 +106,16 @@ class CG_test : public testing::Test { tools.dfuncdx(x, gradient, func_label); residual = 0; - for (int i = 0; i<3 ;++i) residual += gradient[i] * gradient[i]; + for (int i = 0; i<3 ;++i) { residual += gradient[i] * gradient[i]; +} if (residual < tol) { final_iter = iter; break; } cg.next_direct(gradient, cg_label, p); - for (int i = 0; i < 3; ++i) temp_x[i] = x[i]; + for (int i = 0; i < 3; ++i) { temp_x[i] = x[i]; +} task[0] = 'S'; task[1] = 'T'; task[2] = 'A'; task[3] = 'R'; task[4] = 'T'; while (true) { @@ -121,7 +124,8 @@ class CG_test : public testing::Test ds.dcSrch(f, g, step, task); if (task[0] == 'F' && task[1] == 'G') { - for (int j = 0; j < 3; ++j) temp_x[j] = x[j] + step * p[j]; + for (int j = 0; j < 3; ++j) { temp_x[j] = x[j] + step * p[j]; +} continue; } else if (task[0] == 'C' && task[1] == 'O') @@ -137,7 +141,8 @@ class CG_test : public testing::Test break; } } - for (int i = 0; i < 3; ++i) x[i] += step * p[i]; + for (int i = 0; i < 3; ++i) { x[i] += step * p[i]; +} } delete[] temp_x; delete[] gradient; diff --git a/source/module_base/test/opt_TN_test.cpp b/source/module_base/test/opt_TN_test.cpp index ee3b10ad7b..1fc5b7f2d6 100644 --- a/source/module_base/test/opt_TN_test.cpp +++ b/source/module_base/test/opt_TN_test.cpp @@ -17,9 +17,9 @@ class TN_test : public testing::Test double tol = 1e-5; int final_iter = 0; int flag = 0; - char *task = NULL; - double *p = NULL; - double *x = NULL; + char *task = nullptr; + double *p = nullptr; + double *x = nullptr; void SetUp() { @@ -61,7 +61,8 @@ class TN_test : public testing::Test { tools.dfuncdx(x, gradient, func_label); residual = 0; - for (int i = 0; i<3 ;++i) residual += gradient[i] * gradient[i]; + for (int i = 0; i<3 ;++i) { residual += gradient[i] * gradient[i]; +} if (residual < tol) { final_iter = iter; @@ -75,7 +76,8 @@ class TN_test : public testing::Test { tn.next_direct(x, gradient, flag, p, &(tools.mf), &ModuleESolver::ESolver_OF::dfuncdx); } - for (int i = 0; i < 3; ++i) temp_x[i] = x[i]; + for (int i = 0; i < 3; ++i) { temp_x[i] = x[i]; +} task[0] = 'S'; task[1] = 'T'; task[2] = 'A'; task[3] = 'R'; task[4] = 'T'; while (true) { @@ -84,7 +86,8 @@ class TN_test : public testing::Test ds.dcSrch(f, g, step, task); if (task[0] == 'F' && task[1] == 'G') { - for (int j = 0; j < 3; ++j) temp_x[j] = x[j] + step * p[j]; + for (int j = 0; j < 3; ++j) { temp_x[j] = x[j] + step * p[j]; +} continue; } else if (task[0] == 'C' && task[1] == 'O') @@ -100,7 +103,8 @@ class TN_test : public testing::Test break; } } - for (int i = 0; i < 3; ++i) x[i] += step * p[i]; + for (int i = 0; i < 3; ++i) { x[i] += step * p[i]; +} } delete[] temp_x; delete[] gradient;