ABACUS develop
Atomic-orbital Based Ab-initio Computation at UStc
Loading...
Searching...
No Matches
phi_operator_gpu.h
Go to the documentation of this file.
1#ifndef ABACUS_SOURCE_HAMILT_MODULE_GINT_KERNEL_PHI_OPERATOR_GPU_H
2#define ABACUS_SOURCE_HAMILT_MODULE_GINT_KERNEL_PHI_OPERATOR_GPU_H
3
4#include <cstdint>
5#include <cuda_runtime.h>
6#include <memory>
7#include <vector>
8
9#include "cuda_mem_wrapper.h"
10#include "gint_gpu_vars.h"
12#include "source_hamilt/module_gint/gint_type.h" // Vec3i, used by PairInfo
13
14namespace ModuleGint
15{
16
17// Per-atom-pair metadata cached in PhiOperatorGpu::set_bgrid_batch() so that
18// phi_mul_phi / phi_mul_dm skip the O(bgrid * atoms^2) enumeration on every
19// call. Holds only the fields both callers need; HContainer lookups still
20// happen lazily in the hot path because they depend on hRGint / dm.
22{
26 int iat_1;
27 int iat_2;
29 uint16_t nw1;
30 uint16_t nw2;
31 uint8_t ia_le; // (ia_1 <= ia_2) within the bgrid, for is_symm filter
32 uint8_t is_diag; // (ia_1 == ia_2), for phi_mul_dm is_symm alpha
33};
34
35// One non-empty (nw1, nw2) bucket produced by the counting sort in
36// phi_mul_phi / phi_mul_dm: a flattened shape key, the bucket's start offset in
37// the flat gemm_* arrays, and how many atom pairs landed in it.
39{
40 int key; // nw1 * nw_stride_ + nw2
41 int off; // start offset in the gemm_* host/device arrays
42 int cnt; // number of atom pairs in this bucket
43};
44
45template <typename Real = double>
47{
48
49 public:
50 PhiOperatorGpu(std::shared_ptr<const GintGpuVars> gint_gpu_vars, cudaStream_t stream = 0);
52
53 void set_bgrid_batch(std::shared_ptr<BatchBigGrid> bgrid_batch);
54
55 void set_phi(Real *phi_d) const;
56
57 // These remain double-only (for force/stress paths)
59 double *phi_d,
60 double *dphi_x_d,
61 double *dphi_y_d,
62 double *dphi_z_d) const;
63
65 double *ddphi_xx_d,
66 double *ddphi_xy_d,
67 double *ddphi_xz_d,
68 double *ddphi_yy_d,
69 double *ddphi_yz_d,
70 double *ddphi_zz_d) const;
71
73 const Real *vl_d,
74 const Real dr3,
75 const Real *phi_d,
76 Real *result_d) const;
77
78 // The GEMM output buffers (hr in phi_mul_phi, phi_dm in phi_mul_dm) are
79 // always double, independent of Real. When Real=float the per-pair inner
80 // products are reduced in fp32 (cheap); the cross-pair accumulation into a
81 // shared hr/phi_dm element is what runs in fp64, via an atomicAdd into
82 // these double buffers, so summing many atom-pair contributions doesn't
83 // drift.
85 const Real *phi_d,
86 const Real *phi_vldr3_d,
87 HContainer<double> &hRGint,
88 double *hr_d) const;
89
91 const Real *phi_d,
92 const Real *dm_d,
93 const HContainer<Real> &dm,
94 const bool is_symm,
95 double *phi_dm_d);
96
97 // phi_j_d is the output of phi_mul_dm and therefore always double.
98 void phi_dot_phi(const Real *phi_i_d, const double *phi_j_d, double *rho_d) const;
99
100 // These remain double-only (for force/stress paths)
102 const double *phi_d,
103 const double *dphi_x_d,
104 const double *dphi_y_d,
105 const double *dphi_z_d,
106 double *fvl_d) const;
107
109 const double *phi_d,
110 const double *dphi_x_d,
111 const double *dphi_y_d,
112 const double *dphi_z_d,
113 double *svl_d) const;
114
115 private:
116 std::shared_ptr<BatchBigGrid> bgrid_batch_;
117 std::shared_ptr<const GintGpuVars> gint_gpu_vars_;
118
119 // the number of meshgrids on a biggrid
121
123
124 // Stride for flattening a (nw1, nw2) pair into a single dense bucket key
125 // (`nw1 * nw_stride_ + nw2`), so shape-exact bucketing of phi_mul_phi /
126 // phi_mul_dm can index a flat table instead of hashing. Set once in the
127 // ctor to ucell.nwmax + 1 -- nwmax is the largest per-atom orbital count in
128 // the cell, so there is no artificial ceiling: the table is sized to the
129 // actual basis (typical nwmax ~25).
130 int nw_stride_ = 0;
131
132 cudaStream_t stream_ = 0;
133 cudaEvent_t event_;
134
135 // The first number in every group of two represents the number of atoms on that bigcell.
136 // The second number represents the cumulative number of atoms up to that bigcell.
138
139 // the iat of each atom
141
142 // atoms_bgrids_rcoords_ here represents the relative coordinates from the big grid to the atoms
144
145 // the start index of the phi array for each atom
147 // The length of phi for a single meshgrid on each big grid.
149 // The start index of the phi array for each big grid.
151 // Mapping of the index of meshgrid in the batch of biggrids to the index of meshgrid in the local cell
153
159 // C accumulator pointers are always double*: both phi_mul_phi (hr) and
160 // phi_mul_dm (phi_dm) write into fp64 buffers via the GEMM's fp64 atomicAdd.
163
164 // Full (ia_1, ia_2) pair enumeration, rebuilt in set_bgrid_batch().
165 // Consumed by phi_mul_phi (TN, iat_1 <= iat_2 filter) and phi_mul_dm
166 // (NN, optional is_symm upper-triangle filter).
167 std::vector<PairInfo> pair_cache_;
168
169 // Scratch buffer reused across phi_mul_phi / phi_mul_dm calls to cache
170 // per-pair HContainer offsets from Pass 1 and replay them in Pass 2
171 // without a second find_matrix_offset() call.
172 mutable std::vector<int> pair_scratch_offset_;
173
174 // Dense (nw_stride_ * nw_stride_) counting-sort scratch shared by
175 // phi_mul_phi / phi_mul_dm. Sized once in the ctor and just re-zeroed per
176 // call, so the hot path never reallocates.
177 mutable std::vector<int> bucket_counts_;
178 mutable std::vector<int> bucket_base_;
179 mutable std::vector<int> bucket_cursor_;
180
181 // Compact list of non-empty buckets for the current call. Reused (cleared,
182 // not reallocated) by both phi_mul_phi and phi_mul_dm.
183 mutable std::vector<GemmShapeBucket> buckets_;
184};
185
186} // namespace ModuleGint
187
188#endif // ABACUS_SOURCE_HAMILT_MODULE_GINT_KERNEL_PHI_OPERATOR_GPU_H
Definition cuda_mem_wrapper.h:10
std::shared_ptr< BatchBigGrid > bgrid_batch_
Definition phi_operator_gpu.h:116
CudaMemWrapper< int > gemm_ldb_
Definition phi_operator_gpu.h:155
CudaMemWrapper< int > atoms_iat_
Definition phi_operator_gpu.h:140
CudaMemWrapper< const Real * > gemm_A_
Definition phi_operator_gpu.h:157
PhiOperatorGpu(std::shared_ptr< const GintGpuVars > gint_gpu_vars, cudaStream_t stream=0)
int nw_stride_
Definition phi_operator_gpu.h:130
std::vector< int > bucket_base_
Definition phi_operator_gpu.h:178
void set_phi(Real *phi_d) const
CudaMemWrapper< int > gemm_ldc_
Definition phi_operator_gpu.h:156
std::vector< int > bucket_cursor_
Definition phi_operator_gpu.h:179
CudaMemWrapper< double * > gemm_C_
Definition phi_operator_gpu.h:161
std::shared_ptr< const GintGpuVars > gint_gpu_vars_
Definition phi_operator_gpu.h:117
void phi_dot_dphi_r(const double *phi_d, const double *dphi_x_d, const double *dphi_y_d, const double *dphi_z_d, double *svl_d) const
void phi_mul_dm(const Real *phi_d, const Real *dm_d, const HContainer< Real > &dm, const bool is_symm, double *phi_dm_d)
CudaMemWrapper< double3 > atoms_bgrids_rcoords_
Definition phi_operator_gpu.h:143
void phi_mul_vldr3(const Real *vl_d, const Real dr3, const Real *phi_d, Real *result_d) const
int phi_len_
Definition phi_operator_gpu.h:122
void set_phi_dphi(double *phi_d, double *dphi_x_d, double *dphi_y_d, double *dphi_z_d) const
std::vector< GemmShapeBucket > buckets_
Definition phi_operator_gpu.h:183
std::vector< int > pair_scratch_offset_
Definition phi_operator_gpu.h:172
CudaMemWrapper< Real > gemm_alpha_
Definition phi_operator_gpu.h:162
void phi_mul_phi(const Real *phi_d, const Real *phi_vldr3_d, HContainer< double > &hRGint, double *hr_d) const
cudaStream_t stream_
Definition phi_operator_gpu.h:132
cudaEvent_t event_
Definition phi_operator_gpu.h:133
CudaMemWrapper< int > batch_mgrid_lidx_
Definition phi_operator_gpu.h:152
int mgrids_num_
Definition phi_operator_gpu.h:120
void phi_dot_dphi(const double *phi_d, const double *dphi_x_d, const double *dphi_y_d, const double *dphi_z_d, double *fvl_d) const
CudaMemWrapper< int > gemm_lda_
Definition phi_operator_gpu.h:154
std::vector< int > bucket_counts_
Definition phi_operator_gpu.h:177
CudaMemWrapper< int > bgrid_phi_len_
Definition phi_operator_gpu.h:148
CudaMemWrapper< const Real * > gemm_B_
Definition phi_operator_gpu.h:158
CudaMemWrapper< int2 > atoms_num_info_
Definition phi_operator_gpu.h:137
CudaMemWrapper< int > atom_phi_start_
Definition phi_operator_gpu.h:146
void phi_dot_phi(const Real *phi_i_d, const double *phi_j_d, double *rho_d) const
CudaMemWrapper< int > bgrid_phi_start_
Definition phi_operator_gpu.h:150
void set_bgrid_batch(std::shared_ptr< BatchBigGrid > bgrid_batch)
void set_ddphi(double *ddphi_xx_d, double *ddphi_xy_d, double *ddphi_xz_d, double *ddphi_yy_d, double *ddphi_yz_d, double *ddphi_zz_d) const
std::vector< PairInfo > pair_cache_
Definition phi_operator_gpu.h:167
Definition batch_biggrid.cpp:4
hamilt::HContainer< T > HContainer
Definition gint_type.h:15
ModuleBase::Vector3< int > Vec3i
Definition gint_type.h:12
Definition phi_operator_gpu.h:39
int off
Definition phi_operator_gpu.h:41
int cnt
Definition phi_operator_gpu.h:42
int key
Definition phi_operator_gpu.h:40
Definition phi_operator_gpu.h:22
uint16_t nw1
Definition phi_operator_gpu.h:29
int phi_len_mgrid
Definition phi_operator_gpu.h:25
uint8_t is_diag
Definition phi_operator_gpu.h:32
int iat_2
Definition phi_operator_gpu.h:27
int phi_1_offset
Definition phi_operator_gpu.h:23
uint8_t ia_le
Definition phi_operator_gpu.h:31
Vec3i r_diff
Definition phi_operator_gpu.h:28
int phi_2_offset
Definition phi_operator_gpu.h:24
int iat_1
Definition phi_operator_gpu.h:26
uint16_t nw2
Definition phi_operator_gpu.h:30