QMCPACK
WalkerReconfigurationMPI Struct Reference

Class to handle walker controls with simple global sum. More...

+ Inheritance diagram for WalkerReconfigurationMPI:
+ Collaboration diagram for WalkerReconfigurationMPI:

Public Member Functions

 WalkerReconfigurationMPI (Communicate *c=0)
 default constructor More...
 
int branch (int iter, MCWalkerConfiguration &W, FullPrecRealType trigger) override
 perform branch and swap walkers as required More...
 
int swapWalkers (MCWalkerConfiguration &W)
 return the surviving Walkers More...
 
void sendWalkers (MCWalkerConfiguration &W, const std::vector< IndexType > &plus)
 send the extra walkers to other node More...
 
void recvWalkers (MCWalkerConfiguration &W, const std::vector< IndexType > &minus)
 send the missing walkers from other node More...
 
- Public Member Functions inherited from WalkerControlBase
 WalkerControlBase (Communicate *c)
 default constructor More...
 
virtual ~WalkerControlBase ()
 empty destructor to clean up the derived classes More...
 
void start ()
 start a block More...
 
void setWalkerID (MCWalkerConfiguration &walkers)
 start controller and initialize the IDs of walkers More...
 
void measureProperties (int iter)
 take averages and writes to a file More...
 
void setTrialEnergy (FullPrecRealType et)
 set the trial energy More...
 
FullPrecRealType getValue (int i)
 return a value accumulated during a block More...
 
FullPrecRealType getCurrentValue (int i)
 return a current value More...
 
int doNotBranch (int iter, MCWalkerConfiguration &W)
 legacy: return global population update properties without branching More...
 
int sortWalkers (MCWalkerConfiguration &W)
 legacy: sort Walkers between good and bad and prepare branching More...
 
int applyNmaxNmin (int current_population)
 legacy: apply per rank limit Nmax and Nmin More...
 
int copyWalkers (MCWalkerConfiguration &W)
 legacy: copy good walkers to W More...
 
virtual void reset ()
 reset to accumulate data More...
 
bool put (xmlNodePtr cur)
 
void setMinMax (int nw_in, int nmax_in)
 
int get_n_max () const
 
int get_n_min () const
 
FullPrecRealType get_target_sigma () const
 
MCDataType< FullPrecRealType > & get_ensemble_property ()
 
void set_ensemble_property (MCDataType< FullPrecRealType > &ensemble_property)
 
IndexType get_num_contexts () const
 
IndexType get_method () const
 
void set_method (IndexType method)
 
- Public Member Functions inherited from MPIObjectBase
 MPIObjectBase (Communicate *c)
 constructor with communicator More...
 
int rank () const
 return the rank of the communicator More...
 
int getGroupID () const
 return the group id of the communicator More...
 
CommunicategetCommunicator () const
 return myComm More...
 
CommunicategetCommRef () const
 return a TEMPORARY reference to Communicate More...
 
mpi_comm_type getMPI () const
 return MPI communicator if one wants to use MPI directly More...
 
bool is_manager () const
 return true if the rank == 0 More...
 
const std::string & getName () const
 return the name More...
 
void setName (const std::string &aname)
 

Public Attributes

int TotalWalkers
 total number of walkers More...
 
int FirstWalker
 starting index of the local walkers More...
 
int LastWalker
 ending index of the local walkers More...
 
FullPrecRealType UnitZeta
 random number [0,1) More...
 
FullPrecRealType DeltaStep
 random number [0,1)/number of walkers More...
 
FullPrecRealType nwInv
 1/(total number of walkers) More...
 
std::vector< IndexTypedN
 the number of extra/missing walkers More...
 
std::vector< FullPrecRealTypewConf
 
std::vector< FullPrecRealTypewOffset
 
std::vector< FullPrecRealTypewSum
 

Additional Inherited Members

