QMCPACK
PerParticleHamiltonianLogger Class Reference
+ Inheritance diagram for PerParticleHamiltonianLogger:
+ Collaboration diagram for PerParticleHamiltonianLogger:

Public Types

using Real = QMCTraits::RealType
 
using CrowdLogValues = std::unordered_map< std::string, std::vector< Vector< Real > >>
 
- Public Types inherited from OperatorEstBase
using QMCT = QMCTraits
 
using FullPrecRealType = QMCT::FullPrecRealType
 
using MCPWalker = Walker< QMCTraits, PtclOnLatticeTraits >
 
using Data = std::vector< QMCT::RealType >
 

Public Member Functions

 PerParticleHamiltonianLogger (PerParticleHamiltonianLoggerInput &&input, int rank)
 
 PerParticleHamiltonianLogger (const PerParticleHamiltonianLogger &other, DataLocality data_locality)
 
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. More...
 
UPtr< OperatorEstBasespawnCrowdClone () const override
 
void startBlock (int steps) override
 
void registerListeners (QMCHamiltonian &ham_leader) override
 Register 0-many listeners with a leading QMCHamiltonian instance i.e. More...
 
ListenerVector< Real >::ReportingFunction getLogger ()
 return lambda function to register as listener the purpose of this function is to factor out the production of the lambda for unit testing More...
 
void collect (const RefVector< OperatorEstBase > &type_erased_operator_estimators) override
 Reduce estimator result data from crowds to rank. More...
 
void write (CrowdLogValues &values, const std::vector< long > &walkers_ids)
 
int get_block ()
 
- Public Member Functions inherited from OperatorEstBase
 OperatorEstBase (DataLocality dl)
 constructor More...
 
 OperatorEstBase (const OperatorEstBase &oth)
 Shallow copy constructor! This alows us to keep the default copy constructors for derived classes which is quite useful to the spawnCrowdClone design. More...
 
virtual ~OperatorEstBase ()=default
 virtual destructor More...
 
virtual void normalize (QMCT::RealType invToWgt)
 
std::vector< QMCT::RealType > & get_data ()
 
virtual void registerOperatorEstimator (hdf_archive &file)
 
void write (hdf_archive &file)
 Write to previously registered observable_helper hdf5 wrapper. More...
 
void zero ()
 zero data appropriately for the DataLocality More...
 
QMCT::FullPrecRealType get_walkers_weight () const
 Return the total walker weight for this block. More...
 
const std::string & get_my_name () const
 
bool isListenerRequired ()
 
DataLocality get_data_locality () const
 

Private Attributes

bool crowd_clone = false
 
PerParticleHamiltonianLogger *const rank_estimator_
 
PerParticleHamiltonianLoggerInput input_
 
int rank_
 
CrowdLogValues values_
 
std::vector< long > walker_ids_
 
const std::string name_ {"PerParticleHamiltonianLogger"}
 
std::fstream rank_fstream_
 
std::mutex write_lock
 
int block_ = 0
 

Additional Inherited Members

- Protected Attributes inherited from OperatorEstBase
DataLocality data_locality_
 locality for accumulation of estimator data. More...
 
std::string my_name_
 name of this object – only used for debugging and h5 output More...
 
QMCT::FullPrecRealType walkers_weight_
 
std::vector< ObservableHelperh5desc_
 
Data data_
 
bool requires_listener_ = false
 

Detailed Description

Definition at line 28 of file PerParticleHamiltonianLogger.h.

Member Typedef Documentation

◆ CrowdLogValues

using CrowdLogValues = std::unordered_map<std::string, std::vector<Vector<Real> >>

Definition at line 32 of file PerParticleHamiltonianLogger.h.

◆ Real

Definition at line 31 of file PerParticleHamiltonianLogger.h.

Constructor & Destructor Documentation

◆ PerParticleHamiltonianLogger() [1/2]

Definition at line 19 of file PerParticleHamiltonianLogger.cpp.

References qmcplusplus::crowd, PerParticleHamiltonianLoggerInput::get_name(), PerParticleHamiltonianLogger::input_, OperatorEstBase::my_name_, qmcplusplus::rank, PerParticleHamiltonianLogger::rank_, PerParticleHamiltonianLogger::rank_fstream_, and OperatorEstBase::requires_listener_.

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 }
PerParticleHamiltonianLogger *const rank_estimator_
OperatorEstBase(DataLocality dl)
constructor
testing::ValidSpinDensityInput input
std::string my_name_
name of this object – only used for debugging and h5 output

◆ PerParticleHamiltonianLogger() [2/2]

Definition at line 29 of file PerParticleHamiltonianLogger.cpp.

References OperatorEstBase::data_locality_, OperatorEstBase::my_name_, PerParticleHamiltonianLogger::name_, and OperatorEstBase::requires_listener_.

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 }
PerParticleHamiltonianLogger *const rank_estimator_
DataLocality data_locality_
locality for accumulation of estimator data.
OperatorEstBase(DataLocality dl)
constructor
std::string my_name_
name of this object – only used for debugging and h5 output

Member Function Documentation

◆ accumulate()

