Skip to content
Merged
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
1 change: 1 addition & 0 deletions cxx/include/pluginplay/module/module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@
#include <pluginplay/module/macros.hpp>
#include <pluginplay/module/module_base.hpp>
#include <pluginplay/module/module_class.hpp>
#include <pluginplay/module/recorder.hpp>
37 changes: 36 additions & 1 deletion cxx/include/pluginplay/module/module_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,23 @@ class ModuleBase {
// Is this a Python module?
bool is_python() const { return m_is_python_; }

/** @brief May one instance of this module's class stand in for all of
* them?
*
* A ModuleManager keeps one instance of each (C++) module class, and
* uses it for every key the class is added under, which assumes that
* instances of the class are interchangeable. That is not so for modules
* which are configured when they are made (e.g., Recorder, whose property
* type is chosen then) or which are implemented in Python. Such modules
* are not shareable, and a ModuleManager keeps each instance it is given.
*
* @return False if this module called not_shareable in its ctor, or is
* implemented in Python, and true otherwise.
*
* @throw none No throw guarantee.
*/
bool is_shareable() const noexcept { return m_shareable_; }

/** @brief The developer's call graph request.
*
* Drivers configure the modules below them with a CallGraphRequest,
Expand Down Expand Up @@ -462,6 +479,18 @@ class ModuleBase {
*/
void citation(type::description value);

/** @brief Declares that instances of the derived class are not
* interchangeable.
*
* Derived classes whose instances are configured when they are made
* (e.g., with a property type chosen at runtime) must call this in their
* ctor, so that a ModuleManager keeps each instance it is given (see
* is_shareable).
*
* @throw none No throw guarantee.
*/
void not_shareable() noexcept { m_shareable_ = false; }

/** @brief Adds an additional input field to this module.
*
* A module automatically has all of the input fields associated with any
Expand Down Expand Up @@ -651,6 +680,9 @@ class ModuleBase {

/// Is this module implemented in Python?
bool m_is_python_ = false;

/// May one instance of this module's class stand in for all of them?
bool m_shareable_ = true;
}; // class ModuleBase

//--------------------------------Implementations-------------------------------
Expand All @@ -666,6 +698,7 @@ inline bool ModuleBase::operator==(const ModuleBase& rhs) const noexcept {
if(has_description() && get_desc() != rhs.get_desc()) return false;
if(m_citations_ != rhs.m_citations_) return false;
if(m_is_python_ != rhs.m_is_python_) return false;
if(m_shareable_ != rhs.m_shareable_) return false;
return std::tie(inputs(), results(), submods(), property_types(),
python_property_types()) ==
std::tie(rhs.inputs(), rhs.results(), rhs.submods(),
Expand All @@ -686,7 +719,9 @@ ModuleBase::ModuleBase(DerivedType*) noexcept :
}

inline ModuleBase::ModuleBase(const python::PythonWrapper& self, bool) :
m_type_(type::rtti(typeid(python::PythonWrapper))), m_is_python_(true) {}
m_type_(type::rtti(typeid(python::PythonWrapper))),
m_is_python_(true),
m_shareable_(false) {}

inline void ModuleBase::description(type::description value) noexcept {
m_desc_.emplace(std::move(value));
Expand Down
132 changes: 132 additions & 0 deletions cxx/include/pluginplay/module/recorder.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* Copyright 2026 NWChemEx-Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#pragma once
#include <pluginplay/module/module_base.hpp>
#include <pluginplay/types.hpp>
#include <typeinfo>
#include <vector>

namespace pluginplay {

/** @brief A module which records the results of the module it wraps.
*
* A Recorder satisfies one property type, which is chosen when it is made,
* and has one submodule, "Wrapped", of the same property type. When run, it
* forwards its inputs, as they are, to "Wrapped", keeps the results, and
* returns them. This is how results which are only used inside a
* calculation are gotten out of it: put the Recorder between the module which
* computes them and the module which uses them.
*
* From C++, for a property type `PT`:
*
* ```cpp
* auto recorder = std::make_shared<pluginplay::Recorder>(PT{});
* mm.add_module("Recorder", recorder);
* mm.change_submod("Recorder", "Wrapped", "Computes PT");
* mm.change_submod("Uses PT", "PT submodule", "Recorder");
* ```
*
* and from Python, `recorder = pluginplay.Recorder(PT())`. After the
* calculation, `recorder->records()` holds the results of each run.
*
* Recorders are not shareable (see ModuleBase::is_shareable), so a
* ModuleManager can hold several, e.g., one per property type, and each one
* keeps its own records. Copies of a Recorder's module (e.g., by
* ModuleManager::copy_module) share its records, though.
*
* N.B. A run whose results are memoized does not call the Recorder, so it is
* not recorded. To record every run, turn off the memoization of the
* Recorder (or of "Wrapped").
*/
class Recorder : public ModuleBase {
public:
/// Type of the RTTI of a property type
using rtti_type = type::rtti;

/// Type of the inputs of a property type
using input_map = type::input_map;

/// Type of the results of a property type
using result_map = type::result_map;

/// Type of the submodules of a module
using submodule_map = type::submodule_map;

/// Type of the container holding the results of each run
using record_container = std::vector<result_map>;

/** @brief Makes a Recorder satisfying the property type of @p pt.
*
* @tparam PropertyType The property type the Recorder satisfies.
*
* @param[in] pt An instance of @p PropertyType. Only its type is used.
*
* @throw std::bad_alloc if there is insufficient memory. Strong throw
* guarantee.
*/
template<typename PropertyType>
explicit Recorder(const PropertyType& /*pt*/) :
Recorder(rtti_type(typeid(PropertyType)),
to_map_<input_map>(PropertyType::inputs()),
to_map_<result_map>(PropertyType::results())) {}

/** @brief Makes a Recorder satisfying the property type described by its
* RTTI, inputs, and results.
*
* This is how a Recorder is made when the property type is only known at
* runtime, e.g., from Python.
*
* @param[in] rtti The RTTI of the property type.
* @param[in] inputs The inputs of the property type.
* @param[in] results The results of the property type.
*
* @throw std::bad_alloc if there is insufficient memory. Strong throw
* guarantee.
*/
Recorder(rtti_type rtti, input_map inputs, result_map results);

/** @brief The results of each run, in the order of the runs.
*
* @return The results recorded so far.
*
* @throw None No throw guarantee.
*/
const record_container& records() const noexcept { return m_records_; }

/** @brief Forgets the results recorded so far.
*
* @throw None No throw guarantee.
*/
void clear() noexcept { m_records_.clear(); }

private:
/// The fields of a property type, @p fields, as a map of type MapType
template<typename MapType, typename FieldsType>
static MapType to_map_(const FieldsType& fields) {
MapType rv;
rv.insert(fields.begin(), fields.end());
return rv;
}

/// Runs "Wrapped" with @p inputs and records its results
result_map run_(input_map inputs, submodule_map submods) const override;

/// The results of each run. Mutable, since run_ is const.
mutable record_container m_records_;
};

} // namespace pluginplay
2 changes: 2 additions & 0 deletions cxx/src/pluginplay/module/export_module.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ namespace pluginplay {

void export_module_base(py_module_reference m);
void export_module_class(py_module_reference m);
void export_recorder(py_module_reference m);

inline void export_module(py_module_reference m) {
export_module_base(m);
export_module_class(m);
export_recorder(m);
}

} // namespace pluginplay
1 change: 1 addition & 0 deletions cxx/src/pluginplay/module/export_module_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void export_module_base(py_module_reference m) {
.def("submods", static_cast<submod_fxn>(&ModuleBase::submods))
.def("property_types", &ModuleBase::property_types)
.def("python_property_types", &ModuleBase::python_property_types)
.def("is_shareable", &ModuleBase::is_shareable)
.def("get_desc", &ModuleBase::get_desc)
.def("citations", &ModuleBase::citations)
.def("get_runtime", &ModuleBase::get_runtime)
Expand Down
45 changes: 45 additions & 0 deletions cxx/src/pluginplay/module/export_recorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2026 NWChemEx-Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include "export_module.hpp"
#include <memory>
#include <pluginplay/module/recorder.hpp>
#include <pluginplay/python/py_type_info.hpp>
#include <utility>

namespace pluginplay {
namespace {

/// Makes a Recorder satisfying the property type of the Python object @p pt
auto make_recorder(py::object pt) {
auto rtti = pt.attr("type")().cast<python::PyTypeInfo>().value();
auto inputs = pt.attr("inputs")().cast<type::input_map>();
auto results = pt.attr("results")().cast<type::result_map>();
return std::make_shared<Recorder>(std::move(rtti), std::move(inputs),
std::move(results));
}

} // namespace

void export_recorder(py_module_reference m) {
using pointer_type = std::shared_ptr<Recorder>;
py_class_type<Recorder, ModuleBase, pointer_type>(m, "Recorder")
.def(py::init(&make_recorder))
.def("records", &Recorder::records)
.def("clear", &Recorder::clear);
}

} // namespace pluginplay
52 changes: 52 additions & 0 deletions cxx/src/pluginplay/module/recorder.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* Copyright 2026 NWChemEx-Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

#include <pluginplay/module/recorder.hpp>
#include <utility>

namespace pluginplay {
namespace {

const auto desc = R"(
Recorder
--------

Runs the "Wrapped" submodule, records the results it computes, and returns
them. The inputs are forwarded to "Wrapped" as they are.
)";

} // namespace

Recorder::Recorder(rtti_type rtti, input_map inputs, result_map results) :
ModuleBase(this) {
// The property type is chosen at runtime, so Recorders are not
// interchangeable
not_shareable();
description(desc);
satisfies_property_type(rtti, inputs, std::move(results));
submods()["Wrapped"]
.set_type(std::move(rtti), std::move(inputs))
.set_description("The module whose results are recorded");
}

Recorder::result_map Recorder::run_(input_map inputs,
submodule_map submods) const {
auto rv = submods.at("Wrapped").value().run(std::move(inputs));
m_records_.push_back(rv);
return rv;
}

} // namespace pluginplay
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ struct ModuleManagerPIMPL {
/// Type of a pointer to the cache
using cache_pointer = module_manager_type::cache_pointer;

/// Type of a map from key to Python implementation
// TODO: remove when a more elegant solution is determined
using py_base_map = std::map<type::key, module_base_ptr>;
/// Type of a map from key to an implementation which is not shareable
using unshared_base_map = std::map<type::key, module_base_ptr>;

///@}

Expand Down Expand Up @@ -269,10 +268,10 @@ struct ModuleManagerPIMPL {
// by the user
module_map m_modules;

// Part of the hacky patch to make multiple python modules work
// TODO: remove when a more elegant solution is determined
// These are the Python Modules in their developer state
py_base_map m_py_bases;
// These are the Modules which are not shareable (see
// ModuleBase::is_shareable), e.g., Python modules, in their developer
// state. One per key, since their instances are not interchangeable.
unshared_base_map m_unshared_bases;

// These are the loaded plugins, in load order. Each plugin's ModuleMap
// holds the plugin's modules in the state set by the user.
Expand Down
19 changes: 9 additions & 10 deletions cxx/src/pluginplay/module_manager/detail_/module_manager_pimpl.ipp
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,11 @@ inline void ModuleManagerPIMPL::add_module(type::key key,
throw std::invalid_argument("Module implementation for key: '" + key +
"' is null");

// Instances of a shareable class are interchangeable, so one instance is
// used for every key the class is added under. Others are kept as given.
module_base_ptr stored = base;
if(base->is_python()) {
// This is a hacky patch to allow multiple python modules to be
// added while avoiding the type_index collisions.
// TODO: remove when a more elegant solution is determined
m_py_bases[key] = base;
if(!base->is_shareable()) {
m_unshared_bases[key] = base;
} else {
std::type_index type(base->type());
if(!m_bases.count(type)) m_bases[type] = base;
Expand Down Expand Up @@ -202,11 +201,11 @@ inline bool ModuleManagerPIMPL::operator==(
if(m_modules.size() != rhs.m_modules.size()) return false;
if(m_plugins.size() != rhs.m_plugins.size()) return false;

// TODO: Remove with the rest of the python hack
if(m_py_bases.size() != rhs.m_py_bases.size()) return false;
for(const auto& [k, v] : rhs.m_py_bases) {
if(!m_py_bases.count(k)) return false;
if(*m_py_bases.at(k) != *v) return false;
// Instances which are not shareable may differ, so compare them
if(m_unshared_bases.size() != rhs.m_unshared_bases.size()) return false;
for(const auto& [k, v] : rhs.m_unshared_bases) {
if(!m_unshared_bases.count(k)) return false;
if(*m_unshared_bases.at(k) != *v) return false;
}

// Skip checking the values b/c implementations are compared by type
Expand Down
Loading
Loading