- Public Types inherited from WalkerControlBase
enum  {
  ENERGY_INDEX = 0, ENERGY_SQ_INDEX, WALKERSIZE_INDEX, WEIGHT_INDEX,
  EREF_INDEX, R2ACCEPTED_INDEX, R2PROPOSED_INDEX, FNSIZE_INDEX,
  RNONESIZE_INDEX, RNSIZE_INDEX, B_ENERGY_INDEX, B_WGT_INDEX,
  SENTWALKERS_INDEX, LE_MAX
}
 An enum to access curData and accumData for reduction. More...
 
using Walker_t = MCWalkerConfiguration::Walker_t
 typedef of Walker_t More...
 
using FullPrecRealType = QMCTraits::FullPrecRealType
 typedef of FullPrecRealType More...
 
using IndexType = QMCTraits::IndexType
 typedef of IndexType More...
 
- Public Types inherited from MPIObjectBase
using mpi_comm_type = Communicate::mpi_comm_type
 
- Static Public Member Functions inherited from WalkerControlBase
static std::vector< IndexTypesyncFutureWalkersPerRank (Communicate *comm, IndexType n_walkers)
 
- Protected Attributes inherited from WalkerControlBase
IndexType method_
 id for the method More...
 
IndexType n_min_
 minimum number of walkers More...
 
IndexType n_max_
 maximum number of walkers More...
 
IndexType MaxCopy
 maximum copy per walker More...
 
IndexType NumWalkers
 current number of walkers per processor More...
 
FullPrecRealType trialEnergy
 trial energy energy More...
 
FullPrecRealType target_sigma_
 target sigma to limit fluctuations of the trial energy More...
 
std::vector< int > NumPerRank
 number of particle per rank More...
 
std::vector< int > OffSet
 offset of the particle index More...
 
std::vector< int > FairOffSet
 offset of the particle index for a fair distribution More...
 
std::filesystem::path dmcFname
 filename for dmc.dat More...
 
std::unique_ptr< std::ofstream > dmcStream
 file to save energy histogram More...
 
IndexType NumWalkersCreated
 Number of walkers created by this rank. More...
 
IndexType MyContext
 context id More...
 
IndexType num_contexts_
 number of contexts More...
 
IndexType SwapMode
 0 is default More...
 
std::vector< FullPrecRealTypeaccumData
 any accumulated data over a block More...
 
std::vector< FullPrecRealTypecurData
 any temporary data includes many ridiculous conversions of integral types to and from fp More...
 
std::vector< std::unique_ptr< Walker_t > > good_w
 temporary storage for good and bad walkers More...
 
std::vector< std::unique_ptr< Walker_t > > bad_w
 
std::vector< int > ncopy_w
 temporary storage for copy counters More...
 
bool use_nonblocking
 Use non-blocking isend/irecv. More...
 
MCDataType< FullPrecRealTypeensemble_property_
 ensemble properties More...
 
- Protected Attributes inherited from MPIObjectBase
CommunicatemyComm
 pointer to Communicate More...
 
std::string ClassName
 class Name More...
 
std::string myName
 name of the object More...
 

Detailed Description

Class to handle walker controls with simple global sum.

Base class to handle serial mode with branching only

Definition at line 26 of file WalkerReconfigurationMPI.h.

Constructor & Destructor Documentation

◆ WalkerReconfigurationMPI()

default constructor

Set the SwapMode to zero so that instantiation can be done

set SwapMode

Definition at line 31 of file WalkerReconfigurationMPI.cpp.

References WalkerControlBase::SwapMode.

32 {
33  SwapMode = 1;
34 }
WalkerControlBase(Communicate *c)
default constructor
IndexType SwapMode
0 is default

Member Function Documentation

◆ branch()

int branch ( int  iter,
MCWalkerConfiguration W,
FullPrecRealType  trigger 
)
overridevirtual

perform branch and swap walkers as required

Reimplemented from WalkerControlBase.

Definition at line 36 of file WalkerReconfigurationMPI.cpp.

References WalkerConfigurations::begin(), WalkerConfigurations::end(), WalkerControlBase::ensemble_property_, WalkerConfigurations::EnsembleProperty, WalkerControlBase::measureProperties(), WalkerReconfigurationMPI::swapWalkers(), and MCDataType< T >::Weight.

