Made gromacs work again without MPI
[alexxy/gromacs.git] / src / gromacs / gmxlib / network.cpp
index 082d980d42c21078a642f831f3d9b0e0cfc5f040..5bac7f1e0f973059cea997da96fd6d7232f429f9 100644 (file)
@@ -3,7 +3,8 @@
  *
  * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
  * Copyright (c) 2001-2004, The GROMACS development team.
- * Copyright (c) 2013,2014,2015,2016,2017, by the GROMACS development team, led by
+ * Copyright (c) 2013,2014,2015,2016,2017 by the GROMACS development team.
+ * Copyright (c) 2018,2019,2020,2021, 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.
 #include "gromacs/utility/cstringutil.h"
 #include "gromacs/utility/fatalerror.h"
 #include "gromacs/utility/futil.h"
+#include "gromacs/utility/gmxassert.h"
 #include "gromacs/utility/gmxmpi.h"
+#include "gromacs/utility/real.h"
 #include "gromacs/utility/smalloc.h"
 
 /* The source code in this file should be thread-safe.
       Please keep it that way. */
 
-void gmx_fill_commrec_from_mpi(t_commrec gmx_unused *cr)
+CommrecHandle init_commrec(MPI_Comm communicator)
 {
-#if !GMX_MPI
-    gmx_call("gmx_fill_commrec_from_mpi");
-#else
-    if (!gmx_mpi_initialized())
-    {
-        gmx_comm("MPI has not been initialized properly");
-    }
-
-    cr->nnodes           = gmx_node_num();
-    cr->nodeid           = gmx_node_rank();
-    if (PAR(cr) || MULTISIM(cr))
-    {
-        MPI_Comm_split(MPI_COMM_WORLD, gmx_physicalnode_id_hash(), cr->nodeid, &cr->mpi_comm_physicalnode);
-    }
-    cr->sim_nodeid       = cr->nodeid;
-    cr->mpi_comm_mysim   = MPI_COMM_WORLD;
-    cr->mpi_comm_mygroup = MPI_COMM_WORLD;
-
-#endif
-}
-
-t_commrec *init_commrec()
-{
-    t_commrec    *cr;
+    CommrecHandle handle;
+    t_commrec*    cr;
 
     snew(cr, 1);
+    handle.reset(cr);
 
-#if GMX_LIB_MPI
-    gmx_fill_commrec_from_mpi(cr);
+    int rankInCommunicator, sizeOfCommunicator;
+#if GMX_MPI
+#    if GMX_LIB_MPI
+    GMX_RELEASE_ASSERT(gmx_mpi_initialized(), "Must have initialized MPI before building commrec");
+#    endif
+    MPI_Comm_rank(communicator, &rankInCommunicator);
+    MPI_Comm_size(communicator, &sizeOfCommunicator);
 #else
-    cr->mpi_comm_mysim   = nullptr;
-    cr->mpi_comm_mygroup = nullptr;
-    cr->nnodes           = 1;
-    cr->sim_nodeid       = 0;
-    cr->nodeid           = cr->sim_nodeid;
+    GMX_UNUSED_VALUE(communicator);
+    rankInCommunicator = 0;
+    sizeOfCommunicator = 1;
 #endif
 
+    cr->mpiDefaultCommunicator    = communicator;
+    cr->sizeOfDefaultCommunicator = sizeOfCommunicator;
+    cr->rankInDefaultCommunicator = rankInCommunicator;
+
+    // For now, we want things to go horribly wrong if this is used too early...
+    // TODO: Remove when communicators are removed from commrec (#2395)
+    cr->nnodes                    = -1;
+    cr->sizeOfMyGroupCommunicator = -1;
+    cr->nodeid                    = -1;
+    cr->sim_nodeid                = -1;
+    cr->mpi_comm_mysim            = MPI_COMM_NULL;
+    cr->mpi_comm_mygroup          = MPI_COMM_NULL;
+
     // TODO cr->duty should not be initialized here
     cr->duty = (DUTY_PP | DUTY_PME);
 
-#if GMX_MPI && !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;
+    return handle;
 }
 
