Split lines with many copyright years
[alexxy/gromacs.git] / src / gromacs / utility / gmxomp.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
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.
9  *
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.
14  *
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.
19  *
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.
24  *
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.
32  *
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.
35  */
36 /*! \internal \file
37  * \brief
38  * Implements functions from gmxomp.h.
39  *
40  * \ingroup module_utility
41  */
42 #include "gmxpre.h"
43
44 #include "gmxomp.h"
45
46 #include "config.h"
47
48 #include <cstdio>
49 #include <cstdlib>
50
51 #if GMX_OPENMP
52 #    include <omp.h>
53 #endif
54
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"
61
62 int gmx_omp_get_max_threads()
63 {
64 #if GMX_OPENMP
65     return omp_get_max_threads();
66 #else
67     return 1;
68 #endif
69 }
70
71 int gmx_omp_get_num_procs()
72 {
73 #if GMX_OPENMP
74     return omp_get_num_procs();
75 #else
76     return 1;
77 #endif
78 }
79
80 int gmx_omp_get_thread_num()
81 {
82 #if GMX_OPENMP
83     return omp_get_thread_num();
84 #else
85     return 0;
86 #endif
87 }
88
89 void gmx_omp_set_num_threads(int num_threads)
90 {
91 #if GMX_OPENMP
92     omp_set_num_threads(num_threads);
93 #else
94     GMX_UNUSED_VALUE(num_threads);
95 #endif
96 }
97
98 gmx_bool gmx_omp_check_thread_affinity(char** message)
99 {
100     bool shouldSetAffinity = true;
101
102     *message = nullptr;
103 #if GMX_OPENMP
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;
109     try
110     {
111         programName = gmx::getProgramContext().displayName();
112     }
113     GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
114
115     const char* const gomp_env            = getenv("GOMP_CPU_AFFINITY");
116     const bool        bGompCpuAffinitySet = (gomp_env != nullptr);
117
118     /* turn off internal pinning if GOMP_CPU_AFFINITY is set & non-empty */
119     if (bGompCpuAffinitySet && *gomp_env != '\0')
120     {
121         try
122         {
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());
130         }
131         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
132         shouldSetAffinity = false;
133     }
134 #    endif /* __GNUC__ || __INTEL_COMPILER */
135
136 #    if defined(__INTEL_COMPILER)
137     const char* const kmp_env         = getenv("KMP_AFFINITY");
138     const bool        bKmpAffinitySet = (kmp_env != NULL);
139
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))
143     {
144         try
145         {
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());
153         }
154         GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR;
155         shouldSetAffinity = false;
156     }
157 #    endif /* __INTEL_COMPILER */
158
159 #endif /* GMX_OPENMP */
160     return shouldSetAffinity;
161 }