37 {
38  int nwkept = swapWalkers(W);
39  measureProperties(iter);
40  W.EnsembleProperty = ensemble_property_;
41  //RealType wgtInv(1.0/curData[WEIGHT_INDEX]);
42  //accumData[ENERGY_INDEX] += curData[ENERGY_INDEX]*wgtInv;
43  //accumData[ENERGY_SQ_INDEX] += curData[ENERGY_SQ_INDEX]*wgtInv;
44  //accumData[WALKERSIZE_INDEX] += nwkept;
45  ////accumData[WALKERSIZE_INDEX] += curData[WALKERSIZE_INDEX];
46  //accumData[WEIGHT_INDEX] += curData[WEIGHT_INDEX];
47  //set Weight and Multiplicity to default values
48  MCWalkerConfiguration::iterator it(W.begin()), it_end(W.end());
49  while (it != it_end)
50  {
51  (*it)->Weight = 1.0;
52  (*it)->Multiplicity = 1.0;
53  ++it;
54  }
55  return nwkept;
56 }
WalkerList_t::iterator iterator
FIX: a type alias of iterator for an object should not be for just one of many objects it holds...
void measureProperties(int iter)
take averages and writes to a file
MCDataType< FullPrecRealType > ensemble_property_
ensemble properties
int swapWalkers(MCWalkerConfiguration &W)
return the surviving Walkers

◆ recvWalkers()

void recvWalkers ( MCWalkerConfiguration W,
const std::vector< IndexType > &  minus 
)

send the missing walkers from other node

Parameters
minuslocal indices of the walkers to be copied

Definition at line 251 of file WalkerReconfigurationMPI.cpp.

References qmcplusplus::abs(), WalkerReconfigurationMPI::dN, MPIObjectBase::myComm, WalkerControlBase::MyContext, WalkerControlBase::num_contexts_, and WalkerControlBase::NumWalkersCreated.

Referenced by WalkerReconfigurationMPI::swapWalkers().

252 {
253  std::vector<IndexType> minusN, plusN;
254  for (int ip = 0; ip < num_contexts_; ip++)
255  {
256  if (dN[ip] > 0)
257  {
258  plusN.insert(plusN.end(), dN[ip], ip);
259  }
260  else if (dN[ip] < 0)
261  {
262  minusN.insert(minusN.end(), -dN[ip], ip);
263  }
264  }
265  int nswap = plusN.size();
266  int last = std::abs(dN[MyContext]) - 1;
267  int ic = 0;
268  while (ic < nswap && last >= 0)
269  {
270  if (minusN[ic] == MyContext)
271  {
272  int im = minus[last];
273  size_t byteSize = W[im]->byteSize();
274  myComm->comm.receive_n(W[im]->DataSet.data(), byteSize, plusN[ic]);
275  W[im]->copyFromBuffer();
276  W[im]->setParentID(W[im]->getWalkerID());
277  W[im]->setWalkerID((++NumWalkersCreated) * num_contexts_ + MyContext);
278  --last;
279  }
280  ++ic;
281  }
282 }
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
Communicate * myComm
pointer to Communicate
Definition: MPIObjectBase.h:62
std::vector< IndexType > dN
the number of extra/missing walkers
IndexType NumWalkersCreated
Number of walkers created by this rank.
IndexType num_contexts_
number of contexts

◆ sendWalkers()

void sendWalkers ( MCWalkerConfiguration W,
const std::vector< IndexType > &  plus 
)

send the extra walkers to other node

Parameters
pluslocal indices of the walkers to be sent

Definition at line 220 of file WalkerReconfigurationMPI.cpp.

References qmcplusplus::abs(), WalkerReconfigurationMPI::dN, MPIObjectBase::myComm, WalkerControlBase::MyContext, and WalkerControlBase::num_contexts_.

Referenced by WalkerReconfigurationMPI::swapWalkers().

