2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 2015, by the GROMACS development team, led by
5 * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6 * and including many others, as listed in the AUTHORS file in the
7 * top-level source directory and at http://www.gromacs.org.
9 * GROMACS is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public License
11 * as published by the Free Software Foundation; either version 2.1
12 * of the License, or (at your option) any later version.
14 * GROMACS is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with GROMACS; if not, see
21 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * If you want to redistribute modifications to GROMACS, please
25 * consider that scientific software is very special. Version
26 * control is crucial - bugs must be traceable. We will be happy to
27 * consider code for inclusion in the official distribution, but
28 * derived work must not be called official GROMACS. Details are found
29 * in the README & COPYING files - if they are missing, get the
30 * official version at http://www.gromacs.org.
32 * To help us fund GROMACS development, we humbly ask that you cite
33 * the research papers on the package. Check out http://www.gromacs.org.
37 * \brief This file declares functions for mdrun to call to manage the
38 * details of doing a restart (ie. reading checkpoints, appending
41 * \todo Clean up the error-prone logic here. Add doxygen.
43 * \author Berk Hess <hess@kth.se>
44 * \author Erik Lindahl <erik@kth.se>
45 * \author Mark Abraham <mark.j.abraham@gmail.com>
47 * \ingroup module_mdrunutility
52 #include "handlerestart.h"
56 #include "gromacs/fileio/filenm.h"
57 #include "gromacs/fileio/gmxfio.h"
58 #include "gromacs/legacyheaders/checkpoint.h"
59 #include "gromacs/legacyheaders/main.h"
60 #include "gromacs/legacyheaders/types/commrec.h"
61 #include "gromacs/utility/basedefinitions.h"
62 #include "gromacs/utility/smalloc.h"
64 /*! \brief Search for \p fnm_cp in fnm and return true iff found
66 * \todo This could be implemented sanely with a for loop. */
67 static gmx_bool exist_output_file(const char *fnm_cp, int nfile, const t_filenm fnm[])
71 /* Check if the output file name stored in the checkpoint file
72 * is one of the output file names of mdrun.
76 !(is_output(&fnm[i]) && strcmp(fnm_cp, fnm[i].fns[0]) == 0))
81 return (i < nfile && gmx_fexist(fnm_cp));
84 /*! \brief Support handling restarts
86 * \todo Clean this up (next patch)
88 * Read just the simulation 'generation' and with bTryToAppendFiles check files.
89 * This is is needed at the beginning of mdrun,
90 * to be able to rename the logfile correctly.
91 * When file appending is requested, checks which output files are present,
92 * and returns TRUE/FALSE in bDoAppendFiles if all or none are present.
93 * If only some output files are present, give a fatal error.
94 * When bDoAppendFiles is TRUE upon return, bAddPart will tell whether the simulation part
95 * needs to be added to the output file name.
97 * This routine cannot print tons of data, since it is called before
98 * the log file is opened. */
100 read_checkpoint_data(const char *filename, int *simulation_part,
102 gmx_bool bTryToAppendFiles,
103 int nfile, const t_filenm fnm[],
104 const char *part_suffix,
106 gmx_bool *bDoAppendFiles)
110 gmx_file_position_t *outputfiles;
112 char *fn, suf_up[STRLEN];
114 *bDoAppendFiles = FALSE;
118 if (!gmx_fexist(filename) || (!(fp = gmx_fio_open(filename, "r")) ))
120 *simulation_part = 0;
124 read_checkpoint_simulation_part_and_filenames(fp,
129 if (bTryToAppendFiles)
132 for (f = 0; f < nfiles; f++)
134 if (exist_output_file(outputfiles[f].filename, nfile, fnm))
139 if (nexist == nfiles)
141 *bDoAppendFiles = bTryToAppendFiles;
146 "Output file appending has been requested,\n"
147 "but some output files listed in the checkpoint file %s\n"
148 "are not present or are named differently by the current program:\n",
150 fprintf(stderr, "output files present:");
151 for (f = 0; f < nfiles; f++)
153 if (exist_output_file(outputfiles[f].filename,
156 fprintf(stderr, " %s", outputfiles[f].filename);
159 fprintf(stderr, "\n");
160 fprintf(stderr, "output files not present or named differently:");
161 for (f = 0; f < nfiles; f++)
163 if (!exist_output_file(outputfiles[f].filename,
166 fprintf(stderr, " %s", outputfiles[f].filename);
169 fprintf(stderr, "\n");
171 gmx_fatal(FARGS, "File appending requested, but %d of the %d output files are not present or are named differently", nfiles-nexist, nfiles);
179 gmx_fatal(FARGS, "File appending requested, but no output file information is stored in the checkpoint file");
181 fn = outputfiles[0].filename;
182 if (strlen(fn) < 4 ||
183 gmx_strcasecmp(fn+strlen(fn)-4, ftp2ext(efLOG)) == 0)
185 gmx_fatal(FARGS, "File appending requested, but the log file is not the first file listed in the checkpoint file");
187 /* Set bAddPart to whether the suffix string '.part' is present
188 * in the log file name.
190 strcpy(suf_up, part_suffix);
192 *bAddPart = (strstr(fn, part_suffix) != NULL ||
193 strstr(fn, suf_up) != NULL);
201 gmx_bcast(sizeof(*simulation_part), simulation_part, cr);
203 if (*simulation_part > 0 && bTryToAppendFiles)
205 gmx_bcast(sizeof(*bDoAppendFiles), bDoAppendFiles, cr);
206 gmx_bcast(sizeof(*bAddPart), bAddPart, cr);
211 /* This routine cannot print tons of data, since it is called before the log file is opened. */
213 handleRestart(t_commrec *cr,
214 gmx_bool bTryToAppendFiles,
217 gmx_bool *bDoAppendFiles,
218 gmx_bool *bStartFromCpt)
221 int sim_part, sim_part_fn;
222 const char *part_suffix = ".part";
225 bAddPart = !bTryToAppendFiles;
227 /* Check if there is ANY checkpoint file available */
229 sim_part_fn = sim_part;
230 if (opt2bSet("-cpi", NFILE, fnm))
232 read_checkpoint_data(opt2fn_master("-cpi", NFILE, fnm, cr),
234 bTryToAppendFiles, NFILE, fnm,
235 part_suffix, &bAddPart,
237 if (sim_part_fn == 0 && MULTIMASTER(cr))
239 fprintf(stdout, "No previous checkpoint file present, assuming this is a new run.\n");
243 sim_part = sim_part_fn + 1;
246 if (MULTISIM(cr) && MASTER(cr))
250 /* Log file is not yet available, so if there's a
251 * problem we can only write to stderr. */
258 check_multi_int(fpmulti, cr->ms, sim_part, "simulation part", TRUE);
263 *bDoAppendFiles = FALSE;
265 *bStartFromCpt = sim_part > 1;
267 if (!*bDoAppendFiles)
269 sim_part_fn = sim_part;
276 /* Rename all output files (except checkpoint files) */
277 /* create new part name first (zero-filled) */
278 sprintf(suffix, "%s%04d", part_suffix, sim_part_fn);
280 add_suffix_to_output_names(fnm, NFILE, suffix);
283 fprintf(stdout, "Checkpoint file is from part %d, new output files will be suffixed '%s'.\n", sim_part-1, suffix);