QMCPACK
WalkerControl.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 // Ken Esler, kpesler@gmail.com, University of Illinois at Urbana-Champaign
9 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
10 // Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 // Ye Luo, yeluo@anl.gov, Argonne National Laboratory
12 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
13 //
14 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
15 //////////////////////////////////////////////////////////////////////////////////////
16 
17 
18 #ifndef QMCPLUSPLUS_WALKER_CONTROL_BASE_H
19 #define QMCPLUSPLUS_WALKER_CONTROL_BASE_H
20 
21 #include "Configuration.h"
24 #include "Message/MPIObjectBase.h"
25 #include "Message/CommOperators.h"
27 
28 #include <filesystem>
29 
30 namespace qmcplusplus
31 {
32 namespace testing
33 {
34 class UnifiedDriverWalkerControlMPITest;
35 }
36 
37 /** Class for controlling the walkers for DMC simulations.
38  * w and w/o MPI. Fixed and dynamic population in one place.
39  */
41 {
42 public:
43  ///typedef of Walker_t
45  /// distinct type for "new" walker, currently same as Walker_t
47  ///typedef of FullPrecRealType
49  ///typedef of IndexType
51 
52  /** default constructor
53  *
54  * Set the SwapMode to zero so that instantiation can be done
55  */
56  WalkerControl(Communicate* c, RandomBase<FullPrecRealType>& rng, bool use_fixed_pop = false);
57 
58  /** empty destructor to clean up the derived classes */
60 
61  /** start a block */
62  void start();
63 
64  /** take averages and writes to a file */
65  void writeDMCdat(int iter, const std::vector<FullPrecRealType>& curData);
66 
67  /** set the trial energy for writing to dmc.dat
68  */
69  inline void setTrialEnergy(FullPrecRealType et) { trial_energy_ = et; }
70 
71  /** unified: perform branch and swap walkers as required
72  */
73  void branch(int iter, MCPopulation& pop, bool do_not_branch);
74 
75  bool put(xmlNodePtr cur);
76 
77  void setMinMax(int nw_in, int nmax_in);
78 
79  int get_n_max() const { return n_max_; }
80  int get_n_min() const { return n_min_; }
83  {
84  ensemble_property_ = ensemble_property;
85  }
87 
88 private:
89  /// kill dead walkers in the population
90  static void killDeadWalkersOnRank(MCPopulation& pop);
91 
92  static std::vector<IndexType> syncFutureWalkersPerRank(Communicate* comm, IndexType n_walkers);
93 
94  /// compute curData
95  void computeCurData(const UPtrVector<MCPWalker>& walkers, std::vector<FullPrecRealType>& curData);
96 
97  /** creates the distribution plan
98  *
99  * populates the minus and plus vectors they contain 1 copy of a partition index
100  * for each adjustment in population to the context.
101  * \param[in] num_per_rank as if all walkers were copied out to multiplicity
102  * \param[out] fair_offset running population count at each partition boundary
103  * \param[out] minus list of partition indexes one occurrence for each walker removed
104  * \param[out] plus list of partition indexes one occurrence for each walker added
105  */
106  static void determineNewWalkerPopulation(const std::vector<int>& num_per_rank,
107  std::vector<int>& fair_offset,
108  std::vector<int>& minus,
109  std::vector<int>& plus);
110 
111 #if defined(HAVE_MPI)
112  /** swap Walkers with Recv/Send or Irecv/Isend
113  *
114  * The algorithm ensures that the load per node can differ only by one walker.
115  * Each MPI rank can only send or receive or be silent.
116  * The communication is one-dimensional and very local.
117  * If multiple copies of a walker need to be sent to the target rank, only send one.
118  * The number of copies is communicated ahead via blocking send/recv.
119  * Then the walkers are transferred via blocking or non-blocking send/recv.
120  * The blocking send/recv may become serialized and worsen load imbalance.
121  * Non blocking send/recv algorithm avoids serialization completely.
122  */
123  void swapWalkersSimple(MCPopulation& pop);
124 #endif
125 
126  /** An enum to access curData for reduction
127  *
128  * curData is larger than this //LE_MAX + n_node * T
129  */
130  enum
131  {
141  };
142 
143  ///random number generator
145  ///if true, use fixed population
147  ///minimum number of walkers
149  ///maximum number of walkers
151  ///maximum copy per walker
153  ///trial energy energy
155  ///number of walkers on each MPI rank after branching before load balancing
156  std::vector<int> num_per_rank_;
157  ///offset of the particle index for a fair distribution
158  std::vector<int> fair_offset_;
159  ///filename for dmc.dat
160  std::filesystem::path dmcFname;
161  ///file to save energy histogram
162  std::unique_ptr<std::ofstream> dmcStream;
163  ///context id
165  ///number of contexts
167  ///0 is default
169  ///any temporary data includes many ridiculous conversions of integral types to and from fp
170  std::vector<FullPrecRealType> curData;
171  ///Use non-blocking isend/irecv
173  ///disable branching for debugging
175  ///ensemble properties
177  ///timers
179  ///Number of walkers sent during the exchange
181 
183 };
184 
185 } // namespace qmcplusplus
186 #endif
RandomBase< FullPrecRealType > & rng_
random number generator
IndexType saved_num_walkers_sent_
Number of walkers sent during the exchange.
IndexType get_num_contexts() const
Definition: WalkerControl.h:86
std::filesystem::path dmcFname
filename for dmc.dat
Base class for any object which needs to know about a MPI communicator.
Definition: MPIObjectBase.h:26
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
std::vector< int > fair_offset_
offset of the particle index for a fair distribution
FullPrecRealType trial_energy_
trial energy energy
Class for controlling the walkers for DMC simulations.
Definition: WalkerControl.h:40
void writeDMCdat(int iter, const std::vector< FullPrecRealType > &curData)
take averages and writes to a file
std::vector< std::unique_ptr< T > > UPtrVector
const char walkers[]
Definition: HDFVersion.h:36
bool put(xmlNodePtr cur)
declaration of MPIObjectBase
const IndexType rank_num_
context id
bool debug_disable_branching_
disable branching for debugging
void setMinMax(int nw_in, int nmax_in)
const IndexType num_ranks_
number of contexts
void branch(int iter, MCPopulation &pop, bool do_not_branch)
unified: perform branch and swap walkers as required
QMCTraits::IndexType IndexType
Definition: FSUtilities.h:11
IndexType n_min_
minimum number of walkers
bool use_nonblocking_
Use non-blocking isend/irecv.
Wrapping information on parallelism.
Definition: Communicate.h:68
Once there is only one driver type rename.
MCDataType< FullPrecRealType > & get_ensemble_property()
Definition: WalkerControl.h:81
IndexType n_max_
maximum number of walkers
static std::vector< IndexType > syncFutureWalkersPerRank(Communicate *comm, IndexType n_walkers)
static void killDeadWalkersOnRank(MCPopulation &pop)
kill dead walkers in the population
std::vector< int > num_per_rank_
number of walkers on each MPI rank after branching before load balancing
IndexType SwapMode
0 is default
void setTrialEnergy(FullPrecRealType et)
set the trial energy for writing to dmc.dat
Definition: WalkerControl.h:69
void set_ensemble_property(MCDataType< FullPrecRealType > &ensemble_property)
Definition: WalkerControl.h:82
OHMMS_INDEXTYPE IndexType
define other types
Definition: Configuration.h:65
TimerList_t my_timers_
timers
void start()
start a block
WalkerConfigurations::Walker_t Walker_t
Walker< QMCTraits, PtclOnLatticeTraits > MCPWalker
Definition: MCPopulation.h:41
QTFull::RealType FullPrecRealType
Definition: Configuration.h:66
~WalkerControl()
empty destructor to clean up the derived classes
IndexType max_copy_
maximum copy per walker
QMCTraits::IndexType IndexType
typedef of IndexType
Definition: WalkerControl.h:50
Declare a global Random Number Generator.
WalkerControl(Communicate *c, RandomBase< FullPrecRealType > &rng, bool use_fixed_pop=false)
default constructor
MCDataType< FullPrecRealType > ensemble_property_
ensemble properties
Declaration of a MCWalkerConfiguration.
A container class to represent a walker.
Definition: Walker.h:49
void computeCurData(const UPtrVector< MCPWalker > &walkers, std::vector< FullPrecRealType > &curData)
compute curData
QMCTraits::FullPrecRealType FullPrecRealType
typedef of FullPrecRealType
Definition: WalkerControl.h:48
std::unique_ptr< std::ofstream > dmcStream
file to save energy histogram
static void determineNewWalkerPopulation(const std::vector< int > &num_per_rank, std::vector< int > &fair_offset, std::vector< int > &minus, std::vector< int > &plus)
creates the distribution plan
bool use_fixed_pop_
if true, use fixed population
std::vector< FullPrecRealType > curData
any temporary data includes many ridiculous conversions of integral types to and from fp ...