-static void done_mpi_in_place_buf(mpi_in_place_buf_t *buf)
+void done_commrec(t_commrec* cr)
 {
-    if (nullptr != buf)
+    if (MASTER(cr))
     {
-        sfree(buf->ibuf);
-        sfree(buf->libuf);
-        sfree(buf->fbuf);
-        sfree(buf->dbuf);
-        sfree(buf);
+        if (nullptr != cr->dd)
+        {
+            // TODO: implement
+            // done_domdec(cr->dd);
+        }
     }
-}
-
-void done_commrec(t_commrec *cr)
-{
 #if GMX_MPI
-    if (PAR(cr) || MULTISIM(cr))
+    // TODO We need to be able to free communicators, but the
+    // structure of the commrec and domdec initialization code makes
+    // it hard to avoid both leaks and double frees.
+    bool mySimIsMyGroup = (cr->mpi_comm_mysim == cr->mpi_comm_mygroup);
+    if (cr->mpi_comm_mysim != MPI_COMM_NULL && cr->mpi_comm_mysim != MPI_COMM_WORLD)
     {
-        MPI_Comm_free(&cr->mpi_comm_physicalnode);
+        // TODO see above
+        // MPI_Comm_free(&cr->mpi_comm_mysim);
     }
-#endif
-    if (nullptr != cr->dd)
+    if (!mySimIsMyGroup && cr->mpi_comm_mygroup != MPI_COMM_NULL && cr->mpi_comm_mygroup != MPI_COMM_WORLD)
     {
-        // TODO: implement
-        // done_domdec(cr->dd);
+        // TODO see above
+        // MPI_Comm_free(&cr->mpi_comm_mygroup);
     }
-    if (nullptr != cr->ms)
-    {
-        done_mpi_in_place_buf(cr->ms->mpb);
-        sfree(cr->ms);
-    }
-    done_mpi_in_place_buf(cr->mpb);
-    sfree(cr);
-}
-
-t_commrec *reinitialize_commrec_for_this_thread(const t_commrec gmx_unused *cro)
-{
-#if GMX_THREAD_MPI
-    t_commrec *cr;
-
-    /* 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;
-
-    /* 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 nullptr;
 #endif
+    sfree(cr);
 }
 
-void gmx_setup_nodecomm(FILE gmx_unused *fplog, t_commrec *cr)
+void gmx_setup_nodecomm(FILE gmx_unused* fplog, t_commrec* cr)
 {
-    gmx_nodecomm_t *nc;
+    gmx_nodecomm_tnc;
 
     /* Many MPI implementations do not optimize MPI_Allreduce
      * (and probably also other global communication calls)
@@ -191,9 +148,11 @@ void gmx_setup_nodecomm(FILE gmx_unused *fplog, t_commrec *cr)
 
     nc->bUse = FALSE;
 #if !GMX_THREAD_MPI
-#if GMX_MPI
+#    if GMX_MPI
     int n, rank;
 
+    // TODO PhysicalNodeCommunicator could be extended/used to handle
+    // the need for per-node per-group communicators.
     MPI_Comm_size(cr->mpi_comm_mygroup, &n);
     MPI_Comm_rank(cr->mpi_comm_mygroup, &rank);
 
@@ -210,8 +169,7 @@ void gmx_setup_nodecomm(FILE gmx_unused *fplog, t_commrec *cr)
     MPI_Comm_rank(nc->comm_intra, &nc->rank_intra);
     if (debug)
     {
-        fprintf(debug, "In gmx_setup_nodecomm: node ID %d rank within node %d\n",
-                rank, nc->rank_intra);
+        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.
      * We actually only need the one for rank=0,
@@ -225,18 +183,18 @@ void gmx_setup_nodecomm(FILE gmx_unused *fplog, t_commrec *cr)
     MPI_Comm_size(nc->comm_intra, &ni);
     if (debug)
     {
-        fprintf(debug, "In gmx_setup_nodecomm: groups %d, my group size %d\n",
-                ng, ni);
+        fprintf(debug, "In gmx_setup_nodecomm: groups %d, my group size %d\n", ng, ni);
     }
 
-    if (getenv("GMX_NO_NODECOMM") == nullptr &&
-        ((ng > 1 && ng < n) || (ni > 1 && ni < n)))
+    if (getenv("GMX_NO_NODECOMM") == nullptr && ((ng > 1 && ng < n) || (ni > 1 && ni < n)))
     {
         nc->bUse = TRUE;
         if (fplog)
         {
-            fprintf(fplog, "Using two step summing over %d groups of on average %.1f ranks\n\n",
-                    ng, (real)n/(real)ng);
+            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)
         {
@@ -250,156 +208,56 @@ void gmx_setup_nodecomm(FILE gmx_unused *fplog, t_commrec *cr)
         MPI_Comm_free(&nc->comm_intra);
         if (debug)
         {
-            fprintf(debug, "In gmx_setup_nodecomm: not unsing separate inter- and intra-node communicators.\n");
+            fprintf(debug,
+                    "In gmx_setup_nodecomm: not unsing separate inter- and intra-node "
+                    "communicators.\n");
         }
     }
-#endif
+#    endif
 #else
     /* tMPI runs only on a single node so just use the nodeid */
     nc->rank_intra = cr->nodeid;
 #endif
 }
 
