2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team.
6 * Copyright (c) 2013,2014,2015,2016,2017,2018, by the GROMACS development team, led by
7 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
8 * and including many others, as listed in the AUTHORS file in the
9 * top-level source directory and at http://www.gromacs.org.
11 * GROMACS is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public License
13 * as published by the Free Software Foundation; either version 2.1
14 * of the License, or (at your option) any later version.
16 * GROMACS is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with GROMACS; if not, see
23 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 * If you want to redistribute modifications to GROMACS, please
27 * consider that scientific software is very special. Version
28 * control is crucial - bugs must be traceable. We will be happy to
29 * consider code for inclusion in the official distribution, but
30 * derived work must not be called official GROMACS. Details are found
31 * in the README & COPYING files - if they are missing, get the
32 * official version at http://www.gromacs.org.
34 * To help us fund GROMACS development, we humbly ask that you cite
35 * the research papers on the package. Check out http://www.gromacs.org.
49 #include "gromacs/commandline/filenm.h"
50 #include "gromacs/fileio/gmxfio.h"
51 #include "gromacs/gmxlib/network.h"
52 #include "gromacs/mdtypes/commrec.h"
53 #include "gromacs/utility/binaryinformation.h"
54 #include "gromacs/utility/cstringutil.h"
55 #include "gromacs/utility/exceptions.h"
56 #include "gromacs/utility/fatalerror.h"
57 #include "gromacs/utility/futil.h"
58 #include "gromacs/utility/gmxassert.h"
59 #include "gromacs/utility/gmxmpi.h"
60 #include "gromacs/utility/path.h"
61 #include "gromacs/utility/programcontext.h"
62 #include "gromacs/utility/smalloc.h"
63 #include "gromacs/utility/snprintf.h"
64 #include "gromacs/utility/stringutil.h"
65 #include "gromacs/utility/sysinfo.h"
67 /* The source code in this file should be thread-safe.
68 Please keep it that way. */
70 // TODO move this to multi-sim module
71 void check_multi_int(FILE *log
, const gmx_multisim_t
*ms
, int val
,
78 if (nullptr != log
&& !bQuiet
)
80 fprintf(log
, "Multi-checking %s ... ", name
);
86 "check_multi_int called with a NULL communication pointer");
91 gmx_sumi_sim(ms
->nsim
, ibuf
, ms
);
94 for (p
= 1; p
< ms
->nsim
; p
++)
96 bCompatible
= bCompatible
&& (ibuf
[p
-1] == ibuf
[p
]);
101 if (nullptr != log
&& !bQuiet
)
103 fprintf(log
, "OK\n");
110 fprintf(log
, "\n%s is not equal for all subsystems\n", name
);
111 for (p
= 0; p
< ms
->nsim
; p
++)
113 fprintf(log
, " subsystem %d: %d\n", p
, ibuf
[p
]);
116 gmx_fatal(FARGS
, "The %d subsystems are not compatible\n", ms
->nsim
);
122 // TODO move this to multi-sim module
123 void check_multi_int64(FILE *log
, const gmx_multisim_t
*ms
,
124 int64_t val
, const char *name
,
129 gmx_bool bCompatible
;
131 if (nullptr != log
&& !bQuiet
)
133 fprintf(log
, "Multi-checking %s ... ", name
);
139 "check_multi_int called with a NULL communication pointer");
142 snew(ibuf
, ms
->nsim
);
144 gmx_sumli_sim(ms
->nsim
, ibuf
, ms
);
147 for (p
= 1; p
< ms
->nsim
; p
++)
149 bCompatible
= bCompatible
&& (ibuf
[p
-1] == ibuf
[p
]);
154 if (nullptr != log
&& !bQuiet
)
156 fprintf(log
, "OK\n");
161 // TODO Part of this error message would also be good to go to
162 // stderr (from one rank of one sim only)
165 fprintf(log
, "\n%s is not equal for all subsystems\n", name
);
166 for (p
= 0; p
< ms
->nsim
; p
++)
169 /* first make the format string */
170 snprintf(strbuf
, 255, " subsystem %%d: %s\n",
172 fprintf(log
, strbuf
, p
, ibuf
[p
]);
175 gmx_fatal(FARGS
, "The %d subsystems are not compatible\n", ms
->nsim
);
182 void gmx_log_open(const char *lognm
, const t_commrec
*cr
,
183 gmx_bool bAppendFiles
, FILE** fplog
)
187 char timebuf
[STRLEN
];
192 fp
= gmx_fio_fopen(lognm
, bAppendFiles
? "a+" : "w+" );
195 gmx_fatal_set_log_file(fp
);
197 /* Get some machine parameters */
198 gmx_gethostname(host
, 256);
200 gmx_format_current_time(timebuf
, STRLEN
);
207 "-----------------------------------------------------------\n"
208 "Restarting from checkpoint, appending to previous log file.\n"
214 "Log file opened on %s"
215 "Host: %s pid: %d rank ID: %d number of ranks: %d\n",
216 timebuf
, host
, pid
, cr
->nodeid
, cr
->nnodes
);
219 gmx::BinaryInformationSettings settings
;
220 settings
.extendedInfo(true);
221 settings
.copyright(!bAppendFiles
);
222 gmx::printBinaryInformation(fp
, gmx::getProgramContext(), settings
);
224 GMX_CATCH_ALL_AND_EXIT_WITH_FATAL_ERROR
;
232 void gmx_log_close(FILE *fp
)
236 gmx_fatal_set_log_file(nullptr);
241 // TODO move this to multi-sim module
242 gmx_multisim_t
*init_multisystem(MPI_Comm comm
,
243 gmx::ArrayRef
<const std::string
> multidirs
)
247 MPI_Group mpi_group_world
;
251 if (multidirs
.empty())
256 if (!GMX_LIB_MPI
&& !multidirs
.empty())
258 gmx_fatal(FARGS
, "mdrun -multidir is only supported when GROMACS has been "
259 "configured with a proper external MPI library.");
262 if (multidirs
.size() == 1)
264 /* NOTE: It would be nice if this special case worked, but this requires checks/tests. */
265 gmx_fatal(FARGS
, "To run mdrun in multiple simulation mode, more then one "
266 "actual simulation is required. The single simulation case is not supported.");
271 MPI_Comm_size(comm
, &numRanks
);
272 if (numRanks
% multidirs
.size() != 0)
274 gmx_fatal(FARGS
, "The number of ranks (%d) is not a multiple of the number of simulations (%td)", numRanks
, multidirs
.size());
277 int numRanksPerSim
= numRanks
/multidirs
.size();
279 MPI_Comm_rank(comm
, &rankWithinComm
);
283 fprintf(debug
, "We have %td simulations, %d ranks per simulation, local simulation is %d\n", multidirs
.size(), numRanksPerSim
, rankWithinComm
/numRanksPerSim
);
286 ms
= new gmx_multisim_t
;
287 ms
->nsim
= multidirs
.size();
288 ms
->sim
= rankWithinComm
/numRanksPerSim
;
289 /* Create a communicator for the master nodes */
290 snew(rank
, ms
->nsim
);
291 for (int i
= 0; i
< ms
->nsim
; i
++)
293 rank
[i
] = i
*numRanksPerSim
;
295 MPI_Comm_group(comm
, &mpi_group_world
);
296 MPI_Group_incl(mpi_group_world
, ms
->nsim
, rank
, &ms
->mpi_group_masters
);
298 MPI_Comm_create(MPI_COMM_WORLD
, ms
->mpi_group_masters
,
299 &ms
->mpi_comm_masters
);
301 #if !MPI_IN_PLACE_EXISTS
302 /* initialize the MPI_IN_PLACE replacement buffers */
304 ms
->mpb
->ibuf
= NULL
;
305 ms
->mpb
->libuf
= NULL
;
306 ms
->mpb
->fbuf
= NULL
;
307 ms
->mpb
->dbuf
= NULL
;
308 ms
->mpb
->ibuf_alloc
= 0;
309 ms
->mpb
->libuf_alloc
= 0;
310 ms
->mpb
->fbuf_alloc
= 0;
311 ms
->mpb
->dbuf_alloc
= 0;
314 // TODO This should throw upon error
315 gmx_chdir(multidirs
[ms
->sim
].c_str());
317 GMX_UNUSED_VALUE(comm
);
324 void done_multisim(gmx_multisim_t
*ms
)
328 done_mpi_in_place_buf(ms
->mpb
);