QMCPACK
DistanceTable.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) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
9 // Jaron T. Krogel, krogeljt@ornl.gov, Oak Ridge National Laboratory
10 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
11 //
12 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
13 //////////////////////////////////////////////////////////////////////////////////////
14 
15 
16 #ifndef QMCPLUSPLUS_DISTANCETABLEDATAIMPL_H
17 #define QMCPLUSPLUS_DISTANCETABLEDATAIMPL_H
18 
19 #include "Particle/ParticleSet.h"
20 #include <limits>
21 #include "OhmmsPETE/OhmmsVector.h"
22 #include "OhmmsPETE/OhmmsMatrix.h"
25 #include "DTModes.h"
26 
27 namespace qmcplusplus
28 {
29 class ResourceCollection;
30 
31 /** @ingroup nnlist
32  * @brief Abstract class to manage operations on pair data between two ParticleSets.
33  *
34  * Each DistanceTable object is defined by Source and Target of ParticleSet types.
35  * This base class doesn't contain storage. It is intended for update/compute invoked by ParticleSet.
36  * Derived AA/AB classes handle the actual storage and data access.
37  */
39 {
40 public:
41  static constexpr unsigned DIM = OHMMS_DIM;
42 
48 
49 protected:
50  // FIXME. once DT takes only DynamicCoordinates, change this type as well.
52 
53  const size_t num_sources_;
54  const size_t num_targets_;
55 
56  ///name of the table
57  const std::string name_;
58 
59  ///operation modes defined by DTModes
61 
62 public:
63  ///constructor using source and target ParticleSet
64  DistanceTable(const ParticleSet& source, const ParticleSet& target, DTModes modes)
65  : origin_(source),
66  num_sources_(source.getTotalNum()),
67  num_targets_(target.getTotalNum()),
68  name_(source.getName() + "_" + target.getName()),
69  modes_(modes)
70  {}
71 
72  /// copy constructor. deleted
73  DistanceTable(const DistanceTable&) = delete;
74 
75  ///virutal destructor
76  virtual ~DistanceTable() = default;
77 
78  ///get modes
79  inline DTModes getModes() const { return modes_; }
80 
81  ///set modes
82  inline void setModes(DTModes modes) { modes_ = modes; }
83 
84  ///return the name of table
85  inline const std::string& getName() const { return name_; }
86 
87  ///returns the reference the origin particleset
88  const ParticleSet& get_origin() const { return origin_; }
89 
90  ///returns the number of centers
91  inline size_t centers() const { return origin_.getTotalNum(); }
92 
93  ///returns the number of centers
94  inline size_t targets() const { return num_targets_; }
95 
96  ///returns the number of source particles
97  inline size_t sources() const { return num_sources_; }
98 
99  /** evaluate the full Distance Table
100  * @param P the target particle set
101  */
102  virtual void evaluate(ParticleSet& P) = 0;
103  virtual void mw_evaluate(const RefVectorWithLeader<DistanceTable>& dt_list,
104  const RefVectorWithLeader<ParticleSet>& p_list) const
105  {
106  for (int iw = 0; iw < dt_list.size(); iw++)
107  dt_list[iw].evaluate(p_list[iw]);
108  }
109 
110  /** recompute multi walker internal data, recompute
111  * @param dt_list the distance table batch
112  * @param p_list the target particle set batch
113  * @param recompute if true, must recompute. Otherwise, implementation dependent.
114  */
116  const RefVectorWithLeader<ParticleSet>& p_list,
117  const std::vector<bool>& recompute) const
118  {
119  for (int iw = 0; iw < dt_list.size(); iw++)
120  if (recompute[iw])
121  dt_list[iw].evaluate(p_list[iw]);
122  }
123 
124  /** evaluate the temporary pair relations when a move is proposed
125  * @param P the target particle set
126  * @param rnew proposed new position
127  * @param iat the particle to be moved
128  * @param prepare_old if true, prepare (temporary) old distances and displacements for using getOldDists and getOldDispls functions in acceptMove.
129  *
130  * Note: some distance table consumers (WaveFunctionComponent) have optimized code paths which require prepare_old = true for accepting a move.
131  * Drivers/Hamiltonians know whether moves will be accepted or not and manage this flag when calling ParticleSet::makeMoveXXX functions.
132  */
133  virtual void move(const ParticleSet& P, const PosType& rnew, const IndexType iat, bool prepare_old = true) = 0;
134 
135  /** walker batched version of move. this function may be implemented asynchronously.
136  * Additional synchroniziation for collecting results should be handled by the caller.
137  * If DTModes::NEED_TEMP_DATA_ON_HOST, host data will be updated.
138  * If no consumer requests data on the host, the transfer is skipped.
139  */
140  virtual void mw_move(const RefVectorWithLeader<DistanceTable>& dt_list,
141  const RefVectorWithLeader<ParticleSet>& p_list,
142  const std::vector<PosType>& rnew_list,
143  const IndexType iat,
144  bool prepare_old = true) const
145  {
146  for (int iw = 0; iw < dt_list.size(); iw++)
147  dt_list[iw].move(p_list[iw], rnew_list[iw], iat, prepare_old);
148  }
149 
150  /** update the distance table by the pair relations from the temporal position.
151  * Used when a move is accepted in regular mode
152  * @param iat the particle with an accepted move
153  */
154  virtual void update(IndexType jat) = 0;
155 
156  /** fill partially the distance table by the pair relations from the temporary or old particle position.
157  * Used in forward mode when a move is reject
158  * @param iat the particle with an accepted move
159  * @param from_temp if true, copy from temp. if false, copy from old
160  */
161  virtual void updatePartial(IndexType jat, bool from_temp)
162  {
163  if (from_temp)
164  update(jat);
165  }
166 
167  /** walker batched version of updatePartial.
168  * If not DTModes::NEED_TEMP_DATA_ON_HOST, host data is not up-to-date and host distance table will not be updated.
169  */
171  IndexType jat,
172  const std::vector<bool>& from_temp)
173  {
174  for (int iw = 0; iw < dt_list.size(); iw++)
175  dt_list[iw].updatePartial(jat, from_temp[iw]);
176  }
177 
178  /** finalize distance table calculation after particle-by-particle moves
179  * if update() doesn't make the table up-to-date during p-by-p moves
180  * finalizePbyP takes action to bring the table up-to-date
181  */
182  virtual void finalizePbyP(const ParticleSet& P) {}
183 
184  /** walker batched version of finalizePbyP
185  * If not DTModes::NEED_TEMP_DATA_ON_HOST, host distance table data is not updated at all during p-by-p
186  * Thus, a recompute is necessary to update the whole host distance table for consumers like the Coulomb potential.
187  */
189  const RefVectorWithLeader<ParticleSet>& p_list) const
190  {
191  for (int iw = 0; iw < dt_list.size(); iw++)
192  dt_list[iw].finalizePbyP(p_list[iw]);
193  }
194 
195  /** find the first nearest neighbor
196  * @param iat source particle id
197  * @param r distance
198  * @param dr displacement
199  * @param newpos if true, use the data in temp_r_ and temp_dr_ for the proposed move.
200  * if false, use the data in distance_[iat] and displacements_[iat]
201  * @return the id of the nearest particle, -1 not found
202  */
203  virtual int get_first_neighbor(IndexType iat, RealType& r, PosType& dr, bool newpos) const = 0;
204 
205  [[noreturn]] inline void print(std::ostream& os)
206  {
207  throw std::runtime_error("DistanceTable::print is not supported");
208  }
209 
210  /// initialize a shared resource and hand it to a collection
211  virtual void createResource(ResourceCollection& collection) const {}
212 
213  /// acquire a shared resource from a collection
214  virtual void acquireResource(ResourceCollection& collection, const RefVectorWithLeader<DistanceTable>& dt_list) const
215  {}
216 
217  /// return a shared resource to a collection
218  virtual void releaseResource(ResourceCollection& collection, const RefVectorWithLeader<DistanceTable>& dt_list) const
219  {}
220 };
221 
222 /** AA type of DistanceTable containing storage */
224 {
225 protected:
226  /** distances_[num_targets_][num_sources_], [i][3][j] = |r_A2[j] - r_A1[i]|
227  * Note: Derived classes decide if it is a memory view or the actual storage
228  * For only the lower triangle (j<i) data can be accessed safely.
229  * There is no bound check to protect j>=i terms as the nature of operator[].
230  * When the storage of the table is allocated as a single memory segment,
231  * out-of-bound access is still within the segment and
232  * thus doesn't trigger an alarm by the address sanitizer.
233  */
234  std::vector<DistRow> distances_;
235 
236  /** displacements_[num_targets_][3][num_sources_], [i][3][j] = r_A2[j] - r_A1[i]
237  * Note: Derived classes decide if it is a memory view or the actual storage
238  * only the lower triangle (j<i) is defined. See the note of distances_.
239  */
240  std::vector<DisplRow> displacements_;
241 
242  /// temp_r
244 
245  /// temp_dr
247 
248  /// old distances
250 
251  /// old displacements
253 
254 public:
255  ///constructor using source and target ParticleSet
256  DistanceTableAA(const ParticleSet& target, DTModes modes) : DistanceTable(target, target, modes) {}
257 
258  /** return full table distances
259  */
260  const std::vector<DistRow>& getDistances() const { return distances_; }
261 
262  /** return full table displacements
263  */
264  const std::vector<DisplRow>& getDisplacements() const { return displacements_; }
265 
266  /** return a row of distances for a given target particle
267  */
268  const DistRow& getDistRow(int iel) const { return distances_[iel]; }
269 
270  /** return a row of displacements for a given target particle
271  */
272  const DisplRow& getDisplRow(int iel) const { return displacements_[iel]; }
273 
274  /** return the temporary distances when a move is proposed
275  */
276  const DistRow& getTempDists() const { return temp_r_; }
277 
278  /** return the temporary displacements when a move is proposed
279  */
280  const DisplRow& getTempDispls() const { return temp_dr_; }
281 
282  /** return old distances set up by move() for optimized distance table consumers
283  */
284  const DistRow& getOldDists() const { return old_r_; }
285 
286  /** return old displacements set up by move() for optimized distance table consumers
287  */
288  const DisplRow& getOldDispls() const { return old_dr_; }
289 
290  virtual size_t get_num_particls_stored() const { return 0; }
291 
292  /// return multi walker temporary pair distance table data pointer
293  [[noreturn]] virtual const RealType* getMultiWalkerTempDataPtr() const
294  {
295  throw std::runtime_error(name_ + " multi walker data pointer for temp not supported");
296  }
297 
299  const RefVectorWithLeader<ParticleSet>& p_list,
300  size_t range_begin,
301  size_t range_end) const
302  {
303  return nullptr;
304  }
305 };
306 
307 /** AB type of DistanceTable containing storage */
309 {
310 protected:
311  /** distances_[num_targets_][num_sources_], [i][3][j] = |r_A2[j] - r_A1[i]|
312  * Note: Derived classes decide if it is a memory view or the actual storage
313  */
314  std::vector<DistRow> distances_;
315 
316  /** displacements_[num_targets_][3][num_sources_], [i][3][j] = r_A2[j] - r_A1[i]
317  * Note: Derived classes decide if it is a memory view or the actual storage
318  */
319  std::vector<DisplRow> displacements_;
320 
321  /// temp_r
323 
324  /// temp_dr
326 
327 public:
328  ///constructor using source and target ParticleSet
329  DistanceTableAB(const ParticleSet& source, const ParticleSet& target, DTModes modes)
330  : DistanceTable(source, target, modes)
331  {}
332 
333  /** return full table distances
334  */
335  const std::vector<DistRow>& getDistances() const { return distances_; }
336 
337  /** return full table displacements
338  */
339  const std::vector<DisplRow>& getDisplacements() const { return displacements_; }
340 
341  /** return a row of distances for a given target particle
342  */
343  const DistRow& getDistRow(int iel) const { return distances_[iel]; }
344 
345  /** return a row of displacements for a given target particle
346  */
347  const DisplRow& getDisplRow(int iel) const { return displacements_[iel]; }
348 
349  /** return the temporary distances when a move is proposed
350  */
351  const DistRow& getTempDists() const { return temp_r_; }
352 
353  /** return the temporary displacements when a move is proposed
354  */
355  const DisplRow& getTempDispls() const { return temp_dr_; }
356 
357  /// return multi-walker full (all pairs) distance table data pointer
358  [[noreturn]] virtual const RealType* getMultiWalkerDataPtr() const
359  {
360  throw std::runtime_error(name_ + " multi walker data pointer not supported");
361  }
362 
363  /// return stride of per target pctl data. full table data = stride * num of target particles
364  [[noreturn]] virtual size_t getPerTargetPctlStrideSize() const
365  {
366  throw std::runtime_error(name_ + " getPerTargetPctlStrideSize not supported");
367  }
368 };
369 } // namespace qmcplusplus
370 #endif
QMCTraits::RealType RealType
Definition: DistanceTable.h:44
virtual void finalizePbyP(const ParticleSet &P)
finalize distance table calculation after particle-by-particle moves if update() doesn&#39;t make the tab...
void print(std::ostream &os)
const std::vector< DistRow > & getDistances() const
return full table distances
virtual void update(IndexType jat)=0
update the distance table by the pair relations from the temporal position.
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
const DisplRow & getDisplRow(int iel) const
return a row of displacements for a given target particle
virtual size_t getPerTargetPctlStrideSize() const
return stride of per target pctl data. full table data = stride * num of target particles ...
QTBase::RealType RealType
Definition: Configuration.h:58
const DistRow & getDistRow(int iel) const
return a row of distances for a given target particle
virtual ~DistanceTable()=default
virutal destructor
size_t getTotalNum() const
Definition: ParticleSet.h:493
const ParticleSet & get_origin() const
returns the reference the origin particleset
Definition: DistanceTable.h:88
const std::vector< DisplRow > & getDisplacements() const
return full table displacements
Abstract class to manage operations on pair data between two ParticleSets.
Definition: DistanceTable.h:38
Soa Container for D-dim vectors.
virtual void mw_finalizePbyP(const RefVectorWithLeader< DistanceTable > &dt_list, const RefVectorWithLeader< ParticleSet > &p_list) const
walker batched version of finalizePbyP If not DTModes::NEED_TEMP_DATA_ON_HOST, host distance table da...
void setModes(DTModes modes)
set modes
Definition: DistanceTable.h:82
SoA adaptor class for Vector<TinyVector<T,D> >
const DisplRow & getTempDispls() const
return the temporary displacements when a move is proposed
static constexpr unsigned DIM
Definition: DistanceTable.h:41
std::vector< DisplRow > displacements_
displacements_[num_targets_][3][num_sources_], [i][3][j] = r_A2[j] - r_A1[i] Note: Derived classes de...
const DistRow & getTempDists() const
return the temporary distances when a move is proposed
const std::string name_
name of the table
Definition: DistanceTable.h:57
#define OHMMS_DIM
Definition: config.h:64
virtual void mw_recompute(const RefVectorWithLeader< DistanceTable > &dt_list, const RefVectorWithLeader< ParticleSet > &p_list, const std::vector< bool > &recompute) const
recompute multi walker internal data, recompute
QMCTraits::IndexType IndexType
Definition: FSUtilities.h:11
DisplRow old_dr_
old displacements
DistRow old_r_
old distances
virtual size_t get_num_particls_stored() const
Specialized paritlce class for atomistic simulations.
Definition: ParticleSet.h:55
const DisplRow & getTempDispls() const
return the temporary displacements when a move is proposed
virtual void mw_move(const RefVectorWithLeader< DistanceTable > &dt_list, const RefVectorWithLeader< ParticleSet > &p_list, const std::vector< PosType > &rnew_list, const IndexType iat, bool prepare_old=true) const
walker batched version of move.
virtual void updatePartial(IndexType jat, bool from_temp)
fill partially the distance table by the pair relations from the temporary or old particle position...
const DistRow & getTempDists() const
return the temporary distances when a move is proposed
AB type of DistanceTable containing storage.
virtual void evaluate(ParticleSet &P)=0
evaluate the full Distance Table
size_t targets() const
returns the number of centers
Definition: DistanceTable.h:94
DistanceTableAA(const ParticleSet &target, DTModes modes)
constructor using source and target ParticleSet
virtual void move(const ParticleSet &P, const PosType &rnew, const IndexType iat, bool prepare_old=true)=0
evaluate the temporary pair relations when a move is proposed
virtual const RealType * getMultiWalkerDataPtr() const
return multi-walker full (all pairs) distance table data pointer
QTBase::PosType PosType
Definition: Configuration.h:61
const std::string & getName() const
return the name of table
Definition: DistanceTable.h:85
const DistRow & getDistRow(int iel) const
return a row of distances for a given target particle
OHMMS_INDEXTYPE IndexType
define other types
Definition: Configuration.h:65
AA type of DistanceTable containing storage.
const DisplRow & getDisplRow(int iel) const
return a row of displacements for a given target particle
virtual void mw_updatePartial(const RefVectorWithLeader< DistanceTable > &dt_list, IndexType jat, const std::vector< bool > &from_temp)
walker batched version of updatePartial.
virtual void acquireResource(ResourceCollection &collection, const RefVectorWithLeader< DistanceTable > &dt_list) const
acquire a shared resource from a collection
size_t sources() const
returns the number of source particles
Definition: DistanceTable.h:97
virtual const RealType * getMultiWalkerTempDataPtr() const
return multi walker temporary pair distance table data pointer
Declaraton of Vector<T,Alloc> Manage memory through Alloc directly and allow referencing an existing ...
QMCTraits::IndexType IndexType
Definition: DistanceTable.h:43
DTModes getModes() const
get modes
Definition: DistanceTable.h:79
const std::vector< DistRow > & getDistances() const
return full table distances
const std::vector< DisplRow > & getDisplacements() const
return full table displacements
virtual void createResource(ResourceCollection &collection) const
initialize a shared resource and hand it to a collection
virtual void mw_evaluate(const RefVectorWithLeader< DistanceTable > &dt_list, const RefVectorWithLeader< ParticleSet > &p_list) const
size_t centers() const
returns the number of centers
Definition: DistanceTable.h:91
const DistRow & getOldDists() const
return old distances set up by move() for optimized distance table consumers
std::vector< DisplRow > displacements_
displacements_[num_targets_][3][num_sources_], [i][3][j] = r_A2[j] - r_A1[i] Note: Derived classes de...
virtual const RealType * mw_evalDistsInRange(const RefVectorWithLeader< DistanceTable > &dt_list, const RefVectorWithLeader< ParticleSet > &p_list, size_t range_begin, size_t range_end) const
DistanceTableAB(const ParticleSet &source, const ParticleSet &target, DTModes modes)
constructor using source and target ParticleSet
const ParticleSet & origin_
Definition: DistanceTable.h:51
virtual int get_first_neighbor(IndexType iat, RealType &r, PosType &dr, bool newpos) const =0
find the first nearest neighbor
std::vector< DistRow > distances_
distances_[num_targets_][num_sources_], [i][3][j] = |r_A2[j] - r_A1[i]| Note: Derived classes decide ...
virtual void releaseResource(ResourceCollection &collection, const RefVectorWithLeader< DistanceTable > &dt_list) const
return a shared resource to a collection
std::vector< DistRow > distances_
distances_[num_targets_][num_sources_], [i][3][j] = |r_A2[j] - r_A1[i]| Note: Derived classes decide ...
DTModes modes_
operation modes defined by DTModes
Definition: DistanceTable.h:60
DistanceTable(const ParticleSet &source, const ParticleSet &target, DTModes modes)
constructor using source and target ParticleSet
Definition: DistanceTable.h:64
const DisplRow & getOldDispls() const
return old displacements set up by move() for optimized distance table consumers