QMCPACK
PerParticleHamiltonianLogger.cpp
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) 2022 QMCPACK developers.
6 //
7 // File developed by: Peter W. Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
8 //
9 // File created by: Peter W. Doak, doakpw@ornl.gov, Oak Ridge National Laboratory
10 //////////////////////////////////////////////////////////////////////////////////////
11 
13 #include <algorithm>
14 #include <functional>
16 
17 namespace qmcplusplus
18 {
20  : OperatorEstBase(DataLocality::crowd), rank_estimator_(nullptr), input_(input), rank_(rank)
21 {
22  requires_listener_ = true;
23  my_name_ = "PerParticleHamiltonianLogger";
24 
25  std::string filename("rank_" + std::to_string(rank_) + "_" + input_.get_name() + ".dat");
26  rank_fstream_.open(filename, std::ios::out);
27 }
28 
30  : OperatorEstBase(dl), rank_estimator_(const_cast<PerParticleHamiltonianLogger*>(&pphl)), input_(pphl.input_)
31 {
32  requires_listener_ = true;
33  my_name_ = pphl.name_;
34  data_locality_ = dl;
35 }
36 
37 void PerParticleHamiltonianLogger::write(CrowdLogValues& cl_values, const std::vector<long>& walker_ids)
38 {
39  // fstream is not thread safe but it is buffered. If the buffer isn't too small this
40  // should mostly return quickly and the contention for the lock should be manageable.
41  const std::lock_guard<std::mutex> lock(write_lock);
42  for (auto& [component, values] : cl_values)
43  {
44  rank_fstream_ << "operator: " << component << '\n'; // " crowd: " << crowd_id <<
45  for (int iw = 0; iw < values.size(); ++iw)
46  rank_fstream_ << " walker:" << walker_ids[iw] << " " << NativePrint(values[iw]) << '\n';
47  }
48  if (input_.get_to_stdout())
49  {
50  for (auto& [component, values] : cl_values)
51  {
52  std::cout << component << '\n';
53  for (int iw = 0; iw < values.size(); ++iw)
54  std::cout << " walker: " << walker_ids[iw] << " " << NativePrint(values[iw]) << '\n';
55  }
56  }
57 }
58 
60  const RefVector<ParticleSet>& psets,
61  const RefVector<TrialWaveFunction>& wfns,
62  const RefVector<QMCHamiltonian>& hams,
64 
65 {
66  // The hamiltonian doesn't know the walker ID only its index in the walker elements of the crowd
67  // build mapping from that index to global walker id.
68  // This could change every call for DMC.
69  walker_ids_.clear();
70  for (MCPWalker& walker : walkers)
71  walker_ids_.push_back(walker.getWalkerID());
73 
74  // \todo some per crowd reduction.
75  // clear log values
76 }
77 
78 std::unique_ptr<OperatorEstBase> PerParticleHamiltonianLogger::spawnCrowdClone() const
79 {
80  std::size_t data_size = data_.size();
81  auto spawn_data_locality = data_locality_;
82 
83  auto spawn = std::make_unique<PerParticleHamiltonianLogger>(*this, spawn_data_locality);
84  spawn->get_data().resize(data_size, 0.0);
85  return spawn;
86 }
87 
89 {
90  auto& local_values = values_;
91  return [&local_values](const int walker_index, const std::string& name, const Vector<Real>& inputV) {
92  if (walker_index >= local_values[name].size())
93  local_values[name].resize(walker_index + 1);
94  local_values[name][walker_index] = inputV;
95  };
96 }
97 
98 void PerParticleHamiltonianLogger::collect(const RefVector<OperatorEstBase>& type_erased_operator_estimators)
99 {
100  int crowd_count = 0;
101 }
102 
104 {
105  ListenerVector<Real> listener(name_, getLogger());
107 }
108 
110 {
111  ++block_;
112  rank_fstream_ << "starting block: " << block_ << " steps: " << steps << "\n";
113 }
114 
115 } // namespace qmcplusplus
std::unordered_map< std::string, std::vector< Vector< Real > >> CrowdLogValues
The operator<< functions provide output for data structures that can be used to directly initialize t...
helper functions for EinsplineSetBuilder
Definition: Configuration.h:43
UPtr< OperatorEstBase > spawnCrowdClone() const override
Collection of Local Energy Operators.
An object of this type is a listener expecting a callback to the report function with a vector of val...
Definition: Listener.hpp:38
static void mw_registerLocalEnergyListener(QMCHamiltonian &ham_leader, ListenerVector< RealType > listener)
const char walkers[]
Definition: HDFVersion.h:36
PerParticleHamiltonianLogger *const rank_estimator_
This wrapper is to allow us to leave the user facing operator<< for classes alone.
PerParticleHamiltonianLogger(PerParticleHamiltonianLoggerInput &&input, int rank)
DataLocality data_locality_
locality for accumulation of estimator data.
ListenerVector< Real >::ReportingFunction getLogger()
return lambda function to register as listener the purpose of this function is to factor out the prod...
An abstract class for gridded estimators.
std::vector< std::reference_wrapper< T > > RefVector
std::function< void(const int walker_index, const std::string &name, const Vector< T > &values)> ReportingFunction
"Callback" function type for an operator to report a vector of values to a listener ...
Definition: Listener.hpp:49
DataLocality
data locality with respect to walker buffer
Definition: DataLocality.h:19
void collect(const RefVector< OperatorEstBase > &type_erased_operator_estimators) override
Reduce estimator result data from crowds to rank.
void write(CrowdLogValues &values, const std::vector< long > &walkers_ids)
A container class to represent a walker.
Definition: Walker.h:49
void registerListeners(QMCHamiltonian &ham_leader) override
Register 0-many listeners with a leading QMCHamiltonian instance i.e.
void accumulate(const RefVector< MCPWalker > &walkers, const RefVector< ParticleSet > &psets, const RefVector< TrialWaveFunction > &wfns, const RefVector< QMCHamiltonian > &hams, RandomBase< FullPrecRealType > &rng) override
Accumulate whatever it is you are accumulating with respect to walkers.
std::string my_name_
name of this object – only used for debugging and h5 output