2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2012,2013,2014,2016,2017 by the GROMACS development team.
5 * Copyright (c) 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.
38 * Implements functions from gmxomp.h.
40 * \ingroup module_utility
55 #include "gromacs/utility/basedefinitions.h"
56 #include "gromacs/utility/cstringutil.h"
57 #include "gromacs/utility/exceptions.h"
58 #include "gromacs/utility/fatalerror.h"
59 #include "gromacs/utility/programcontext.h"
60 #include "gromacs/utility/stringutil.h"
62 int gmx_omp_get_max_threads()
65 return omp_get_max_threads();
71 int gmx_omp_get_num_procs()
74 return omp_get_num_procs();
80 int gmx_omp_get_thread_num()
83 return omp_get_thread_num();
89 void gmx_omp_set_num_threads(int num_threads
)
92 omp_set_num_threads(num_threads
);
94 GMX_UNUSED_VALUE(num_threads
);
98 gmx_bool
gmx_omp_check_thread_affinity(char** message
)
100 bool shouldSetAffinity
= true;
104 /* We assume that the affinity setting is available on all platforms
105 * gcc supports. Even if this is not the case (e.g. Mac OS) the user
106 * will only get a warning. */
107 # if defined(__GNUC__) || defined(__INTEL_COMPILER)
108 const char* programName
;
111 programName
= gmx::getProgramContext().displayName();
113 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
115 const char* const gomp_env
= getenv("GOMP_CPU_AFFINITY");
116 const bool bGompCpuAffinitySet
= (gomp_env
!= nullptr);
118 /* turn off internal pinning if GOMP_CPU_AFFINITY is set & non-empty */
119 if (bGompCpuAffinitySet
&& *gomp_env
!= '\0')
123 std::string buf
= gmx::formatString(
124 "NOTE: GOMP_CPU_AFFINITY set, will turn off %s internal affinity\n"
125 " setting as the two can conflict and cause performance degradation.\n"
126 " To keep using the %s internal affinity setting, unset the\n"
127 " GOMP_CPU_AFFINITY environment variable.",
128 programName
, programName
);
129 *message
= gmx_strdup(buf
.c_str());
131 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
132 shouldSetAffinity
= false;
134 # endif /* __GNUC__ || __INTEL_COMPILER */
136 # if defined(__INTEL_COMPILER)
137 const char* const kmp_env
= getenv("KMP_AFFINITY");
138 const bool bKmpAffinitySet
= (kmp_env
!= NULL
);
140 // turn off internal pinning if KMP_AFFINITY is set but does not contain
141 // the settings 'disabled' or 'none'.
142 if (bKmpAffinitySet
&& (strstr(kmp_env
, "disabled") == NULL
) && (strstr(kmp_env
, "none") == NULL
))
146 std::string buf
= gmx::formatString(
147 "NOTE: KMP_AFFINITY set, will turn off %s internal affinity\n"
148 " setting as the two can conflict and cause performance degradation.\n"
149 " To keep using the %s internal affinity setting, unset the\n"
150 " KMP_AFFINITY environment variable or set it to 'none' or 'disabled'.",
151 programName
, programName
);
152 *message
= gmx_strdup(buf
.c_str());
154 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
;
155 shouldSetAffinity
= false;
157 # endif /* __INTEL_COMPILER */
159 #endif /* GMX_OPENMP */
160 return shouldSetAffinity
;