Remove some utility -> legacyheaders dependencies
[alexxy/gromacs.git] / src / gromacs / gmxlib / gmx_omp_nthreads.c
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2012,2013,2014, 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 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <assert.h>
40
41 #ifdef HAVE_CONFIG_H
42 #include <config.h>
43 #endif
44
45 #include "gromacs/utility/fatalerror.h"
46 #include "typedefs.h"
47 #include "types/commrec.h"
48 #include "macros.h"
49 #include "network.h"
50 #include "copyrite.h"
51 #include "gmx_omp_nthreads.h"
52 #include "md_logging.h"
53
54 #include "gromacs/utility/gmxomp.h"
55
56 /** Structure with the number of threads for each OpenMP multi-threaded
57  *  algorithmic module in mdrun. */
58 typedef struct
59 {
60     int      gnth;          /**< Global num. of threads per PP or PP+PME process/tMPI thread. */
61     int      gnth_pme;      /**< Global num. of threads per PME only process/tMPI thread. */
62
63     int      nth[emntNR];   /**< Number of threads for each module, indexed with module_nth_t */
64     gmx_bool initialized;   /**< TRUE if the module as been initialized. */
65 } omp_module_nthreads_t;
66
67 /** Names of environment variables to set the per module number of threads.
68  *
69  *  Indexed with the values of module_nth_t.
70  * */
71 static const char *modth_env_var[emntNR] =
72 {
73     "GMX_DEFAULT_NUM_THREADS should never be set",
74     "GMX_DOMDEC_NUM_THREADS", "GMX_PAIRSEARCH_NUM_THREADS",
75     "GMX_NONBONDED_NUM_THREADS", "GMX_BONDED_NUM_THREADS",
76     "GMX_PME_NUM_THREADS", "GMX_UPDATE_NUM_THREADS",
77     "GMX_VSITE_NUM_THREADS",
78     "GMX_LINCS_NUM_THREADS", "GMX_SETTLE_NUM_THREADS"
79 };
80
81 /** Names of the modules. */
82 static const char *mod_name[emntNR] =
83 {
84     "default", "domain decomposition", "pair search", "non-bonded",
85     "bonded", "PME", "update", "LINCS", "SETTLE"
86 };
87
88 /** Number of threads for each algorithmic module.
89  *
90  *  File-scope global variable that gets set once in pick_module_nthreads()
91  *  and queried via gmx_omp_nthreads_get().
92  *
93  *  All fields are initialized to 0 which should result in errors if
94  *  the init call is omitted.
95  * */
96 static omp_module_nthreads_t modth = { 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0}, FALSE};
97
98
99 /** Determine the number of threads for module \p mod.
100  *
101  *  \p m takes values form the module_nth_t enum and maps these to the
102  *  corresponding value in modth_env_var.
103  *
104  *  Each number of threads per module takes the default value unless
105  *  GMX_*_NUM_THERADS env var is set, case in which its value overrides
106  *  the deafult.
107  *
108  *  The "group" scheme supports OpenMP only in PME and in thise case all but
109  *  the PME nthread values default to 1.
110  */
111 static void pick_module_nthreads(FILE *fplog, int m,
112                                  gmx_bool bSimMaster,
113                                  gmx_bool bFullOmpSupport,
114                                  gmx_bool bSepPME)
115 {
116     char    *env;
117     int      nth;
118     char     sbuf[STRLEN];
119     gmx_bool bOMP;
120
121 #ifdef GMX_OPENMP
122     bOMP = TRUE;
123 #else
124     bOMP = FALSE;
125 #endif /* GMX_OPENMP */
126
127     /* The default should never be set through a GMX_*_NUM_THREADS env var
128      * as it's always equal with gnth. */
129     if (m == emntDefault)
130     {
131         return;
132     }
133
134     /* check the environment variable */
135     if ((env = getenv(modth_env_var[m])) != NULL)
136     {
137         sscanf(env, "%d", &nth);
138
139         if (!bOMP)
140         {
141             gmx_warning("%s=%d is set, but %s is compiled without OpenMP!",
142                         modth_env_var[m], nth, ShortProgram());
143         }
144
145         /* with the verlet codepath, when any GMX_*_NUM_THREADS env var is set,
146          * OMP_NUM_THREADS also has to be set */
147         if (bFullOmpSupport && getenv("OMP_NUM_THREADS") == NULL)
148         {
149             gmx_fatal(FARGS, "%s=%d is set, the default number of threads also "
150                       "needs to be set with OMP_NUM_THREADS!",
151                       modth_env_var[m], nth);
152         }
153
154         /* with the group scheme warn if any env var except PME is set */
155         if (!bFullOmpSupport)
156         {
157             if (m != emntPME)
158             {
159                 gmx_warning("%s=%d is set, but OpenMP multithreading is not "
160                             "supported in %s!",
161                             modth_env_var[m], nth, mod_name[m]);
162                 nth = 1;
163             }
164         }
165
166         /* only babble if we are really overriding with a different value */
167         if ((bSepPME && m == emntPME && nth != modth.gnth_pme) || (nth != modth.gnth))
168         {
169             sprintf(sbuf, "%s=%d set, overriding the default number of %s threads",
170                     modth_env_var[m], nth, mod_name[m]);
171             if (bSimMaster)
172             {
173                 fprintf(stderr, "\n%s\n", sbuf);
174             }
175             if (fplog)
176             {
177                 fprintf(fplog, "%s\n", sbuf);
178             }
179         }
180     }
181     else
182     {
183         /* pick the global PME node nthreads if we are setting the number
184          * of threads in separate PME nodes  */
185         nth = (bSepPME && m == emntPME) ? modth.gnth_pme : modth.gnth;
186     }
187
188     gmx_omp_nthreads_set(m, nth);
189 }
190
191 void gmx_omp_nthreads_read_env(int     *nthreads_omp,
192                                gmx_bool bIsSimMaster)
193 {
194     char    *env;
195     gmx_bool bCommandLineSetNthreadsOMP = *nthreads_omp > 0;
196     char     buffer[STRLEN];
197
198     assert(nthreads_omp);
199
200     if ((env = getenv("OMP_NUM_THREADS")) != NULL)
201     {
202         int nt_omp;
203
204         sscanf(env, "%d", &nt_omp);
205         if (nt_omp <= 0)
206         {
207             gmx_fatal(FARGS, "OMP_NUM_THREADS is invalid: '%s'", env);
208         }
209
210         if (bCommandLineSetNthreadsOMP && nt_omp != *nthreads_omp)
211         {
212             gmx_fatal(FARGS, "Environment variable OMP_NUM_THREADS (%d) and the number of threads requested on the command line (%d) have different values. Either omit one, or set them both to the same value.", nt_omp, *nthreads_omp);
213         }
214
215         /* Setting the number of OpenMP threads. */
216         *nthreads_omp = nt_omp;
217
218         /* Output the results */
219         sprintf(buffer,
220                 "The number of OpenMP threads was set by environment variable OMP_NUM_THREADS to %d%s\n",
221                 nt_omp,
222                 bCommandLineSetNthreadsOMP ? " (and the command-line setting agreed with that)" : "");
223         if (bIsSimMaster)
224         {
225             /* This prints once per simulation for multi-simulations,
226              * which might help diagnose issues with inhomogenous
227              * cluster setups. */
228             fputs(buffer, stderr);
229         }
230         if (debug)
231         {
232             /* This prints once per process for real MPI (i.e. once
233              * per debug file), and once per simulation for thread MPI
234              * (because of logic in the calling function). */
235             fputs(buffer, debug);
236         }
237     }
238 }
239
240 void gmx_omp_nthreads_init(FILE *fplog, t_commrec *cr,
241                            int nthreads_hw_avail,
242                            int omp_nthreads_req,
243                            int omp_nthreads_pme_req,
244                            gmx_bool gmx_unused bThisNodePMEOnly,
245                            gmx_bool bFullOmpSupport)
246 {
247     int      nth, nth_pmeonly, gmx_maxth, nppn;
248     char    *env;
249     gmx_bool bSepPME, bOMP;
250
251 #ifdef GMX_OPENMP
252     bOMP = TRUE;
253 #else
254     bOMP = FALSE;
255 #endif /* GMX_OPENMP */
256
257     /* number of MPI processes/threads per physical node */
258     nppn = cr->nrank_intranode;
259
260     bSepPME = ( (cr->duty & DUTY_PP) && !(cr->duty & DUTY_PME)) ||
261         (!(cr->duty & DUTY_PP) &&  (cr->duty & DUTY_PME));
262
263 #ifdef GMX_THREAD_MPI
264     /* modth is shared among tMPI threads, so for thread safety do the
265      * detection is done on the master only. It is not thread-safe with
266      * multiple simulations, but that's anyway not supported by tMPI. */
267     if (SIMMASTER(cr))
268 #endif
269     {
270         /* just return if the initialization has already been done */
271         if (modth.initialized)
272         {
273             return;
274         }
275
276         /* With full OpenMP support (verlet scheme) set the number of threads
277          * per process / default:
278          * - 1 if not compiled with OpenMP or
279          * - OMP_NUM_THREADS if the env. var is set, or
280          * - omp_nthreads_req = #of threads requested by the user on the mdrun
281          *   command line, otherwise
282          * - take the max number of available threads and distribute them
283          *   on the processes/tMPI threads.
284          * ~ The GMX_*_NUM_THREADS env var overrides the number of threads of
285          *   the respective module and it has to be used in conjunction with
286          *   OMP_NUM_THREADS.
287          *
288          * With the group scheme OpenMP multithreading is only supported in PME,
289          * for all other modules nthreads is set to 1.
290          * The number of PME threads is equal to:
291          * - 1 if not compiled with OpenMP or
292          * - GMX_PME_NUM_THREADS if defined, otherwise
293          * - OMP_NUM_THREADS if defined, otherwise
294          * - 1
295          */
296         nth = 1;
297         if ((env = getenv("OMP_NUM_THREADS")) != NULL)
298         {
299             if (!bOMP && (strncmp(env, "1", 1) != 0))
300             {
301                 gmx_warning("OMP_NUM_THREADS is set, but %s was compiled without OpenMP support!",
302                             ShortProgram());
303             }
304             else
305             {
306                 nth = gmx_omp_get_max_threads();
307             }
308         }
309         else if (omp_nthreads_req > 0)
310         {
311             nth = omp_nthreads_req;
312         }
313         else if (bFullOmpSupport && bOMP)
314         {
315             /* max available threads per node */
316             nth = nthreads_hw_avail;
317
318             /* divide the threads among the MPI processes/tMPI threads */
319             if (nth >= nppn)
320             {
321                 nth /= nppn;
322             }
323             else
324             {
325                 nth = 1;
326             }
327         }
328
329         /* now we have the global values, set them:
330          * - 1 if not compiled with OpenMP and for the group scheme
331          * - nth for the verlet scheme when compiled with OpenMP
332          */
333         if (bFullOmpSupport && bOMP)
334         {
335             modth.gnth = nth;
336         }
337         else
338         {
339             modth.gnth = 1;
340         }
341
342         if (bSepPME)
343         {
344             if (omp_nthreads_pme_req > 0)
345             {
346                 modth.gnth_pme = omp_nthreads_pme_req;
347             }
348             else
349             {
350                 modth.gnth_pme = nth;
351             }
352         }
353         else
354         {
355             modth.gnth_pme = 0;
356         }
357
358         /* now set the per-module values */
359         modth.nth[emntDefault] = modth.gnth;
360         pick_module_nthreads(fplog, emntDomdec, SIMMASTER(cr), bFullOmpSupport, bSepPME);
361         pick_module_nthreads(fplog, emntPairsearch, SIMMASTER(cr), bFullOmpSupport, bSepPME);
362         pick_module_nthreads(fplog, emntNonbonded, SIMMASTER(cr), bFullOmpSupport, bSepPME);
363         pick_module_nthreads(fplog, emntBonded, SIMMASTER(cr), bFullOmpSupport, bSepPME);
364         pick_module_nthreads(fplog, emntPME, SIMMASTER(cr), bFullOmpSupport, bSepPME);
365         pick_module_nthreads(fplog, emntUpdate, SIMMASTER(cr), bFullOmpSupport, bSepPME);
366         pick_module_nthreads(fplog, emntVSITE, SIMMASTER(cr), bFullOmpSupport, bSepPME);
367         pick_module_nthreads(fplog, emntLINCS, SIMMASTER(cr), bFullOmpSupport, bSepPME);
368         pick_module_nthreads(fplog, emntSETTLE, SIMMASTER(cr), bFullOmpSupport, bSepPME);
369
370         /* set the number of threads globally */
371         if (bOMP)
372         {
373 #ifndef GMX_THREAD_MPI
374             if (bThisNodePMEOnly)
375             {
376                 gmx_omp_set_num_threads(modth.gnth_pme);
377             }
378             else
379 #endif      /* GMX_THREAD_MPI */
380             {
381                 if (bFullOmpSupport)
382                 {
383                     gmx_omp_set_num_threads(nth);
384                 }
385                 else
386                 {
387                     gmx_omp_set_num_threads(1);
388                 }
389             }
390         }
391
392         modth.initialized = TRUE;
393     }
394 #ifdef GMX_THREAD_MPI
395     /* Non-master threads have to wait for the detection to be done. */
396     if (PAR(cr))
397     {
398         MPI_Barrier(cr->mpi_comm_mysim);
399     }
400 #endif
401
402     /* inform the user about the settings */
403     if (bOMP)
404     {
405 #ifdef GMX_THREAD_MPI
406         const char *mpi_str = "per tMPI thread";
407 #else
408         const char *mpi_str = "per MPI process";
409 #endif
410
411         /* for group scheme we print PME threads info only */
412         if (bFullOmpSupport)
413         {
414             md_print_info(cr, fplog, "Using %d OpenMP thread%s %s\n",
415                           modth.gnth, modth.gnth > 1 ? "s" : "",
416                           cr->nnodes > 1 ? mpi_str : "");
417         }
418         if (bSepPME && modth.gnth_pme != modth.gnth)
419         {
420             md_print_info(cr, fplog, "Using %d OpenMP thread%s %s for PME\n",
421                           modth.gnth_pme, modth.gnth_pme > 1 ? "s" : "",
422                           cr->nnodes > 1 ? mpi_str : "");
423         }
424     }
425
426     /* detect and warn about oversubscription
427      * TODO: enable this for separate PME nodes as well! */
428     if (!bSepPME && cr->rank_pp_intranode == 0)
429     {
430         char sbuf[STRLEN], sbuf1[STRLEN], sbuf2[STRLEN];
431
432         if (modth.gnth*nppn > nthreads_hw_avail)
433         {
434             sprintf(sbuf, "threads");
435             sbuf1[0] = '\0';
436             sprintf(sbuf2, "O");
437 #ifdef GMX_MPI
438             if (modth.gnth == 1)
439             {
440 #ifdef GMX_THREAD_MPI
441                 sprintf(sbuf, "thread-MPI threads");
442 #else
443                 sprintf(sbuf, "MPI processes");
444                 sprintf(sbuf1, " per node");
445                 sprintf(sbuf2, "On node %d: o", cr->sim_nodeid);
446 #endif
447             }
448 #endif
449             md_print_warn(cr, fplog,
450                           "WARNING: %sversubscribing the available %d logical CPU cores%s with %d %s.\n"
451                           "         This will cause considerable performance loss!",
452                           sbuf2, nthreads_hw_avail, sbuf1, nppn*modth.gnth, sbuf);
453         }
454     }
455 }
456
457 int gmx_omp_nthreads_get(int mod)
458 {
459     if (mod < 0 || mod >= emntNR)
460     {
461         /* invalid module queried */
462         return -1;
463     }
464     else
465     {
466         return modth.nth[mod];
467     }
468 }
469
470 void
471 gmx_omp_nthreads_set(int mod, int nthreads)
472 {
473     /* Catch an attempt to set the number of threads on an invalid
474      * OpenMP module. */
475     assert(mod >= 0 && mod < emntNR);
476
477     modth.nth[mod] = nthreads;
478 }