221 {
222  std::vector<int> minusN, plusN;
223  for (int ip = 0; ip < num_contexts_; ip++)
224  {
225  if (dN[ip] > 0)
226  {
227  plusN.insert(plusN.end(), dN[ip], ip);
228  }
229  else if (dN[ip] < 0)
230  {
231  minusN.insert(minusN.end(), -dN[ip], ip);
232  }
233  }
234  int nswap = plusN.size();
235  int last = std::abs(dN[MyContext]) - 1;
236  int ic = 0;
237  while (ic < nswap && last >= 0)
238  {
239  if (plusN[ic] == MyContext)
240  {
241  int im = plus[last];
242  size_t byteSize = W[im]->byteSize();
243  W[im]->updateBuffer();
244  myComm->comm.send_n(W[im]->DataSet.data(), byteSize, minusN[ic]);
245  --last;
246  }
247  ++ic;
248  }
249 }
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
Communicate * myComm
pointer to Communicate
Definition: MPIObjectBase.h:62
std::vector< IndexType > dN
the number of extra/missing walkers
IndexType num_contexts_
number of contexts

◆ swapWalkers()

int swapWalkers ( MCWalkerConfiguration W)

return the surviving Walkers

Definition at line 58 of file WalkerReconfigurationMPI.cpp.

References qmcplusplus::abs(), Communicate::allreduce(), Communicate::bcast(), WalkerConfigurations::begin(), WalkerControlBase::curData, WalkerReconfigurationMPI::DeltaStep, WalkerReconfigurationMPI::dN, qmcplusplus::Units::charge::e, WalkerConfigurations::end(), WalkerControlBase::ENERGY_INDEX, WalkerControlBase::ENERGY_SQ_INDEX, WalkerConfigurations::EnsembleProperty, WalkerControlBase::EREF_INDEX, WalkerReconfigurationMPI::FirstWalker, WalkerControlBase::FNSIZE_INDEX, WalkerConfigurations::getActiveWalkers(), WalkerReconfigurationMPI::LastWalker, WalkerControlBase::LE_MAX, qmcplusplus::Units::distance::m, omptarget::min(), MPIObjectBase::myComm, WalkerControlBase::MyContext, WalkerControlBase::ncopy_w, WalkerControlBase::num_contexts_, MCDataType< T >::NumSamples, WalkerControlBase::NumWalkersCreated, WalkerReconfigurationMPI::nwInv, WalkerConfigurations::pop_back(), WalkerControlBase::R2ACCEPTED_INDEX, WalkerControlBase::R2PROPOSED_INDEX, Random, WalkerReconfigurationMPI::recvWalkers(), WalkerControlBase::RNONESIZE_INDEX, WalkerReconfigurationMPI::sendWalkers(), WalkerControlBase::SENTWALKERS_INDEX, WalkerReconfigurationMPI::TotalWalkers, WalkerReconfigurationMPI::UnitZeta, WalkerControlBase::WALKERSIZE_INDEX, WalkerReconfigurationMPI::wConf, MCDataType< T >::Weight, WalkerControlBase::WEIGHT_INDEX, and WalkerReconfigurationMPI::wOffset.

Referenced by WalkerReconfigurationMPI::branch().

