Gromacs  2026.0-dev-20251119-5f0a571d
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
Functions
#include "gmxpre.h"
#include "multisim.h"
#include "config.h"
#include <cinttypes>
#include <filesystem>
#include <limits>
#include "gromacs/gmxlib/network.h"
#include "gromacs/mdtypes/commrec.h"
#include "gromacs/utility/arrayref.h"
#include "gromacs/utility/exceptions.h"
#include "gromacs/utility/fatalerror.h"
#include "gromacs/utility/futil.h"
#include "gromacs/utility/gmxassert.h"
#include "gromacs/utility/logger.h"
#include "gromacs/utility/mpitypes.h"
#include "gromacs/utility/smalloc.h"
+ Include dependency graph for multisim.cpp:

Description

Implements the multi-simulation support routines.

Author
Mark Abraham mark..nosp@m.j.ab.nosp@m.raham.nosp@m.@gma.nosp@m.il.co.nosp@m.m
Berk Hess hess@.nosp@m.kth..nosp@m.se

Functions

std::unique_ptr< gmx_multisim_tbuildMultiSimulation (MPI_Comm worldComm, gmx::ArrayRef< const std::string > multidirs)
 Builder function for gmx_multisim_t. More...
 
template<typename T >
static void mpiAllSumReduce (const int nr, T r[], MPI_Comm mpi_comm)
 Sums array r of size nr over mpi_comm, the result is available on all ranks.
 
template<typename T >
static void mpiAllSumReduce (gmx::ArrayRef< T > r, MPI_Comm mpi_comm)
 Sums array r over mpi_comm, the result is available on all ranks, size of r should not exceed MAX_INT.
 
void gmx_sumd_sim (int nr, double r[], const gmx_multisim_t *ms)
 Calculate the sum over the simulations of an array of doubles.
 
void gmx_sumf_sim (int nr, float r[], const gmx_multisim_t *ms)
 Calculate the sum over the simulations of an array of floats.
 
void gmx_sumi_sim (int nr, int r[], const gmx_multisim_t *ms)
 Calculate the sum over the simulations of an array of ints.
 
void gmx_sumli_sim (gmx::ArrayRef< int64_t > r, const gmx_multisim_t &ms)
 Calculate the sum over the simulations of an array of large ints, the size of r cannot exceed MAX_INT.
 
std::vector< int > gatherIntFromMultiSimulation (const gmx_multisim_t *ms, const int localValue)
 Return a vector containing the gathered values of localValue found on the main rank of each simulation.
 
template<typename T >
static bool multiSimValuesAllAreEqual (const gmx_multisim_t &ms, const T value, std::vector< T > *externalBuffer=nullptr)
 Returns whether value is equal on all main ranks in ms. More...
 
template<typename T >
static void checkMultiInt (FILE *log, const gmx_multisim_t &ms, const T val, const char *name)
 Checks whether values val are equal on all main ranks of ms. More...
 
void check_multi_int (FILE *log, const gmx_multisim_t &ms, int val, const char *name, bool beQuiet)
 Check if val is the same on all simulations for a mdrun -multidir run. More...
 
void check_multi_int64 (FILE *log, const gmx_multisim_t &ms, int64_t val, const char *name, bool beQuiet)
 Check if val is the same on all simulations for a mdrun -multidir run. More...
 
bool findIsSimulationMainRank (const gmx_multisim_t *ms, MPI_Comm communicator)
 Return whether this rank is the main rank of a simulation, using ms (if it is valid) and otherwise communicator.
 
bool isMainSim (const gmx_multisim_t *ms)
 Are we the main simulation of a possible multi-simulation?
 
bool isMainSimMainRank (const gmx_multisim_t *ms, const bool isMain)
 Are we the main rank (of the main simulation, for a multi-sim). More...
 
void logInitialMultisimStatus (const gmx_multisim_t &ms, const t_commrec *cr, const gmx::MDLogger &mdlog, const bool simulationsShareState, const int numSteps, const int initialStep)
 Log the initial state of the multi-sim. More...
 

Function Documentation

std::unique_ptr<gmx_multisim_t> buildMultiSimulation ( MPI_Comm  worldComm,
gmx::ArrayRef< const std::string >  multidirs 
)

Builder function for gmx_multisim_t.

Parameters
[in]worldCommMPI communicator to split when multi-simulation is requested.
[in]multidirsStrings naming the subdirectories when multi-simulation is requested, otherwise empty

Splits worldComm into multidirs.size() separate simulations, if >1, and creates a communication structure between the main ranks of these simulations.

Valid to call regardless of build configuration, but multidirs must be empty unless a real MPI build is used.

Exceptions
NotImplementedErrorwhen multidirs is non-empty unless using real MPI is true
NotImplementedErrorwhen multidirs has exactly one element
InconsistentInputErrorwhen the number of MPI ranks is not a multiple of the number of multidirs
FileIOErrorwhen the simulation cannot change to the working directory in multidirs
void check_multi_int ( FILE *  log,
const gmx_multisim_t ms,
int  val,
const char *  name,
bool  beQuiet 
)

Check if val is the same on all simulations for a mdrun -multidir run.

The string name is used to print to the log file and in a fatal error if the val's don't match. If beQuiet is true and the check passes, no output is written.

void check_multi_int64 ( FILE *  log,
const gmx_multisim_t ms,
int64_t  val,
const char *  name,
bool  beQuiet 
)

Check if val is the same on all simulations for a mdrun -multidir run.

The string name is used to print to the log file and in a fatal error if the val's don't match. If beQuiet is true and the check passes, no output is written.

template<typename T >
static void checkMultiInt ( FILE *  log,
const gmx_multisim_t ms,
const T  val,
const char *  name 
)
static

Checks whether values val are equal on all main ranks of ms.

Parameters
[in]logPointer to the loga file, can be nullptr
[in]msMulti-simulation communication setup
[in]valThe value to check
[in]nameThe name to print to log, can be nullptr when log==nullptr
bool isMainSimMainRank ( const gmx_multisim_t ms,
bool  isMain 
)

Are we the main rank (of the main simulation, for a multi-sim).

This rank prints the remaining run time etc.

void logInitialMultisimStatus ( const gmx_multisim_t ms,
const t_commrec *  cr,
const gmx::MDLogger mdlog,
bool  simulationsShareState,
int  numSteps,
int  initialStep 
)

Log the initial state of the multi-sim.

The simulations may be at different steps, etc so we report that.

Parameters
[in]msThe multi-sum object
[in]crThe commrec object
[in]mdlogLogger
[in]simulationsShareStateWhether the simulations share state
[in]numStepsThe number of steps in this simulation
[in]initialStepThe initial step for this simulation
template<typename T >
static bool multiSimValuesAllAreEqual ( const gmx_multisim_t ms,
const T  value,
std::vector< T > *  externalBuffer = nullptr 
)
static

Returns whether value is equal on all main ranks in ms.

Optionally an external buffer can be passed in externaBuffer. This will then hold values of value of each main rank in ms.