Sort all includes in src/gromacs
[alexxy/gromacs.git] / src / gromacs / gmxlib / network.c
index 9e3f501f828ec2bcf3d152c813978af9ff832556..94905661e1c4b89bc11b4cc88c76f18b043f9d62 100644 (file)
-/* -*- 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
- *
- *                        VERSION 3.2.0
- * Written by David van der Spoel, Erik Lindahl, Berk Hess, and others.
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
- * Copyright (c) 2001-2004, 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) 2001-2004, The GROMACS development team.
+ * Copyright (c) 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.
  *
- * 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.
+ * 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.
  *
- * For more info, check our website at http://www.gromacs.org
+ * 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.
  *
- * And Hey:
- * GROningen Mixture of Alchemy and Childrens' Stories
+ * To help us fund GROMACS development, we humbly ask that you cite
+ * the research papers on the package. Check out http://www.gromacs.org.
  */
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
+#include "gmxpre.h"
 
-#include <string.h>
-#include "gmx_fatal.h"
-#include "main.h"
-#include "smalloc.h"
-#include "network.h"
-#include "copyrite.h"
-#include "statutil.h"
-#include <ctype.h>
-#include "macros.h"
+#include "gromacs/legacyheaders/network.h"
 
-#ifdef GMX_LIB_MPI
-#include <mpi.h>
-#endif
+#include "config.h"
 
-#ifdef GMX_THREAD_MPI
-#include "tmpi.h"
-#endif
+#include <ctype.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <string.h>
 
+#include "gromacs/legacyheaders/copyrite.h"
+#include "gromacs/legacyheaders/macros.h"
+#include "gromacs/legacyheaders/types/commrec.h"
+#include "gromacs/utility/basenetwork.h"
+#include "gromacs/utility/cstringutil.h"
+#include "gromacs/utility/fatalerror.h"
+#include "gromacs/utility/futil.h"
+#include "gromacs/utility/gmxmpi.h"
+#include "gromacs/utility/smalloc.h"
 
 /* The source code in this file should be thread-safe.
       Please keep it that way. */
 
-gmx_bool gmx_mpi_initialized(void)
-{
-    int n;
-#ifndef GMX_MPI
-    return 0;
-#else
-    MPI_Initialized(&n);
-
-    return n;
-#endif
-}
-
-int gmx_setup(int *argc, char **argv, int *nnodes)
+void gmx_fill_commrec_from_mpi(t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
-    gmx_call("gmx_setup");
-    return 0;
-#else
-    char   buf[256];
-    int    resultlen;             /* actual length of node name      */
-    int    i, flag;
-    int    mpi_num_nodes;
-    int    mpi_my_rank;
-    char   mpi_hostname[MPI_MAX_PROCESSOR_NAME];
-
-    /* Call the MPI routines */
-#ifdef GMX_LIB_MPI
-#ifdef GMX_FAHCORE
-    (void) fah_MPI_Init(argc, &argv);
+    gmx_call("gmx_fill_commrec_from_mpi");
 #else
-    (void) MPI_Init(argc, &argv);
-#endif
-#endif
-    (void) MPI_Comm_size( MPI_COMM_WORLD, &mpi_num_nodes );
-    (void) MPI_Comm_rank( MPI_COMM_WORLD, &mpi_my_rank );
-    (void) MPI_Get_processor_name( mpi_hostname, &resultlen );
-
-#ifdef GMX_LIB_MPI
-    if (debug)
+    if (!gmx_mpi_initialized())
     {
-        fprintf(debug, "NNODES=%d, MYRANK=%d, HOSTNAME=%s\n",
-                mpi_num_nodes, mpi_my_rank, mpi_hostname);
+        gmx_comm("MPI has not been initialized properly");
     }
-#endif
 
-    *nnodes = mpi_num_nodes;
+    cr->nnodes           = gmx_node_num();
+    cr->nodeid           = gmx_node_rank();
+    cr->sim_nodeid       = cr->nodeid;
+    cr->mpi_comm_mysim   = MPI_COMM_WORLD;
+    cr->mpi_comm_mygroup = MPI_COMM_WORLD;
 
-    return mpi_my_rank;
 #endif
 }
 
