QMCPACK
OperatorEstBase.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) 2020 QMCPACK developers.
6 //
7 // File developed by: Peter Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File refactored from: OperatorBase.h
10 //////////////////////////////////////////////////////////////////////////////////////
11 
12 
13 /**@file
14  */
15 #ifndef QMCPLUSPLUS_OPERATORESTBASE_H
16 #define QMCPLUSPLUS_OPERATORESTBASE_H
17 
18 #include "Particle/ParticleSet.h"
25 #include "hdf/hdf_archive.h"
26 #include <bitset>
27 
28 namespace qmcplusplus
29 {
30 class TrialWaveFunction;
31 namespace testing
32 {
33 class OEBAccessor;
34 }
35 /** @ingroup Estimators
36  * @brief An abstract class for gridded estimators
37  *
38  */
40 {
41 public:
42  using QMCT = QMCTraits;
45 
46  using Data = std::vector<QMCT::RealType>;
47 
48  ///constructor
50  /** Shallow copy constructor!
51  * This alows us to keep the default copy constructors for derived classes which
52  * is quite useful to the spawnCrowdClone design.
53  * Data is likely to be quite large and since the OperatorEstBase design is that the children
54  * reduce to the parent it is infact undesirable for them to copy the data the parent has.
55  * Initialization of Data (i.e. call to resize) if any is the responsibility of the derived class.
56  */
57  OperatorEstBase(const OperatorEstBase& oth);
58  ///virtual destructor
59  virtual ~OperatorEstBase() = default;
60 
61  /** Accumulate whatever it is you are accumulating with respect to walkers
62  *
63  * This method is assumed to be called from the crowd context
64  * It provides parallelism with respect to computational effort of the estimator
65  * without causing a global sync.
66  * Depending on data locality the accumlation of the result may be different from
67  * the single thread write directly into the OperatorEstimator data.
68  * \param[in] walkers
69  * \param[inout] pset_target crowd scope target pset (should be returned to starting state after call)
70  * \param[in] psets per walker psets
71  * \param[in] wnfs per walker TrialWaveFunction
72  * \param[inout] rng crowd scope RandomGenerator
73  */
74  virtual void accumulate(const RefVector<MCPWalker>& walkers,
75  const RefVector<ParticleSet>& psets,
76  const RefVector<TrialWaveFunction>& wfns,
77  const RefVector<QMCHamiltonian>& hams,
79 
80  /** Reduce estimator result data from crowds to rank
81  *
82  * This is assumed to be called from only from one thread per crowds->rank
83  * reduction. Implied is this is during a global sync or there is a guarantee
84  * that the crowd operator estimators accumulation data is not being written to.
85  *
86  * There could be concurrent operations inside the scope of the collect call.
87  */
88  virtual void collect(const RefVector<OperatorEstBase>& oebs);
89 
90  virtual void normalize(QMCT::RealType invToWgt);
91 
92  virtual void startBlock(int steps) = 0;
93 
94  std::vector<QMCT::RealType>& get_data() { return data_; }
95 
96  /*** create and tie OperatorEstimator's observable_helper hdf5 wrapper to stat.h5 file
97  * @param gid hdf5 group to which the observables belong
98  *
99  * The default implementation does nothing. The derived classes which compute
100  * big data, e.g. density, should overwrite this function.
101  */
102  virtual void registerOperatorEstimator(hdf_archive& file) {}
103 
104  virtual std::unique_ptr<OperatorEstBase> spawnCrowdClone() const = 0;
105 
106  /** Write to previously registered observable_helper hdf5 wrapper.
107  *
108  * if you haven't registered Operator Estimator
109  * this will do nothing.
110  */
111  void write(hdf_archive& file);
112 
113  /** zero data appropriately for the DataLocality
114  */
115  void zero();
116 
117  /** Return the total walker weight for this block
118  */
120 
121  const std::string& get_my_name() const { return my_name_; }
122 
123  /** Register 0-many listeners with a leading QMCHamiltonian instance i.e. a QMCHamiltonian
124  * that has acquired the crowd scope QMCHamiltonianMultiWalkerResource.
125  * This must be called for each crowd scope estimator that listens to register listeners into
126  * the crowd scope QMCHamiltonianMultiWalkerResource.
127  *
128  * Many estimators don't need per particle values so the default implementation is no op.
129  */
130  virtual void registerListeners(QMCHamiltonian& ham_leader){};
131 
133 
135 
136 protected:
137  /** locality for accumulation of estimator data.
138  * This designates the memory scheme used for the estimator
139  * The default is:
140  * DataLocality::Crowd, each crowd and the rank level estimator have a full representation of the data
141  * Memory Savings Schemes:
142  * One:
143  * DataLocality::Rank, This estimator has the full representation of the data but its crowd spawn will have
144  * One per crowd:
145  * DataLocality::Queue This estimator accumulates queue of values to collect to the Rank estimator data
146  * DataLocality::? Another way to reduce memory use on thread/crowd local estimators.
147  */
149 
150  ///name of this object -- only used for debugging and h5 output
151  std::string my_name_;
152 
154 
155  // convenient Descriptors hdf5 for Operator Estimators only populated for rank scope OperatorEstimator
156  std::vector<ObservableHelper> h5desc_;
157 
159 
160  bool requires_listener_ = false;
161 
162  friend testing::OEBAccessor;
163 };
164 } // namespace qmcplusplus
165 #endif
std::vector< QMCT::RealType > & get_data()
virtual std::unique_ptr< OperatorEstBase > spawnCrowdClone() const =0
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
QTBase::RealType RealType
Definition: Configuration.h:58
std::vector< ObservableHelper > h5desc_
Collection of Local Energy Operators.
virtual void normalize(QMCT::RealType invToWgt)
class to handle hdf file
Definition: hdf_archive.h:51
virtual void startBlock(int steps)=0
const char walkers[]
Definition: HDFVersion.h:36
DataLocality data_locality_
locality for accumulation of estimator data.
Declaration of ObservableHelper and other helper class for observables.
DataLocality get_data_locality() const
QMCT::FullPrecRealType walkers_weight_
OperatorEstBase(DataLocality dl)
constructor
void zero()
zero data appropriately for the DataLocality
virtual void registerOperatorEstimator(hdf_archive &file)
An abstract class for gridded estimators.
const std::string & get_my_name() const
QMCT::FullPrecRealType FullPrecRealType
QMCT::FullPrecRealType get_walkers_weight() const
Return the total walker weight for this block.
std::vector< QMCT::RealType > Data
std::vector< std::reference_wrapper< T > > RefVector
virtual void registerListeners(QMCHamiltonian &ham_leader)
Register 0-many listeners with a leading QMCHamiltonian instance i.e.
DataLocality
data locality with respect to walker buffer
Definition: DataLocality.h:19
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
break encapsulation of data_ by OperatorEstBase only for testing!
traits for QMC variables
Definition: Configuration.h:49
void write(hdf_archive &file)
Write to previously registered observable_helper hdf5 wrapper.
Declare a global Random Number Generator.
A container class to represent a walker.
Definition: Walker.h:49
Declaration of QMCHamiltonian.
virtual void accumulate(const RefVector< MCPWalker > &walkers, const RefVector< ParticleSet > &psets, const RefVector< TrialWaveFunction > &wfns, const RefVector< QMCHamiltonian > &hams, RandomBase< FullPrecRealType > &rng)=0
Accumulate whatever it is you are accumulating with respect to walkers.
virtual void collect(const RefVector< OperatorEstBase > &oebs)
Reduce estimator result data from crowds to rank.
std::string my_name_
name of this object – only used for debugging and h5 output
virtual ~OperatorEstBase()=default
virtual destructor