ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
LocalContext.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020-2022 The ESPResSo project
3 *
4 * This file is part of ESPResSo.
5 *
6 * ESPResSo is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * ESPResSo is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20#pragma once
21
22#include "Context.hpp"
23#include "ObjectHandle.hpp"
25
26#include <utils/Factory.hpp>
27
28#include <boost/mpi/communicator.hpp>
29
30#include <cassert>
31#include <memory>
32#include <stdexcept>
33#include <string>
34#include <string_view>
35#include <utility>
36
37namespace ScriptInterface {
38
39/**
40 * @brief Trivial context.
41 *
42 * This context just maintains a local copy of an object.
43 */
44class LocalContext : public Context {
46 bool m_is_head_node;
47 boost::mpi::communicator const &m_comm;
48 ParallelExceptionHandler m_parallel_exception_handler;
49
50public:
52 boost::mpi::communicator const &comm)
53 : m_factory(std::move(factory)), m_is_head_node(comm.rank() == 0),
54 m_comm(comm),
55 // NOLINTNEXTLINE(bugprone-throw-keyword-missing)
56 m_parallel_exception_handler(comm) {}
57
58 const Utils::Factory<ObjectHandle> &factory() const { return m_factory; }
59
60 void notify_call_method(const ObjectHandle *, std::string const &,
61 VariantMap const &) override {}
62 void notify_set_parameter(const ObjectHandle *, std::string const &,
63 Variant const &) override {}
64
65 std::shared_ptr<ObjectHandle>
66 make_shared(std::string const &name, const VariantMap &parameters) override {
67 auto sp = m_factory.make(name);
68 set_context(sp.get());
69
70 sp->construct(parameters);
71
72 return sp;
73 }
74
75 std::string_view name(const ObjectHandle *o) const override {
76 assert(o);
77
78 return factory().type_name(*o);
79 }
80
81 bool is_head_node() const override { return m_is_head_node; }
82 void parallel_try_catch(std::function<void()> const &cb) const override {
83 m_parallel_exception_handler.parallel_try_catch<std::exception>(cb);
84 }
85 boost::mpi::communicator const &get_comm() const override { return m_comm; }
86};
87} // namespace ScriptInterface
ScriptInterface::Context decorates ScriptInterface::ObjectHandle objects with a context: a creation p...
Context of an object handle.
Definition Context.hpp:53
void set_context(ObjectHandle *o)
Set the context of an object to this.
Definition Context.hpp:95
std::shared_ptr< ObjectHandle > make_shared(std::string const &name, const VariantMap &parameters) override
boost::mpi::communicator const & get_comm() const override
bool is_head_node() const override
void notify_call_method(const ObjectHandle *, std::string const &, VariantMap const &) override
LocalContext(Utils::Factory< ObjectHandle > factory, boost::mpi::communicator const &comm)
void parallel_try_catch(std::function< void()> const &cb) const override
std::string_view name(const ObjectHandle *o) const override
const Utils::Factory< ObjectHandle > & factory() const
void notify_set_parameter(const ObjectHandle *, std::string const &, Variant const &) override
Base class for interface handles.
Handle exceptions thrown in MPI parallel code.
void parallel_try_catch(std::function< void()> const &callback) const
Handle exceptions in synchronous code.
Factory template.
Definition Factory.hpp:78
pointer_type make(const std::string &name) const
Construct an instance by name.
Definition Factory.hpp:89
cudaStream_t stream[1]
CUDA streams for parallel computing on CPU and GPU.
std::unordered_map< std::string, Variant > VariantMap
Definition Variant.hpp:133
STL namespace.
Recursive variant implementation.
Definition Variant.hpp:84