Skip to content

Commit 693dec0

Browse files
dzzz2001claude
andcommitted
perf(gint): optimize phi_operator kernels
- set_phi_dphi_kernel: add WantPhi non-type template parameter and dispatch from the launch site. The dphi-only callers (gint_tau) pass phi=nullptr; with WantPhi==false the compiler drops the phi[] stores and the per-iw `phi != nullptr` branch entirely. - phi_dot_dphi_kernel / phi_dot_dphi_r_kernel: replace the shared- memory tree reduce with a single-warp warpReduceSum and drop the dynamic shared-memory allocation at the launch sites. Launch configuration is pinned at blockDim.x == 32; a comment guards the invariant. - Plain `if` (not `if constexpr`) on WantPhi keeps the code C++11-compliant — ABACUS targets C++11 and nvcc otherwise emits warning deepmodeling#2912-D. WantPhi is still a non-type template parameter, so the compiler folds the constant and eliminates the dead branch. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent f09748a commit 693dec0

3 files changed

Lines changed: 171 additions & 169 deletions

File tree

source/source_lcao/module_gint/kernel/phi_operator_gpu.cu

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "phi_operator_kernel.cuh"
33
#include "dgemm_vbatch.h"
44
#include <cuda_runtime.h>
5+
#include <type_traits>
56
#include "source_base/module_device/device_check.h"
67

78
namespace ModuleGint
@@ -124,30 +125,41 @@ void PhiOperatorGpu<Real>::set_phi_dphi(double* phi_d, double* dphi_x_d, double*
124125
{
125126
dim3 grid_dim(mgrids_num_, bgrid_batch_->get_batch_size());
126127
dim3 threads_per_block(64);
127-
set_phi_dphi_kernel<<<grid_dim, threads_per_block, 0, stream_>>>(
128-
gint_gpu_vars_->nwmax,
129-
mgrids_num_,
130-
gint_gpu_vars_->nr_max,
131-
gint_gpu_vars_->dr_uniform,
132-
gint_gpu_vars_->ucell_atom_nwl_d,
133-
gint_gpu_vars_->atom_iw2_new_d,
134-
gint_gpu_vars_->atom_iw2_ylm_d,
135-
gint_gpu_vars_->atom_iw2_l_d,
136-
gint_gpu_vars_->atom_nw_d,
137-
gint_gpu_vars_->iat2it_d,
138-
gint_gpu_vars_->rcut_d,
139-
gint_gpu_vars_->psi_u_d,
140-
gint_gpu_vars_->dpsi_u_d,
141-
gint_gpu_vars_->mgrids_pos_d,
142-
atoms_iat_.get_device_ptr(),
143-
atoms_bgrids_rcoords_.get_device_ptr(),
144-
atoms_num_info_.get_device_ptr(),
145-
atom_phi_start_.get_device_ptr(),
146-
bgrid_phi_len_.get_device_ptr(),
147-
phi_d,
148-
dphi_x_d,
149-
dphi_y_d,
150-
dphi_z_d);
128+
// Dispatch the WantPhi template based on whether phi is requested.
129+
// Lets the compiler drop the phi[] stores entirely in the dphi-only case
130+
// (gint_tau) without paying a per-iw `phi != nullptr` branch in the loop.
131+
auto launch = [&](auto want_phi) {
132+
constexpr bool WantPhi = decltype(want_phi)::value;
133+
set_phi_dphi_kernel<WantPhi><<<grid_dim, threads_per_block, 0, stream_>>>(
134+
gint_gpu_vars_->nwmax,
135+
mgrids_num_,
136+
gint_gpu_vars_->nr_max,
137+
gint_gpu_vars_->dr_uniform,
138+
gint_gpu_vars_->ucell_atom_nwl_d,
139+
gint_gpu_vars_->atom_iw2_new_d,
140+
gint_gpu_vars_->atom_iw2_ylm_d,
141+
gint_gpu_vars_->atom_iw2_l_d,
142+
gint_gpu_vars_->atom_nw_d,
143+
gint_gpu_vars_->iat2it_d,
144+
gint_gpu_vars_->rcut_d,
145+
gint_gpu_vars_->psi_u_d,
146+
gint_gpu_vars_->dpsi_u_d,
147+
gint_gpu_vars_->mgrids_pos_d,
148+
atoms_iat_.get_device_ptr(),
149+
atoms_bgrids_rcoords_.get_device_ptr(),
150+
atoms_num_info_.get_device_ptr(),
151+
atom_phi_start_.get_device_ptr(),
152+
bgrid_phi_len_.get_device_ptr(),
153+
phi_d,
154+
dphi_x_d,
155+
dphi_y_d,
156+
dphi_z_d);
157+
};
158+
if (phi_d != nullptr) {
159+
launch(std::true_type{});
160+
} else {
161+
launch(std::false_type{});
162+
}
151163
CHECK_LAST_CUDA_ERROR("kernel launch");
152164
}
153165

@@ -427,8 +439,9 @@ void PhiOperatorGpu<Real>::phi_dot_dphi(
427439
{
428440
dim3 grid_dim(bgrid_batch_->get_max_atoms_per_bgrid(),
429441
bgrid_batch_->get_batch_size());
442+
// Kernel reduce is single-warp; blockDim.x MUST stay 32.
430443
dim3 threads_per_block(32);
431-
phi_dot_dphi_kernel<<<grid_dim, threads_per_block, sizeof(double) * 32 * 3, stream_>>>(
444+
phi_dot_dphi_kernel<<<grid_dim, threads_per_block, 0, stream_>>>(
432445
phi_d,
433446
dphi_x_d,
434447
dphi_y_d,
@@ -454,8 +467,9 @@ void PhiOperatorGpu<Real>::phi_dot_dphi_r(
454467
{
455468
dim3 grid_dim(mgrids_num_,
456469
bgrid_batch_->get_batch_size());
470+
// Kernel reduce is single-warp; blockDim.x MUST stay 32.
457471
dim3 threads_per_block(32);
458-
phi_dot_dphi_r_kernel<<<grid_dim, threads_per_block, sizeof(double) * 32 * 6, stream_>>>(
472+
phi_dot_dphi_r_kernel<<<grid_dim, threads_per_block, 0, stream_>>>(
459473
phi_d,
460474
dphi_x_d,
461475
dphi_y_d,

0 commit comments

Comments
 (0)