QMCPACK
DiracDeterminantBatched.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2021 QMCPACK developers.
6 //
7 // File developed by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
8 // Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Ye Luo, yeluo@anl.gov, Argonne National Laboratory
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 /**@file DiracDeterminantBatched.h
15  * @brief Declaration of DiracDeterminantBatched with a S(ingle)P(article)O(rbital)Set
16  */
17 #ifndef QMCPLUSPLUS_DIRACDETERMINANTBATCHED_H
18 #define QMCPLUSPLUS_DIRACDETERMINANTBATCHED_H
19 
21 #include "WaveFunctionTypes.hpp"
24 #if defined(ENABLE_CUDA) && defined(ENABLE_OFFLOAD)
26 #endif
28 
29 namespace qmcplusplus
30 {
31 
32 //forward declaration
33 class TWFFastDerivWrapper;
34 
35 template<PlatformKind PL, typename VT>
37 
38 template<typename VT>
40 {
42 };
43 
44 #if defined(ENABLE_CUDA) && defined(ENABLE_OFFLOAD)
45 template<typename VT>
47 {
49 };
50 #endif
51 
52 #if defined(ENABLE_SYCL) && defined(ENABLE_OFFLOAD)
53 template<typename VT>
54 struct UpdateEngineSelector<PlatformKind::SYCL, VT>
55 {
56  using Engine = DelayedUpdateBatched<PlatformKind::SYCL, VT>;
57 };
58 #endif
59 
60 template<PlatformKind UEPL, typename FPVT>
62 {
64 };
65 
66 #if defined(ENABLE_CUDA) && defined(ENABLE_OFFLOAD)
67 template<typename FPVT>
69 {
71 };
72 #endif
73 
74 template<PlatformKind PL, typename VT, typename FPVT>
76 {
77 public:
81  using Value = typename WFT::Value;
83  using PsiValue = typename WFT::PsiValue;
84  using LogValue = typename WFT::LogValue;
85  using Grad = typename WFT::Grad;
86  using Hess = typename WFT::Hess;
87  using Real = typename WFT::Real;
89 
90  // the understanding of dual memory space needs to follow UpdateEngine
91  template<typename DT>
93  template<typename DT>
95  template<typename DT>
98 
100 
101  /** constructor
102  *@param spos the single-particle orbital set
103  *@param first index of the first particle
104  *@param last index of last particle
105  *@param ndelay delayed update rank
106  */
107  DiracDeterminantBatched(std::unique_ptr<SPOSet>&& spos,
108  int first,
109  int last,
110  int ndelay = 1,
111  DetMatInvertor matrix_inverter_kind = DetMatInvertor::ACCEL);
112 
113  // copy constructor and assign operator disabled
116 
117  std::string getClassName() const override { return "DiracDeterminant"; }
118 
120  const opt_variables_type& active,
121  Vector<Value>& dlogpsi,
122  Vector<Value>& dhpsioverpsi) override;
123 
124  void evaluateDerivativesWF(ParticleSet& P, const opt_variables_type& optvars, Vector<ValueType>& dlogpsi) override;
125 
126  void registerData(ParticleSet& P, WFBufferType& buf) override;
127 
128  LogValue updateBuffer(ParticleSet& P, WFBufferType& buf, bool fromscratch = false) override;
129 
130  void copyFromBuffer(ParticleSet& P, WFBufferType& buf) override;
131 
132  /** return the ratio only for the iat-th partcle move
133  * @param P current configuration
134  * @param iat the particle thas is being moved
135  */
136  PsiValue ratio(ParticleSet& P, int iat) override;
137 
139  const RefVectorWithLeader<ParticleSet>& p_list,
140  int iat,
141  std::vector<PsiValue>& ratios) const override;
142 
143  /** compute multiple ratios for a particle move
144  */
145  void evaluateRatios(const VirtualParticleSet& VP, std::vector<Value>& ratios) override;
146 
148  const std::pair<ValueVector, ValueVector>& spinor_multiplier,
149  std::vector<Value>& ratios) override;
150 
153  std::vector<std::vector<Value>>& ratios) const override;
154 
156  const opt_variables_type& optvars,
157  std::vector<ValueType>& ratios,
158  Matrix<ValueType>& dratios) override;
159 
160  PsiValue ratioGrad(ParticleSet& P, int iat, Grad& grad_iat) override;
161 
163  const RefVectorWithLeader<ParticleSet>& p_list,
164  int iat,
165  std::vector<PsiValue>& ratios,
166  std::vector<Grad>& grad_new) const override;
167 
169  const RefVectorWithLeader<ParticleSet>& p_list,
170  int iat,
171  std::vector<PsiValue>& ratios,
172  std::vector<Grad>& grad_new,
173  std::vector<ComplexType>& spingrad_new) const override;
174 
175  PsiValue ratioGradWithSpin(ParticleSet& P, int iat, Grad& grad_iat, ComplexType& spingrad) override;
176 
177  Grad evalGrad(ParticleSet& P, int iat) override;
178 
180  const RefVectorWithLeader<ParticleSet>& p_list,
181  int iat,
182  std::vector<Grad>& grad_now) const override;
183 
184  Grad evalGradWithSpin(ParticleSet& P, int iat, ComplexType& spingrad) override;
185 
187  const RefVectorWithLeader<ParticleSet>& p_list,
188  int iat,
189  std::vector<Grad>& grad_now,
190  std::vector<ComplexType>& spingrad_now) const override;
191 
192  /** \todo would be great to have docs.
193  * Note: Can result in substantial CPU memory allocation on first call.
194  * 31 * n^2 * sizeof(Value) bytes per DDB
195  */
196  Grad evalGradSource(ParticleSet& P, ParticleSet& source, int iat) override;
197 
199  ParticleSet& source,
200  int iat,
203 
204  /** move was accepted, update the real container
205  */
206  void acceptMove(ParticleSet& P, int iat, bool safe_to_delay = false) override;
207 
209  const RefVectorWithLeader<ParticleSet>& p_list,
210  int iat,
211  const std::vector<bool>& isAccepted,
212  bool safe_to_delay = false) const override;
213 
214  /** complete any left over determinant matrix updates.
215  * Usually this is the end of pbyp moves for a given spin of electrons
216  * The psiM, dpsiM, d2psiM should be up-to-date on both device and host sides.
217  */
218  void completeUpdates() override;
219 
220  void mw_completeUpdates(const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
221 
222  /** move was rejected. copy the real container to the temporary to move on
223  */
224  void restore(int iat) override;
225 
226  /** evaluate from scratch pretty much everything for this single walker determinant
227  *
228  * return of the log of the dirac determinant is the least of what it does.
229  *
230  * call to generate valid initial state for determinant and when you
231  * suspect psiMinv or other state variables may have picked up error.
232  */
235  ParticleSet::ParticleLaplacian& L) override;
236 
238  const RefVectorWithLeader<ParticleSet>& p_list,
240  const RefVector<ParticleSet::ParticleLaplacian>& L_list) const override;
241 
242  void recompute(const ParticleSet& P) override;
243 
244  /** Does a Phi->mw_evaluate_notranspose then mw_invertPsiM over a set of
245  * elements filtered based on the recompute mask.
246  *
247  */
249  const RefVectorWithLeader<ParticleSet>& p_list,
250  const std::vector<bool>& recompute) const override;
251 
255  bool fromscratch) override;
256 
258  const RefVectorWithLeader<ParticleSet>& p_list,
261  bool fromscratch) const override;
262 
263  void evaluateHessian(ParticleSet& P, HessVector& grad_grad_psi) override;
264 
265  void createResource(ResourceCollection& collection) const override;
266  void acquireResource(ResourceCollection& collection,
267  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
268  void releaseResource(ResourceCollection& collection,
269  const RefVectorWithLeader<WaveFunctionComponent>& wfc_list) const override;
270 
271  void registerTWFFastDerivWrapper(const ParticleSet& P, TWFFastDerivWrapper& twf) const override;
272  /** cloning function
273  * @param tqp target particleset
274  * @param spo spo set
275  *
276  * This interface is exposed only to SlaterDet and its derived classes
277  * can overwrite to clone itself correctly.
278  */
279  std::unique_ptr<DiracDeterminantBase> makeCopy(std::unique_ptr<SPOSet>&& spo) const override;
280 
281  void evaluateRatiosAlltoOne(ParticleSet& P, std::vector<Value>& ratios) override;
282 
283  const auto& get_psiMinv() const { return psiMinv_; }
284 
285 private:
286  /** @defgroup LegacySingleData
287  * @brief Single Walker Data Members of Legacy OO design
288  * High and flexible throughput of walkers requires would ideally separate
289  * walker data which should be "SoA" and functions over it i.e. leave behind
290  * the OO pattern of a single set of data and functions on it.
291  *
292  * @ingroup LegacySingleData
293  * @{
294  */
295  /* inverse transpose of psiM(j,i) \f$= \psi_j({\bf r}_i)\f$
296  * Only NumOrbitals x NumOrbitals subblock has meaningful data
297  * The number of rows is equal to NumOrbitals
298  * The number of columns in each row is padded to a multiple of QMC_SIMD_ALIGNMENT
299  */
301  /// fused memory for psiM, dpsiM and d2psiM. [5][norb*norb]
303  /** psiM(j,i) \f$= \psi_j({\bf r}_i)\f$. partial memory view of psiM_vgl
304  * Only this one is Dual since only psiM is a Dual argument
305  * in the det_engine single walker API.
306  */
309  /// dpsiM(i,j) \f$= \nabla_i \psi_j({\bf r}_i)\f$. partial memory view of psiM_vgl
311  /// d2psiM(i,j) \f$= \nabla_i^2 \psi_j({\bf r}_i)\f$. partial memory view of psiM_vgl
313 
314  /// Used for force computations
317 
321 
322  /// value of single-particle orbital for particle-by-particle update
331 
332  /// psi(r')/psi(r) during a PbyP move
334  /**@}*/
335 
338 
339  ///reset the size: with the number of particles and number of orbtials
340  void resize(int nel, int morb);
341 
342  /// Delayed update engine 1 per walker.
344 
345  /// slow but doesn't consume device memory
347 
348  /// matrix inversion engine this a crowd scope resource and only the leader engine gets it
350 
351  /// compute G and L assuming psiMinv, dpsiM, d2psiM are ready for use
353 
354  /// single invert logdetT(psiM)
355  /// as a side effect this->log_value_ gets the log determinant of logdetT
356  void invertPsiM(const DualMatrix<Value>& psiM, DualMatrix<Value>& psiMinv);
357 
358  /** Inverts and finds log det for a batch of matrices
359  *
360  * Right now this takes filtered lists and the full recompute mask. It passes
361  * all these elements down to the det_engine_.
362  * This allows minimal change for implementation code while establishing the API
363  * I'd prefer for direct inversion.
364  * I think the det_engine_ mw method should receive the complete
365  * list of walker elements and the implementation should decide what to do re
366  * the compute mask. See future PR for those changes, or drop of compute_mask argument.
367  */
368  static void mw_invertPsiM(const RefVectorWithLeader<WaveFunctionComponent>& wfc_list,
369  const RefVector<const DualMatrix<Value>>& logdetT_list,
370  const RefVector<DualMatrix<Value>>& a_inv_lis);
371 
372  /// Resize all temporary arrays required for force computation.
374 
375  /// maximal number of delayed updates
376  const int ndelay_;
377 
378  /// selected scheme for inversion with walker batching
380 
381  /// timers
383 };
384 
388 #if defined(ENABLE_CUDA) && defined(ENABLE_OFFLOAD)
390 #endif
391 #if defined(ENABLE_SYCL) && defined(ENABLE_OFFLOAD)
393 #endif
394 
395 
396 } // namespace qmcplusplus
397 #endif
DualMatrix< Value > psiM_temp
psiM(j,i) .
void copyFromBuffer(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
Declaration of DiracDeterminantBase with a S(ingle)P(article)O(rbital)Set.
DiracDeterminantBatched & operator=(const DiracDeterminantBatched &s)=delete
implements dirac matrix delayed update using OpenMP offload and CUDA.
Fixed-size array.
Definition: OhmmsTinyMeta.h:30
void resizeScratchObjectsForIonDerivs()
Resize all temporary arrays required for force computation.
DiracMatrixComputeOMPTarget< FPVT > Inverter
DiracMatrix< FullPrecValue > host_inverter_
slow but doesn&#39;t consume device memory
const int ndelay_
maximal number of delayed updates
WaveFunctionComponent::PsiValue PsiValue
Definition: SlaterDet.cpp:25
LogValue evaluateGL(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L, bool fromscratch) override
compute gradients and laplacian of the TWF with respect to each particle.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
Matrix< Grad > grad_source_psiM
Used for force computations.
void mw_ratioGrad(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios, std::vector< Grad > &grad_new) const override
class to compute matrix inversion and the log value of determinant of a batch of DiracMatrixes.
void restore(int iat) override
move was rejected.
void computeGL(ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) const
compute G and L assuming psiMinv, dpsiM, d2psiM are ready for use
void acquireResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
acquire a shared resource from a collection
void mw_completeUpdates(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
complete all the delayed or asynchronous operations for all the walkers in a batch before leaving the...
const DetMatInvertor matrix_inverter_kind_
selected scheme for inversion with walker batching
ResourceHandle manages the temporary resource referenced from a collection.
RealType ValueType
Definition: QMCTypes.h:42
DetMatInvertor
determinant matrix inverter select
A ParticleSet that handles virtual moves of a selected particle of a given physical ParticleSet Virtu...
Consistent way to get the set of types used in the QMCWaveFunction module, without resorting to ifdef...
TinyVector< Value, OHMMS_DIM > Grad
typename DetInverterSelector< PL, FPVT >::Inverter DetInverter
typename UpdateEngineSelector< PL, VT >::Engine UpdateEngine
Grad evalGradWithSpin(ParticleSet &P, int iat, ComplexType &spingrad) override
return the current spin gradient for the iat-th particle Default implementation assumes that WaveFunc...
DualVector< Value > psiV
value of single-particle orbital for particle-by-particle update
void mw_evalGrad(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< Grad > &grad_now) const override
void evaluateHessian(ParticleSet &P, HessVector &grad_grad_psi) override
Timer accumulates time and call counts.
Definition: NewTimer.h:135
static void mw_invertPsiM(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVector< const DualMatrix< Value >> &logdetT_list, const RefVector< DualMatrix< Value >> &a_inv_lis)
Inverts and finds log det for a batch of matrices.
std::string getClassName() const override
return class name
Attaches a unit to a Vector for IO.
TWFFastDerivWrapper is a wrapper class for TrialWavefunction that provides separate and low level acc...
Grad evalGrad(ParticleSet &P, int iat) override
return the current gradient for the iat-th particle
void recompute(const ParticleSet &P) override
recompute the value of the WaveFunctionComponents which require critical accuracy.
PsiValue ratioGradWithSpin(ParticleSet &P, int iat, Grad &grad_iat, ComplexType &spingrad) override
void evaluateRatios(const VirtualParticleSet &VP, std::vector< Value > &ratios) override
compute multiple ratios for a particle move
void mw_calcRatio(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios) const override
void evaluateDerivativesWF(ParticleSet &P, const opt_variables_type &optvars, Vector< ValueType > &dlogpsi) override
Compute the derivatives of the log of the wavefunction with respect to optimizable parameters...
UpdateEngine det_engine_
Delayed update engine 1 per walker.
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
Array< ValueType, 3, OffloadPinnedAllocator< ValueType > > OffloadMWVGLArray
Definition: SPOSet.h:58
void mw_evaluateRatios(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< const VirtualParticleSet > &vp_list, std::vector< std::vector< Value >> &ratios) const override
void mw_evaluateGL(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const RefVector< ParticleSet::ParticleGradient > &G_list, const RefVector< ParticleSet::ParticleLaplacian > &L_list, bool fromscratch) const override
evaluate gradients and laplacian of the same type WaveFunctionComponent of multiple walkers ...
QTBase::ValueType ValueType
Definition: Configuration.h:60
void createResource(ResourceCollection &collection) const override
initialize a shared resource and hand it to a collection
Grad evalGradSource(ParticleSet &P, ParticleSet &source, int iat) override
LogValue evaluateLog(const ParticleSet &P, ParticleSet::ParticleGradient &G, ParticleSet::ParticleLaplacian &L) override
evaluate from scratch pretty much everything for this single walker determinant
class to handle a set of variables that can be modified during optimizations
Definition: VariableSet.h:49
DualVGLVector psiM_vgl
fused memory for psiM, dpsiM and d2psiM. [5][norb*norb]
void evaluateSpinorRatios(const VirtualParticleSet &VP, const std::pair< ValueVector, ValueVector > &spinor_multiplier, std::vector< Value > &ratios) override
PsiValue ratioGrad(ParticleSet &P, int iat, Grad &grad_iat) override
std::complex< FullPrecReal > LogValue
void evaluateDerivRatios(const VirtualParticleSet &VP, const opt_variables_type &optvars, std::vector< ValueType > &ratios, Matrix< ValueType > &dratios) override
evaluate ratios to evaluate the non-local PP
typename SPOSet::OffloadMWVGLArray OffloadMWVGLArray
std::vector< std::reference_wrapper< T > > RefVector
void registerTWFFastDerivWrapper(const ParticleSet &P, TWFFastDerivWrapper &twf) const override
Register the component with the TWFFastDerivWrapper wrapper.
class defining a compute and memory resource to compute matrix inversion and the log determinants of ...
void invertPsiM(const DualMatrix< Value > &psiM, DualMatrix< Value > &psiMinv)
single invert logdetT(psiM) as a side effect this->log_value_ gets the log determinant of logdetT ...
Matrix< Grad > dpsiM
dpsiM(i,j) . partial memory view of psiM_vgl
void mw_recompute(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const std::vector< bool > &recompute) const override
Does a Phi->mw_evaluate_notranspose then mw_invertPsiM over a set of elements filtered based on the r...
void acceptMove(ParticleSet &P, int iat, bool safe_to_delay=false) override
move was accepted, update the real container
QMCTraits::ComplexType ComplexType
OrbitalSetTraits< ValueType >::HessVector HessVector
std::complex< double > LogValue
void completeUpdates() override
complete any left over determinant matrix updates.
void releaseResource(ResourceCollection &collection, const RefVectorWithLeader< WaveFunctionComponent > &wfc_list) const override
return a shared resource to a collection
typename WFT::FullPrecValue FullPrecValue
Tensor< Value, OHMMS_DIM > Hess
void mw_ratioGradWithSpin(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< PsiValue > &ratios, std::vector< Grad > &grad_new, std::vector< ComplexType > &spingrad_new) const override
LogValue updateBuffer(ParticleSet &P, WFBufferType &buf, bool fromscratch=false) override
For particle-by-particle move.
void evaluateDerivatives(ParticleSet &P, const opt_variables_type &active, Vector< Value > &dlogpsi, Vector< Value > &dhpsioverpsi) override
ResourceHandle< DiracDeterminantBatchedMultiWalkerResource > mw_res_handle_
void resize(int nel, int morb)
reset the size: with the number of particles and number of orbtials
DiracDeterminantBatched(std::unique_ptr< SPOSet > &&spos, int first, int last, int ndelay=1, DetMatInvertor matrix_inverter_kind=DetMatInvertor::ACCEL)
constructor
std::unique_ptr< DiracDeterminantBase > makeCopy(std::unique_ptr< SPOSet > &&spo) const override
cloning function
PsiValue curRatio
psi(r&#39;)/psi(r) during a PbyP move
void evaluateRatiosAlltoOne(ParticleSet &P, std::vector< Value > &ratios) override
void mw_accept_rejectMove(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, const std::vector< bool > &isAccepted, bool safe_to_delay=false) const override
moves of the iat-th particle on some walkers in a batch is accepted.
void mw_evaluateLog(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, const RefVector< ParticleSet::ParticleGradient > &G_list, const RefVector< ParticleSet::ParticleLaplacian > &L_list) const override
evaluate from scratch the same type WaveFunctionComponent of multiple walkers
void registerData(ParticleSet &P, WFBufferType &buf) override
For particle-by-particle move.
Matrix< Value > d2psiM
d2psiM(i,j) . partial memory view of psiM_vgl
void mw_evalGradWithSpin(const RefVectorWithLeader< WaveFunctionComponent > &wfc_list, const RefVectorWithLeader< ParticleSet > &p_list, int iat, std::vector< Grad > &grad_now, std::vector< ComplexType > &spingrad_now) const override
ResourceHandle< DetInverter > accel_inverter_
matrix inversion engine this a crowd scope resource and only the leader engine gets it ...
PsiValue ratio(ParticleSet &P, int iat) override
return the ratio only for the iat-th partcle move