void accumulate ( const RefVector< MCPWalker > &  walkers,
const RefVector< ParticleSet > &  psets,
const RefVector< TrialWaveFunction > &  wfns,
const RefVector< QMCHamiltonian > &  hams,
RandomBase< FullPrecRealType > &  rng 
)
overridevirtual

Accumulate whatever it is you are accumulating with respect to walkers.

This method is assumed to be called from the crowd context It provides parallelism with respect to computational effort of the estimator without causing a global sync. Depending on data locality the accumlation of the result may be different from the single thread write directly into the OperatorEstimator data.

Parameters
[in]walkers
[in,out]pset_targetcrowd scope target pset (should be returned to starting state after call)
[in]psetsper walker psets
[in]wnfsper walker TrialWaveFunction
[in,out]rngcrowd scope RandomGenerator

Implements OperatorEstBase.

Definition at line 59 of file PerParticleHamiltonianLogger.cpp.

References PerParticleHamiltonianLogger::rank_estimator_, PerParticleHamiltonianLogger::values_, qmcplusplus::walker, PerParticleHamiltonianLogger::walker_ids_, qmcplusplus::hdf::walkers, and PerParticleHamiltonianLogger::write().

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 }
const char walkers[]
Definition: HDFVersion.h:36
PerParticleHamiltonianLogger *const rank_estimator_
void write(CrowdLogValues &values, const std::vector< long > &walkers_ids)
Walker< QMCTraits, PtclOnLatticeTraits > MCPWalker

◆ collect()

void collect ( const RefVector< OperatorEstBase > &  oebs)
overridevirtual

Reduce estimator result data from crowds to rank.

This is assumed to be called from only from one thread per crowds->rank reduction. Implied is this is during a global sync or there is a guarantee that the crowd operator estimators accumulation data is not being written to.

There could be concurrent operations inside the scope of the collect call.

Reimplemented from OperatorEstBase.

Definition at line 98 of file PerParticleHamiltonianLogger.cpp.

Referenced by qmcplusplus::TEST_CASE().

99 {
100  int crowd_count = 0;
101 }

◆ get_block()

int get_block ( )
inline

◆ getLogger()

ListenerVector< QMCTraits::RealType >::ReportingFunction getLogger ( )

return lambda function to register as listener the purpose of this function is to factor out the production of the lambda for unit testing

Parameters
[out]values

Definition at line 88 of file PerParticleHamiltonianLogger.cpp.

References PerParticleHamiltonianLogger::values_.

Referenced by PerParticleHamiltonianLogger::registerListeners().

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 }

◆ registerListeners()

void registerListeners ( QMCHamiltonian ham_leader)
overridevirtual

Register 0-many listeners with a leading QMCHamiltonian instance i.e.

a QMCHamiltonian that has acquired the crowd scope QMCHamiltonianMultiWalkerResource. This must be called for each crowd scope estimator that listens to register listeners into the crowd scope QMCHamiltonianMultiWalkerResource.

Many estimators don't need per particle values so the default implementation is no op.

Reimplemented from OperatorEstBase.

Definition at line 103 of file PerParticleHamiltonianLogger.cpp.

References PerParticleHamiltonianLogger::getLogger(), QMCHamiltonian::mw_registerLocalEnergyListener(), and PerParticleHamiltonianLogger::name_.

104 {
105  ListenerVector<Real> listener(name_, getLogger());
107 }
static void mw_registerLocalEnergyListener(QMCHamiltonian &ham_leader, ListenerVector< RealType > listener)
ListenerVector< Real >::ReportingFunction getLogger()
return lambda function to register as listener the purpose of this function is to factor out the prod...

◆ spawnCrowdClone()

std::unique_ptr< OperatorEstBase > spawnCrowdClone ( ) const
overridevirtual

Implements OperatorEstBase.

Definition at line 78 of file PerParticleHamiltonianLogger.cpp.

References OperatorEstBase::data_, and OperatorEstBase::data_locality_.

Referenced by qmcplusplus::TEST_CASE().

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 }
DataLocality data_locality_
locality for accumulation of estimator data.

◆ startBlock()

void startBlock ( int  steps)
overridevirtual

◆ write()

void write ( CrowdLogValues values,
const std::vector< long > &  walkers_ids 
)

Definition at line 37 of file PerParticleHamiltonianLogger.cpp.

References PerParticleHamiltonianLoggerInput::get_to_stdout(), PerParticleHamiltonianLogger::input_, PerParticleHamiltonianLogger::rank_fstream_, and PerParticleHamiltonianLogger::write_lock.

Referenced by PerParticleHamiltonianLogger::accumulate().

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 }

Member Data Documentation

◆ block_

◆ crowd_clone

bool crowd_clone = false
private

Definition at line 60 of file PerParticleHamiltonianLogger.h.

◆ input_

◆ name_

const std::string name_ {"PerParticleHamiltonianLogger"}
private

◆ rank_

int rank_
private

◆ rank_estimator_

PerParticleHamiltonianLogger* const rank_estimator_
private

◆ rank_fstream_

◆ values_

◆ walker_ids_

std::vector<long> walker_ids_
private

◆ write_lock

std::mutex write_lock
private

Definition at line 68 of file PerParticleHamiltonianLogger.h.

Referenced by PerParticleHamiltonianLogger::write().


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