-void gmx_init_intranode_counters(t_commrec *cr)
+void gmx_barrier(MPI_Comm gmx_unused communicator)
 {
-    /* counters for PP+PME and PP-only processes on my physical node */
-    int nrank_intranode, rank_intranode;
-    int nrank_pp_intranode, rank_pp_intranode;
-    /* thread-MPI is not initialized when not running in parallel */
-#if GMX_MPI && !GMX_THREAD_MPI
-    int nrank_world, rank_world;
-    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 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(hash,   nrank_world);
-    snew(hash_s, nrank_world);
-    snew(hash_pp,   nrank_world);
-    snew(hash_pp_s, nrank_world);
-
-    hash_s[rank_world]    = myhash;
-    hash_pp_s[rank_world] = thisRankHasDuty(cr, DUTY_PP) ? myhash : -1;
-
-    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;
-    nrank_pp_intranode = 0;
-    rank_pp_intranode  = 0;
-    for (i = 0; i < nrank_world; i++)
+    if (communicator == MPI_COMM_NULL)
     {
-        if (hash[i] == myhash)
-        {
-            nrank_intranode++;
-            if (i < rank_world)
-            {
-                rank_intranode++;
-            }
-        }
-        if (hash_pp[i] == myhash)
-        {
-            nrank_pp_intranode++;
-            if (thisRankHasDuty(cr, DUTY_PP) && i < rank_world)
-            {
-                rank_pp_intranode++;
-            }
-        }
+        return;
     }
-    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;
-    rank_intranode     = cr->sim_nodeid;
-    nrank_pp_intranode = cr->nnodes - cr->npmenodes;
-    rank_pp_intranode  = cr->nodeid;
-#endif
-
-    if (debug)
-    {
-        char sbuf[STRLEN];
-        if (thisRankHasDuty(cr, DUTY_PP) && thisRankHasDuty(cr, DUTY_PME))
-        {
-            sprintf(sbuf, "PP+PME");
-        }
-        else
-        {
-            sprintf(sbuf, "%s", thisRankHasDuty(cr, DUTY_PP) ? "PP" : "PME");
-        }
-        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,
-                nrank_pp_intranode, rank_pp_intranode);
-    }
-
-    cr->nrank_intranode    = nrank_intranode;
-    cr->rank_intranode     = rank_intranode;
-    cr->nrank_pp_intranode = nrank_pp_intranode;
-    cr->rank_pp_intranode  = rank_pp_intranode;
-}
-
-
-void gmx_barrier(const t_commrec gmx_unused *cr)
-{
 #if !GMX_MPI
-    gmx_call("gmx_barrier");
+    GMX_RELEASE_ASSERT(false, "Invalid call to gmx_barrier");
 #else
-    MPI_Barrier(cr->mpi_comm_mygroup);
+    MPI_Barrier(communicator);
 #endif
 }
 
-void gmx_barrier_physical_node(const t_commrec gmx_unused *cr)
+void gmx_bcast(int gmx_unused nbytes, void gmx_unused* b, MPI_Comm gmx_unused communicator)
 {
-#if !GMX_MPI
-    gmx_call("gmx_barrier_physical_node");
-#else
-    MPI_Barrier(cr->mpi_comm_physicalnode);
-#endif
-}
-
-void gmx_bcast(int gmx_unused nbytes, void gmx_unused *b, const t_commrec gmx_unused *cr)
-{
-#if !GMX_MPI
-    gmx_call("gmx_bast");
-#else
-    MPI_Bcast(b, nbytes, MPI_BYTE, MASTERRANK(cr), cr->mpi_comm_mygroup);
+    // Without MPI we have a single rank, so bcast is a no-op
+#if GMX_MPI
+    MPI_Bcast(b, nbytes, MPI_BYTE, 0, communicator);
 #endif
 }
 
