Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / gmxlib / gmx_omp_nthreads.c
index 8be120d79df9068f3a1114eb6f2115cc472c1729..e9a415c403a9473a22cb230aba6119ffd0048d1c 100644 (file)
@@ -1,64 +1,67 @@
-/* -*- mode: c; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; c-file-style: "stroustrup"; -*-
+/*
+ * This file is part of the GROMACS molecular simulation package.
  *
- * 
- *                This source code is part of
- * 
- *                 G   R   O   M   A   C   S
- * 
- *          GROningen MAchine for Chemical Simulations
- * 
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
- * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2010, The GROMACS development team,
- * check out http://www.gromacs.org for more information.
-
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU General Public License
- * as published by the Free Software Foundation; either version 2
+ * Copyright (c) 2012,2013,2014, by the GROMACS development team, led by
+ * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
+ * and including many others, as listed in the AUTHORS file in the
+ * top-level source directory and at http://www.gromacs.org.
+ *
+ * GROMACS is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
  * of the License, or (at your option) any later version.
- * 
- * If you want to redistribute modifications, please consider that
- * scientific software is very special. Version control is crucial -
- * bugs must be traceable. We will be happy to consider code for
- * inclusion in the official distribution, but derived work must not
- * be called official GROMACS. Details are found in the README & COPYING
- * files - if they are missing, get the official version at www.gromacs.org.
- * 
+ *
+ * GROMACS is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GROMACS; if not, see
+ * http://www.gnu.org/licenses, or write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
+ *
+ * If you want to redistribute modifications to GROMACS, please
+ * consider that scientific software is very special. Version
+ * control is crucial - bugs must be traceable. We will be happy to
+ * consider code for inclusion in the official distribution, but
+ * derived work must not be called official GROMACS. Details are found
+ * in the README & COPYING files - if they are missing, get the
+ * official version at http://www.gromacs.org.
+ *
  * To help us fund GROMACS development, we humbly ask that you cite
- * the papers on the package - you can find them in the top README file.
- * 
- * For more info, check our website at http://www.gromacs.org
- * 
- * And Hey:
- * Gallium Rubidium Oxygen Manganese Argon Carbon Silicon
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
 
+#include "gmxpre.h"
+
+#include "gromacs/legacyheaders/gmx_omp_nthreads.h"
+
+#include "config.h"
+
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
-#include <assert.h>
-
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
 
-#include "gmx_fatal.h"
-#include "typedefs.h"
-#include "macros.h"
-#include "network.h"
-#include "statutil.h"
-#include "gmx_omp.h"
-#include "gmx_omp_nthreads.h"
-#include "md_logging.h"
+#include "gromacs/legacyheaders/copyrite.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/legacyheaders/md_logging.h"
+#include "gromacs/legacyheaders/network.h"
+#include "gromacs/legacyheaders/typedefs.h"
+#include "gromacs/legacyheaders/types/commrec.h"
+#include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/gmxomp.h"
 
 /** Structure with the number of threads for each OpenMP multi-threaded
  *  algorithmic module in mdrun. */
 typedef struct
 {
-    int gnth;               /**< Global num. of threads per PP or PP+PME process/tMPI thread. */
-    int gnth_pme;           /**< Global num. of threads per PME only process/tMPI thread. */
+    int      gnth;          /**< Global num. of threads per PP or PP+PME process/tMPI thread. */
+    int      gnth_pme;      /**< Global num. of threads per PME only process/tMPI thread. */
 
-    int nth[emntNR];        /**< Number of threads for each module, indexed with module_nth_t */
+    int      nth[emntNR];   /**< Number of threads for each module, indexed with module_nth_t */
     gmx_bool initialized;   /**< TRUE if the module as been initialized. */
 } omp_module_nthreads_t;
 
@@ -85,8 +88,8 @@ static const char *mod_name[emntNR] =
 
 /** Number of threads for each algorithmic module.
  *
- *  File-scope global variable that gets set once in \init_module_nthreads
- *  and queried via gmx_omp_nthreads_get.
+ *  File-scope global variable that gets set once in pick_module_nthreads()
+ *  and queried via gmx_omp_nthreads_get().
  *
  *  All fields are initialized to 0 which should result in errors if
  *  the init call is omitted.
@@ -94,9 +97,9 @@ static const char *mod_name[emntNR] =
 static omp_module_nthreads_t modth = { 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0}, FALSE};
 
 
-/** Determine the number of threads for module \mod.
+/** Determine the number of threads for module \mod.
  *
- *  \m takes values form the module_nth_t enum and maps these to the
+ *  \m takes values form the module_nth_t enum and maps these to the
  *  corresponding value in modth_env_var.
  *
  *  Each number of threads per module takes the default value unless
@@ -106,14 +109,14 @@ static omp_module_nthreads_t modth = { 0, 0, {0, 0, 0, 0, 0, 0, 0, 0, 0}, FALSE}
  *  The "group" scheme supports OpenMP only in PME and in thise case all but
  *  the PME nthread values default to 1.
  */
