Skip to content

Commit c925ec6

Browse files
committed
Fix(GPU): fix compilation errors for GPU build with CUDA
- Add unique_cuda_ptr and unique_host_ptr RAII wrappers in cuda.h used by cusolver.h for automatic GPU/host memory management - Fix libxc_abacus.h declarations: add missing hybrid_alpha and hse_omega parameters to init_func, v_xc_libxc, v_xc_meta, xc_spin_libxc, gcxc_libxc, gcxc_spin_libxc, tau_xc, tau_xc_spin to match their implementations - Fix GPU stress_op.h DeltaSpin: add missing isk parameter to match implementation and caller - Fix GPU force_op.h/cu/hip.cu DeltaSpin: remove isk parameter that was absent from CPU version and not passed by caller - Fix test CMakeLists: add missing source files for linking (psi_paging.cpp, spin_constrain.cpp, xc_functional_gga_noncol_sf_builtin.cpp) - Fix spin_constrain_test: update set_input_parameters call to include new sc_acceleration_mode and sc_acceleration_rms_thr params Verified paged mode (device_memory_mode=paged) produces identical energy to full_gpu mode on a 2x2x2 k-point PW test case.
1 parent ca65a72 commit c925ec6

9 files changed

Lines changed: 72 additions & 21 deletions

File tree

source/source_base/module_container/base/macros/cuda.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,44 @@
1010

1111
#define THREADS_PER_BLOCK 256
1212

13+
template <typename T>
14+
class unique_cuda_ptr {
15+
T* ptr_;
16+
public:
17+
unique_cuda_ptr() : ptr_(nullptr) {}
18+
explicit unique_cuda_ptr(T* ptr) : ptr_(ptr) {}
19+
~unique_cuda_ptr() { if (ptr_) cudaFree(ptr_); }
20+
unique_cuda_ptr(const unique_cuda_ptr&) = delete;
21+
unique_cuda_ptr& operator=(const unique_cuda_ptr&) = delete;
22+
unique_cuda_ptr(unique_cuda_ptr&& other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; }
23+
unique_cuda_ptr& operator=(unique_cuda_ptr&& other) noexcept {
24+
if (this != &other) { if (ptr_) cudaFree(ptr_); ptr_ = other.ptr_; other.ptr_ = nullptr; }
25+
return *this;
26+
}
27+
T* get() const { return ptr_; }
28+
operator T*() const { return ptr_; }
29+
void reset(T* ptr = nullptr) { if (ptr_) cudaFree(ptr_); ptr_ = ptr; }
30+
};
31+
32+
template <typename T>
33+
class unique_host_ptr {
34+
T* ptr_;
35+
public:
36+
unique_host_ptr() : ptr_(nullptr) {}
37+
explicit unique_host_ptr(T* ptr) : ptr_(ptr) {}
38+
~unique_host_ptr() { if (ptr_) free(ptr_); }
39+
unique_host_ptr(const unique_host_ptr&) = delete;
40+
unique_host_ptr& operator=(const unique_host_ptr&) = delete;
41+
unique_host_ptr(unique_host_ptr&& other) noexcept : ptr_(other.ptr_) { other.ptr_ = nullptr; }
42+
unique_host_ptr& operator=(unique_host_ptr&& other) noexcept {
43+
if (this != &other) { if (ptr_) free(ptr_); ptr_ = other.ptr_; other.ptr_ = nullptr; }
44+
return *this;
45+
}
46+
T* get() const { return ptr_; }
47+
operator T*() const { return ptr_; }
48+
void reset(T* ptr = nullptr) { if (ptr_) free(ptr_); ptr_ = ptr; }
49+
};
50+
1351
template <typename T>
1452
struct GetTypeThrust
1553
{

source/source_estate/test/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ AddTest(
5252
../occupy.cpp
5353
../module_charge/charge_mpi.cpp
5454
../../source_psi/psi.cpp
55+
../../source_psi/psi_paging.cpp
56+
../../source_lcao/module_deltaspin/spin_constrain.cpp
57+
../../source_hamilt/module_xc/xc_functional_gga_noncol_sf_builtin.cpp
5558
../../source_base/module_device/memory_op.cpp
5659
)
5760

source/source_hamilt/module_xc/libxc_abacus.h

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ namespace XC_Functional_Libxc
4444
* followed by a list of parameters.
4545
*/
4646
extern std::vector<xc_func_type> init_func(const std::vector<int> &func_id,
47-
const int xc_polarized);
47+
const int xc_polarized,
48+
const double hybrid_alpha = 0.0,
49+
const double hse_omega = 0.0);
4850

