ESPResSo
Extensible Simulation Package for Research on Soft Matter Systems
Loading...
Searching...
No Matches
core/communication.hpp
Go to the documentation of this file.
1/*
2 * Copyright (C) 2010-2022 The ESPResSo project
3 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009,2010
4 * Max-Planck-Institute for Polymer Research, Theory Group
5 *
6 * This file is part of ESPResSo.
7 *
8 * ESPResSo is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * ESPResSo is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21#ifndef CORE_COMMUNICATION_HPP
22#define CORE_COMMUNICATION_HPP
23/** \file
24 * This file contains the asynchronous MPI communication.
25 *
26 * It is the header file for communication.cpp.
27 *
28 * The asynchronous MPI communication is used during the script
29 * evaluation. Except for the head node that interprets the interface
30 * script, all other nodes wait in @ref mpi_loop() for the head node to
31 * issue an action using @ref mpi_call(). @ref mpi_loop() immediately
32 * executes an @c MPI_Bcast and therefore waits for the head node to
33 * broadcast a command, which is done by @ref mpi_call(). The request
34 * consists of a callback function with an arbitrary number of arguments.
35 *
36 * To add new actions (e.g. to implement new interface functionality), do the
37 * following:
38 * - write the @c mpi_* function that is executed on the head node
39 * - write the @c mpi_*_local function that is executed on worker nodes
40 * - register the local function with one of the @c REGISTER_CALLBACK macros
41 *
42 * After this, your procedure is free to do anything. However, it has
43 * to be in (MPI) sync with what your new @c mpi_*_local does. This
44 * procedure is called immediately after the broadcast.
45 */
46
47#include "MpiCallbacks.hpp"
48
49#include <utils/Vector.hpp>
50
51#include <boost/mpi/communicator.hpp>
52#include <boost/mpi/environment.hpp>
53
54#include <memory>
55#include <utility>
56
57/** The number of this node. */
58extern int this_node;
59/** The communicator */
60extern boost::mpi::communicator comm_cart;
61
63 boost::mpi::communicator &comm;
65 /** @brief The MPI rank. */
67 /** @brief The MPI world size. */
68 int size;
69
71 void init_comm_cart();
73 /** @brief Calculate the node index in the Cartesian topology. */
75 /** @brief Set new Cartesian topology. */
76 void set_node_grid(Utils::Vector3i const &value);
77};
78
80
81namespace Communication {
82/**
83 * @brief Returns a reference to the global callback class instance.
84 */
85MpiCallbacks &mpiCallbacks();
86std::shared_ptr<MpiCallbacks> mpiCallbacksHandle();
87} // namespace Communication
88
89/**************************************************
90 * for every procedure requesting a MPI negotiation,
91 * a callback exists which processes this request on
92 * the worker nodes. It is denoted by *_local.
93 **************************************************/
94
95/** Initialize MPI. */
96std::shared_ptr<boost::mpi::environment> mpi_init(int argc = 0,
97 char **argv = nullptr);
98
99/** @brief Call a local function.
100 * @tparam Args Local function argument types
101 * @tparam ArgRef Local function argument types
102 * @param fp Local function
103 * @param args Local function arguments
104 */
105template <class... Args, class... ArgRef>
106void mpi_call(void (*fp)(Args...), ArgRef &&...args) {
107 Communication::mpiCallbacks().call(fp, std::forward<ArgRef>(args)...);
108}
109
110/** @brief Call a local function.
111 * @tparam Args Local function argument types
112 * @tparam ArgRef Local function argument types
113 * @param fp Local function
114 * @param args Local function arguments
115 */
116template <class... Args, class... ArgRef>
117void mpi_call_all(void (*fp)(Args...), ArgRef &&...args) {
118 Communication::mpiCallbacks().call_all(fp, std::forward<ArgRef>(args)...);
119}
120
121/** Process requests from head node. Worker nodes main loop. */
122void mpi_loop();
123
124namespace Communication {
125/**
126 * @brief Init globals for communication.
127 *
128 * @param mpi_env MPI environment that should be used
129 */
130void init(std::shared_ptr<boost::mpi::environment> mpi_env);
131void deinit();
132} // namespace Communication
133
135 std::shared_ptr<boost::mpi::environment> m_mpi_env;
136 MpiContainerUnitTest(int argc, char **argv) {
137 m_mpi_env = mpi_init(argc, argv);
139 }
141};
142#endif
Communication::MpiCallbacks manages MPI communication using a visitor pattern.
Vector implementation and trait types for boost qvm interoperability.
auto call_all(void(*fp)(Args...), ArgRef &&...args) const -> std::enable_if_t< std::is_void_v< decltype(fp(args...))> >
Call a callback on all nodes.
void mpi_loop()
Process requests from head node.
Communicator communicator
void mpi_call(void(*fp)(Args...), ArgRef &&...args)
Call a local function.
void mpi_call_all(void(*fp)(Args...), ArgRef &&...args)
Call a local function.
boost::mpi::communicator comm_cart
The communicator.
std::shared_ptr< boost::mpi::environment > mpi_init(int argc=0, char **argv=nullptr)
Initialize MPI.
int this_node
The number of this node.
std::shared_ptr< MpiCallbacks > mpiCallbacksHandle()
void init(std::shared_ptr< boost::mpi::environment > mpi_env)
Init globals for communication.
MpiCallbacks & mpiCallbacks()
Returns a reference to the global callback class instance.
boost::mpi::communicator & comm
void full_initialization()
int size
The MPI world size.
int & this_node
The MPI rank.
void init_comm_cart()
Utils::Vector3i calc_node_index() const
Calculate the node index in the Cartesian topology.
Utils::Vector3i node_grid
void set_node_grid(Utils::Vector3i const &value)
Set new Cartesian topology.
MpiContainerUnitTest(int argc, char **argv)
std::shared_ptr< boost::mpi::environment > m_mpi_env