-static int pick_module_nthreads(FILE *fplog, int m,
-                                gmx_bool bSimMaster,
-                                gmx_bool bFullOmpSupport,
-                                gmx_bool bSepPME)
+static void pick_module_nthreads(FILE *fplog, int m,
+                                 gmx_bool bSimMaster,
+                                 gmx_bool bFullOmpSupport,
+                                 gmx_bool bSepPME)
 {
-    char *env;
-    int  nth;
-    char sbuf[STRLEN];
+    char    *env;
+    int      nth;
+    char     sbuf[STRLEN];
     gmx_bool bOMP;
 
 #ifdef GMX_OPENMP
@@ -126,7 +129,7 @@ static int pick_module_nthreads(FILE *fplog, int m,
      * as it's always equal with gnth. */
     if (m == emntDefault)
     {
-        return modth.nth[emntDefault];
+        return;
     }
 
     /* check the environment variable */
@@ -144,9 +147,9 @@ static int pick_module_nthreads(FILE *fplog, int m,
          * OMP_NUM_THREADS also has to be set */
         if (bFullOmpSupport && getenv("OMP_NUM_THREADS") == NULL)
         {
-            gmx_fatal(FARGS, "%s=%d is set, the default number of threads also "
-                      "needs to be set with OMP_NUM_THREADS!",
-                      modth_env_var[m], nth);
+            gmx_warning("%s=%d is set, the default number of threads also "
+                        "needs to be set with OMP_NUM_THREADS!",
+                        modth_env_var[m], nth);
         }
 
         /* with the group scheme warn if any env var except PME is set */
@@ -183,12 +186,15 @@ static int pick_module_nthreads(FILE *fplog, int m,
         nth = (bSepPME && m == emntPME) ? modth.gnth_pme : modth.gnth;
     }
 
-    return modth.nth[m] = nth;
+    gmx_omp_nthreads_set(m, nth);
 }
 
-void gmx_omp_nthreads_read_env(int *nthreads_omp)
+void gmx_omp_nthreads_read_env(int     *nthreads_omp,
+                               gmx_bool bIsSimMaster)
 {
-    char *env;
+    char    *env;
+    gmx_bool bCommandLineSetNthreadsOMP = *nthreads_omp > 0;
+    char     buffer[STRLEN];
 
     assert(nthreads_omp);
 
@@ -196,23 +202,39 @@ void gmx_omp_nthreads_read_env(int *nthreads_omp)
     {
         int nt_omp;
 
-        sscanf(env,"%d",&nt_omp);
+        sscanf(env, "%d", &nt_omp);
         if (nt_omp <= 0)
         {
-            gmx_fatal(FARGS,"OMP_NUM_THREADS is invalid: '%s'",env);
+            gmx_fatal(FARGS, "OMP_NUM_THREADS is invalid: '%s'", env);
         }
 
-        if (*nthreads_omp > 0 && nt_omp != *nthreads_omp)
+        if (bCommandLineSetNthreadsOMP && nt_omp != *nthreads_omp)
         {
-            gmx_fatal(FARGS,"OMP_NUM_THREADS (%d) and the number of threads requested on the command line (%d) have different values",nt_omp,*nthreads_omp);
+            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);
         }
 
-        /* Setting the number of OpenMP threads.
-         * NOTE: with tMPI this function is only called on the master node,
-         * but with MPI on all nodes which means lots of messages on stderr.
-         */
-        fprintf(stderr,"Getting the number of OpenMP threads from OMP_NUM_THREADS: %d\n",nt_omp);
+        /* Setting the number of OpenMP threads. */
         *nthreads_omp = nt_omp;
+
+        /* Output the results */
+        sprintf(buffer,
+                "The number of OpenMP threads was set by environment variable OMP_NUM_THREADS to %d%s\n",
+                nt_omp,
+                bCommandLineSetNthreadsOMP ? " (and the command-line setting agreed with that)" : "");
+        if (bIsSimMaster)
+        {
+            /* This prints once per simulation for multi-simulations,
+             * which might help diagnose issues with inhomogenous
+             * cluster setups. */
+            fputs(buffer, stderr);
+        }
+        if (debug)
+        {
+            /* This prints once per process for real MPI (i.e. once
+             * per debug file), and once per simulation for thread MPI
+             * (because of logic in the calling function). */
+            fputs(buffer, debug);
+        }
     }
 }
 
@@ -220,11 +242,11 @@ void gmx_omp_nthreads_init(FILE *fplog, t_commrec *cr,
                            int nthreads_hw_avail,
                            int omp_nthreads_req,
                            int omp_nthreads_pme_req,
-                           gmx_bool bThisNodePMEOnly,
+                           gmx_bool gmx_unused bThisNodePMEOnly,
                            gmx_bool bFullOmpSupport)
 {
-    int  nth, nth_pmeonly, gmx_maxth, nppn;
-    char *env;
+    int      nth, nth_pmeonly, gmx_maxth, nppn;
+    char    *env;
     gmx_bool bSepPME, bOMP;
 
 #ifdef GMX_OPENMP
@@ -233,11 +255,11 @@ void gmx_omp_nthreads_init(FILE *fplog, t_commrec *cr,
     bOMP = FALSE;
 #endif /* GMX_OPENMP */
 
-    /* number of processes per node */
-    nppn = cr->nnodes_intra;
+    /* number of MPI processes/threads per physical node */
+    nppn = cr->nrank_intranode;
 
     bSepPME = ( (cr->duty & DUTY_PP) && !(cr->duty & DUTY_PME)) ||
-              (!(cr->duty & DUTY_PP) &&  (cr->duty & DUTY_PME));
+        (!(cr->duty & DUTY_PP) &&  (cr->duty & DUTY_PME));
 
 #ifdef GMX_THREAD_MPI
     /* modth is shared among tMPI threads, so for thread safety do the
@@ -355,7 +377,7 @@ void gmx_omp_nthreads_init(FILE *fplog, t_commrec *cr,
                 gmx_omp_set_num_threads(modth.gnth_pme);
             }
             else
-#endif /* GMX_THREAD_MPI */
+#endif      /* GMX_THREAD_MPI */
             {
                 if (bFullOmpSupport)
                 {
@@ -379,32 +401,32 @@ void gmx_omp_nthreads_init(FILE *fplog, t_commrec *cr,
 #endif
 
     /* inform the user about the settings */
-    if (SIMMASTER(cr) && bOMP)
+    if (bOMP)
     {
 #ifdef GMX_THREAD_MPI
-        const char *mpi_str="per tMPI thread";
+        const char *mpi_str = "per tMPI thread";
 #else
-        const char *mpi_str="per MPI process";
+        const char *mpi_str = "per MPI process";
 #endif
 
         /* for group scheme we print PME threads info only */
         if (bFullOmpSupport)
         {
-            fprintf(stderr, "Using %d OpenMP thread%s %s\n",
-                    modth.gnth,modth.gnth > 1 ? "s" : "",
-                    cr->nnodes > 1 ? mpi_str : "");
+            md_print_info(cr, fplog, "Using %d OpenMP thread%s %s\n",
+                          modth.gnth, modth.gnth > 1 ? "s" : "",
+                          cr->nnodes > 1 ? mpi_str : "");
         }
         if (bSepPME && modth.gnth_pme != modth.gnth)
         {
-            fprintf(stderr, "Using %d OpenMP thread%s %s for PME\n",
-                    modth.gnth_pme,modth.gnth_pme > 1 ? "s" : "",
-                    cr->nnodes > 1 ? mpi_str : "");
+            md_print_info(cr, fplog, "Using %d OpenMP thread%s %s for PME\n",
+                          modth.gnth_pme, modth.gnth_pme > 1 ? "s" : "",
+                          cr->nnodes > 1 ? mpi_str : "");
         }
     }
 
     /* detect and warn about oversubscription
      * TODO: enable this for separate PME nodes as well! */
-    if (!bSepPME && cr->nodeid_intra == 0)
+    if (!bSepPME && cr->rank_pp_intranode == 0)
     {
         char sbuf[STRLEN], sbuf1[STRLEN], sbuf2[STRLEN];
 
@@ -420,8 +442,8 @@ void gmx_omp_nthreads_init(FILE *fplog, t_commrec *cr,
                 sprintf(sbuf, "thread-MPI threads");
 #else
                 sprintf(sbuf, "MPI processes");
-                sprintf(sbuf1, " per node");
-                sprintf(sbuf2, "On node %d: o", cr->sim_nodeid);
+                sprintf(sbuf1, " per rank");
+                sprintf(sbuf2, "On rank %d: o", cr->sim_nodeid);
 #endif
             }
 #endif
@@ -445,3 +467,13 @@ int gmx_omp_nthreads_get(int mod)
         return modth.nth[mod];
     }
 }
+
+void
+gmx_omp_nthreads_set(int mod, int nthreads)
+{
+    /* Catch an attempt to set the number of threads on an invalid
+     * OpenMP module. */
+    assert(mod >= 0 && mod < emntNR);
+
+    modth.nth[mod] = nthreads;
+}