4951
extern void finish_func(std::vector<xc_func_type> &funcs);
5052

@@ -59,15 +61,22 @@ namespace XC_Functional_Libxc
5961
const double &omega, // volume of cell
6062
const double tpiba,
6163
const Charge* const chr, // charge density
62-
const std::map<int, double>* scaling_factor = nullptr); // added by jghan, 2024-10-10
64+
const int nspin_in,
65+
const bool domag,
66+
const bool domag_z,
67+
const std::map<int, double>* scaling_factor = nullptr,
68+
const double hybrid_alpha = 0.0,
69+
const double hse_omega = 0.0);
6370

64-
// for mGGA functional
6571
extern std::tuple<double,double,ModuleBase::matrix,ModuleBase::matrix> v_xc_meta(
6672
const std::vector<int> &func_id,
6773
const int &nrxx, // number of real-space grid
6874
const double &omega, // volume of cell
6975
const double tpiba,
70-
const Charge* const chr);
76+
const Charge* const chr,
77+
const int nspin,
78+
const double hybrid_alpha = 0.0,
79+
const double hse_omega = 0.0);
7180

7281

7382
//-------------------
@@ -182,7 +191,9 @@ namespace XC_Functional_Libxc
182191
extern void xc_spin_libxc(
183192
const std::vector<int> &func_id,
184193
const double &rhoup, const double &rhodw,
185-
double &exc, double &vxcup, double &vxcdw);
194+
double &exc, double &vxcup, double &vxcdw,
195+
const double hybrid_alpha = 0.0,
196+
const double hse_omega = 0.0);
186197

187198

188199
//-------------------
@@ -193,14 +204,18 @@ namespace XC_Functional_Libxc
193204
extern void gcxc_libxc(
194205
const std::vector<int> &func_id,
195206
const double &rho, const double &grho,
196-
double &sxc, double &v1xc, double &v2xc);
207+
double &sxc, double &v1xc, double &v2xc,
208+
const double hybrid_alpha = 0.0,
209+
const double hse_omega = 0.0);
197210

198211
// the entire GGA functional, for nspin=2 case
199212
extern void gcxc_spin_libxc(
200213
const std::vector<int> &func_id,
201214
const double rhoup, const double rhodw,
202215
const ModuleBase::Vector3<double> gdr1, const ModuleBase::Vector3<double> gdr2,
203-
double &sxc, double &v1xcup, double &v1xcdw, double &v2xcup, double &v2xcdw, double &v2xcud);
216+
double &sxc, double &v1xcup, double &v1xcdw, double &v2xcup, double &v2xcdw, double &v2xcud,
217+
const double hybrid_alpha = 0.0,
218+
const double hse_omega = 0.0);
204219

205220

206221
//-------------------
@@ -211,15 +226,19 @@ namespace XC_Functional_Libxc
211226
extern void tau_xc(
212227
const std::vector<int> &func_id,
213228
const double &rho, const double &grho, const double &atau, double &sxc,
214-
double &v1xc, double &v2xc, double &v3xc);
229+
double &v1xc, double &v2xc, double &v3xc,
230+
const double& hybrid_alpha = 0.0,
231+
const double& hse_omega = 0.0);
215232

216233
extern void tau_xc_spin(
217234
const std::vector<int> &func_id,
218235
double rhoup, double rhodw,
219236
ModuleBase::Vector3<double> gdr1, ModuleBase::Vector3<double> gdr2,
220237
double tauup, double taudw,
221238
double &sxc, double &v1xcup, double &v1xcdw, double &v2xcup, double &v2xcdw, double &v2xcud,
222-
double &v3xcup, double &v3xcdw);
239+
double &v3xcup, double &v3xcdw,
240+
const double& hybrid_alpha = 0.0,
241+
const double& hse_omega = 0.0);
223242

