Interface updates allowing client to provide MPI communicator.
[alexxy/gromacs.git] / api / gmxapi / include / multiprocessingresources.cmakein.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2020, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35
36 #ifndef GMXAPI_MULTIPROCESSINGRESOURCES_H
37 #define GMXAPI_MULTIPROCESSINGRESOURCES_H
38
39 /*! \file
40  * \brief Provide build-specific overloads for client-MPI-dependent stuff.
41  *
42  * Define the interfaces that a client must implement to generate MpiContextManager for the library.
43  * Client code should use the gmxapi_mpi.h template header to generate code supporting
44  * the required interface. (Client code should not need to include this header directly.)
45  *
46  * \note This is a generated header. Some definitions are determined when the GROMACS library
47  * build is configured.
48  *
49  * If the library is built with tMPI, CommHandle is empty and offerComm is not defined.
50  *
51  * If the library is built with an MPI library, CommHandle holds a native MPI_Comm object and
52  * the library-provided offerComm is used by the client MultiProcessingResources implementation
53  * to support passing a copy of the communicator.
54  *
55  * Note: The communicator is not `dup`ed in this call. If the library later duplicates
56  * the offered communicator, the library will be responsible for freeing the duplicate.
57  * However, the caller is responsible for keeping any MPI environment valid while the library is
58  * in use. For details, \see MpiContextManager.
59  *
60  * \author "M. Eric Irrgang <ericirrgang@gmail.com>"
61  */
62
63 #include <functional>
64 #include <memory>
65
66 // The interface in this header is determined when the GROMACS library build is configured.
67 #cmakedefine01 GMX_LIB_MPI
68 #if GMX_LIB_MPI
69 #    include <mpi.h>
70 #endif
71
72 namespace gmxapi
73 {
74
75 // Forward declaration for library resources.
76 // CommHandle is opaque to the public API, but allows extensions to implement
77 // required library interfaces by composing the behavior of helpers like offerComm().
78 // The abstraction is important because the library implementations depend on
79 // the options with which GROMACS was built.
80 class CommHandle;
81
82 /*!
83  * \brief Convey the resources that the client has directed the library to use within a Context.
84  *
85  * The object itself does not convey ownership of resources. However, the interfaces
86  * for declaring assigned resources must have well-specified (and documented)
87  * semantics for resource ownership. See assignResource().
88  * (Note: the initial implementation only allows for assignment of an MPI Communicator.)
89  *
90  * The library and client share this interface definition. The implementation is
91  * provided by client code with the aid of a template header.
92  * Client developers should refer instead to gmxapi_mpi.h.
93  */
94 class ResourceAssignment
95 {
96 public:
97     virtual ~ResourceAssignment();
98     [[nodiscard]] virtual int size() const = 0;
99     [[nodiscard]] virtual int rank() const = 0;
100     virtual void              applyCommunicator(class CommHandle* dst) const;
101 };
102
103 /*! \brief Offer the client communicator to the library.
104  *
105  * Helper function allowing clients to provide the MPI communicator for the library.
106  * \param src client-provided communicator.
107  * \param dst library recipient, abstracted to hide library MPI type details.
108  *
109  */
110 template<typename CommT>
111 void offerComm([[maybe_unused]] CommT src, [[maybe_unused]] CommHandle* dst)
112 {
113     // Default overload does nothing.
114 }
115
116 #if GMX_LIB_MPI
117 /*! \brief Offer the client communicator to the library.
118  *
119  * Helper function allowing clients to provide communicator to library.
120  * \param src communicator offered by client
121  * \param dst opaque pointer to library resource destination
122  *
123  * This function is only available in MPI-enabled GROMACS.
124  * Used indirectly in template helpers for client code to implement library interfaces.
125  * Clients should use the assignResource() higher level function in explicit code,
126  * which will evaluable appropriately for the target GROMACS library.
127  *
128  * \todo Provide a stub for docs even when not available.
129  */
130 void offerComm(MPI_Comm src, CommHandle* dst);
131 #endif
132
133 } // end namespace gmxapi
134
135 #endif // GMXAPI_MULTIPROCESSINGRESOURCES_H