--- /dev/null
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2018, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+
+#ifndef GMXAPI_SESSION_RESOURCES_H
+#define GMXAPI_SESSION_RESOURCES_H
+
+/*! \file
+ * \brief Define interface to Session Resources for active (running) gmxapi operations.
+ */
+
+namespace gmxapi
+{
+
+/*!
+ * \brief Handle to Session-provided resources.
+ *
+ * Session handle for workflow elements requiring resources provided through the Session.
+ *
+ * Provided during launch through gmx::IRestraintPotential::bindSession()
+ *
+ * No public interface yet. Use accompanying free functions.
+ * \see gmxapi::getMdrunnerSignal()
+ */
+class SessionResources;
+
+} // end namespace gmxapi
+
+#endif //GMXAPI_SESSION_RESOURCES_H
#include "gromacs/mdrun/simulationcontext.h"
#include "gmxapi/context.h"
-#include "gmxapi/status.h"
+#include "gmxapi/md.h"
#include "gmxapi/md/mdmodule.h"
+#include "gmxapi/session/resources.h"
+#include "gmxapi/status.h"
namespace gmxapi
{
*/
Status addRestraint(std::shared_ptr<gmxapi::MDModule> module);
+ /*!
+ * \brief Get a handle to the resources for the named session operation.
+ *
+ * \param name unique name of element in workflow
+ * \return temporary access to the resources.
+ *
+ * If called on a non-const Session, creates the resource if it does not yet exist.
+ * If called on a const Session,
+ * returns nullptr if the resource does not exist.
+ */
+ gmxapi::SessionResources* getResources(const std::string &name) const noexcept;
+
+ /*!
+ * \brief Create SessionResources for a module and bind the module.
+ *
+ * Adds a new managed resources object to the Session for the uniquely named module.
+ * Allows the module to bind to the SignalManager and to the resources object.
+ *
+ * \param module
+ * \return non-owning pointer to created resources or nullptr for error.
+ *
+ * If the named module is already registered, calling createResources again is considered an
+ * error and nullptr is returned.
+ */
+ gmxapi::SessionResources* createResources(std::shared_ptr<gmxapi::MDModule> module) noexcept;
+
/*! \internal
* \brief API implementation function to retrieve the current runner.
*
gmx_multisim_t * multiSim);
private:
+ /*!
+ * \brief Manage session resources for named workflow elements.
+ */
+ std::map< std::string, std::unique_ptr<SessionResources> > resources_;
+
/*!
* \brief Extend the life of the owning context.
*
#include "gromacs/mdlib/sighandler.h"
#include "gromacs/mdrun/logging.h"
#include "gromacs/mdrun/multisim.h"
+#include "gromacs/restraint/restraintpotential.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/basenetwork.h"
#include "gromacs/utility/init.h"
+#include "gmxapi/context.h"
+#include "gmxapi/exceptions.h"
#include "gmxapi/session.h"
#include "gmxapi/status.h"
+#include "gmxapi/md/mdmodule.h"
#include "session-impl.h"
+#include "sessionresources.h"
namespace gmxapi
{
if (restraint != nullptr)
{
restraints_.emplace(std::make_pair(name, restraint));
- runner_->addPotential(restraint, module->name());
- status = true;
+ auto sessionResources = createResources(module);
+ if (!sessionResources)
+ {
+ status = false;
+ }
+ else
+ {
+ runner_->addPotential(restraint, module->name());
+ status = true;
+ }
}
}
}
return runner;
}
+gmxapi::SessionResources *SessionImpl::getResources(const std::string &name) const noexcept
+{
+ gmxapi::SessionResources * resources = nullptr;
+ try
+ {
+ resources = resources_.at(name).get();
+ }
+ catch (const std::out_of_range &e)
+ {
+ // named operation does not have any resources registered.
+ }
+
+ return resources;
+}
+
+gmxapi::SessionResources *SessionImpl::createResources(std::shared_ptr<gmxapi::MDModule> module) noexcept
+{
+ // check if resources already exist for this module
+ // If not, create resources and return handle.
+ // Return nullptr for any failure.
+ gmxapi::SessionResources * resources = nullptr;
+ const auto &name = module->name();
+ if (resources_.find(name) == resources_.end())
+ {
+ auto resourcesInstance = gmx::compat::make_unique<SessionResources>(this, name);
+ resources_.emplace(std::make_pair(name, std::move(resourcesInstance)));
+ resources = resources_.at(name).get();
+
+ std::shared_ptr<gmx::IRestraintPotential> restraint = nullptr;
+ if (restraints_.find(name) != restraints_.end())
+ {
+ auto restraintRef = restraints_.at(name);
+ auto restraint = restraintRef.lock();
+ if (restraint)
+ {
+ restraint->bindSession(resources);
+ }
+ }
+ }
+ ;
+ return resources;
+}
+
Session::Session(std::unique_ptr<SessionImpl> impl) noexcept
{
if (impl != nullptr)
SessionImpl::~SessionImpl() = default;
+SessionResources::SessionResources(gmxapi::SessionImpl *session,
+ std::string name) :
+ sessionImpl_(session),
+ name_(std::move(name))
+{
+ // Avoid compiler warning.
+ // TODO: remove void cast when sessionImpl_ is used in future functionality.
+ (void)sessionImpl_;
+}
+
+SessionResources::~SessionResources() = default;
+
+const std::string SessionResources::name() const
+{
+ return name_;
+}
+
} // end namespace gmxapi
--- /dev/null
+/*
+ * This file is part of the GROMACS molecular simulation package.
+ *
+ * Copyright (c) 2018, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
+ */
+
+#ifndef GMXAPI_SESSION_RESOURCES_IMPL_H
+#define GMXAPI_SESSION_RESOURCES_IMPL_H
+
+/*! \file
+ * \brief Implementation details for SessionResources infrastructure.
+ *
+ * Define the library interface for classes with opaque external interfaces.
+ *
+ * These are specifically details of the gmx Mdrunner session implementation.
+ *
+ * \author M. Eric Irrgang <ericirrgang@gmail.com>
+ * \ingroup gmxapi
+ */
+
+#include "gmxapi/session/resources.h"
+
+#include <string>
+
+#include "gmxapi/session.h"
+
+namespace gmxapi
+{
+
+/*!
+ * \brief Consumer-specific access to Session resources.
+ *
+ * Each element of work that is managed by a Session and which may need access to Session resources
+ * is uniquely identified. SessionResources objects allow client code to be identified by the
+ * Session so that appropriate resources can be acquired when needed.
+ *
+ * Resources are configured at Session launch by SessionImpl::createResources()
+ *
+ * \ingroup gmxapi
+ */
+class SessionResources final
+{
+ public:
+ /*!
+ * \brief Construct a resources object for the named operation.
+ *
+ * \param session implementation object backing these resources.
+ * \param name Unique name of workflow operation.
+ */
+ SessionResources(SessionImpl* session, std::string name);
+
+ /*!
+ * \brief no default constructor.
+ *
+ * \see SessionResources(SessionImpl* session, std::string name)
+ */
+ SessionResources() = delete;
+
+ ///@{
+ /*!
+ * \brief Not moveable or copyable.
+ *
+ * Objects of this type should only exist in their Session container.
+ * If necessary, ownership can be transferred by owning through a unique_ptr handle.
+ */
+ SessionResources(const SessionResources&) = delete;
+ SessionResources &operator=(const SessionResources &) = delete;
+ SessionResources(SessionResources &&) = delete;
+ SessionResources &operator=(SessionResources &&) = delete;
+ ///@}
+
+ ~SessionResources();
+
+ /*!
+ * \brief Get the name of the gmxapi operation for which these resources exist.
+ *
+ * \return workflow element name
+ */
+ const std::string name() const;
+
+ private:
+ /*!
+ * \brief pointer to the session owning these resources
+ */
+ SessionImpl* sessionImpl_ = nullptr;
+
+ /*!
+ * \brief name of the associated gmxapi operation
+ */
+ std::string name_;
+};
+
+} // end namespace gmxapi
+
+#endif //GMXAPI_SESSION_RESOURCES_IMPL_H
{
return {{0, 1}};
}
+
+ void bindSession(gmxapi::SessionResources* resources) override
+ {
+ (void)resources;
+ }
//! \endcond
/*!
double gmx_unused t)
{}
+void IRestraintPotential::bindSession(gmxapi::SessionResources* /* resources */)
+{}
+
} // end namespace gmx
namespace gmxapi
{
class Session;
+class SessionResources;
}
namespace gmx
*/
virtual std::vector<int> sites() const = 0;
+ /*!
+ * \brief Allow Session-mediated interaction with other resources or workflow elements.
+ *
+ * \param resources temporary access to the resources provided by the session for additional configuration.
+ *
+ * A module implements this method to receive a handle to resources configured for this particular workflow
+ * element.
+ *
+ * \internal
+ * \todo This should be more general than the RestraintPotential interface.
+ */
+ virtual void bindSession(gmxapi::SessionResources* resources);
};
} // end namespace gmx
return std::vector<int>();
}
+ void bindSession(gmxapi::SessionResources *session) override
+ {
+ (void)session;
+ }
};
TEST(RestraintManager, restraintList)