Split lines with many copyright years
[gromacs.git] / src / gromacs / mdrunutility / threadaffinity.h
blob1983c58ce5ad728ba7f95f397b5e028e5b526f17
1 /*
2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2015,2016 by the GROMACS development team.
5 * Copyright (c) 2017,2018,2019,2020, by the GROMACS development team, led by
6 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
7 * and including many others, as listed in the AUTHORS file in the
8 * top-level source directory and at http://www.gromacs.org.
10 * GROMACS is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public License
12 * as published by the Free Software Foundation; either version 2.1
13 * of the License, or (at your option) any later version.
15 * GROMACS is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with GROMACS; if not, see
22 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
23 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * If you want to redistribute modifications to GROMACS, please
26 * consider that scientific software is very special. Version
27 * control is crucial - bugs must be traceable. We will be happy to
28 * consider code for inclusion in the official distribution, but
29 * derived work must not be called official GROMACS. Details are found
30 * in the README & COPYING files - if they are missing, get the
31 * official version at http://www.gromacs.org.
33 * To help us fund GROMACS development, we humbly ask that you cite
34 * the research papers on the package. Check out http://www.gromacs.org.
36 /*! \libinternal \file
37 * \brief
38 * Declares functions for managing mdrun thread affinity.
40 * \inlibraryapi
41 * \ingroup module_mdrunutility
43 #ifndef GMX_MDRUNUTILITY_THREADAFFINITY_H
44 #define GMX_MDRUNUTILITY_THREADAFFINITY_H
46 #include <cstdio>
48 #include "gromacs/utility/basedefinitions.h"
50 struct gmx_hw_opt_t;
51 struct gmx_multisim_t;
52 struct t_commrec;
54 namespace gmx
57 class HardwareTopology;
58 class MDLogger;
59 class PhysicalNodeCommunicator;
61 class IThreadAffinityAccess
63 public:
64 virtual bool isThreadAffinitySupported() const = 0;
65 virtual bool setCurrentThreadAffinityToCore(int core) = 0;
67 protected:
68 virtual ~IThreadAffinityAccess();
71 } // namespace gmx
73 /*! \brief Communicates within physical nodes to discover the
74 * distribution of threads over ranks. */
75 void analyzeThreadsOnThisNode(const gmx::PhysicalNodeCommunicator& physicalNodeComm,
76 int numThreadsOnThisRank,
77 int* numThreadsOnThisNode,
78 int* intraNodeThreadOffset);
80 /*! \brief
81 * Sets the thread affinity using the requested setting stored in hw_opt.
83 * See analyzeThreadsOnThisNode(), which prepares some of the input.
85 * \param[out] mdlog Logger.
86 * \param[in] cr Communication handler.
87 * \param[in] hw_opt Accesses user choices for thread affinity handling.
88 * \param[in] hwTop Detected hardware topology.
89 * \param[in] numThreadsOnThisRank The number of threads on this rank.
90 * \param[in] numThreadsOnThisNode The number of threads on all ranks of this node.
91 * \param[in] intraNodeThreadOffset The index of the first hardware thread of this rank
92 * in the set of all the threads of all MPI ranks within a node (ordered by MPI rank ID).
93 * \param[in] affinityAccess Interface for low-level access to affinity details.
95 void gmx_set_thread_affinity(const gmx::MDLogger& mdlog,
96 const t_commrec* cr,
97 const gmx_hw_opt_t* hw_opt,
98 const gmx::HardwareTopology& hwTop,
99 int numThreadsOnThisRank,
100 int numThreadsOnThisNode,
101 int intraNodeThreadOffset,
102 gmx::IThreadAffinityAccess* affinityAccess);
104 /*! \brief
105 * Checks the process affinity mask and if it is found to be non-zero,
106 * will honor it and disable mdrun internal affinity setting.
108 * This function should be called first before the OpenMP library gets
109 * initialized with the last argument FALSE (which will detect affinity
110 * set by external tools like taskset), and later, after the OpenMP
111 * initialization, with the last argument TRUE to detect affinity changes
112 * made by the OpenMP library.
114 * Note that this will only work on Linux as we use a GNU feature.
115 * With bAfterOpenmpInit false, it will also detect whether OpenMP environment
116 * variables for setting the affinity are set.
118 void gmx_check_thread_affinity_set(const gmx::MDLogger& mdlog,
119 gmx_hw_opt_t* hw_opt,
120 int ncpus,
121 gmx_bool bAfterOpenmpInit);
123 #endif