59 {
60  //ostringstream o;
61  //o << "check." << MyContext << ".dat";
62  //ofstream fout(o.str().c_str(),std::ios::app);
63  int nw = W.getActiveWalkers();
64  if (TotalWalkers != nw * num_contexts_)
65  {
66  FirstWalker = nw * MyContext;
67  LastWalker = FirstWalker + nw;
69  nwInv = 1.0 / static_cast<FullPrecRealType>(TotalWalkers);
70  ncopy_w.resize(nw);
71  wConf.resize(nw);
72  //wSum.resize(NumContexts);
73  wOffset.resize(num_contexts_ + 1);
74  dN.resize(num_contexts_ + 4);
75  }
76  UnitZeta = Random();
79  //std::fill(wSum.begin(),wSum.end(),0.0);
80  MCWalkerConfiguration::iterator it(W.begin()), it_end(W.end());
81  int iw = 0;
82  FullPrecRealType esum = 0.0, e2sum = 0.0, wtot = 0.0, ecum = 0.0;
83  FullPrecRealType r2_accepted = 0.0, r2_proposed = 0.0;
84  while (it != it_end)
85  {
86  r2_accepted += (*it)->Properties(WP::R2ACCEPTED);
87  r2_proposed += (*it)->Properties(WP::R2PROPOSED);
88  FullPrecRealType wgt((*it)->Weight);
89  FullPrecRealType e((*it)->Properties(WP::LOCALENERGY));
90  esum += wgt * e;
91  e2sum += wgt * e * e;
92  wtot += wgt;
93  ecum += e;
94  wConf[iw++] = wgt;
95  ++it;
96  }
97  //wSum[MyContext]=wtot;
98  curData[ENERGY_INDEX] = esum;
99  curData[ENERGY_SQ_INDEX] = e2sum;
101  curData[WEIGHT_INDEX] = wtot;
102  curData[EREF_INDEX] = ecum;
103  curData[R2ACCEPTED_INDEX] = r2_accepted;
104  curData[R2PROPOSED_INDEX] = r2_proposed;
105  std::fill(curData.begin() + LE_MAX, curData.end(), 0.0);
106  curData[LE_MAX + MyContext] = wtot;
107  //collect everything
109  //update EnsembleProperty
110  W.EnsembleProperty.NumSamples = curData[WALKERSIZE_INDEX];
111  W.EnsembleProperty.Weight = curData[WEIGHT_INDEX];
112  //wOffset[ip] is the partial sum update to ip
113  wOffset[0] = 0;
114  //for(int ip=0; ip<NumContexts; ip++) wOffset[ip+1]=wOffset[ip]+wSum[ip];
115  for (int ip = 0, jp = LE_MAX; ip < num_contexts_; ip++, jp++)
116  wOffset[ip + 1] = wOffset[ip] + curData[jp];
117  wtot = wOffset[num_contexts_]; //wtot is the total weight
118  //find the lower and upper bound of index
119  int minIndex =
120  static_cast<int>((wOffset[MyContext] / wtot - DeltaStep) * static_cast<FullPrecRealType>(TotalWalkers)) - 1;
121  int maxIndex =
122  static_cast<int>((wOffset[MyContext + 1] / wtot - DeltaStep) * static_cast<FullPrecRealType>(TotalWalkers)) + 1;
123  int nb = maxIndex - minIndex + 1;
124  std::vector<FullPrecRealType> Zeta(nb);
125  for (int i = minIndex, ii = 0; i < maxIndex; i++, ii++)
126  {
127  Zeta[ii] = wtot * (DeltaStep + static_cast<FullPrecRealType>(i) * nwInv);
128  }
130  int ind = 0;
131  while (Zeta[ind] < wCur)
132  {
133  ind++;
134  }
135  //surviving walkers
136  int icdiff = 0;
137  for (iw = 0; iw < nw; iw++)
138  {
139  FullPrecRealType tryp = wCur + std::abs(wConf[iw]);
140  int ni = 0;
141  while (Zeta[ind] < tryp && Zeta[ind] >= wCur)
142  {
143  ind++;
144  ni++;
145  }
146  wCur += std::abs(wConf[iw]);
147  if (ni)
148  {
149  icdiff++;
150  }
151  ncopy_w[iw] = ni;
152  }
153  //plus: a list of walkers to be duplicated
154  //minus: a list of walkers to be removed
155  std::vector<int> plus, minus;
156  for (iw = 0; iw < nw; iw++)
157  {
158  int m = ncopy_w[iw];
159  if (m > 1)
160  // add the index of this walker to plus, duplicate m-1 times
161  {
162  plus.insert(plus.end(), m - 1, iw);
163  }
164  else if (m == 0)
165  // add the walker index to be killed/overwritten
166  {
167  minus.push_back(iw);
168  }
169  }
170  //save the number of local walkers to be removed. This will be collected later.
171  int nw_removed = minus.size();
172  //copy within the local node
173  int lower = std::min(plus.size(), minus.size());
174  while (lower > 0)
175  {
176  --lower;
177  int im = minus[lower]; //walker index to be replaced
178  int ip = plus[lower]; //walker index to be duplicated
179  W[im]->makeCopy(*(W[ip])); //copy the walker
180  W[im]->setParentID(W[ip]->getWalkerID());
181  W[im]->setWalkerID((++NumWalkersCreated) * num_contexts_ + MyContext);
182  minus.pop_back(); //remove it
183  plus.pop_back(); //remove it
184  }
185  std::fill(dN.begin(), dN.end(), 0);
186  //dN[ip] extra/missing walkers
187  if (plus.size())
188  {
189  dN[MyContext] = plus.size();
190  }
191  if (minus.size())
192  {
193  dN[MyContext] = -minus.size();
194  }
195  //dN[NumContexts] contains the number of surviving walkers
196  dN[num_contexts_] = icdiff;
197  //other data to compute survival rate and how many walkers are swapped
198  dN[num_contexts_ + 1] = nw_removed;
199  dN[num_contexts_ + 2] = plus.size();
200  dN[num_contexts_ + 3] = minus.size();
201  //collect the data
202  myComm->allreduce(dN);
203  //Each task will send or recv not both.
204  if (plus.size())
205  sendWalkers(W, plus);
206  if (minus.size())
207  recvWalkers(W, minus);
208  //app_log() << "RECONFIGURATION plus= " << dN[NumContexts+2] << " minus= " << dN[NumContexts+3] << std::endl;
209  //app_log() << "RECONFIGURATION ";
210  //for(int i=0; i<NumContexts; ++i) app_log() << dN[i] << " ";
211  //app_log() << std::endl;
212  //record the number of walkers created/destroyed
216  //collect surviving walkers
217  return dN[num_contexts_];
218 }
MakeReturn< UnaryNode< FnFabs, typename CreateLeaf< Vector< T1, C1 > >::Leaf_t > >::Expression_t abs(const Vector< T1, C1 > &l)
FullPrecRealType UnitZeta
random number [0,1)
std::vector< int > ncopy_w
temporary storage for copy counters
#define Random
int LastWalker
ending index of the local walkers
FullPrecRealType DeltaStep
random number [0,1)/number of walkers
T min(T a, T b)
std::vector< FullPrecRealType > wConf
void allreduce(T &)
WalkerList_t::iterator iterator
FIX: a type alias of iterator for an object should not be for just one of many objects it holds...
int FirstWalker
starting index of the local walkers
void recvWalkers(MCWalkerConfiguration &W, const std::vector< IndexType > &minus)
send the missing walkers from other node
Communicate * myComm
pointer to Communicate
Definition: MPIObjectBase.h:62
std::vector< IndexType > dN
the number of extra/missing walkers
std::vector< FullPrecRealType > curData
any temporary data includes many ridiculous conversions of integral types to and from fp ...
IndexType NumWalkersCreated
Number of walkers created by this rank.
IndexType num_contexts_
number of contexts
std::vector< FullPrecRealType > wOffset
void sendWalkers(MCWalkerConfiguration &W, const std::vector< IndexType > &plus)
send the extra walkers to other node
void bcast(T &)
QMCTraits::FullPrecRealType FullPrecRealType
typedef of FullPrecRealType
FullPrecRealType nwInv
1/(total number of walkers)

Member Data Documentation

◆ DeltaStep

FullPrecRealType DeltaStep

random number [0,1)/number of walkers

Definition at line 37 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ dN

std::vector<IndexType> dN

◆ FirstWalker

int FirstWalker

starting index of the local walkers

Definition at line 31 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ LastWalker

int LastWalker

ending index of the local walkers

Definition at line 33 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ nwInv

1/(total number of walkers)

Definition at line 39 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ TotalWalkers

int TotalWalkers

total number of walkers

Definition at line 29 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ UnitZeta

FullPrecRealType UnitZeta

random number [0,1)

Definition at line 35 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ wConf

std::vector<FullPrecRealType> wConf

Definition at line 43 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ wOffset

std::vector<FullPrecRealType> wOffset

Definition at line 45 of file WalkerReconfigurationMPI.h.

Referenced by WalkerReconfigurationMPI::swapWalkers().

◆ wSum

std::vector<FullPrecRealType> wSum

Definition at line 47 of file WalkerReconfigurationMPI.h.


The documentation for this struct was generated from the following files: