Apply clang-format to source tree
[alexxy/gromacs.git] / src / gromacs / mdrunutility / threadaffinity.h
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014,2015,2016,2017,2018,2019, 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 /*! \libinternal \file
36  * \brief
37  * Declares functions for managing mdrun thread affinity.
38  *
39  * \inlibraryapi
40  * \ingroup module_mdrunutility
41  */
42 #ifndef GMX_MDRUNUTILITY_THREADAFFINITY_H
43 #define GMX_MDRUNUTILITY_THREADAFFINITY_H
44
45 #include <cstdio>
46
47 #include "gromacs/utility/basedefinitions.h"
48
49 struct gmx_hw_opt_t;
50 struct gmx_multisim_t;
51 struct t_commrec;
52
53 namespace gmx
54 {
55
56 class HardwareTopology;
57 class MDLogger;
58 class PhysicalNodeCommunicator;
59
60 class IThreadAffinityAccess
61 {
62 public:
63     virtual bool isThreadAffinitySupported() const        = 0;
64     virtual bool setCurrentThreadAffinityToCore(int core) = 0;
65
66 protected:
67     virtual ~IThreadAffinityAccess();
68 };
69
70 } // namespace gmx
71
72 /*! \brief Communicates within physical nodes to discover the
73  * distribution of threads over ranks. */
74 void analyzeThreadsOnThisNode(const gmx::PhysicalNodeCommunicator& physicalNodeComm,
75                               int                                  numThreadsOnThisRank,
76                               int*                                 numThreadsOnThisNode,
77                               int*                                 intraNodeThreadOffset);
78
79 /*! \brief
80  * Sets the thread affinity using the requested setting stored in hw_opt.
81  *
82  * See analyzeThreadsOnThisNode(), which prepares some of the input.
83  *
84  * \param[out] mdlog                  Logger.
85  * \param[in]  cr                     Communication handler.
86  * \param[in]  hw_opt                 Accesses user choices for thread affinity handling.
87  * \param[in]  hwTop                  Detected hardware topology.
88  * \param[in]  numThreadsOnThisRank   The number of threads on this rank.
89  * \param[in]  numThreadsOnThisNode   The number of threads on all ranks of this node.
90  * \param[in]  intraNodeThreadOffset  The index of the first hardware thread of this rank
91  *   in the set of all the threads of all MPI ranks within a node (ordered by MPI rank ID).
92  * \param[in]  affinityAccess         Interface for low-level access to affinity details.
93  */
94 void gmx_set_thread_affinity(const gmx::MDLogger&         mdlog,
95                              const t_commrec*             cr,
96                              const gmx_hw_opt_t*          hw_opt,
97                              const gmx::HardwareTopology& hwTop,
98                              int                          numThreadsOnThisRank,
99                              int                          numThreadsOnThisNode,
100                              int                          intraNodeThreadOffset,
101                              gmx::IThreadAffinityAccess*  affinityAccess);
102
103 /*! \brief
104  * Checks the process affinity mask and if it is found to be non-zero,
105  * will honor it and disable mdrun internal affinity setting.
106  *
107  * This function should be called first before the OpenMP library gets
108  * initialized with the last argument FALSE (which will detect affinity
109  * set by external tools like taskset), and later, after the OpenMP
110  * initialization, with the last argument TRUE to detect affinity changes
111  * made by the OpenMP library.
112  *
113  * Note that this will only work on Linux as we use a GNU feature.
114  * With bAfterOpenmpInit false, it will also detect whether OpenMP environment
115  * variables for setting the affinity are set.
116  */
117 void gmx_check_thread_affinity_set(const gmx::MDLogger& mdlog,
118                                    gmx_hw_opt_t*        hw_opt,
119                                    int                  ncpus,
120                                    gmx_bool             bAfterOpenmpInit);
121
122 #endif