-void gmx_bcast_sim(int gmx_unused nbytes, void gmx_unused *b, const t_commrec gmx_unused *cr)
+void gmx_sumd(int gmx_unused nr, double gmx_unused r[], const t_commrec gmx_unused* cr)
 {
-#if !GMX_MPI
-    gmx_call("gmx_bast");
-#else
-    MPI_Bcast(b, nbytes, MPI_BYTE, MASTERRANK(cr), cr->mpi_comm_mysim);
-#endif
-}
+    // Without MPI we have a single rank, so sum is a no-op
+#if GMX_MPI
+    if (cr->sizeOfMyGroupCommunicator == 1)
+    {
+        return;
+    }
 
-void gmx_sumd(int gmx_unused nr, double gmx_unused r[], const t_commrec gmx_unused *cr)
-{
-#if !GMX_MPI
-    gmx_call("gmx_sumd");
-#else
-#if MPI_IN_PLACE_EXISTS
     if (cr->nc.bUse)
     {
         if (cr->nc.rank_intra == 0)
         {
             /* Use two step summing. */
-            MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, 0,
-                       cr->nc.comm_intra);
+            MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, 0, cr->nc.comm_intra);
             /* Sum the roots of the internal (intra) buffers. */
-            MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM,
-                          cr->nc.comm_inter);
+            MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, cr->nc.comm_inter);
         }
         else
         {
@@ -411,58 +269,28 @@ void gmx_sumd(int gmx_unused nr, double gmx_unused r[], const t_commrec gmx_unus
     }
     else
     {
-        MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM,
-                      cr->mpi_comm_mygroup);
+        MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_DOUBLE, MPI_SUM, cr->mpi_comm_mygroup);
     }
-#else
-    int i;
-
-    if (nr > cr->mpb->dbuf_alloc)
-    {
-        cr->mpb->dbuf_alloc = nr;
-        srenew(cr->mpb->dbuf, cr->mpb->dbuf_alloc);
-    }
-    if (cr->nc.bUse)
-    {
-        /* Use two step summing */
-        MPI_Allreduce(r, cr->mpb->dbuf, nr, MPI_DOUBLE, MPI_SUM, cr->nc.comm_intra);
-        if (cr->nc.rank_intra == 0)
-        {
-            /* Sum with the buffers reversed */
-            MPI_Allreduce(cr->mpb->dbuf, r, nr, MPI_DOUBLE, MPI_SUM,
-                          cr->nc.comm_inter);
-        }
-        MPI_Bcast(r, nr, MPI_DOUBLE, 0, cr->nc.comm_intra);
-    }
-    else
-    {
-        MPI_Allreduce(r, cr->mpb->dbuf, nr, MPI_DOUBLE, MPI_SUM,
-                      cr->mpi_comm_mygroup);
-        for (i = 0; i < nr; i++)
-        {
-            r[i] = cr->mpb->dbuf[i];
-        }
-    }
-#endif
 #endif
 }
 
-void gmx_sumf(int gmx_unused nr, float gmx_unused r[], const t_commrec gmx_unused *cr)
+void gmx_sumf(int gmx_unused nr, float gmx_unused r[], const t_commrec gmx_unusedcr)
 {
-#if !GMX_MPI
-    gmx_call("gmx_sumf");
-#else
-#if MPI_IN_PLACE_EXISTS
+    // Without MPI we have a single rank, so sum is a no-op
+#if GMX_MPI
+    if (cr->sizeOfMyGroupCommunicator == 1)
+    {
+        return;
+    }
+
     if (cr->nc.bUse)
     {
         /* Use two step summing.  */
         if (cr->nc.rank_intra == 0)
         {
-            MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, 0,
-                       cr->nc.comm_intra);
+            MPI_Reduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, 0, cr->nc.comm_intra);
             /* Sum the roots of the internal (intra) buffers */
-            MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM,
-                          cr->nc.comm_inter);
+            MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, cr->nc.comm_inter);
         }
         else
         {
@@ -476,45 +304,18 @@ void gmx_sumf(int gmx_unused nr, float gmx_unused r[], const t_commrec gmx_unuse
     {
         MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_FLOAT, MPI_SUM, cr->mpi_comm_mygroup);
     }
-#else
-    int i;
-
-    if (nr > cr->mpb->fbuf_alloc)
-    {
-        cr->mpb->fbuf_alloc = nr;
-        srenew(cr->mpb->fbuf, cr->mpb->fbuf_alloc);
-    }
-    if (cr->nc.bUse)
-    {
-        /* Use two step summing */
-        MPI_Allreduce(r, cr->mpb->fbuf, nr, MPI_FLOAT, MPI_SUM, cr->nc.comm_intra);
-        if (cr->nc.rank_intra == 0)
-        {
-            /* Sum with the buffers reversed */
-            MPI_Allreduce(cr->mpb->fbuf, r, nr, MPI_FLOAT, MPI_SUM,
-                          cr->nc.comm_inter);
-        }
-        MPI_Bcast(r, nr, MPI_FLOAT, 0, cr->nc.comm_intra);
-    }
-    else
-    {
-        MPI_Allreduce(r, cr->mpb->fbuf, nr, MPI_FLOAT, MPI_SUM,
-                      cr->mpi_comm_mygroup);
-        for (i = 0; i < nr; i++)
-        {
-            r[i] = cr->mpb->fbuf[i];
-        }
-    }
-#endif
 #endif
 }
 
