Add gmxapi session resources.
authorM. Eric Irrgang <ericirrgang@gmail.com>
Thu, 11 Oct 2018 20:31:00 +0000 (23:31 +0300)
committerM. Eric Irrgang <ericirrgang@gmail.com>
Fri, 12 Oct 2018 20:35:36 +0000 (23:35 +0300)
Allow simulation support code to access resources from the running
session. Provide some abstraction for resources implemented or
managed differently in different execution environments or API contexts.
Examples of resources are input and output data streams or simulation
control signals.

Support gmxapi milestone 10

Refs #2620

Change-Id: Ic0ed02b876e4d7a253ad75b58cd24593d7e68acf

src/api/cpp/include/gmxapi/session/resources.h [new file with mode: 0644]
src/api/cpp/session-impl.h
src/api/cpp/session.cpp
src/api/cpp/sessionresources.h [new file with mode: 0644]
src/api/cpp/tests/restraint.cpp
src/gromacs/restraint/restraintpotential.cpp
src/gromacs/restraint/restraintpotential.h
src/gromacs/restraint/tests/manager.cpp

diff --git a/src/api/cpp/include/gmxapi/session/resources.h b/src/api/cpp/include/gmxapi/session/resources.h
new file mode 100644 (file)
index 0000000..8f2651f
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * 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
index 3fddca00bc592f0611e903958667fb3f0f266389..247d9bb65592306223ffc54615a51f83b833a99b 100644 (file)
 #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
 {
@@ -131,6 +133,32 @@ class SessionImpl
          */
         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.
          *
@@ -155,6 +183,11 @@ class SessionImpl
                     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.
          *
index 087849c6efa39b3db00b842a57b023bee5a86097..9c035924c2a04e7e39c6edde7a528374bdb94874 100644 (file)
 #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
 {
@@ -216,8 +221,16 @@ Status SessionImpl::addRestraint(std::shared_ptr<gmxapi::MDModule> module)
             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;
+                }
             }
         }
     }
@@ -234,6 +247,49 @@ gmx::Mdrunner *SessionImpl::getRunner()
     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)
@@ -318,4 +374,21 @@ std::shared_ptr<Session> launchSession(Context* context, const Workflow &work) n
 
 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
diff --git a/src/api/cpp/sessionresources.h b/src/api/cpp/sessionresources.h
new file mode 100644 (file)
index 0000000..74e8f55
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * 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
index 167a7ddcfff83aa814984d69262974c04eeae695..b44d2981fdc0e031966f16c1073758a362e4b88d 100644 (file)
@@ -76,6 +76,11 @@ class NullRestraint : public gmx::IRestraintPotential
         {
             return {{0, 1}};
         }
+
+        void bindSession(gmxapi::SessionResources* resources) override
+        {
+            (void)resources;
+        }
         //! \endcond
 
         /*!
index 6c5cbd767ec0b2cda0e126dc4960b551524022ad..c6b0f75d54a09d2f061da870d50908aa96abe872 100644 (file)
@@ -54,4 +54,7 @@ void IRestraintPotential::update(gmx::Vector gmx_unused v,
                                  double gmx_unused      t)
 {}
 
+void IRestraintPotential::bindSession(gmxapi::SessionResources* /* resources */)
+{}
+
 } // end namespace gmx
index b3cebfa6d6cde9bb0aee25250071a2cdc98bfcc0..812cfdaff70d9bc6866d4fbe42ffbf85edd3d2d7 100644 (file)
@@ -73,6 +73,7 @@ struct t_pbc;
 namespace gmxapi
 {
 class Session;
+class SessionResources;
 }
 
 namespace gmx
@@ -217,6 +218,18 @@ class IRestraintPotential
          */
         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
index 0f61ff148906d08eed5663fa7da265a9732199fc..f37ba9aa2229dcc7f569ab69a1e0c6e0321677c3 100644 (file)
@@ -64,6 +64,10 @@ class DummyRestraint : public gmx::IRestraintPotential
             return std::vector<int>();
         }
 
+        void bindSession(gmxapi::SessionResources *session) override
+        {
+            (void)session;
+        }
 };
 
 TEST(RestraintManager, restraintList)