Skip to content

Commit 8a6df3b

Browse files
authored
Feature: Enable noncollinear magnetism and SOC in PW RT-TDDFT with the ETRS propagator and adaptive time stepping (#8223)
* Feature: Support nspin=4 PW RT-TDDFT with SOC, magnetization, spin-current, and torque outputs * Test: Add unit tests and SOC integration tests for nspin=4 PW RT-TDDFT * Continue when `scf_nmax` is reached with consistent endpoint states * Feature: Add automatic half-step retries for unconverged PW RT-TDDFT propagation * Feature: Add independent td_scf_nmax limits for PW and LCAO RT-TDDFT * Change default gga_grad in RT-TDDFT to 2 * Refactor: Move PW RT-TDDFT code and tests into dedicated modules * Feature: Add PW RT-TDDFT ETRS propagation with Lanczos and Taylor exponentials * Refactor RT-TDDFT parameter checks to use the InputDomain API * Perf: Accelerate PW RT-TDDFT ETRS Lanczos propagation * Remove redundant workspaces, projection overload, and unused CN settings from ETRS tests * Refactor test fixtures to isolate parameter state and remove unnecessary global dependencies * Decouple density restart output from convergence flags and improve TDDFT workflow readability * Refactor PW RT-TDDFT solver by extracting state handling, diagnostics, and output control with consistent naming * Separate PW-specific Eij output from shared formatting to fix LCAO test linking * Refactor PW RT-TDDFT interfaces to replace redundant parameter bundles with explicit dependencies and clarify naming * Fix PW RT-TDDFT workspace initialization for single-band calculations and empty plane-wave partitions * Move exponential workspace into Lanczos and remove unused TD history include * Set a per-case current tolerance of 5e-10 for the PW ETRS retry regression test * Improve PW regression accuracy and tolerances, refresh references and timings, and replace CUDA triangular inversion with cuBLAS TRSM * Resolve CI/CD failure by setting a per-case current tolerance for `31_PW_retry_TDDFT` * Retrigger CI check * Strengthen CUDA TRTRI and magnetic PW RT-TDDFT integration test coverage * Reduce Charge header dependencies and name intermediate values * Retrigger CI checks * Retrigger CI checks * Revert CUDA trtri unit test * Restore direct Charge header includes to follow ABACUS coding guidelines * Replace auto with explicit types * Standardize unit test directories and filenames * Use ABACUS math wrappers and share the cuBLAS handle across translation units * Organize `HSolver` numerical algorithms under `module_linalg` and move PW propagation helpers to `module_rt` * Trigger GitHub CI
1 parent e5ad530 commit 8a6df3b

408 files changed

Lines changed: 9751 additions & 3527 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎source/CMakeLists.txt‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@ list(
641641
source_basis/module_pw/kernels/pw_op.cpp
642642
source_hsolver/kernels/hegvd_op.cpp
643643
source_hsolver/kernels/bpcg_kernel_op.cpp
644-
source_hsolver/kernels/linear_op.cpp
644+
source_hsolver/module_linalg/kernels/linear_op.cpp
645645
source_estate/kernels/elecstate_op.cpp
646646
source_base/module_device/device.cpp
647647
source_base/module_device/device_helpers.cpp
@@ -675,7 +675,7 @@ if(USE_CUDA)
675675
source_basis/module_pw/kernels/cuda/pw_op.cu
676676
source_hsolver/kernels/cuda/hegvd_op.cu
677677
source_hsolver/kernels/cuda/bpcg_kernel_op.cu
678-
source_hsolver/kernels/cuda/linear_op.cu
678+
source_hsolver/module_linalg/kernels/cuda/linear_op.cu
679679
source_estate/kernels/cuda/elecstate_op.cu
680680
source_base/module_device/cuda/memory_op.cu
681681
source_pw/module_pwdft/kernels/cuda/force_op.cu
@@ -714,7 +714,7 @@ if(USE_ROCM)
714714
source_basis/module_pw/kernels/rocm/pw_op.hip.cu
715715
source_hsolver/kernels/rocm/hegvd_op.hip.cu
716716
source_hsolver/kernels/rocm/bpcg_kernel_op.hip.cu
717-
source_hsolver/kernels/rocm/linear_op.hip.cu
717+
source_hsolver/module_linalg/kernels/rocm/linear_op.hip.cu
718718
source_estate/kernels/rocm/elecstate_op.hip.cu
719719
source_base/module_device/rocm/memory_op.hip.cu
720720
source_pw/module_pwdft/kernels/rocm/force_op.hip.cu
@@ -757,9 +757,11 @@ target_link_libraries(
757757
driver
758758
esolver
759759
hsolver
760+
hsolver_linalg
760761
hamilt_general
761762
elecstate
762763
module_pwdft
764+
module_rt_pw
763765
module_proj
764766
nao_smoothgen
765767
module_ofdft

‎source/Makefile.Objects‎

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ VPATH=./src_global:\
3636
./source_base/module_fft:\
3737
./source_esolver:\
3838
./source_hsolver:\
39+
./source_hsolver/module_linalg:\
40+
./source_hsolver/module_linalg/kernels:\
3941
./source_hsolver/kernels:\
4042
./source_hsolver/module_genelpa:\
4143
./source_hsolver/module_pexsi:\
@@ -54,6 +56,7 @@ VPATH=./src_global:\
5456
./source_hamilt/module_xc/kernels:\
5557
./source_pw/module_proj:\
5658
./source_pw/module_pwdft:\
59+
./source_pw/module_rt:\
5760
./source_pw/module_ofdft:\
5861
./source_pw/module_stodft:\
5962
./source_pw/module_pwdft/kernels:\
@@ -380,7 +383,15 @@ OBJS_GINT=batch_biggrid.o\
380383
OBJS_HAMILT=hamilt_pw.o\
381384
pw_hybrid_nscf.o\
382385
pw_hybrid_source.o\
386+
td_prop_pw.o\
387+
td_hamilt_pw.o\
388+
prop_cn_pw.o\
389+
prop_etrs_pw.o\
383390
td_history_pw.o\
391+
td_conserv_pw.o\
392+
td_endpoint_pw.o\
393+
td_vel_pw.o\
394+
td_spin_pw.o\
384395
td_pw.o\
385396
hs_matrix_k.o\
386397
sto_hamilt_pw.o\
@@ -489,7 +500,12 @@ OBJS_HSOLVER=diago_cg.o\
489500
para_lin_tf.o\
490501
hsolver.o\
491502
hsolver_pw.o\
492-
hsolver_pw_tddft.o\
503+
cn_solver_pw.o\
504+
td_orbital_ops.o\
505+
orb_buffer.o\
506+
exp_action.o\
507+
exp_taylor.o\
508+
exp_lanczos.o\
493509
orthonormal.o\
494510
orthonormal_small.o\
495511
hsolver_lcaopw.o\
@@ -701,6 +717,9 @@ OBJS_IO=module_parameter/input_conv.o\
701717
module_dos/write_dos_pw.o\
702718
module_energy/write_bands.o\
703719
module_energy/td_eij_io.o\
720+
module_energy/td_eij_pw.o\
721+
module_ctrl/ctrl_output_td_pw.o\
722+
module_output/td_conserv_io.o\
704723
module_dos/cal_dos.o\
705724
module_dos/cal_pdos_gamma.o\
706725
module_dos/cal_pdos_multik.o\
@@ -741,8 +760,9 @@ OBJS_IO=module_parameter/input_conv.o\
741760
module_current/td_current_io.o\
742761
module_current/td_current_io_comm.o\
743762
module_current/td_current_pw.o\
763+
module_current/td_spin_pw_io.o\
744764
td_efield_io.o\
745-
td_vector_pot_io.o\
765+
td_vecpot_io.o\
746766
module_chgpot/write_libxc_r.o\
747767
module_chgpot/get_pchg_pw.o\
748768
module_output/output_log.o\

‎source/source_base/module_container/ATen/kernels/cuda/blas.cu‎

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
#include <ATen/kernels/blas.h>
2+
#include <ATen/kernels/lapack.h>
23
#include <base/third_party/blas.h>
34

45
#include <cuda_runtime.h>
56
#include <cublas_v2.h>
67

8+
#include <algorithm>
9+
#include <stdexcept>
10+
#include <string>
11+
#include <vector>
12+
713
namespace container {
814
namespace kernels {
915

@@ -22,6 +28,86 @@ void destroyGpuBlasHandle() {
2228
}
2329
}
2430

31+
namespace {
32+
template <typename T>
33+
__global__ void set_identity(T* matrix, const int n)
34+
{
35+
const size_t count = static_cast<size_t>(n) * n;
36+
const size_t stride = static_cast<size_t>(blockDim.x) * gridDim.x;
37+
for (size_t i = static_cast<size_t>(blockIdx.x) * blockDim.x + threadIdx.x; i < count; i += stride)
38+
{
39+
matrix[i] = i % n == i / n ? T(1) : T(0);
40+
}
41+
}
42+
43+
template <typename T>
44+
__global__ void copy_triangle(const T* inverse, T* matrix, const int n, const int lda, const bool upper)
45+
{
46+
const size_t count = static_cast<size_t>(n) * n;
47+
const size_t stride = static_cast<size_t>(blockDim.x) * gridDim.x;
48+
for (size_t i = static_cast<size_t>(blockIdx.x) * blockDim.x + threadIdx.x; i < count; i += stride)
49+
{
50+
const size_t row = i % n;
51+
const size_t column = i / n;
52+
if (upper ? row <= column : row >= column)
53+
{
54+
matrix[column * lda + row] = inverse[i];
55+
}
56+
}
57+
}
58+
} // namespace
59+
60+
template <typename T>
61+
struct lapack_trtri<T, DEVICE_GPU> {
62+
void operator()(const char& uplo, const char& diag, const int& n, T* matrix, const int& lda)
63+
{
64+
if (n < 0 || lda < std::max(1, n))
65+
{
66+
throw std::invalid_argument("trtri: invalid matrix dimensions");
67+
}
68+
const bool upper = cublas_fill_mode(uplo) == CUBLAS_FILL_MODE_UPPER;
69+
const bool unit_diag = cublas_diag_type(diag) == CUBLAS_DIAG_UNIT;
70+
if (n == 0)
71+
{
72+
return;
73+
}
74+
if (!unit_diag)
75+
{
76+
// TRSM does not report singularity; preserve TRTRI's zero-diagonal rejection.
77+
std::vector<T> diagonal(n);
78+
const size_t pitch = (static_cast<size_t>(lda) + 1) * sizeof(T);
79+
CHECK_CUDA(cudaMemcpy2D(diagonal.data(), sizeof(T), matrix, pitch, sizeof(T), n, cudaMemcpyDeviceToHost));
80+
for (int i = 0; i < n; ++i)
81+
{
82+
if (diagonal[i] == T(0))
83+
{
84+
throw std::runtime_error("trtri: zero diagonal at index " + std::to_string(i + 1));
85+
}
86+
}
87+
}
88+
89+
// Solve A * inverse = I without the Xtrtri workspace overrun seen in CUDA 11.8/12.1.
90+
Tensor inverse(DataTypeToEnum<T>::value, DeviceType::GpuDevice, {n, n});
91+
T* data = inverse.data<T>();
92+
using CudaType = typename GetTypeThrust<T>::type;
93+
CudaType* cuda_data = reinterpret_cast<CudaType*>(data);
94+
CudaType* cuda_matrix = reinterpret_cast<CudaType*>(matrix);
95+
const size_t count = static_cast<size_t>(n) * n;
96+
const size_t blocks_needed = (count + THREADS_PER_BLOCK - 1) / THREADS_PER_BLOCK;
97+
const int blocks = std::min<size_t>(65535, blocks_needed);
98+
set_identity<<<blocks, THREADS_PER_BLOCK>>>(cuda_data, n);
99+
CHECK_CUDA(cudaGetLastError());
100+
cuBlasConnector::trsm(cublas_handle, uplo, diag, n, matrix, lda, data);
101+
copy_triangle<<<blocks, THREADS_PER_BLOCK>>>(cuda_data, cuda_matrix, n, lda, upper);
102+
CHECK_CUDA_SYNC();
103+
}
104+
};
105+
106+
template struct lapack_trtri<float, DEVICE_GPU>;
107+
template struct lapack_trtri<double, DEVICE_GPU>;
108+
template struct lapack_trtri<std::complex<float>, DEVICE_GPU>;
109+
template struct lapack_trtri<std::complex<double>, DEVICE_GPU>;
110+
25111
template <typename T>
26112
struct blas_nrm2<T, DEVICE_GPU> {
27113
using Real = typename GetTypeReal<T>::type;

‎source/source_base/module_container/ATen/kernels/cuda/lapack.cu‎

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,6 @@ struct set_matrix<T, DEVICE_GPU> {
6767

6868

6969
// --- 1. Matrix Decomposition ---
70-
template <typename T>
71-
struct lapack_trtri<T, DEVICE_GPU> {
72-
void operator()(
73-
const char& uplo,
74-
const char& diag,
75-
const int& dim,
76-
T* Mat,
77-
const int& lda)
78-
{
79-
// TODO: trtri is not implemented in this method yet
80-
// Cause the trtri in cuSolver is not stable for ABACUS!
81-
cuSolverConnector::trtri(cusolver_handle, uplo, diag, dim, Mat, lda);
82-
// cuSolverConnector::potri(cusolver_handle, uplo, diag, dim, Mat, lda);
83-
}
84-
};
85-
8670
template <typename T>
8771
struct lapack_potrf<T, DEVICE_GPU> {
8872
void operator()(
@@ -343,11 +327,6 @@ template struct set_matrix<double, DEVICE_GPU>;
343327
template struct set_matrix<std::complex<float>, DEVICE_GPU>;
344328
template struct set_matrix<std::complex<double>, DEVICE_GPU>;
345329

346-
template struct lapack_trtri<float, DEVICE_GPU>;
347-
template struct lapack_trtri<double, DEVICE_GPU>;
348-
template struct lapack_trtri<std::complex<float>, DEVICE_GPU>;
349-
template struct lapack_trtri<std::complex<double>, DEVICE_GPU>;
350-
351330
template struct lapack_potrf<float, DEVICE_GPU>;
352331
template struct lapack_potrf<double, DEVICE_GPU>;
353332
template struct lapack_potrf<std::complex<float>, DEVICE_GPU>;

‎source/source_base/module_container/base/third_party/blas.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ namespace container {
141141
// Usage example: BlasConnector::functionname(parameter list).
142142
namespace BlasConnector {
143143

144+
// Column-major rank-one update; preserve the supplied vector strides and leading dimension.
145+
static inline
146+
void ger(const int m, const int n, const double alpha,
147+
const double* x, const int incx, const double* y, const int incy,
148+
double* a, const int lda)
149+
{
150+
dger_(&m, &n, &alpha, x, &incx, y, &incy, a, &lda);
151+
}
152+
144153
static inline
145154
void axpy( const int& n, const float& alpha, const float *x, const int& incx, float *y, const int& incy)
146155
{

‎source/source_base/module_container/base/third_party/cublas.h‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,50 @@
88
namespace container {
99
namespace cuBlasConnector {
1010

11+
static inline
12+
void trsm(cublasHandle_t& handle, const char uplo, const char diag, const int n,
13+
const float* matrix, const int lda, float* inverse)
14+
{
15+
const float one = 1.0f;
16+
const cublasFillMode_t triangle = cublas_fill_mode(uplo);
17+
const cublasDiagType_t diagonal = cublas_diag_type(diag);
18+
CHECK_CUBLAS(cublasStrsm(handle, CUBLAS_SIDE_LEFT, triangle, CUBLAS_OP_N, diagonal, n, n, &one, matrix, lda, inverse, n));
19+
}
20+
21+
static inline
22+
void trsm(cublasHandle_t& handle, const char uplo, const char diag, const int n,
23+
const double* matrix, const int lda, double* inverse)
24+
{
25+
const double one = 1.0;
26+
const cublasFillMode_t triangle = cublas_fill_mode(uplo);
27+
const cublasDiagType_t diagonal = cublas_diag_type(diag);
28+
CHECK_CUBLAS(cublasDtrsm(handle, CUBLAS_SIDE_LEFT, triangle, CUBLAS_OP_N, diagonal, n, n, &one, matrix, lda, inverse, n));
29+
}
30+
31+
static inline
32+
void trsm(cublasHandle_t& handle, const char uplo, const char diag, const int n,
33+
const std::complex<float>* matrix, const int lda, std::complex<float>* inverse)
34+
{
35+
const cuComplex one = make_cuComplex(1.0f, 0.0f);
36+
const cublasFillMode_t triangle = cublas_fill_mode(uplo);
37+
const cublasDiagType_t diagonal = cublas_diag_type(diag);
38+
const cuComplex* cuda_matrix = reinterpret_cast<const cuComplex*>(matrix);
39+
cuComplex* cuda_inverse = reinterpret_cast<cuComplex*>(inverse);
40+
CHECK_CUBLAS(cublasCtrsm(handle, CUBLAS_SIDE_LEFT, triangle, CUBLAS_OP_N, diagonal, n, n, &one, cuda_matrix, lda, cuda_inverse, n));
41+
}
42+
43+
static inline
44+
void trsm(cublasHandle_t& handle, const char uplo, const char diag, const int n,
45+
const std::complex<double>* matrix, const int lda, std::complex<double>* inverse)
46+
{
47+
const cuDoubleComplex one = make_cuDoubleComplex(1.0, 0.0);
48+
const cublasFillMode_t triangle = cublas_fill_mode(uplo);
49+
const cublasDiagType_t diagonal = cublas_diag_type(diag);
50+
const cuDoubleComplex* cuda_matrix = reinterpret_cast<const cuDoubleComplex*>(matrix);
51+
cuDoubleComplex* cuda_inverse = reinterpret_cast<cuDoubleComplex*>(inverse);
52+
CHECK_CUBLAS(cublasZtrsm(handle, CUBLAS_SIDE_LEFT, triangle, CUBLAS_OP_N, diagonal, n, n, &one, cuda_matrix, lda, cuda_inverse, n));
53+
}
54+
1155
static inline
1256
void copy(cublasHandle_t& handle, const int& n, const float *x, const int& incx, float *y, const int& incy)
1357
{

‎source/source_base/module_container/base/third_party/cusolver.h‎

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,6 @@ struct CudaDeleter {
2929
template <typename T>
3030
using unique_cuda_ptr = std::unique_ptr<T, CudaDeleter>;
3131

32-
struct HostDeleter {
33-
void operator()(void* ptr) const noexcept {
34-
if (ptr) free(ptr);
35-
}
36-
};
37-
38-
template <typename T>
39-
using unique_host_ptr = std::unique_ptr<T, HostDeleter>;
40-
41-
#if CUDA_VERSION >= 11000
42-
// Generic API (CUDA 11.0+)
43-
template <typename T>
44-
static inline
45-
void trtri (cusolverDnHandle_t& cusolver_handle, const char& uplo, const char& diag, const int& n, T* A, const int& lda)
46-
{
47-
size_t d_lwork = 0, h_lwork = 0;
48-
using Type = typename GetTypeThrust<T>::type;
49-
CHECK_CUSOLVER(cusolverDnXtrtri_bufferSize(cusolver_handle, cublas_fill_mode(uplo), cublas_diag_type(diag), n, GetTypeCuda<T>::cuda_data_type, reinterpret_cast<Type*>(A), lda, &d_lwork, &h_lwork));
50-
void* raw_d_work = nullptr;
51-
CHECK_CUDA(cudaMalloc((void**)&raw_d_work, d_lwork));
52-
unique_cuda_ptr<void> d_work(raw_d_work);
53-
unique_host_ptr<void> h_work;
54-
if (h_lwork) {
55-
void* raw_h_work = malloc(h_lwork);
56-
if (raw_h_work == nullptr) {
57-
throw std::bad_alloc();
58-
}
59-
h_work.reset(raw_h_work);
60-
}
61-
int h_info = 0;
62-
int* raw_d_info = nullptr;
63-
CHECK_CUDA(cudaMalloc((void**)&raw_d_info, sizeof(int)));
64-
unique_cuda_ptr<int> d_info(raw_d_info);
65-
CHECK_CUSOLVER(cusolverDnXtrtri(cusolver_handle, cublas_fill_mode(uplo), cublas_diag_type(diag), n, GetTypeCuda<T>::cuda_data_type, reinterpret_cast<Type*>(A), n, d_work.get(), d_lwork, h_work.get(), h_lwork, d_info.get()));
66-
CHECK_CUDA(cudaMemcpy(&h_info, d_info.get(), sizeof(int), cudaMemcpyDeviceToHost));
67-
if (h_info != 0) {
68-
throw std::runtime_error("trtri: failed to invert matrix");
69-
}
70-
}
71-
#else
72-
#error "CUDA version < 11.0 is not supported. cusolverDnXtrtri (CUDA 11.0+) is required."
73-
#endif
7432

7533
static inline
7634
void potri (cusolverDnHandle_t& cusolver_handle, const char& uplo, const char& diag, const int& n, float * A, const int& lda)

0 commit comments

Comments
 (0)