-void gmx_sumi(int gmx_unused nr, int gmx_unused r[], const t_commrec gmx_unused *cr)
+void gmx_sumi(int gmx_unused nr, int gmx_unused r[], const t_commrec gmx_unusedcr)
 {
-#if !GMX_MPI
-    gmx_call("gmx_sumi");
-#else
-#if MPI_IN_PLACE_EXISTS
+    // Without MPI we have a single rank, so sum is a no-op
+#if GMX_MPI
+    if (cr->sizeOfMyGroupCommunicator == 1)
+    {
+        return;
+    }
+
     if (cr->nc.bUse)
     {
         /* Use two step summing */
@@ -536,232 +337,26 @@ void gmx_sumi(int gmx_unused nr, int gmx_unused r[], const t_commrec gmx_unused
     {
         MPI_Allreduce(MPI_IN_PLACE, r, nr, MPI_INT, MPI_SUM, cr->mpi_comm_mygroup);
     }
-#else
-    int i;
-
-    if (nr > cr->mpb->ibuf_alloc)
-    {
-        cr->mpb->ibuf_alloc = nr;
-        srenew(cr->mpb->ibuf, cr->mpb->ibuf_alloc);
-    }
-    if (cr->nc.bUse)
-    {
-        /* Use two step summing */
-        MPI_Allreduce(r, cr->mpb->ibuf, nr, MPI_INT, MPI_SUM, cr->nc.comm_intra);
-        if (cr->nc.rank_intra == 0)
-        {
-            /* Sum with the buffers reversed */
-            MPI_Allreduce(cr->mpb->ibuf, r, nr, MPI_INT, MPI_SUM, cr->nc.comm_inter);
-        }
-        MPI_Bcast(r, nr, MPI_INT, 0, cr->nc.comm_intra);
-    }
-    else
-    {
-        MPI_Allreduce(r, cr->mpb->ibuf, nr, MPI_INT, MPI_SUM, cr->mpi_comm_mygroup);
-        for (i = 0; i < nr; i++)
-        {
-            r[i] = cr->mpb->ibuf[i];
-        }
-    }
-#endif
-#endif
-}
-
-void gmx_sumli(int gmx_unused nr, gmx_int64_t gmx_unused r[], const t_commrec gmx_unused *cr)
-{
-#if !GMX_MPI
-    gmx_call("gmx_sumli");
-#else
-#if 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, MPI_INT64_T, MPI_SUM, 0,
-                       cr->nc.comm_intra);
-            /* Sum with the buffers reversed */
-            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, nullptr, nr, MPI_INT64_T, MPI_SUM, 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, MPI_INT64_T, MPI_SUM, cr->mpi_comm_mygroup);
-    }
-#else
-    int i;
-
-    if (nr > cr->mpb->libuf_alloc)
-    {
-        cr->mpb->libuf_alloc = nr;
-        srenew(cr->mpb->libuf, cr->mpb->libuf_alloc);
-    }
-    if (cr->nc.bUse)
-    {
-        /* Use two step summing */
-        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, MPI_INT64_T, MPI_SUM,
-                          cr->nc.comm_inter);
-        }
-        MPI_Bcast(r, nr, MPI_INT64_T, 0, cr->nc.comm_intra);
-    }
-    else
-    {
-        MPI_Allreduce(r, cr->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
-                      cr->mpi_comm_mygroup);
-        for (i = 0; i < nr; i++)
-        {
-            r[i] = cr->mpb->libuf[i];
-        }
-    }
-#endif
-#endif
-}
-
-
-
-#if GMX_MPI
-static void gmx_sumd_comm(int nr, double r[], MPI_Comm mpi_comm)
-{
-#if 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,
-       (during setup, when comm_rec is not the appropriate communication
-       structure), so this isn't as bad as it looks. */
-    double *buf;
-    int     i;
-
-    snew(buf, nr);
-    MPI_Allreduce(r, buf, nr, MPI_DOUBLE, MPI_SUM, mpi_comm);
-    for (i = 0; i < nr; i++)
-    {
-        r[i] = buf[i];
-    }
-    sfree(buf);
-#endif
-}
-#endif
-
-#if GMX_MPI
-static void gmx_sumf_comm(int nr, float r[], MPI_Comm mpi_comm)
-{
-#if 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,
-       (during setup, when comm_rec is not the appropriate communication
-       structure), so this isn't as bad as it looks. */
-    float *buf;
-    int    i;
-
-    snew(buf, nr);
-    MPI_Allreduce(r, buf, nr, MPI_FLOAT, MPI_SUM, mpi_comm);
-    for (i = 0; i < nr; i++)
-    {
-        r[i] = buf[i];
-    }
-    sfree(buf);
-#endif
-}
-#endif
-
-void gmx_sumd_sim(int gmx_unused nr, double gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
-{
-#if !GMX_MPI
-    gmx_call("gmx_sumd_sim");
-#else
-    gmx_sumd_comm(nr, r, ms->mpi_comm_masters);
-#endif
-}
-
-void gmx_sumf_sim(int gmx_unused nr, float gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
-{
-#if !GMX_MPI
-    gmx_call("gmx_sumf_sim");
-#else
-    gmx_sumf_comm(nr, r, ms->mpi_comm_masters);
-#endif
-}
-
-void gmx_sumi_sim(int gmx_unused nr, int gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
-{
-#if !GMX_MPI
-    gmx_call("gmx_sumi_sim");
-#else
-#if 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: */
-    int i;
-
-    if (nr > ms->mpb->ibuf_alloc)
-    {
-        ms->mpb->ibuf_alloc = nr;
-        srenew(ms->mpb->ibuf, ms->mpb->ibuf_alloc);
-    }
-    MPI_Allreduce(r, ms->mpb->ibuf, nr, MPI_INT, MPI_SUM, ms->mpi_comm_masters);
-    for (i = 0; i < nr; i++)
-    {
-        r[i] = ms->mpb->ibuf[i];
-    }
-#endif
-#endif
-}
-
-void gmx_sumli_sim(int gmx_unused nr, gmx_int64_t gmx_unused r[], const gmx_multisim_t gmx_unused *ms)
-{
-#if !GMX_MPI
-    gmx_call("gmx_sumli_sim");
-#else
-#if 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: */
-    int i;
-
-    if (nr > ms->mpb->libuf_alloc)
-    {
-        ms->mpb->libuf_alloc = nr;
-        srenew(ms->mpb->libuf, ms->mpb->libuf_alloc);
-    }
-    MPI_Allreduce(r, ms->mpb->libuf, nr, MPI_INT64_T, MPI_SUM,
-                  ms->mpi_comm_masters);
-    for (i = 0; i < nr; i++)
-    {
-        r[i] = ms->mpb->libuf[i];
-    }
-#endif
 #endif
 }
 
-const char *opt2fn_master(const char *opt, int nfile, const t_filenm fnm[],
-                          t_commrec *cr)
+const char* opt2fn_master(const char* opt, int nfile, const t_filenm fnm[], t_commrec* cr)
 {
     return SIMMASTER(cr) ? opt2fn(opt, nfile, fnm) : nullptr;
 }
 
-void gmx_fatal_collective(int f_errno, const char *file, int line,
-                          MPI_Comm comm, gmx_bool bMaster,
-                          const char *fmt, ...)
+void gmx_fatal_collective(int                    f_errno,
+                          const char*            file,
+                          int                    line,
+                          MPI_Comm               comm,
+                          gmx_bool               bMaster,
+                          gmx_fmtstr const char* fmt,
+                          ...)
 {
     va_list  ap;
     gmx_bool bFinalize;
 #if GMX_MPI
-    int      result;
+    int result;
     /* Check if we are calling on all processes in MPI_COMM_WORLD */
     MPI_Comm_compare(comm, MPI_COMM_WORLD, &result);
     /* Any result except MPI_UNEQUAL allows us to call MPI_Finalize */