-int  gmx_node_num(void)
+t_commrec *init_commrec()
 {
-#ifndef GMX_MPI
-    return 1;
-#else
-    int i;
-    (void) MPI_Comm_size(MPI_COMM_WORLD, &i);
-    return i;
-#endif
-}
+    t_commrec    *cr;
 
-int gmx_node_rank(void)
-{
-#ifndef GMX_MPI
-    return 0;
+    snew(cr, 1);
+
+#ifdef GMX_LIB_MPI
+    gmx_fill_commrec_from_mpi(cr);
 #else
-    int i;
-    (void) MPI_Comm_rank(MPI_COMM_WORLD, &i);
-    return i;
-#endif
+    cr->mpi_comm_mysim   = NULL;
+    cr->mpi_comm_mygroup = NULL;
+    cr->nnodes           = 1;
+    cr->sim_nodeid       = 0;
+    cr->nodeid           = cr->sim_nodeid;
+#endif
+
+    // TODO cr->duty should not be initialized here
+    cr->duty = (DUTY_PP | DUTY_PME);
+
+#if defined GMX_MPI && !defined MPI_IN_PLACE_EXISTS
+    /* initialize the MPI_IN_PLACE replacement buffers */
+    snew(cr->mpb, 1);
+    cr->mpb->ibuf        = NULL;
+    cr->mpb->libuf       = NULL;
+    cr->mpb->fbuf        = NULL;
+    cr->mpb->dbuf        = NULL;
+    cr->mpb->ibuf_alloc  = 0;
+    cr->mpb->libuf_alloc = 0;
+    cr->mpb->fbuf_alloc  = 0;
+    cr->mpb->dbuf_alloc  = 0;
+#endif
+
+    return cr;
 }
 
-
-int gmx_hostname_num()
+t_commrec *reinitialize_commrec_for_this_thread(const t_commrec gmx_unused *cro)
 {
-#ifndef GMX_MPI
-    return 0;
-#else
 #ifdef GMX_THREAD_MPI
-    /* thread-MPI currently puts the thread number in the process name,
-     * we might want to change this, as this is inconsistent with what
-     * most MPI implementations would do when running on a single node.
-     */
-    return 0;
-#else
-    int  resultlen, hostnum, i, j;
-    char mpi_hostname[MPI_MAX_PROCESSOR_NAME], hostnum_str[MPI_MAX_PROCESSOR_NAME];
+    t_commrec *cr;
 
-    MPI_Get_processor_name(mpi_hostname, &resultlen);
-    /* This procedure can only differentiate nodes with host names
-     * that end on unique numbers.
-     */
-    i = 0;
-    j = 0;
-    /* Only parse the host name up to the first dot */
-    while (i < resultlen && mpi_hostname[i] != '.')
-    {
-        if (isdigit(mpi_hostname[i]))
-        {
-            hostnum_str[j++] = mpi_hostname[i];
-        }
-        i++;
-    }
-    hostnum_str[j] = '\0';
-    if (j == 0)
-    {
-        hostnum = 0;
-    }
-    else
-    {
-        /* Use only the last 9 decimals, so we don't overflow an int */
-        hostnum = strtol(hostnum_str + max(0, j-9), NULL, 10);
-    }
+    /* make a thread-specific commrec */
+    snew(cr, 1);
+    /* now copy the whole thing, so settings like the number of PME nodes
+       get propagated. */
+    *cr = *cro;
 
-    if (debug)
-    {
-        fprintf(debug, "In gmx_setup_nodecomm: hostname '%s', hostnum %d\n",
-                mpi_hostname, hostnum);
-    }
-    return hostnum;
-#endif
+    /* and we start setting our own thread-specific values for things */
+    gmx_fill_commrec_from_mpi(cr);
+
+    // TODO cr->duty should not be initialized here
+    cr->duty             = (DUTY_PP | DUTY_PME);
+
+    return cr;
+#else
+    return NULL;
 #endif
 }
 
