1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
|
/*
* This file is part of the GROMACS molecular simulation package.
*
* Copyright 2018- The GROMACS Authors
* and the project initiators Erik Lindahl, Berk Hess and David van der Spoel.
* Consult the AUTHORS/COPYING files and https://www.gromacs.org for details.
*
* 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
* https://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 https://www.gromacs.org.
*
* To help us fund GROMACS development, we humbly ask that you cite
* the research papers on the package. Check out https://www.gromacs.org.
*/
#include "gmxapi/session.h"
#include <functional>
#include <map>
#include <memory>
#include <stdexcept>
#include <utility>
#include "gromacs/mdlib/sighandler.h"
#include "gromacs/mdlib/stophandler.h"
#include "gromacs/mdrun/runner.h"
#include "gromacs/mdrun/simulationcontext.h"
#include "gromacs/mdrunutility/logging.h"
#include "gromacs/restraint/restraintpotential.h"
#include "gromacs/utility/basenetwork.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/init.h"
#include "gmxapi/context.h"
#include "gmxapi/exceptions.h"
#include "gmxapi/md/mdmodule.h"
#include "gmxapi/md/mdsignals.h"
#include "gmxapi/status.h"
#include "createsession.h"
#include "mdsignals.h"
#include "session_impl.h"
#include "sessionresources.h"
namespace gmxapi
{
class ContextImpl;
SignalManager::SignalManager(gmx::StopHandlerBuilder* stopHandlerBuilder) :
state_(std::make_shared<gmx::StopSignal>(gmx::StopSignal::noSignal))
{
/*!
* \brief Signal issuer managed by this object.
*
* Created and bound when the runner is built. Subsequently, client
* stop signals are proxied to the simulation through the session
* resources. The MD internal signal handler calls this functor
* during the MD loop to see if the simulation should be stopped.
* Thus, this should execute within a very small fraction of an MD
* step and not require any synchronization.
*/
auto currentState = state_;
auto stopSignalIssuer = [currentState]() { return *currentState; };
stopHandlerBuilder->registerStopCondition(stopSignalIssuer);
}
//! \cond
SignalManager::~SignalManager() = default;
//! \endcond
bool SessionImpl::isOpen() const noexcept
{
// Currently, an active session is equivalent to an active Mdrunner.
return bool(runner_);
}
Status SessionImpl::close()
{
// Assume unsuccessful until proven otherwise.
auto successful = Status(false);
// When the Session is closed, we need to know that the MD output has been finalized.
runner_.reset();
logFilePtr_.reset();
// \todo provide meaningful result.
// We should be checking that resources were properly shut down, but
// there isn't currently a way to do that.
successful = true;
return successful;
}
Status SessionImpl::run() noexcept
{
// Status is failure until proven otherwise.
auto successful = Status(false);
GMX_ASSERT(runner_, "SessionImpl invariant implies valid Mdrunner handle.");
auto rc = runner_->mdrunner();
if (rc == 0)
{
successful = true;
}
return successful;
}
std::unique_ptr<SessionImpl> SessionImpl::create(std::shared_ptr<ContextImpl> context,
gmx::MdrunnerBuilder&& runnerBuilder,
gmx::SimulationContext&& simulationContext,
gmx::LogFilePtr logFilehandle)
{
// We should be able to get a communicator (or subcommunicator) through the
// Context.
return std::make_unique<SessionImpl>(std::move(context),
std::move(runnerBuilder),
std::move(simulationContext),
std::move(logFilehandle));
}
SessionImpl::SessionImpl(std::shared_ptr<ContextImpl> context,
gmx::MdrunnerBuilder&& runnerBuilder,
gmx::SimulationContext&& simulationContext,
gmx::LogFilePtr fplog) :
context_(std::move(context)),
simulationContext_(std::move(simulationContext)),
logFilePtr_(std::move(fplog))
{
GMX_ASSERT(context_, "SessionImpl invariant implies valid ContextImpl handle.");
// \todo Session objects can have logic specialized for the runtime environment.
auto stopHandlerBuilder = std::make_unique<gmx::StopHandlerBuilder>();
signalManager_ = std::make_unique<SignalManager>(stopHandlerBuilder.get());
GMX_ASSERT(signalManager_, "SessionImpl invariant includes a valid SignalManager.");
runnerBuilder.addStopHandlerBuilder(std::move(stopHandlerBuilder));
runner_ = std::make_unique<gmx::Mdrunner>(runnerBuilder.build());
GMX_ASSERT(runner_, "SessionImpl invariant implies valid Mdrunner handle.");
// For the libgromacs context, a session should explicitly reset global variables that could
// have been set in a previous simulation during the same process.
gmx_reset_stop_condition();
}
std::shared_ptr<Session> createSession(std::shared_ptr<ContextImpl> context,
gmx::MdrunnerBuilder&& runnerBuilder,
gmx::SimulationContext&& simulationContext,
gmx::LogFilePtr logFilehandle)
{
auto newSession = SessionImpl::create(std::move(context),
std::move(runnerBuilder),
std::move(simulationContext),
std::move(logFilehandle));
auto launchedSession = std::make_shared<Session>(std::move(newSession));
return launchedSession;
}
Status SessionImpl::addRestraint(std::shared_ptr<gmxapi::MDModule> module)
{
GMX_ASSERT(runner_, "SessionImpl invariant implies valid Mdrunner handle.");
Status status{ false };
if (module != nullptr)
{
const auto& name = module->name();
if (restraints_.find(name) == restraints_.end())
{
auto restraint = module->getRestraint();
if (restraint != nullptr)
{
restraints_.emplace(name, restraint);
auto sessionResources = createResources(module);
if (!sessionResources)
{
status = false;
}
else
{
runner_->addPotential(restraint, module->name());
status = true;
}
}
}
}
return status;
}
SignalManager* SessionImpl::getSignalManager()
{
SignalManager* ptr = nullptr;
if (isOpen())
{
ptr = signalManager_.get();
}
return ptr;
}
gmx::Mdrunner* SessionImpl::getRunner()
{
gmx::Mdrunner* runner = nullptr;
if (runner_)
{
runner = runner_.get();
}
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 = std::make_unique<SessionResources>(this, name);
resources_.emplace(name, std::move(resourcesInstance));
resources = resources_.at(name).get();
// To do: This should be more dynamic.
getSignalManager()->addSignaller(name);
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)
{
impl_ = std::move(impl);
}
GMX_ASSERT(impl_->isOpen(), "SessionImpl invariant implies valid Mdrunner handle.");
}
Status Session::run() noexcept
{
GMX_ASSERT(impl_, "Session invariant implies valid implementation object handle.");
const Status status = impl_->run();
return status;
}
Status Session::close()
{
GMX_ASSERT(impl_, "Session invariant implies valid implementation object handle.");
auto status = Status(false);
if (isOpen())
{
// \todo catch exceptions when we know what they might be
status = impl_->close();
}
return status;
}
Session::~Session()
{
GMX_ASSERT(impl_, "Session invariant implies valid implementation object handle.");
if (isOpen())
{
try
{
impl_->close();
}
catch (const std::exception&)
{
// \todo find some exception-safe things to do with this via the Context interface.
}
}
}
bool Session::isOpen() const noexcept
{
GMX_ASSERT(impl_, "Session invariant implies valid implementation object handle.");
const auto result = impl_->isOpen();
return result;
}
Status addSessionRestraint(Session* session, std::shared_ptr<gmxapi::MDModule> restraint)
{
auto status = gmxapi::Status(false);
if (session != nullptr && restraint != nullptr)
{
// \todo Improve the external / library API facets
// so the public API does not need to offer raw pointers.
auto sessionImpl = session->getRaw();
GMX_RELEASE_ASSERT(sessionImpl,
"Session invariant implies valid implementation object handle.");
// GMX_ASSERT alone is not strong enough to convince linters not to warn of possible nullptr.
if (sessionImpl)
{
status = sessionImpl->addRestraint(std::move(restraint));
}
}
return status;
}
//! \cond internal
SessionImpl* Session::getRaw() const noexcept
{
return impl_.get();
}
//! \endcond
std::shared_ptr<Session> launchSession(Context* context, const Workflow& work) noexcept
{
auto session = context->launch(work);
return session;
}
SessionImpl::~SessionImpl() = default;
SessionResources::SessionResources(gmxapi::SessionImpl* session, std::string name) :
sessionImpl_(session), name_(std::move(name))
{
}
SessionResources::~SessionResources() = default;
std::string SessionResources::name() const
{
return name_;
}
Signal SessionResources::getMdrunnerSignal(md::signals signal)
{
//// while there is only one choice...
if (signal != md::signals::STOP)
{
throw gmxapi::MissingImplementationError("This signaller only handles stop signals.");
}
// Get a signalling proxy for the caller.
auto signalManager = sessionImpl_->getSignalManager();
if (signalManager == nullptr)
{
throw gmxapi::ProtocolError(
"Client requested access to a signaller that is not available.");
}
auto functor = signalManager->getSignal(name_, signal);
return functor;
}
} // end namespace gmxapi
|