diff --git a/CMakeLists.txt b/CMakeLists.txt index 8e53bad92fe..30376b62708 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -465,6 +465,7 @@ endif() # version. Use cmake with AMD-ROCm: # https://rocmdocs.amd.com/en/latest/Installation_Guide/Using-CMake-with-AMD-ROCm.html if(USE_ROCM) + set_if_higher(CMAKE_CXX_STANDARD 14) message( DEPRECATION "Option USE_ROCM is deprecated and will be ignored in a future release. ") @@ -485,7 +486,6 @@ if(USE_ROCM) endif() endif() set(CMAKE_MODULE_PATH "${HIP_PATH}/cmake" ${CMAKE_MODULE_PATH}) - set(HIP_HIPCC_FLAGS -fno-gpu-rdc; --std=c++14) # --amdgpu-target=gfx906 # find_package(ROCM REQUIRED) find_package(HIP REQUIRED) diff --git a/docs/advanced/install.md b/docs/advanced/install.md index 1d786c03b20..20e6678fa97 100644 --- a/docs/advanced/install.md +++ b/docs/advanced/install.md @@ -11,6 +11,13 @@ The [ELPA](https://elpa.mpcdf.mpg.de/) library provides highly efficient and hig To build ABACUS with ELPA support, pass `-DENABLE_ELPA=ON` to CMake. +For AMD GPUs and DCUs, build ELPA 2024.03.001 or later with `--enable-amd-gpu` +and make its pkg-config directory available through `PKG_CONFIG_PATH`. Configure ABACUS +with `-DENABLE_MPI=ON -DENABLE_LCAO=ON -DENABLE_ELPA=ON -DUSE_ROCM=ON`. +Select `basis_type lcao`, `ks_solver elpa`, and `device gpu` in INPUT. +For a distributed single-k-point solve, set `kpar 1` and launch one MPI rank +per GPU. + ## Build with Libxc ABACUS use exchange-correlation functionals by default. However, for some functionals (such as HSE hybrid functional), Libxc is required. diff --git a/source/CMakeLists.txt b/source/CMakeLists.txt index 07aa5999d69..ce60f4fa3ee 100644 --- a/source/CMakeLists.txt +++ b/source/CMakeLists.txt @@ -380,14 +380,8 @@ if(USE_CUDA) endif() if(USE_ROCM) - list( - APPEND - _abacus_feature_libs - hip::host - hip::device - hip::hipfft - roc::hipblas - roc::hipsolver) + list(APPEND _abacus_feature_libs hip::host hip::hipfft roc::hipblas + roc::hipsolver) list(APPEND _abacus_feature_include_dirs ${ROCM_PATH}/include) endif() @@ -428,7 +422,6 @@ foreach( cusolverMp::cusolverMp cublasMp::cublasMp hip::host - hip::device hip::hipfft roc::hipblas roc::hipsolver) @@ -737,6 +730,7 @@ if(USE_ROCM) source_hsolver/module_linalg/kernels/rocm/linear_op.hip.cu source_estate/kernels/rocm/elecstate_op.hip.cu source_base/module_device/rocm/memory_op.hip.cu + source_pw/module_pwdft/kernels/rocm/exx_pw_op.hip.cu source_pw/module_pwdft/kernels/rocm/force_op.hip.cu source_pw/module_pwdft/kernels/rocm/stress_op.hip.cu source_cell/kernels/rocm/wf_op.hip.cu @@ -744,7 +738,10 @@ if(USE_ROCM) source_base/kernels/rocm/math_kernel_op.hip.cu source_base/kernels/rocm/math_kernel_op_vec.hip.cu source_base/kernels/rocm/math_ylm_op.hip.cu - source_hamilt/module_xc/kernels/rocm/xc_functional_op.hip.cu) + source_hamilt/module_xc/kernels/rocm/xc_functional_op.hip.cu + HIPCC_OPTIONS + -fno-gpu-rdc + --std=c++${CMAKE_CXX_STANDARD}) endif() if(USE_DSP) @@ -756,8 +753,8 @@ add_library(device OBJECT ${device_srcs}) if(USE_CUDA) target_link_libraries(device PRIVATE cusolver cublas cufft) elseif(USE_ROCM) - target_link_libraries(device PRIVATE device_rocm hip::host hip::device - hip::hipfft roc::hipblas roc::hipsolver) + target_link_libraries(device PRIVATE device_rocm hip::host hip::hipfft + roc::hipblas roc::hipsolver) endif() # base uses symbols from device (memory_op and math_ylm_op). diff --git a/source/source_base/kernels/rocm/math_kernel_op.hip.cu b/source/source_base/kernels/rocm/math_kernel_op.hip.cu index 2d6f471b3c9..5a27e5ad8ad 100644 --- a/source/source_base/kernels/rocm/math_kernel_op.hip.cu +++ b/source/source_base/kernels/rocm/math_kernel_op.hip.cu @@ -495,7 +495,7 @@ void matrix_mul_vector_op::operator()(const int double *a, const int &lda, const double *b, const double alpha, double *c, const int &ldc){ dim3 thread(16, 16, 1); dim3 block((m + thread.x - 1) / thread.x, (n + thread.y - 1) / thread.y, 1); - hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel), dim3(block, thread), + hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel), block, thread, 0, 0, m, n, a, lda, b, alpha, c, ldc); hipCheckOnDebug(); } @@ -505,7 +505,7 @@ void matrix_mul_vector_op, base_device::DEVICE_GPU>::operato std::complex *a, const int &lda, const float *b, const float alpha, std::complex *c, const int &ldc){ dim3 thread(16, 16, 1); dim3 block((m + thread.x - 1) / thread.x, (n + thread.y - 1) / thread.y, 1); - hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel, float>), dim3(block, thread), + hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel, float>), block, thread, 0, 0, m, n, reinterpret_cast*>(a), lda, b, alpha, reinterpret_cast*>(c), ldc); hipCheckOnDebug(); @@ -517,7 +517,7 @@ void matrix_mul_vector_op, base_device::DEVICE_GPU>::operat { dim3 thread(16, 16, 1); dim3 block((m + thread.x - 1) / thread.x, (n + thread.y - 1) / thread.y, 1); - hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel, double>), dim3(block, thread), + hipLaunchKernelGGL(HIP_KERNEL_NAME(matrix_multiply_vector_kernel, double>), block, thread, 0, 0, m, n, reinterpret_cast*>(a), lda, b, alpha, reinterpret_cast*>(c), ldc); hipCheckOnDebug(); diff --git a/source/source_base/kernels/rocm/math_kernel_op_vec.hip.cu b/source/source_base/kernels/rocm/math_kernel_op_vec.hip.cu index e6ebdd4e91c..b0710a16ebc 100644 --- a/source/source_base/kernels/rocm/math_kernel_op_vec.hip.cu +++ b/source/source_base/kernels/rocm/math_kernel_op_vec.hip.cu @@ -1,4 +1,5 @@ #include "source_base/kernels/math_kernel_op.h" +#include "source_base/parallel_reduce.h" #include #include diff --git a/source/source_base/module_container/ATen/kernels/rocm/blas.hip.cu b/source/source_base/module_container/ATen/kernels/rocm/blas.hip.cu index 9fa7f63f08b..d6420db82dc 100644 --- a/source/source_base/module_container/ATen/kernels/rocm/blas.hip.cu +++ b/source/source_base/module_container/ATen/kernels/rocm/blas.hip.cu @@ -25,17 +25,26 @@ void destroyGpuBlasHandle() { template struct blas_nrm2 { - T operator()( + using Real = typename GetTypeReal::type; + Real operator()( const int n, const T *x, const int incx) { - T result; + Real result; hipBlasConnector::nrm2(hipblas_handle, n, x, incx, &result); return result; } }; +template +struct blas_copy { + void operator()(const int n, const T* x, const int incx, T* y, const int incy) + { + hipBlasConnector::copy(hipblas_handle, n, x, incx, y, incy); + } +}; + template struct blas_dot { void operator()( @@ -209,6 +218,11 @@ struct blas_gemm_batched_strided { }; // Explicitly instantiate functors for the types of functor registered. +template struct blas_copy; +template struct blas_copy; +template struct blas_copy, DEVICE_GPU>; +template struct blas_copy, DEVICE_GPU>; + template struct blas_nrm2; template struct blas_nrm2; template struct blas_nrm2 , DEVICE_GPU>; @@ -260,4 +274,4 @@ template struct blas_gemm_batched_strided, DEVICE_GPU>; template struct blas_gemm_batched_strided, DEVICE_GPU>; } // namespace kernels -} // namespace container \ No newline at end of file +} // namespace container diff --git a/source/source_base/module_container/ATen/kernels/rocm/lapack.hip.cu b/source/source_base/module_container/ATen/kernels/rocm/lapack.hip.cu index 07572a657ab..876087de9fe 100644 --- a/source/source_base/module_container/ATen/kernels/rocm/lapack.hip.cu +++ b/source/source_base/module_container/ATen/kernels/rocm/lapack.hip.cu @@ -97,20 +97,37 @@ template struct lapack_heevd { using Real = typename GetTypeReal::type; void operator()( - const char& jobz, - const char& uplo, + const int dim, T* Mat, - const int& dim, + const int lda, Real* eigen_val) { - // hipSolverConnector::heevd(hipsolver_handle, jobz, uplo, dim, Mat, dim, eigen_val); - std::vector H_Mat(dim * dim, static_cast(0.0)); - std::vector H_eigen_val(dim, static_cast(0.0)); - hipMemcpy(H_Mat.data(), Mat, sizeof(T) * H_Mat.size(), hipMemcpyDeviceToHost); - hipMemcpy(H_eigen_val.data(), eigen_val, sizeof(Real) * H_eigen_val.size(), hipMemcpyDeviceToHost); - lapack_heevd()(jobz, uplo, H_Mat.data(), dim, H_eigen_val.data()); - hipMemcpy(Mat, H_Mat.data(), sizeof(T) * H_Mat.size(), hipMemcpyHostToDevice); - hipMemcpy(eigen_val, H_eigen_val.data(), sizeof(Real) * H_eigen_val.size(), hipMemcpyHostToDevice); + std::vector host_mat(static_cast(lda) * dim); + std::vector host_eigenvalues(dim); + const size_t matrix_bytes = sizeof(T) * host_mat.size(); + const size_t eigenvalue_bytes = sizeof(Real) * host_eigenvalues.size(); + hipErrcheck(hipMemcpy(host_mat.data(), Mat, matrix_bytes, hipMemcpyDeviceToHost)); + lapack_heevd()(dim, host_mat.data(), lda, host_eigenvalues.data()); + hipErrcheck(hipMemcpy(Mat, host_mat.data(), matrix_bytes, hipMemcpyHostToDevice)); + hipErrcheck(hipMemcpy(eigen_val, host_eigenvalues.data(), eigenvalue_bytes, hipMemcpyHostToDevice)); + } +}; + +template +struct lapack_heevx { + using Real = typename GetTypeReal::type; + void operator()(const int n, const int lda, const T* mat, const int neig, Real* eigenvalues, T* eigenvectors) + { + std::vector host_mat(static_cast(lda) * n); + std::vector host_eigenvalues(n); + std::vector host_eigenvectors(static_cast(lda) * neig); + const size_t matrix_bytes = sizeof(T) * host_mat.size(); + const size_t eigenvalue_bytes = sizeof(Real) * neig; + const size_t eigenvector_bytes = sizeof(T) * host_eigenvectors.size(); + hipErrcheck(hipMemcpy(host_mat.data(), mat, matrix_bytes, hipMemcpyDeviceToHost)); + lapack_heevx()(n, lda, host_mat.data(), neig, host_eigenvalues.data(), host_eigenvectors.data()); + hipErrcheck(hipMemcpy(eigenvalues, host_eigenvalues.data(), eigenvalue_bytes, hipMemcpyHostToDevice)); + hipErrcheck(hipMemcpy(eigenvectors, host_eigenvectors.data(), eigenvector_bytes, hipMemcpyHostToDevice)); } }; @@ -153,6 +170,11 @@ template struct lapack_heevd; template struct lapack_heevd, DEVICE_GPU>; template struct lapack_heevd, DEVICE_GPU>; +template struct lapack_heevx; +template struct lapack_heevx; +template struct lapack_heevx, DEVICE_GPU>; +template struct lapack_heevx, DEVICE_GPU>; + template struct lapack_hegvd; template struct lapack_hegvd; template struct lapack_hegvd, DEVICE_GPU>; diff --git a/source/source_base/module_container/CMakeLists.txt b/source/source_base/module_container/CMakeLists.txt index aedef550ed4..2393c18435e 100644 --- a/source/source_base/module_container/CMakeLists.txt +++ b/source/source_base/module_container/CMakeLists.txt @@ -15,7 +15,8 @@ add_subdirectory(ATen/core) add_subdirectory(ATen/kernels) if(USE_ROCM) - hip_add_library(container_rocm STATIC ${ATen_ROCM_SRCS}) + hip_add_library(container_rocm STATIC ${ATen_ROCM_SRCS} HIPCC_OPTIONS + -fno-gpu-rdc --std=c++${CMAKE_CXX_STANDARD}) set(ATen_ROCM_DEPENDENCY_LIBS container_rocm) endif() diff --git a/source/source_base/module_container/base/third_party/hipblas.h b/source/source_base/module_container/base/third_party/hipblas.h index 52db82b6509..72531d817f7 100644 --- a/source/source_base/module_container/base/third_party/hipblas.h +++ b/source/source_base/module_container/base/third_party/hipblas.h @@ -1,6 +1,7 @@ #ifndef BASE_THIRD_PARTY_HIPBLAS_H_ #define BASE_THIRD_PARTY_HIPBLAS_H_ +#include #include #include @@ -10,6 +11,60 @@ namespace container { namespace hipBlasConnector { +static inline +void nrm2(hipblasHandle_t handle, int n, const float* x, int incx, float* result) +{ + hipblasErrcheck(hipblasSnrm2(handle, n, x, incx, result)); +} + +static inline +void nrm2(hipblasHandle_t handle, int n, const double* x, int incx, double* result) +{ + hipblasErrcheck(hipblasDnrm2(handle, n, x, incx, result)); +} + +static inline +void nrm2(hipblasHandle_t handle, int n, const std::complex* x, int incx, float* result) +{ + const hipblasComplex* input = reinterpret_cast(x); + hipblasErrcheck(hipblasScnrm2(handle, n, input, incx, result)); +} + +static inline +void nrm2(hipblasHandle_t handle, int n, const std::complex* x, int incx, double* result) +{ + const hipblasDoubleComplex* input = reinterpret_cast(x); + hipblasErrcheck(hipblasDznrm2(handle, n, input, incx, result)); +} + +static inline +void copy(hipblasHandle_t handle, int n, const float* x, int incx, float* y, int incy) +{ + hipblasErrcheck(hipblasScopy(handle, n, x, incx, y, incy)); +} + +static inline +void copy(hipblasHandle_t handle, int n, const double* x, int incx, double* y, int incy) +{ + hipblasErrcheck(hipblasDcopy(handle, n, x, incx, y, incy)); +} + +static inline +void copy(hipblasHandle_t handle, int n, const std::complex* x, int incx, std::complex* y, int incy) +{ + const hipblasComplex* input = reinterpret_cast(x); + hipblasComplex* output = reinterpret_cast(y); + hipblasErrcheck(hipblasCcopy(handle, n, input, incx, output, incy)); +} + +static inline +void copy(hipblasHandle_t handle, int n, const std::complex* x, int incx, std::complex* y, int incy) +{ + const hipblasDoubleComplex* input = reinterpret_cast(x); + hipblasDoubleComplex* output = reinterpret_cast(y); + hipblasErrcheck(hipblasZcopy(handle, n, input, incx, output, incy)); +} + static inline void dot(hipblasHandle_t& handle, const int& n, const float *x, const int& incx, const float *y, const int& incy, float* result) { diff --git a/source/source_base/module_device/gpu_runtime.h b/source/source_base/module_device/gpu_runtime.h index ce759c0d98c..98ba61f5d49 100644 --- a/source/source_base/module_device/gpu_runtime.h +++ b/source/source_base/module_device/gpu_runtime.h @@ -64,7 +64,7 @@ #define gpuDeviceCanAccessPeer hipDeviceCanAccessPeer // Error check macro -#define gpuErrcheck CHECK_CUDA +#define gpuErrcheck hipErrcheck #endif // __CUDA / __ROCM diff --git a/source/source_base/module_device/rocm/memory_op.hip.cu b/source/source_base/module_device/rocm/memory_op.hip.cu index 6b3f1c9d3e9..c8331afddfc 100644 --- a/source/source_base/module_device/rocm/memory_op.hip.cu +++ b/source/source_base/module_device/rocm/memory_op.hip.cu @@ -95,6 +95,60 @@ void synchronize_memory_op +void synchronize_memory_2d_op::operator()( + FPTYPE* arr_out, + const size_t dpitch, + const FPTYPE* arr_in, + const size_t spitch, + const size_t width, + const size_t height) +{ + hipErrcheck(hipMemcpy2D(arr_out, + dpitch * sizeof(FPTYPE), + arr_in, + spitch * sizeof(FPTYPE), + width * sizeof(FPTYPE), + height, + hipMemcpyDeviceToHost)); +} + +template +void synchronize_memory_2d_op::operator()( + FPTYPE* arr_out, + const size_t dpitch, + const FPTYPE* arr_in, + const size_t spitch, + const size_t width, + const size_t height) +{ + hipErrcheck(hipMemcpy2D(arr_out, + dpitch * sizeof(FPTYPE), + arr_in, + spitch * sizeof(FPTYPE), + width * sizeof(FPTYPE), + height, + hipMemcpyHostToDevice)); +} + +template +void synchronize_memory_2d_op::operator()( + FPTYPE* arr_out, + const size_t dpitch, + const FPTYPE* arr_in, + const size_t spitch, + const size_t width, + const size_t height) +{ + hipErrcheck(hipMemcpy2D(arr_out, + dpitch * sizeof(FPTYPE), + arr_in, + spitch * sizeof(FPTYPE), + width * sizeof(FPTYPE), + height, + hipMemcpyDeviceToDevice)); +} + template struct cast_memory_op { void operator()(FPTYPE_out* arr_out, @@ -175,6 +229,22 @@ template struct set_memory_op; template struct set_memory_op, base_device::DEVICE_GPU>; template struct set_memory_op, base_device::DEVICE_GPU>; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op; +template struct synchronize_memory_2d_op, base_device::DEVICE_CPU, base_device::DEVICE_GPU>; +template struct synchronize_memory_2d_op, base_device::DEVICE_GPU, base_device::DEVICE_CPU>; +template struct synchronize_memory_2d_op, base_device::DEVICE_GPU, base_device::DEVICE_GPU>; +template struct synchronize_memory_2d_op, base_device::DEVICE_CPU, base_device::DEVICE_GPU>; +template struct synchronize_memory_2d_op, base_device::DEVICE_GPU, base_device::DEVICE_CPU>; +template struct synchronize_memory_2d_op, base_device::DEVICE_GPU, base_device::DEVICE_GPU>; + template struct synchronize_memory_op; template struct synchronize_memory_op; template struct synchronize_memory_op; diff --git a/source/source_estate/kernels/rocm/elecstate_op.hip.cu b/source/source_estate/kernels/rocm/elecstate_op.hip.cu index 8ab453e0c35..8eed5a5f0d3 100644 --- a/source/source_estate/kernels/rocm/elecstate_op.hip.cu +++ b/source/source_estate/kernels/rocm/elecstate_op.hip.cu @@ -14,11 +14,12 @@ __global__ void elecstate_pw( const int nrxx, const FPTYPE w1, FPTYPE* rho, - const thrust::complex* wfcr) + const thrust::complex* wfcr, + const int nrxx_dense) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if(idx >= nrxx) {return;} - rho[spin * nrxx + idx] += w1 * norm(wfcr[idx]); + rho[spin * nrxx_dense + idx] += w1 * norm(wfcr[idx]); } template @@ -29,31 +30,32 @@ __global__ void elecstate_pw( const FPTYPE w1, FPTYPE* rho, const thrust::complex* wfcr, - const thrust::complex* wfcr_another_spin) + const thrust::complex* wfcr_another_spin, + const int nrxx_dense) { int idx = blockIdx.x * blockDim.x + threadIdx.x; if(idx >= nrxx) {return;} - rho[0 * nrxx + idx] += w1 * (norm(wfcr[idx]) + norm(wfcr_another_spin[idx])); + rho[idx] += w1 * (norm(wfcr[idx]) + norm(wfcr_another_spin[idx])); if (DOMAG) { - rho[1 * nrxx + idx] += w1 * 2.0 + rho[nrxx_dense + idx] += w1 * 2.0 * (wfcr[idx].real() * wfcr_another_spin[idx].real() + wfcr[idx].imag() * wfcr_another_spin[idx].imag()); - rho[2 * nrxx + idx] += w1 * 2.0 + rho[2 * nrxx_dense + idx] += w1 * 2.0 * (wfcr[idx].real() * wfcr_another_spin[idx].imag() - wfcr_another_spin[idx].real() * wfcr[idx].imag()); - rho[3 * nrxx + idx] += w1 * (norm(wfcr[idx]) - norm(wfcr_another_spin[idx])); + rho[3 * nrxx_dense + idx] += w1 * (norm(wfcr[idx]) - norm(wfcr_another_spin[idx])); } else if(DOMAG_Z) { - rho[1 * nrxx + idx] = 0; - rho[2 * nrxx + idx] = 0; - rho[3 * nrxx + idx] += w1 * (norm(wfcr[idx]) - norm(wfcr_another_spin[idx])); + rho[nrxx_dense + idx] = 0; + rho[2 * nrxx_dense + idx] = 0; + rho[3 * nrxx_dense + idx] += w1 * (norm(wfcr[idx]) - norm(wfcr_another_spin[idx])); } else { // Keep the scalar charge accumulated above; only magnetization is disabled. - rho[1 * nrxx + idx] = 0; - rho[2 * nrxx + idx] = 0; - rho[3 * nrxx + idx] = 0; + rho[nrxx_dense + idx] = 0; + rho[2 * nrxx_dense + idx] = 0; + rho[3 * nrxx_dense + idx] = 0; } } @@ -61,6 +63,7 @@ template void elecstate_pw_op::operator()(const base_device::DEVICE_GPU* ctx, const int& spin, const int& nrxx, + const int& nrxx_dense, const FPTYPE& w1, FPTYPE** rho, const std::complex* wfcr) @@ -68,7 +71,7 @@ void elecstate_pw_op::operator()(const base_dev const int block = (nrxx + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK; hipLaunchKernelGGL(HIP_KERNEL_NAME(elecstate_pw), dim3(block), dim3(THREADS_PER_BLOCK), 0, 0, spin, nrxx, w1, rho[0], - reinterpret_cast*>(wfcr) + reinterpret_cast*>(wfcr), nrxx_dense ); hipCheckOnDebug(); @@ -79,6 +82,7 @@ void elecstate_pw_op::operator()(const base_dev const bool& DOMAG, const bool& DOMAG_Z, const int& nrxx, + const int& nrxx_dense, const FPTYPE& w1, FPTYPE** rho, const std::complex* wfcr, @@ -88,7 +92,7 @@ void elecstate_pw_op::operator()(const base_dev hipLaunchKernelGGL(HIP_KERNEL_NAME(elecstate_pw), dim3(block), dim3(THREADS_PER_BLOCK), 0, 0, DOMAG, DOMAG_Z, nrxx, w1, rho[0], reinterpret_cast*>(wfcr), - reinterpret_cast*>(wfcr_another_spin) + reinterpret_cast*>(wfcr_another_spin), nrxx_dense ); hipCheckOnDebug(); diff --git a/source/source_hsolver/diago_elpa_native.cpp b/source/source_hsolver/diago_elpa_native.cpp index 9f37ecc9539..ebfdc84f5c9 100644 --- a/source/source_hsolver/diago_elpa_native.cpp +++ b/source/source_hsolver/diago_elpa_native.cpp @@ -1,6 +1,7 @@ #include "diago_elpa_native.h" #include "source_base/global_function.h" +#include "source_base/module_device/device.h" #include "source_base/module_external/blas_connector.h" #include "source_base/module_external/blacs_connector.h" #include "source_base/timer.h" @@ -135,11 +136,17 @@ void DiagoElpaNative::diag_pool(ModuleBase::MatrixBlock& h_mat, #define ELPA_WITH_AMD_GPU_VERSION 0 #define ELPA_WITH_SYCL_GPU_VERSION 0 */ -#if ELPA_WITH_NVIDIA_GPU_VERSION +#if ELPA_WITH_NVIDIA_GPU_VERSION || (defined(__ROCM) && ELPA_WITH_AMD_GPU_VERSION && ELPA_API_VERSION >= 20241103) if (this->use_gpu) { +#if ELPA_WITH_NVIDIA_GPU_VERSION elpa_set(handle, "nvidia-gpu", 1, &success); elpa_set(handle, "real_kernel", ELPA_2STAGE_REAL_NVIDIA_GPU, &success); +#else + elpa_set(handle, "amd-gpu", 1, &success); +#endif + const int device_id = base_device::DeviceContext::instance().get_device_id(); + elpa_set(handle, "use_gpu_id", device_id, &success); elpa_setup_gpu(handle); } #endif diff --git a/source/source_hsolver/diago_elpa_native.h b/source/source_hsolver/diago_elpa_native.h index 0bdc5ac42c4..25f67e9c9de 100644 --- a/source/source_hsolver/diago_elpa_native.h +++ b/source/source_hsolver/diago_elpa_native.h @@ -18,7 +18,7 @@ class DiagoElpaNative public: /// @param nlocal_in global dimension of the NAO Hamiltonian /// @param nbands_in number of lowest eigenpairs to compute - /// @param use_gpu_in offload to the NVIDIA-GPU ELPA kernels when ELPA was built with GPU support + /// @param use_gpu_in request GPU acceleration in ELPA DiagoElpaNative(const int nlocal_in, const int nbands_in, const bool use_gpu_in) : DiagoElpaNative(nlocal_in, nbands_in, use_gpu_in, DecomposedState) {} diff --git a/source/source_hsolver/kernels/rocm/hegvd_op.hip.cu b/source/source_hsolver/kernels/rocm/hegvd_op.hip.cu index 93b3457af69..00b75d1b076 100644 --- a/source/source_hsolver/kernels/rocm/hegvd_op.hip.cu +++ b/source/source_hsolver/kernels/rocm/hegvd_op.hip.cu @@ -2,6 +2,7 @@ #include #include +#include namespace hsolver { @@ -106,7 +107,7 @@ void hegvd_op, base_device::DEVICE_GPU>::operator()(const ba const int nstart, const int ldh, const std::complex* _hcc, - const std::complex* _scc, + std::complex* _scc, float* _eigenvalue, std::complex* _vcc) { @@ -178,7 +179,7 @@ void hegvd_op, base_device::DEVICE_GPU>::operator()(const b const int nstart, const int ldh, const std::complex* _hcc, - const std::complex* _scc, + std::complex* _scc, double* _eigenvalue, std::complex* _vcc ) @@ -365,4 +366,4 @@ void hegvx_op::operator()(const base_device::DE } #endif // __LCAO -} // namespace hsolver \ No newline at end of file +} // namespace hsolver diff --git a/source/source_hsolver/unittests/CMakeLists.txt b/source/source_hsolver/unittests/CMakeLists.txt index 92f2bf96b15..71c052733a6 100644 --- a/source/source_hsolver/unittests/CMakeLists.txt +++ b/source/source_hsolver/unittests/CMakeLists.txt @@ -172,6 +172,7 @@ if(ENABLE_MPI) if(TARGET ELPA::ELPA) addtest( TARGET MODULE_HSOLVER_LCAO + KEEP_FEATURE_DEFINITIONS __ROCM LIBS parameter ELPA::ELPA base genelpa psi device SOURCES test_diago_lcao.cpp ../diago_elpa.cpp diff --git a/source/source_hsolver/unittests/test_diago_lcao.cpp b/source/source_hsolver/unittests/test_diago_lcao.cpp index 956b3752464..7b7305d6eb1 100644 --- a/source/source_hsolver/unittests/test_diago_lcao.cpp +++ b/source/source_hsolver/unittests/test_diago_lcao.cpp @@ -6,11 +6,17 @@ #include "string.h" #include "gtest/gtest.h" +#include #include #ifdef __ELPA #include "source_hsolver/diago_elpa.h" #include "source_hsolver/diago_elpa_native.h" #include "source_hsolver/module_genelpa/elpa_solver.h" +#ifdef __ROCM +#include "source_base/module_device/device.h" + +#include +#endif #endif #include "source_base/module_external/scalapack_connector.h" @@ -158,7 +164,7 @@ template class DiagoPrepare } #ifdef __ELPA - void diago_native_with_poisoned_lower() + void diago_native_with_poisoned_lower(const bool use_gpu) { this->pb2d(); this->distribute_data(); @@ -191,7 +197,7 @@ template class DiagoPrepare ModuleBase::MatrixBlock h_mat; ModuleBase::MatrixBlock s_mat; hmtest.matrix(h_mat, s_mat); - hsolver::DiagoElpaNative solver(nlocal, nbands, false); + hsolver::DiagoElpaNative solver(nlocal, nbands, use_gpu); solver.diag( h_mat, s_mat, @@ -382,6 +388,11 @@ template class DiagoPrepare { // EXPECT_NEAR(e_lapack[i], e_solver[i], PASSTHRESHOLD); double error = std::abs(e_lapack[i] - e_solver[i]); + if (!std::isfinite(error)) + { + out_info << "Non-finite eigenvalue difference at index " << i; + return false; + } if (error > maxerror) { maxerror = error; @@ -511,7 +522,7 @@ TEST_P(DiagoElpaNativeUpperTest, PreservesInputsAndIgnoresLowerTriangle) "H-KPoints-Si2.dat", "S-KPoints-Si2.dat"); ASSERT_TRUE(dp.produce_HS()); - dp.diago_native_with_poisoned_lower(); + dp.diago_native_with_poisoned_lower(false); if (dp.myrank == 0) { dp.diago_lapack(); @@ -522,6 +533,56 @@ TEST_P(DiagoElpaNativeUpperTest, PreservesInputsAndIgnoresLowerTriangle) INSTANTIATE_TEST_SUITE_P(BlockSizes, DiagoElpaNativeUpperTest, ::testing::Values(1, 2, 3)); +#ifdef __ROCM +class DiagoElpaNativeAmdGpuTest : public ::testing::Test +{ + protected: + void SetUp() override + { + if (std::getenv("ABACUS_TEST_ELPA_AMD_GPU") == nullptr) + { + GTEST_SKIP() << "Set ABACUS_TEST_ELPA_AMD_GPU on a node with AMD GPUs and an AMD GPU ELPA build."; + } + base_device::DeviceContext::instance().init(); + } +}; + +TEST_F(DiagoElpaNativeAmdGpuTest, MatchesLapack) +{ + DiagoPrepare> dp(0, 0, 2, 0, "genelpa", "H-KPoints-Si2.dat", "S-KPoints-Si2.dat"); + ASSERT_TRUE(dp.produce_HS()); + dp.diago_native_with_poisoned_lower(true); + if (dp.myrank == 0) + { + dp.diago_lapack(); + std::stringstream out_info; + EXPECT_TRUE(dp.compare_eigen(out_info)) << out_info.str(); + } +} + +TEST_F(DiagoElpaNativeAmdGpuTest, RealMatchesLapack) +{ + DiagoPrepare dp(0, 0, 2, 0, "genelpa", "H-GammaOnly-Si2.dat", "S-GammaOnly-Si2.dat"); + ASSERT_TRUE(dp.produce_HS()); + dp.pb2d(); + dp.distribute_data(); + dp.hmtest.h_local = dp.h_local; + dp.hmtest.s_local = dp.s_local; + ModuleBase::MatrixBlock h_mat; + ModuleBase::MatrixBlock s_mat; + dp.hmtest.matrix(h_mat, s_mat); + int overlap_factor = 0; + hsolver::DiagoElpaNative solver(dp.nlocal, dp.nbands, true, overlap_factor); + solver.diag(h_mat, s_mat, dp.psi, dp.e_solver.data()); + if (dp.myrank == 0) + { + dp.diago_lapack(); + std::stringstream out_info; + EXPECT_TRUE(dp.compare_eigen(out_info)) << out_info.str(); + } +} +#endif + TEST(DiagoElpaComplexTest, UsesAuthoritativeUpperTriangle) { std::stringstream out_info; diff --git a/source/source_io/module_parameter/read_inp_estruc.cpp b/source/source_io/module_parameter/read_inp_estruc.cpp index ce58dd9caab..ef764a96004 100644 --- a/source/source_io/module_parameter/read_inp_estruc.cpp +++ b/source/source_io/module_parameter/read_inp_estruc.cpp @@ -61,7 +61,7 @@ For numerical atomic orbitals basis, * scalapack_gvx: Use Scalapack to diagonalize the Hamiltonian. * cusolver: Use CUSOLVER to diagonalize the Hamiltonian, at least one GPU is needed. * cusolvermp: Use CUSOLVER to diagonalize the Hamiltonian, supporting multi-GPU devices. Note that you should set the number of MPI processes equal to the number of GPUs. -* elpa: The ELPA solver supports both CPU and GPU. By setting the `device` to GPU, you can launch the ELPA solver with GPU acceleration (provided that you have installed a GPU-supported version of ELPA, which requires you to manually compile and install ELPA, and the ABACUS should be compiled with -DENABLE_ELPA=ON and -DUSE_CUDA=ON). The ELPA solver also supports multi-GPU acceleration. +* elpa: Use native ELPA for CPU or distributed GPU diagonalization. For `device gpu`, build ABACUS with `-DENABLE_ELPA=ON` and either `-DUSE_CUDA=ON` or `-DUSE_ROCM=ON`, and link an ELPA installation built for the matching GPU backend. If you set ks_solver=`genelpa` for basis_type=`pw`, the program will stop with an error message: diff --git a/source/source_pw/module_pwdft/kernels/rocm/exx_pw_op.hip.cu b/source/source_pw/module_pwdft/kernels/rocm/exx_pw_op.hip.cu new file mode 100644 index 00000000000..fae2f6a65d6 --- /dev/null +++ b/source/source_pw/module_pwdft/kernels/rocm/exx_pw_op.hip.cu @@ -0,0 +1,166 @@ +#include "source_pw/module_pwdft/kernels/cal_density_real_op.h" +#include "source_pw/module_pwdft/kernels/exx_cal_energy_op.h" +#include "source_pw/module_pwdft/kernels/mul_potential_op.h" +#include "source_pw/module_pwdft/kernels/vec_mul_cx_op.h" + +#include "source_base/module_device/device.h" + +#include +#include +#include +#include + +namespace hamilt +{ +namespace +{ +constexpr int threads_per_block = 256; + +template +__global__ void cal_density_real_kernel(const thrust::complex* psi1, + const thrust::complex* psi2, + thrust::complex* out, + const Real omega, + const int size) +{ + const int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i < size) + { + out[i] = psi1[i] * thrust::conj(psi2[i]) / omega; + } +} + +template +__global__ void mul_potential_kernel(const Real* pot, thrust::complex* density, const int size) +{ + const int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i < size) + { + density[i] *= pot[i]; + } +} + +template +__global__ void vec_mul_vec_complex_kernel(const thrust::complex* a, + const thrust::complex* b, + thrust::complex* out, + const int size) +{ + const int i = blockIdx.x * blockDim.x + threadIdx.x; + if (i < size) + { + out[i] = a[i] * b[i]; + } +} + +template +__global__ void exx_energy_kernel(const thrust::complex* density, + const Real* pot, + Real* result, + const int size) +{ + using BlockReduce = hipcub::BlockReduce; + __shared__ typename BlockReduce::TempStorage storage; + + const int i = blockIdx.x * blockDim.x + threadIdx.x; + const Real contribution = i < size ? thrust::norm(density[i]) * pot[i] : Real(0); + const Real sum = BlockReduce(storage).Sum(contribution); + if (threadIdx.x == 0) + { + atomicAdd(result, sum); + } +} +} // namespace + +template +struct cal_density_real_op, base_device::DEVICE_GPU> +{ + using Complex = std::complex; + + void operator()(const Complex* psi1, const Complex* psi2, Complex* out, double omega, int size) + { + if (size == 0) + { + return; + } + const int blocks = (size + threads_per_block - 1) / threads_per_block; + cal_density_real_kernel<<>>( + reinterpret_cast*>(psi1), + reinterpret_cast*>(psi2), + reinterpret_cast*>(out), + static_cast(omega), + size); + hipErrcheck(hipGetLastError()); + } +}; + +template +struct mul_potential_op, base_device::DEVICE_GPU> +{ + void operator()(const Real* pot, std::complex* density, int size, int, int, int) + { + if (size == 0) + { + return; + } + // The GPU caller passes the potential slice for this k-point pair. + const int blocks = (size + threads_per_block - 1) / threads_per_block; + mul_potential_kernel<<>>(pot, reinterpret_cast*>(density), size); + hipErrcheck(hipGetLastError()); + } +}; + +template +struct vec_mul_vec_complex_op, base_device::DEVICE_GPU> +{ + using Complex = std::complex; + + void operator()(const Complex* a, const Complex* b, Complex* out, int size) + { + if (size == 0) + { + return; + } + const int blocks = (size + threads_per_block - 1) / threads_per_block; + vec_mul_vec_complex_kernel<<>>( + reinterpret_cast*>(a), + reinterpret_cast*>(b), + reinterpret_cast*>(out), + size); + hipErrcheck(hipGetLastError()); + } +}; + +template +struct exx_cal_energy_op, base_device::DEVICE_GPU> +{ + Real operator()(const std::complex* density, const Real* pot, Real scale, int size) + { + if (size == 0) + { + return Real(0); + } + Real* device_result = nullptr; + hipErrcheck(hipMalloc(&device_result, sizeof(Real))); + hipErrcheck(hipMemset(device_result, 0, sizeof(Real))); + const int blocks = (size + threads_per_block - 1) / threads_per_block; + exx_energy_kernel<<>>( + reinterpret_cast*>(density), pot, device_result, size); + hipErrcheck(hipGetLastError()); + + Real result; + hipErrcheck(hipMemcpy(&result, device_result, sizeof(Real), hipMemcpyDeviceToHost)); + hipErrcheck(hipFree(device_result)); + return scale * result; + } +}; + +template struct cal_density_real_op, base_device::DEVICE_GPU>; +template struct cal_density_real_op, base_device::DEVICE_GPU>; +template struct mul_potential_op, base_device::DEVICE_GPU>; +template struct mul_potential_op, base_device::DEVICE_GPU>; +template struct vec_mul_vec_complex_op, base_device::DEVICE_GPU>; +template struct vec_mul_vec_complex_op, base_device::DEVICE_GPU>; +template struct exx_cal_energy_op, base_device::DEVICE_GPU>; +template struct exx_cal_energy_op, base_device::DEVICE_GPU>; +} // namespace hamilt diff --git a/source/source_pw/module_pwdft/kernels/rocm/force_op.hip.cu b/source/source_pw/module_pwdft/kernels/rocm/force_op.hip.cu index c2eb572a384..32f0ef73214 100644 --- a/source/source_pw/module_pwdft/kernels/rocm/force_op.hip.cu +++ b/source/source_pw/module_pwdft/kernels/rocm/force_op.hip.cu @@ -1,9 +1,12 @@ #include "source_pw/module_pwdft/kernels/force_op.h" +#include "source_base/constants.h" + #include #include #include +#include #include #define THREADS_PER_BLOCK 256 @@ -641,6 +644,183 @@ void revertVkbValues( hipCheckOnDebug(); } +template +__device__ void store_force(const int iat, + const FPTYPE scale, + const FPTYPE force_x, + const FPTYPE force_y, + const FPTYPE force_z, + FPTYPE* force) +{ + using BlockReduce = hipcub::BlockReduce; + __shared__ typename BlockReduce::TempStorage storage_x; + __shared__ typename BlockReduce::TempStorage storage_y; + __shared__ typename BlockReduce::TempStorage storage_z; + + const FPTYPE sum_x = BlockReduce(storage_x).Sum(force_x); + const FPTYPE sum_y = BlockReduce(storage_y).Sum(force_y); + const FPTYPE sum_z = BlockReduce(storage_z).Sum(force_z); + if (threadIdx.x == 0) + { + force[iat * 3] = scale * sum_x; + force[iat * 3 + 1] = scale * sum_y; + force[iat * 3 + 2] = scale * sum_z; + } +} + +template +__global__ void force_loc_kernel( + const int npw, + const FPTYPE tpiba_omega, + const int *iat2it, + const int *ig2gg_d, + const FPTYPE *gcar_d, + const FPTYPE *tau_d, + const thrust::complex *aux_d, + const FPTYPE *vloc_d, + const int vloc_nc, + FPTYPE *forcelc_d) +{ + const int iat = blockIdx.x; + const int tid = threadIdx.x; + const int it = iat2it[iat]; // get the type of atom + + // Initialize force components + FPTYPE force_x = 0.0; + FPTYPE force_y = 0.0; + FPTYPE force_z = 0.0; + + const FPTYPE tau_x = tau_d[iat * 3 + 0]; + const FPTYPE tau_y = tau_d[iat * 3 + 1]; + const FPTYPE tau_z = tau_d[iat * 3 + 2]; + + // Process all plane waves in chunks of blockDim.x + for (int ig = tid; ig < npw; ig += blockDim.x) + { + const FPTYPE gcar_x = gcar_d[ig * 3 + 0]; + const FPTYPE gcar_y = gcar_d[ig * 3 + 1]; + const FPTYPE gcar_z = gcar_d[ig * 3 + 2]; + + // Calculate phase factor + const FPTYPE phase = ModuleBase::TWO_PI * (gcar_x * tau_x + gcar_y * tau_y + gcar_z * tau_z); + const FPTYPE sinp = std::sin(phase); + const FPTYPE cosp = std::cos(phase); + + // Get vloc value + const FPTYPE vloc_val = vloc_d[it * vloc_nc + ig2gg_d[ig]]; + + // Calculate factor + const thrust::complex aux_val = aux_d[ig]; + const FPTYPE factor = vloc_val * (cosp * aux_val.imag() + sinp * aux_val.real()); + + // Multiply by gcar components + force_x += gcar_x * factor; + force_y += gcar_y * factor; + force_z += gcar_z * factor; + } + + store_force(iat, tpiba_omega, force_x, force_y, force_z, forcelc_d); +} + +template +__global__ void force_ew_kernel( + const int npw, + const int ig_gge0, + const int *iat2it, + const FPTYPE *gcar_d, + const FPTYPE *tau_d, + const FPTYPE *it_fact_d, + const thrust::complex *aux_d, + FPTYPE *forceion_d) +{ + const int iat = blockIdx.x; + const int tid = threadIdx.x; + const int it = iat2it[iat]; // get the type of atom + const FPTYPE it_fact_val = it_fact_d[it]; // Get it_fact value + + // Initialize force components + FPTYPE force_x = 0.0; + FPTYPE force_y = 0.0; + FPTYPE force_z = 0.0; + + const FPTYPE tau_x = tau_d[iat * 3 + 0]; + const FPTYPE tau_y = tau_d[iat * 3 + 1]; + const FPTYPE tau_z = tau_d[iat * 3 + 2]; + + for (int ig = tid; ig < npw; ig += blockDim.x) + { + if (ig == ig_gge0) + { + continue; + } + const FPTYPE gcar_x = gcar_d[ig * 3 + 0]; + const FPTYPE gcar_y = gcar_d[ig * 3 + 1]; + const FPTYPE gcar_z = gcar_d[ig * 3 + 2]; + + // Calculate phase factor + const FPTYPE phase = ModuleBase::TWO_PI * (gcar_x * tau_x + gcar_y * tau_y + gcar_z * tau_z); + const FPTYPE sinp = std::sin(phase); + const FPTYPE cosp = std::cos(phase); + + // Calculate force contribution + const FPTYPE sumnb = -cosp * aux_d[ig].imag() + sinp * aux_d[ig].real(); + + // Multiply by gcar components + force_x += gcar_x * sumnb; + force_y += gcar_y * sumnb; + force_z += gcar_z * sumnb; + } + + store_force(iat, it_fact_val, force_x, force_y, force_z, forceion_d); +} + +template +void cal_force_loc_op::operator()( + const int nat, + const int npw, + const FPTYPE tpiba_omega, + const int *iat2it, + const int *ig2igg, + const FPTYPE *gcar, + const FPTYPE *tau, + const std::complex *aux, + const FPTYPE *vloc, + const int vloc_nc, + FPTYPE *forcelc) +{ + if (nat == 0) + { + return; + } + force_loc_kernel<<>>( + npw, tpiba_omega, iat2it, ig2igg, gcar, tau, reinterpret_cast *>(aux), vloc, + vloc_nc, + forcelc); + hipCheckOnDebug(); +} + +template +void cal_force_ew_op::operator()( + const int nat, + const int npw, + const int ig_gge0, + const int *iat2it, + const FPTYPE *gcar, + const FPTYPE *tau, + const FPTYPE *it_fact, + const std::complex *aux, + FPTYPE *forceion) +{ + if (nat == 0) + { + return; + } + force_ew_kernel<<>>( + npw, ig_gge0, iat2it, gcar, tau, it_fact, reinterpret_cast *>(aux), + forceion); + hipCheckOnDebug(); +} + // for revertVkbValues functions instantiation template void revertVkbValues( const int *gcar_zero_ptrs, @@ -688,4 +868,9 @@ template struct cal_force_nl_op; template struct cal_vkb1_nl_op; template struct cal_force_nl_op; +template struct cal_force_loc_op; +template struct cal_force_loc_op; +template struct cal_force_ew_op; +template struct cal_force_ew_op; + } // namespace hamilt diff --git a/source/source_pw/module_pwdft/kernels/rocm/stress_op.hip.cu b/source/source_pw/module_pwdft/kernels/rocm/stress_op.hip.cu index d9482b8e4f5..d7408da1196 100644 --- a/source/source_pw/module_pwdft/kernels/rocm/stress_op.hip.cu +++ b/source/source_pw/module_pwdft/kernels/rocm/stress_op.hip.cu @@ -699,52 +699,55 @@ __global__ void cal_stress_drhoc_aux3( template __global__ void cal_force_npw( const thrust::complex *psiv, - const FPTYPE *gv_x, - const FPTYPE *gv_y, - const FPTYPE *gv_z, + const FPTYPE *gv, const FPTYPE *rhocgigg_vec, FPTYPE *force, - const FPTYPE pos_x, - const FPTYPE pos_y, - const FPTYPE pos_z, + const FPTYPE *tau, const int npw, const FPTYPE omega, const FPTYPE tpiba) { const double TWO_PI = 2.0 * 3.14159265358979323846; - int tid = blockIdx.x * blockDim.x + threadIdx.x; - int begin_idx = tid * 1024; - if (begin_idx > npw) - { - return; - } + const int ia = blockIdx.x; + const FPTYPE pos_x = tau[ia * 3]; + const FPTYPE pos_y = tau[ia * 3 + 1]; + const FPTYPE pos_z = tau[ia * 3 + 2]; FPTYPE t_force0 = 0; FPTYPE t_force1 = 0; FPTYPE t_force2 = 0; - for (int ig = begin_idx; ig < begin_idx + 1024 && ig < npw; ig++) + for (int ig = threadIdx.x; ig < npw; ig += blockDim.x) { const thrust::complex psiv_conj = conj(psiv[ig]); - const FPTYPE arg = TWO_PI * (gv_x[ig] * pos_x + gv_y[ig] * pos_y + gv_z[ig] * pos_z); + const FPTYPE gx = gv[ig * 3]; + const FPTYPE gy = gv[ig * 3 + 1]; + const FPTYPE gz = gv[ig * 3 + 2]; + const FPTYPE arg = TWO_PI * (gx * pos_x + gy * pos_y + gz * pos_z); const FPTYPE sinp = sin(arg); const FPTYPE cosp = cos(arg); const thrust::complex expiarg = thrust::complex(sinp, cosp); const thrust::complex tmp_var = psiv_conj * expiarg * tpiba * omega * rhocgigg_vec[ig]; - const thrust::complex ipol0 = tmp_var * gv_x[ig]; + const thrust::complex ipol0 = tmp_var * gx; t_force0 += ipol0.real(); - const thrust::complex ipol1 = tmp_var * gv_y[ig]; + const thrust::complex ipol1 = tmp_var * gy; t_force1 += ipol1.real(); - const thrust::complex ipol2 = tmp_var * gv_z[ig]; + const thrust::complex ipol2 = tmp_var * gz; t_force2 += ipol2.real(); } - atomicAdd(&force[0], t_force0); - atomicAdd(&force[1], t_force1); - atomicAdd(&force[2], t_force2); + warp_reduce(t_force0); + warp_reduce(t_force1); + warp_reduce(t_force2); + if (threadIdx.x % WARP_SIZE == 0) + { + atomicAdd(&force[ia * 3], t_force0); + atomicAdd(&force[ia * 3 + 1], t_force1); + atomicAdd(&force[ia * 3 + 2], t_force2); + } } template @@ -1022,7 +1025,6 @@ __global__ void cal_stress_onsite( sum += nprojs; pot_onsite += npol * npol * tlp1_2; } // ia - __syncwarp(); warp_reduce(stress_var); if (threadIdx.x % WARP_SIZE == 0) { @@ -1094,7 +1096,6 @@ __global__ void cal_stress_onsite( ++iat; sum += nprojs; } // ia - __syncwarp(); warp_reduce(stress_var); if (threadIdx.x % WARP_SIZE == 0) { @@ -1200,4 +1201,4 @@ template struct cal_force_npw_op; template struct cal_multi_dot_op; template struct cal_multi_dot_op; -} // namespace hamilt \ No newline at end of file +} // namespace hamilt diff --git a/source/source_pw/module_rt/kernels/rocm/ion_deriv_op.hip.cu b/source/source_pw/module_rt/kernels/rocm/ion_deriv_op.hip.cu index 60400595c20..473cb5d8be4 100644 --- a/source/source_pw/module_rt/kernels/rocm/ion_deriv_op.hip.cu +++ b/source/source_pw/module_rt/kernels/rocm/ion_deriv_op.hip.cu @@ -1,7 +1,7 @@ #include "source_pw/module_rt/kernels/ion_deriv_op.h" #include #include -#include +#include #include namespace pw diff --git a/source/source_pw/module_rt/kernels/rocm/ion_pi_op.hip.cu b/source/source_pw/module_rt/kernels/rocm/ion_pi_op.hip.cu index 0999303cf49..a40bba8563c 100644 --- a/source/source_pw/module_rt/kernels/rocm/ion_pi_op.hip.cu +++ b/source/source_pw/module_rt/kernels/rocm/ion_pi_op.hip.cu @@ -2,7 +2,7 @@ #include #include -#include +#include #include #include