Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Remove Observable Helper #4540

Draft
wants to merge 6 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/Estimators/EstimatorManagerNew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ bool EstimatorManagerNew::areThereListeners() const
return std::any_of(operator_ests_.begin(), operator_ests_.end(),
[](auto& oper_est) { return oper_est->isListenerRequired(); });
}

template<class EstInputType, typename... Args>
bool EstimatorManagerNew::createEstimator(EstimatorInput& input, Args&&... args)
{
Expand Down Expand Up @@ -373,7 +373,7 @@ void EstimatorManagerNew::reduceOperatorEstimators()
{
operator_data_sizes[iop] = operator_ests_[iop]->get_data().size();
}
// 1 larger because we put the weight in to avoid dependence of the Scalar estimators being reduced firt.
// 1 larger because we put the weight in to avoid dependence of the Scalar estimators being reduced first.
size_t nops = *(std::max_element(operator_data_sizes.begin(), operator_data_sizes.end())) + 1;
std::vector<RealType> operator_send_buffer;
std::vector<RealType> operator_recv_buffer;
Expand Down
18 changes: 9 additions & 9 deletions src/Estimators/InputSection.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
#include <unordered_map>

#include "Configuration.h"
#include "OhmmsData/ParameterSet.h"
#include "Containers/OhmmsPETE/TinyVector.h"

#include "Message/UniformCommunicateError.h"
Expand All @@ -44,12 +43,12 @@ class InputSection
// Becuase it hurts to read all the trailing _ in the constructors of input section subtypes
// NOLINTBEGIN(readability-indentifier-naming)

std::string section_name; // name of the input section
std::string section_name; // name of the input section

std::unordered_set<std::string> attributes; // list of attribute variables
std::unordered_set<std::string> parameters; // list of parameter variables
std::unordered_set<std::string> delegates; // input nodes delegate to next level of input parsing.
std::unordered_set<std::string> required; // list of required variables
std::unordered_set<std::string> attributes; // list of attribute variables
std::unordered_set<std::string> parameters; // list of parameter variables
std::unordered_set<std::string> delegates; // input nodes delegate to next level of input parsing.
std::unordered_set<std::string> required; // list of required variables

std::unordered_set<std::string> strings; // list of string variables that can have one value
std::unordered_set<std::string> multi_strings; // list of string variables that can one or more values
Expand Down Expand Up @@ -146,8 +145,7 @@ class InputSection
* \param[in] tag parmater name or node ename delgation is controlled by
* \param[in] delegate_handler factory function for delegated input function.
*/
void registerDelegate(const std::string& tag,
DelegateHandler delegate_handler);
void registerDelegate(const std::string& tag, DelegateHandler delegate_handler);

/** Do validation for a particular subtype of InputSection
* Called by check_valid.
Expand Down Expand Up @@ -176,7 +174,9 @@ class InputSection
/** Derived class can overrides this to do custom parsing of the element values for Custom elements
* These can have a name attribute only.
*/
[[noreturn]] virtual void setFromStreamCustom(const std::string& ename, const std::string& name, std::istringstream& svalue)
[[noreturn]] virtual void setFromStreamCustom(const std::string& ename,
const std::string& name,
std::istringstream& svalue)
{
throw std::runtime_error("derived class must provide handleCustom method if custom parameters are used");
}
Expand Down
6 changes: 4 additions & 2 deletions src/Estimators/OperatorEstBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,11 +102,13 @@ class OperatorEstBase
virtual std::unique_ptr<OperatorEstBase> spawnCrowdClone() const = 0;

/** Write to previously registered observable_helper hdf5 wrapper.
*
* You need to override this if your estimator does anything more than just write its data to a single h5 descriptor.
*
* if you haven't registered Operator Estimator
* this will do nothing.
* this will/should do nothing.
*/
void write(hdf_archive& file);
virtual void write(hdf_archive& file);

/** zero data appropriately for the DataLocality
*/
Expand Down
93 changes: 65 additions & 28 deletions src/Estimators/SpinDensityNew.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,25 @@ void SpinDensityNew::accumulate(const RefVector<MCPWalker>& walkers,
walkers_weight_ += weight;
int p = 0;
size_t offset = 0;
QMCT::PosType u;
for (int s = 0; s < species_.size(); ++s, offset += dp_.npoints)
for (int ps = 0; ps < species_size_[s]; ++ps, ++p)
{
QMCT::PosType u = lattice_.toUnit(pset.R[p] - dp_.corner);
size_t point = offset;
switch (pset.R.getUnit())
{
case PosUnit::Cartesian:
u = lattice_.toUnit(pset.R[p] - dp_.corner);
break;
case PosUnit::Lattice:
#ifndef NDEBUG
if (!(p < pset.getTotalNum()))
throw std::runtime_error("p < pset.getTotalNum(): " + std::to_string(p) + " " +
std::to_string(pset.getTotalNum()));
#endif
u = pset.R[p] - lattice_.toUnit(dp_.corner);
break;
}
size_t point = offset;
for (int d = 0; d < QMCT::DIM; ++d)
point += dp_.gdims[d] * ((int)(dp_.grid[d] * (u[d] - std::floor(u[d])))); //periodic only
accumulateToData(point, weight);
Expand Down Expand Up @@ -195,41 +209,64 @@ void SpinDensityNew::collect(const RefVector<OperatorEstBase>& type_erased_opera
}
}

void SpinDensityNew::report(const std::string& pad)
void SpinDensityNew::report(const std::string& pad) const { report(pad, app_log()); }

void SpinDensityNew::report(const std::string& pad, std::ostream& out) const
{
auto& dp_ = derived_parameters_;
app_log() << pad << "SpinDensity report" << std::endl;
app_log() << pad << " dim = " << QMCT::DIM << std::endl;
app_log() << pad << " npoints = " << dp_.npoints << std::endl;
app_log() << pad << " grid = " << dp_.grid << std::endl;
app_log() << pad << " gdims = " << dp_.gdims << std::endl;
app_log() << pad << " corner = " << dp_.corner << std::endl;
app_log() << pad << " center = " << dp_.corner + lattice_.Center << std::endl;
app_log() << pad << " cell " << std::endl;
out << pad << "SpinDensity report" << std::endl;
out << pad << " dim = " << QMCT::DIM << std::endl;
out << pad << " npoints = " << dp_.npoints << std::endl;
out << pad << " grid = " << dp_.grid << std::endl;
out << pad << " gdims = " << dp_.gdims << std::endl;
out << pad << " corner = " << dp_.corner << std::endl;
out << pad << " center = " << dp_.corner + lattice_.Center << std::endl;
out << pad << " cell " << std::endl;
for (int d = 0; d < QMCT::DIM; ++d)
app_log() << pad << " " << d << " " << lattice_.Rv[d] << std::endl;
app_log() << pad << " end cell " << std::endl;
app_log() << pad << " nspecies = " << species_.size() << std::endl;
out << pad << " " << d << " " << lattice_.Rv[d] << std::endl;
out << pad << " end cell " << std::endl;
out << pad << " nspecies = " << species_.size() << std::endl;
for (int s = 0; s < species_.size(); ++s)
app_log() << pad << " species[" << s << "]"
<< " = " << species_.speciesName[s] << " " << species_size_[s] << std::endl;
app_log() << pad << "end SpinDensity report" << std::endl;
out << pad << " species[" << s << "]"
<< " = " << species_.speciesName[s] << " " << species_size_[s] << std::endl;
out << pad << "end SpinDensity report" << std::endl;
}

void SpinDensityNew::registerOperatorEstimator(hdf_archive& file)

void SpinDensityNew::registerOperatorEstimator(hdf_archive& file) {}

void SpinDensityNew::write(hdf_archive& file)
{
std::vector<size_t> my_indexes;
auto writeFullPrecData = [&](auto& file, auto& fp_data) {
std::vector<size_t> my_indexes;
std::array<hsize_t, 2> ng{1, derived_parameters_.npoints};
hdf_path hdf_name{my_name_};
file.push(hdf_name);
Vector<QMCT::FullPrecRealType> data;
std::size_t offset = 0;
for (int s = 0; s < species_.size(); ++s)
{
data.attachReference(fp_data.data() + offset, derived_parameters_.npoints);
hid_t g_id = file.push(species_.speciesName[s], true);
file.append(data, "value", append_index_);
offset += derived_parameters_.npoints;
file.pop();
}
++append_index_;
file.pop();
};

std::vector<int> ng(1, derived_parameters_.npoints);
#ifdef MIXED_PRECISION
std::vector<QMCT::FullPrecRealType> expanded_data(data_.size(), 0.0);
std::copy_n(data_.begin(), data_.size(), expanded_data.begin());
Comment on lines +260 to +261
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we do the cast during construction of expanded_data?

Suggested change
std::vector<QMCT::FullPrecRealType> expanded_data(data_.size(), 0.0);
std::copy_n(data_.begin(), data_.size(), expanded_data.begin());
std::vector<QMCT::FullPrecRealType> expanded_data(data_.cbegin(), data_.cend());

assert(!data_.empty());
writeFullPrecData(file, expanded_data);
// auto total = std::accumulate(data_->begin(), data_->end(), 0.0);
// std::cout << "data size: " << data_->size() << " : " << total << '\n';

hdf_path hdf_name{my_name_};
for (int s = 0; s < species_.size(); ++s)
{
h5desc_.emplace_back(hdf_name / species_.speciesName[s]);
auto& oh = h5desc_.back();
oh.set_dimensions(ng, 0);
}
#else
writeFullPrecData(file, data_);
#endif
}


} // namespace qmcplusplus
10 changes: 9 additions & 1 deletion src/Estimators/SpinDensityNew.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class SpinDensityNew : public OperatorEstBase
*/
void registerOperatorEstimator(hdf_archive& file) override;

/** Custom write method since spin density new has its own hdf5 format
*/
void write(hdf_archive& file) override;

private:
SpinDensityNew(const SpinDensityNew& sdn) = default;

Expand All @@ -118,7 +122,8 @@ class SpinDensityNew : public OperatorEstBase
size_t getFullDataSize();
void accumulateToData(size_t point, QMCT::RealType weight);
void reset();
void report(const std::string& pad);
void report(const std::string& pad) const;
void report(const std::string& pad, std::ostream& out) const;

//data members
const SpinDensityInput input_;
Expand All @@ -138,6 +143,9 @@ class SpinDensityNew : public OperatorEstBase
SpinDensityInput::DerivedParameters derived_parameters_;
/**}@*/

/// current index for h5d_append
hsize_t append_index_ = 0;

friend class testing::SpinDensityNewTests;
};

Expand Down
Loading