224243
} // namespace XC_Functional_Libxc
225244

source/source_hamilt/module_xc/test/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ AddTest(
7171
../xc_gga_corr.cpp ../xc_lda_corr.cpp ../xc_gga_exch.cpp
7272
../xc_lda_exch.cpp ../xc_hcth.cpp
7373
../xc_pot.cpp
74+
../xc_functional_gga_noncol_sf_builtin.cpp
7475
../libxc_pot.cpp
7576
../libxc_tools.cpp
7677
../../../source_base/module_external/blas_connector_base.cpp ../../../source_base/module_external/blas_connector_vector.cpp ../../../source_base/module_external/blas_connector_matrix.cpp

source/source_lcao/module_deltaspin/test/spin_constrain_test.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ TYPED_TEST(SpinConstrainTest, SetInputParameters)
134134
double alpha_trial = 0.01;
135135
double sccut = 3.0;
136136
double sc_drop_thr = 1e-3;
137-
this->sc.set_input_parameters(sc_thr, nsc, nsc_min, alpha_trial, sccut, sc_drop_thr);
137+
this->sc.set_input_parameters(sc_thr, nsc, nsc_min, alpha_trial, sccut, sc_drop_thr, "none", 0.0);
138138
EXPECT_DOUBLE_EQ(this->sc.get_sc_thr(), sc_thr);
139139
EXPECT_EQ(this->sc.get_nsc(), nsc);
140140
EXPECT_EQ(this->sc.get_nsc_min(), nsc_min);

source/source_pw/module_pwdft/kernels/cuda/force_op.cu

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,16 +545,11 @@ void cal_force_nl_op<FPTYPE, base_device::DEVICE_GPU>::operator()(const base_dev
545545
const FPTYPE& tpiba,
546546
const FPTYPE* d_wg,
547547
const FPTYPE* lambda,
548-
const int* isk,
549548
const std::complex<FPTYPE>* becp,
550549
const std::complex<FPTYPE>* dbecp,
551550
FPTYPE* force)
552551
{
553552
int spin_sign = 1;
554-
if (isk != nullptr && npol == 1)
555-
{
556-
spin_sign = (isk[ik] == 0) ? 1 : -1;
557-
}
558553
if (npol == 1)
559554
{
560555
cal_force_onsite<FPTYPE, 1>

source/source_pw/module_pwdft/kernels/force_op.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,6 @@ struct cal_force_nl_op<FPTYPE, base_device::DEVICE_GPU>
277277
const FPTYPE& tpiba,
278278
const FPTYPE* d_wg,
279279
const FPTYPE* lambda,
280-
const int* isk,
281280
const std::complex<FPTYPE>* becp,
282281
const std::complex<FPTYPE>* dbecp,
283282
FPTYPE* force);

source/source_pw/module_pwdft/kernels/rocm/force_op.hip.cu

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -539,16 +539,11 @@ void cal_force_nl_op<FPTYPE, base_device::DEVICE_GPU>::operator()(const base_dev
539539
const FPTYPE& tpiba,
540540
const FPTYPE* d_wg,
541541
const FPTYPE* lambda,
542-
const int* isk,
543542
const std::complex<FPTYPE>* becp,
544543
const std::complex<FPTYPE>* dbecp,
545544
FPTYPE* force)
546545
{
547546
int spin_sign = 1;
548-
if (isk != nullptr && npol == 1)
549-
{
550-
spin_sign = (isk[ik] == 0) ? 1 : -1;
551-
}
552547
if (npol == 1)
553548
{
554549
hipLaunchKernelGGL(HIP_KERNEL_NAME(cal_force_onsite<FPTYPE, 1>),

source/source_pw/module_pwdft/kernels/stress_op.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ struct cal_stress_nl_op<FPTYPE, base_device::DEVICE_GPU>
358358
const int* atom_na,
359359
const FPTYPE* d_wg,
360360
const double* lambda,
361+
const int* isk,
361362
const std::complex<FPTYPE>* becp,
362363
const std::complex<FPTYPE>* dbecp,
363364
FPTYPE* stress);

0 commit comments

Comments
 (0)