-void gmx_setup_nodecomm(FILE *fplog, t_commrec *cr)
+void gmx_setup_nodecomm(FILE gmx_unused *fplog, t_commrec *cr)
 {
     gmx_nodecomm_t *nc;
-    int             n, rank, hostnum, ng, ni;
+    int             n, rank, nodehash, ng, ni;
 
     /* Many MPI implementations do not optimize MPI_Allreduce
      * (and probably also other global communication calls)
@@ -207,7 +159,7 @@ void gmx_setup_nodecomm(FILE *fplog, t_commrec *cr)
     MPI_Comm_size(cr->mpi_comm_mygroup, &n);
     MPI_Comm_rank(cr->mpi_comm_mygroup, &rank);
 
-    hostnum = gmx_hostname_num();
+    nodehash = gmx_physicalnode_id_hash();
 
     if (debug)
     {
@@ -216,11 +168,11 @@ void gmx_setup_nodecomm(FILE *fplog, t_commrec *cr)
 
 
     /* The intra-node communicator, split on node number */
-    MPI_Comm_split(cr->mpi_comm_mygroup, hostnum, rank, &nc->comm_intra);
+    MPI_Comm_split(cr->mpi_comm_mygroup, nodehash, rank, &nc->comm_intra);
     MPI_Comm_rank(nc->comm_intra, &nc->rank_intra);
     if (debug)
     {
-        fprintf(debug, "In gmx_setup_nodecomm: node rank %d rank_intra %d\n",
+        fprintf(debug, "In gmx_setup_nodecomm: node ID %d rank within node %d\n",
                 rank, nc->rank_intra);
     }
     /* The inter-node communicator, split on rank_intra.
@@ -243,7 +195,7 @@ void gmx_setup_nodecomm(FILE *fplog, t_commrec *cr)
         nc->bUse = TRUE;
         if (fplog)
         {
-            fprintf(fplog, "Using two step summing over %d groups of on average %.1f processes\n\n",
+            fprintf(fplog, "Using two step summing over %d groups of on average %.1f ranks\n\n",
                     ng, (real)n/(real)ng);
         }
         if (nc->rank_intra > 0)
@@ -276,25 +228,25 @@ void gmx_init_intranode_counters(t_commrec *cr)
     /* thread-MPI is not initialized when not running in parallel */
 #if defined GMX_MPI && !defined GMX_THREAD_MPI
     int nrank_world, rank_world;
-    int i, mynum, *num, *num_s, *num_pp, *num_pp_s;
+    int i, myhash, *hash, *hash_s, *hash_pp, *hash_pp_s;
 
     MPI_Comm_size(MPI_COMM_WORLD, &nrank_world);
     MPI_Comm_rank(MPI_COMM_WORLD, &rank_world);
 
-    /* Get the node number from the hostname to identify the nodes */
-    mynum = gmx_hostname_num();
+    /* Get a (hopefully unique) hash that identifies our physical node */
+    myhash = gmx_physicalnode_id_hash();
 
     /* We can't rely on MPI_IN_PLACE, so we need send and receive buffers */
-    snew(num,   nrank_world);
-    snew(num_s, nrank_world);
-    snew(num_pp,   nrank_world);
-    snew(num_pp_s, nrank_world);
+    snew(hash,   nrank_world);
+    snew(hash_s, nrank_world);
+    snew(hash_pp,   nrank_world);
+    snew(hash_pp_s, nrank_world);
 
-    num_s[rank_world]    = mynum;
-    num_pp_s[rank_world] = (cr->duty & DUTY_PP) ? mynum : -1;
+    hash_s[rank_world]    = myhash;
+    hash_pp_s[rank_world] = (cr->duty & DUTY_PP) ? myhash : -1;
 
-    MPI_Allreduce(num_s,    num,    nrank_world, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
-    MPI_Allreduce(num_pp_s, num_pp, nrank_world, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
+    MPI_Allreduce(hash_s,    hash,    nrank_world, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
+    MPI_Allreduce(hash_pp_s, hash_pp, nrank_world, MPI_INT, MPI_SUM, MPI_COMM_WORLD);
 
     nrank_intranode    = 0;
     rank_intranode     = 0;
@@ -302,7 +254,7 @@ void gmx_init_intranode_counters(t_commrec *cr)
     rank_pp_intranode  = 0;
     for (i = 0; i < nrank_world; i++)
     {
-        if (num[i] == mynum)
+        if (hash[i] == myhash)
         {
             nrank_intranode++;
             if (i < rank_world)
@@ -310,19 +262,19 @@ void gmx_init_intranode_counters(t_commrec *cr)
                 rank_intranode++;
             }
         }
-        if ((cr->duty & DUTY_PP) && num_pp[i] == mynum)
+        if (hash_pp[i] == myhash)
         {
             nrank_pp_intranode++;
-            if (i < rank_world)
+            if ((cr->duty & DUTY_PP) && i < rank_world)
             {
                 rank_pp_intranode++;
             }
         }
     }
-    sfree(num);
-    sfree(num_s);
-    sfree(num_pp);
-    sfree(num_pp_s);
+    sfree(hash);
+    sfree(hash_s);
+    sfree(hash_pp);
+    sfree(hash_pp_s);
 #else
     /* Serial or thread-MPI code: we run within a single physical node */
     nrank_intranode    = cr->nnodes;
@@ -342,7 +294,7 @@ void gmx_init_intranode_counters(t_commrec *cr)
         {
             sprintf(sbuf, "%s", cr->duty & DUTY_PP ? "PP" : "PME");
         }
-        fprintf(debug, "On %3s node %d: nrank_intranode=%d, rank_intranode=%d, "
+        fprintf(debug, "On %3s rank %d: nrank_intranode=%d, rank_intranode=%d, "
                 "nrank_pp_intranode=%d, rank_pp_intranode=%d\n",
                 sbuf, cr->sim_nodeid,
                 nrank_intranode, rank_intranode,
@@ -356,7 +308,7 @@ void gmx_init_intranode_counters(t_commrec *cr)
 }
 
 
-void gmx_barrier(const t_commrec *cr)
+void gmx_barrier(const t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_barrier");
@@ -365,34 +317,7 @@ void gmx_barrier(const t_commrec *cr)
 #endif
 }
 
-void gmx_abort(int noderank, int nnodes, int errorno)
-{
-#ifndef GMX_MPI
-    gmx_call("gmx_abort");
-#else
-#ifdef GMX_THREAD_MPI
-    fprintf(stderr, "Halting program %s\n", ShortProgram());
-    thanx(stderr);
-    exit(1);
-#else
-    if (nnodes > 1)
-    {
-        fprintf(stderr, "Halting parallel program %s on CPU %d out of %d\n",
-                ShortProgram(), noderank, nnodes);
-    }
-    else
-    {
-        fprintf(stderr, "Halting program %s\n", ShortProgram());
-    }
-
-    thanx(stderr);
-    MPI_Abort(MPI_COMM_WORLD, errorno);
-    exit(1);
-#endif
-#endif
-}
-
-void gmx_bcast(int nbytes, void *b, const t_commrec *cr)
+void gmx_bcast(int gmx_unused nbytes, void gmx_unused *b, const t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_bast");
@@ -401,7 +326,7 @@ void gmx_bcast(int nbytes, void *b, const t_commrec *cr)
 #endif
 }
 
-void gmx_bcast_sim(int nbytes, void *b, const t_commrec *cr)
+void gmx_bcast_sim(int gmx_unused nbytes, void gmx_unused *b, const t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_bast");
@@ -410,12 +335,12 @@ void gmx_bcast_sim(int nbytes, void *b, const t_commrec *cr)
 #endif
 }
 
-void gmx_sumd(int nr, double r[], const t_commrec *cr)
+void gmx_sumd(int gmx_unused nr, double gmx_unused r[], const t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumd");
 #else
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
+#if defined(MPI_IN_PLACE_EXISTS)
     if (cr->nc.bUse)
     {
         if (cr->nc.rank_intra == 0)
@@ -473,12 +398,12 @@ void gmx_sumd(int nr, double r[], const t_commrec *cr)
 #endif
 }
 
-void gmx_sumf(int nr, float r[], const t_commrec *cr)
+void gmx_sumf(int gmx_unused nr, float gmx_unused r[], const t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumf");
 #else
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
+#if defined(MPI_IN_PLACE_EXISTS)
     if (cr->nc.bUse)
     {
         /* Use two step summing.  */
@@ -535,12 +460,12 @@ void gmx_sumf(int nr, float r[], const t_commrec *cr)
 #endif
 }
 
-void gmx_sumi(int nr, int r[], const t_commrec *cr)
+void gmx_sumi(int gmx_unused nr, int gmx_unused r[], const t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumi");
 #else
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
+#if defined(MPI_IN_PLACE_EXISTS)
     if (cr->nc.bUse)
     {
         /* Use two step summing */
@@ -593,34 +518,34 @@ void gmx_sumi(int nr, int r[], const t_commrec *cr)
 #endif
 }
 
-void gmx_sumli(int nr, gmx_large_int_t r[], const t_commrec *cr)
+void gmx_sumli(int gmx_unused nr, gmx_int64_t gmx_unused r[], const t_commrec gmx_unused *cr)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumli");
 #else
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
+#if defined(MPI_IN_PLACE_EXISTS)
     if (cr->nc.bUse)
     {
         /* Use two step summing */
         if (cr->nc.rank_intra == 0)
         {
-            MPI_Reduce(MPI_IN_PLACE, r, nr, GMX_MPI_LARGE_INT, MPI_SUM, 0,
+            MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM, 0,
                        cr->nc.comm_intra);
             /* Sum with the buffers reversed */
-            MPI_Allreduce(MPI_IN_PLACE, r, nr, GMX_MPI_LARGE_INT, MPI_SUM,
+            MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM,
                           cr->nc.comm_inter);
         }
         else
         {
             /* This is here because of the silly MPI specification
                 that MPI_IN_PLACE should be put in sendbuf instead of recvbuf */
-            MPI_Reduce(r, NULL, nr, GMX_MPI_LARGE_INT, MPI_SUM, 0, cr->nc.comm_intra);
+            MPI_Reduce(r, NULL, nr, MPI_INT64_T, MPI_SUM, 0, cr->nc.comm_intra);
         }
-        MPI_Bcast(r, nr, GMX_MPI_LARGE_INT, 0, cr->nc.comm_intra);
+        MPI_Bcast(r, nr, MPI_INT64_T, 0, cr->nc.comm_intra);
     }
     else
     {
-        MPI_Allreduce(MPI_IN_PLACE, r, nr, GMX_MPI_LARGE_INT, MPI_SUM, cr->mpi_comm_mygroup);
+        MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM, cr->mpi_comm_mygroup);
     }
 #else
     int i;
@@ -633,19 +558,19 @@ void gmx_sumli(int nr, gmx_large_int_t r[], const t_commrec *cr)
     if (cr->nc.bUse)
     {
         /* Use two step summing */
-        MPI_Allreduce(r, cr->mpb->libuf, nr, GMX_MPI_LARGE_INT, MPI_SUM,
+        MPI_Allreduce(r, cr->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
                       cr->nc.comm_intra);
         if (cr->nc.rank_intra == 0)
         {
             /* Sum with the buffers reversed */
-            MPI_Allreduce(cr->mpb->libuf, r, nr, GMX_MPI_LARGE_INT, MPI_SUM,
+            MPI_Allreduce(cr->mpb->libuf, r, nr, MPI_INT64_T, MPI_SUM,
                           cr->nc.comm_inter);
         }
-        MPI_Bcast(r, nr, GMX_MPI_LARGE_INT, 0, cr->nc.comm_intra);
+        MPI_Bcast(r, nr, MPI_INT64_T, 0, cr->nc.comm_intra);
     }
     else
     {
-        MPI_Allreduce(r, cr->mpb->libuf, nr, GMX_MPI_LARGE_INT, MPI_SUM,
+        MPI_Allreduce(r, cr->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
                       cr->mpi_comm_mygroup);
         for (i = 0; i < nr; i++)
         {
@@ -659,9 +584,9 @@ void gmx_sumli(int nr, gmx_large_int_t r[], const t_commrec *cr)
 
 
 #ifdef GMX_MPI
-void gmx_sumd_comm(int nr, double r[], MPI_Comm mpi_comm)
+static void gmx_sumd_comm(int nr, double r[], MPI_Comm mpi_comm)
 {
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
+#if defined(MPI_IN_PLACE_EXISTS)
     MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, mpi_comm);
 #else
     /* this function is only used in code that is not performance critical,
@@ -682,9 +607,9 @@ void gmx_sumd_comm(int nr, double r[], MPI_Comm mpi_comm)
 #endif
 
 #ifdef GMX_MPI
-void gmx_sumf_comm(int nr, float r[], MPI_Comm mpi_comm)
+static void gmx_sumf_comm(int nr, float r[], MPI_Comm mpi_comm)
 {
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
+#if defined(MPI_IN_PLACE_EXISTS)
     MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, mpi_comm);
 #else
     /* this function is only used in code that is not performance critical,
@@ -704,7 +629,7 @@ void gmx_sumf_comm(int nr, float r[], MPI_Comm mpi_comm)
 }
 #endif
 
-void gmx_sumd_sim(int nr, double r[], const gmx_multisim_t *ms)
+void gmx_sumd_sim(int gmx_unused nr, double gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumd_sim");
@@ -713,7 +638,7 @@ void gmx_sumd_sim(int nr, double r[], const gmx_multisim_t *ms)
 #endif
 }
 
-void gmx_sumf_sim(int nr, float r[], const gmx_multisim_t *ms)
+void gmx_sumf_sim(int gmx_unused nr, float gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumf_sim");
@@ -722,12 +647,12 @@ void gmx_sumf_sim(int nr, float r[], const gmx_multisim_t *ms)
 #endif
 }
 
-void gmx_sumi_sim(int nr, int r[], const gmx_multisim_t *ms)
+void gmx_sumi_sim(int gmx_unused nr, int gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumi_sim");
 #else
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
+#if defined(MPI_IN_PLACE_EXISTS)
     MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, ms->mpi_comm_masters);
 #else
     /* this is thread-unsafe, but it will do for now: */
@@ -747,13 +672,13 @@ void gmx_sumi_sim(int nr, int r[], const gmx_multisim_t *ms)
 #endif
 }
 
-void gmx_sumli_sim(int nr, gmx_large_int_t r[], const gmx_multisim_t *ms)
+void gmx_sumli_sim(int gmx_unused nr, gmx_int64_t gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
 {
 #ifndef GMX_MPI
     gmx_call("gmx_sumli_sim");
 #else
-#if defined(MPI_IN_PLACE_EXISTS) || defined(GMX_THREAD_MPI)
-    MPI_Allreduce(MPI_IN_PLACE, r, nr, GMX_MPI_LARGE_INT, MPI_SUM,
+#if defined(MPI_IN_PLACE_EXISTS)
+    MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT64_T, MPI_SUM,
                   ms->mpi_comm_masters);
 #else
     /* this is thread-unsafe, but it will do for now: */
@@ -764,7 +689,7 @@ void gmx_sumli_sim(int nr, gmx_large_int_t r[], const gmx_multisim_t *ms)
         ms->mpb->libuf_alloc = nr;
         srenew(ms->mpb->libuf, ms->mpb->libuf_alloc);
     }
-    MPI_Allreduce(r, ms->mpb->libuf, nr, GMX_MPI_LARGE_INT, MPI_SUM,
+    MPI_Allreduce(r, ms->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
                   ms->mpi_comm_masters);
     for (i = 0; i < nr; i++)
     {
@@ -774,55 +699,46 @@ void gmx_sumli_sim(int nr, gmx_large_int_t r[], const gmx_multisim_t *ms)
 #endif
 }
 
-
-void gmx_finalize_par(void)
+gmx_bool gmx_fexist_master(const char *fname, t_commrec *cr)
 {
-#ifndef GMX_MPI
-    /* Compiled without MPI, no MPI finalizing needed */
-    return;
-#else
-    int initialized, finalized;
-    int ret;
+    gmx_bool bExist;
 
-    MPI_Initialized(&initialized);
-    if (!initialized)
+    if (SIMMASTER(cr))
     {
-        return;
+        bExist = gmx_fexist(fname);
     }
-    /* just as a check; we don't want to finalize twice */
-    MPI_Finalized(&finalized);
-    if (finalized)
+    if (PAR(cr))
     {
-        return;
+        gmx_bcast(sizeof(bExist), &bExist, cr);
     }
+    return bExist;
+}
 
-    /* We sync the processes here to try to avoid problems
-     * with buggy MPI implementations that could cause
-     * unfinished processes to terminate.
-     */
-    MPI_Barrier(MPI_COMM_WORLD);
-
-    /*
-       if (DOMAINDECOMP(cr)) {
-       if (cr->npmenodes > 0 || cr->dd->bCartesian)
-        MPI_Comm_free(&cr->mpi_comm_mygroup);
-       if (cr->dd->bCartesian)
-        MPI_Comm_free(&cr->mpi_comm_mysim);
-       }
-     */
-
-    /* Apparently certain mpich implementations cause problems
-     * with MPI_Finalize. In that case comment out MPI_Finalize.
-     */
-    if (debug)
+void gmx_fatal_collective(int f_errno, const char *file, int line,
+                          const t_commrec *cr, gmx_domdec_t *dd,
+                          const char *fmt, ...)
+{
+    va_list  ap;
+    gmx_bool bMaster, bFinalize;
+#ifdef GMX_MPI
+    int      result;
+    /* Check if we are calling on all processes in MPI_COMM_WORLD */
+    if (cr != NULL)
     {
-        fprintf(debug, "Will call MPI_Finalize now\n");
+        MPI_Comm_compare(cr->mpi_comm_mysim, MPI_COMM_WORLD, &result);
     }
-
-    ret = MPI_Finalize();
-    if (debug)
+    else
     {
-        fprintf(debug, "Return code from MPI_Finalize = %d\n", ret);
+        MPI_Comm_compare(dd->mpi_comm_all, MPI_COMM_WORLD, &result);
     }
+    /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */
+    bFinalize = (result != MPI_UNEQUAL);
+#else
+    bFinalize = TRUE;
 #endif
+    bMaster = (cr != NULL && MASTER(cr)) || (dd != NULL && DDMASTER(dd));
+
+    va_start(ap, fmt);
+    gmx_fatal_mpi_va(f_errno, file, line, bMaster, bFinalize, fmt, ap);
+    va_end(ap);
 }